package com.iamberry.wechat.service.apparatus;

import com.iamberry.wechat.core.entity.admin.ShopSystemRule;
import com.iamberry.wechat.core.entity.apparatus.Apparatus;
import com.iamberry.wechat.core.entity.integral.IntegralConfig;
import com.iamberry.wechat.core.entity.integral.MemberIntegral;
import com.iamberry.wechat.core.entity.integral.RankRule;
import com.iamberry.wechat.core.entity.integral.UseIntegral;
import com.iamberry.wechat.core.entity.member.Member;
import com.iamberry.wechat.face.apparatus.ApparatusService;
import com.iamberry.wechat.service.mapper.ApparatusMapper;
import com.iamberry.wechat.service.mapper.SystemMapper;
import com.iamberry.wechat.service.mapper.UseIntegralMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Created by liuzhiwei on 2017/12/28.
 */
@Service
public class ApparatusServiceImpl implements ApparatusService {

    @Autowired
    private ApparatusMapper apparatusMapper;
    @Autowired
    private SystemMapper systemMapper;
    @Autowired
    private UseIntegralMapper useIntegralMapper;

    @Override
    public List<Apparatus> listApparatusPage(Apparatus apparatus) {
        return apparatusMapper.listApparatusPage(apparatus);
    }

    @Override
    public Integer addApparatus(Apparatus apparatus) {
        return apparatusMapper.addApparatus(apparatus);
    }

    @Override
    public boolean registerMember(Apparatus apparatus, Member member) {
        //注册会员获取积分id
        ShopSystemRule shopSystemRule = systemMapper.selectOneShopRuleById(247);
        if (shopSystemRule == null) {
            return false;
        }
        IntegralConfig config = new IntegralConfig();
        config.setIntegralConfigId(shopSystemRule.getRuleNum().intValue());
        //获取会员获取积分配置信息
        List<IntegralConfig> configList = useIntegralMapper.listIntegralConfig(config);
        if (configList == null || configList.size() == 0) {
            return false;
        }
        config = configList.get(0);
        //查询会员积分等级规则信息
        RankRule rankRule = new RankRule();
        rankRule.setRankRuleStatus(1);
        List<RankRule> rankRuleList = useIntegralMapper.listRankRule(rankRule);
        if (rankRuleList == null || rankRuleList.size() == 0) {
            return false;
        }
        int ruleId = 0;
        //积分等级按积分数倒序排列，如果满足一个，则跳出整个循环
        for (RankRule rule : rankRuleList) {
            if (config.getIntegralConfigNumber().intValue() >= rule.getRankRuleIntegral()) {
                ruleId = rankRule.getRankRuleId();
                break;
            }
        }

        MemberIntegral memberIntegral = new MemberIntegral();
        memberIntegral.setUserOpenid(member.getUserOpenid());
        memberIntegral.setRankRuleId(ruleId);
        memberIntegral.setMemberStatus(1);
        memberIntegral.setMemberAllIntegral(config.getIntegralConfigNumber());
        memberIntegral.setMemberSurplusIntegral(config.getIntegralConfigNumber());
        memberIntegral.setMemberUsedIntegral(0);
        //添加会员信息
        int sum = useIntegralMapper.addMemberIntegral(memberIntegral);

        UseIntegral useIntegral = new UseIntegral();
        useIntegral.setUseConfigId(config.getIntegralConfigId());
        useIntegral.setUseIntegralNumber(config.getIntegralConfigNumber());
        useIntegral.setUserOpenId(member.getUserOpenid());
        useIntegral.setUseType(1);
        useIntegral.setUseIntegralRemarks("注册会员获取积分");
        //添加积分获取日志信息
        int count = useIntegralMapper.addUseIntegral(useIntegral);
        //添加机器信息
        int num = apparatusMapper.addApparatus(apparatus);
        if (sum > 0 && count > 0 && num > 0) {
            return true;
        } else {
            return false;
        }
    }
}
