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.UseIntegral;
import com.iamberry.wechat.core.entity.member.Member;
import com.iamberry.wechat.core.entity.integral.StayIntegral;
import com.iamberry.wechat.face.apparatus.ApparatusService;
import com.iamberry.wechat.service.mapper.ApparatusMapper;
import com.iamberry.wechat.service.mapper.MemberMapper;
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 org.springframework.transaction.annotation.Transactional;

import java.util.Calendar;
import java.util.Date;
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;
    @Autowired
    private MemberMapper memberMapper;


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

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

    @Override
    @Transactional
    public boolean registerMember(Apparatus apparatus, Member member) {

        //获取会员信息，判断是否已经注册会员
        Member memberInfo = memberMapper.getMemberByUserOpenId(member.getUserOpenid());
        if(memberInfo == null){
            return false;
        }
        //添加机器信息
        int num = apparatusMapper.addApparatus(apparatus);
        if(num > 0){
            //这里判断是否是会员 1.会员 2.普通用户
            if(memberInfo.getUserIdentity() == 2){
                //赠送新注册积分
                addStayIntegral(member.getUserOpenid(),1,0,"");

            }
            //赠送400新增牙刷积分
            addStayIntegral(member.getUserOpenid(),2,0,"");
           //修改用户信息
            member.setUserIdentity(1);
            if(memberMapper.updateMemberByUserId(member) < 1){
                return false;
            }
        } else {
            return false;
        }
        return true;
    }


    /**
     * 获取数据库配置的积分数量
     * @param id
     * @return
     */
    public IntegralConfig getIntegralId(Integer id){
        //新增加牙刷获取积分id
        ShopSystemRule shopSystemRule = systemMapper.selectOneShopRuleById(id);
        if (shopSystemRule == null) {
            return null;
        }
        IntegralConfig config = new IntegralConfig();
        config.setIntegralConfigId(shopSystemRule.getRuleNum().intValue());
        //获取会员获取积分配置信息
        List<IntegralConfig> configList = useIntegralMapper.listIntegralConfig(config);
        if (configList == null || configList.size() == 0) {
            return null;
        }
        return configList.get(0);
    }
    @Override
    public Apparatus getApparatus(Apparatus apparatus) {
        return apparatusMapper.getApparatus(apparatus);
    }

    @Override
    public Integer getColorIdByProductId(String productId) {
        return apparatusMapper.getColorIdByProductId(productId);
    }

    /**
     * 添加待入账积分统一调用该接口
     * @param openId
     * @param IntegralType 1.新注册会员送一百积分  2.新增牙刷赠送400积分 3.购买牙刷，赠送订单金额乘以2的积分
     * @param orderAllIntegral  订单金额  IntegralType为3时有值
     * @param orderId  订单id  IntegralType为3时有值
     * @return
     */
    @Override
    @Transactional
    public boolean addStayIntegral(String openId,Integer IntegralType,Integer orderAllIntegral,String orderId){
        if(openId == null||openId.equals("") || IntegralType < 1){
            return false;
        }
        IntegralConfig integralConfig = null;
        StayIntegral stayIntegral = new StayIntegral();
        Integer stayNumber = 0;
        Calendar calendar = Calendar.getInstance();
        switch (IntegralType){
            case 1:
                //新注册会员积分数量
                integralConfig = getIntegralId(247);
                stayNumber = integralConfig.getIntegralConfigNumber();
                //规则表查询新注册会员和新增牙刷积分到账时间（多少天到账）
                ShopSystemRule registeredSystemRule=systemMapper.selectOneShopRuleById(251);
                calendar.set(Calendar.DAY_OF_MONTH,calendar.get(Calendar.DAY_OF_MONTH)+registeredSystemRule.getRuleNum().intValue());
                stayIntegral.setStayRecordedTime(calendar.getTime());
                break;
            case 2:
                //新增加牙刷赠送积分数量
                integralConfig = getIntegralId(248);
                stayNumber = integralConfig.getIntegralConfigNumber();
                //规则表查询新注册会员和新增牙刷积分到账时间（多少天到账）
                ShopSystemRule addToothRule=systemMapper.selectOneShopRuleById(251);
                calendar.set(Calendar.DAY_OF_MONTH,calendar.get(Calendar.DAY_OF_MONTH)+addToothRule.getRuleNum().intValue());
                stayIntegral.setStayRecordedTime(calendar.getTime());
                break;
            case 3:
                if(orderAllIntegral == 0){
                    break;
                }
                integralConfig = getIntegralId(250);
                stayNumber = orderAllIntegral;
                stayIntegral.setOrderId(orderId);
                //规则表查询新注册会员和新增牙刷积分到账时间（多少天到账）
                ShopSystemRule shopSystemRule=systemMapper.selectOneShopRuleById(252);
                Integer day = shopSystemRule.getRuleNum().intValue();
                calendar.set(Calendar.DAY_OF_MONTH,calendar.get(Calendar.DAY_OF_MONTH)+day);
                stayIntegral.setStayRecordedTime(calendar.getTime());
                break;
            default:
                 break;
        }
        if(integralConfig == null){
            return false;
        }
        stayIntegral.setUserOpenId(openId);
        stayIntegral.setConfigId(integralConfig.getIntegralConfigId());
        stayIntegral.setStayNumber(stayNumber);

        stayIntegral.setStayStatus(1);
        Integer msg = memberMapper.insertStayIntegral(stayIntegral);
        if(msg > 0){
            //添加待入账积分
            Member memberInfo = memberMapper.getMemberByUserOpenId(openId);
            memberInfo.setUserStayIntegral(stayNumber);
            if(memberMapper.updateUserStayIntegralByOpenId(memberInfo) > 0){
                //添加日志
                UseIntegral useIntegral = new UseIntegral();
                useIntegral.setConfigId(integralConfig.getIntegralConfigId());
                useIntegral.setUserOpenId(openId);
                useIntegral.setIntegralType(3);
                useIntegral.setStayId(stayIntegral.getStayId());
                useIntegral.setIntegralNumber(stayNumber);
                useIntegral.setIntegralBeforeNumber(memberInfo.getUserSurplusIntegral());
                useIntegral.setIntegralEndNumber(memberInfo.getUserSurplusIntegral());
                useIntegral.setIntegralRemarks(integralConfig.getIntegralConfigName());
                //添加积分获取日志信息
                if(useIntegralMapper.addUseIntegral(useIntegral) < 1){
                    return false;
                }
            }
        }
        return true;
    }
}
