package com.hai.service.impl;

import com.hai.common.exception.ErrorCode;
import com.hai.common.exception.ErrorHelp;
import com.hai.common.exception.SysCode;
import com.hai.common.utils.DateUtil;
import com.hai.common.utils.IDGenerator;
import com.hai.dao.HighActivityAwardActualPrizeMapper;
import com.hai.dao.HighActivityAwardMapper;
import com.hai.dao.HighActivityAwardSerialNumMapper;
import com.hai.dao.HighActivityPrizeMapper;
import com.hai.entity.*;
import com.hai.enum_type.ActivityAwardStatus;
import com.hai.enum_type.ActivityInfoStatus;
import com.hai.service.HighActivityAwardService;
import com.hai.service.HighDiscountAgentCodeService;
import com.hai.service.HighDiscountAgentRelService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Service("activityAwardService")
public class HighActivityAwardServiceImpl implements HighActivityAwardService {

    @Resource
    private HighActivityAwardMapper highActivityAwardMapper;

    @Resource
    private HighActivityPrizeMapper highActivityPrizeMapper;

    @Resource
    private HighActivityAwardSerialNumMapper highActivityAwardSerialNumM;

    @Resource
    private HighDiscountAgentRelService highDiscountAgentRelService;

    @Resource
    private HighDiscountAgentCodeService highDiscountAgentCodeService;

    @Resource
    private HighActivityAwardActualPrizeMapper highActivityAwardActualPrizeM;


    @Override
    public List<HighActivityAward> getListByActivityId(Long activityId) {
        HighActivityAwardExample example = new HighActivityAwardExample();
        example.createCriteria().andActivityInfoIdEqualTo(activityId).andStatusNotEqualTo(0);
        return highActivityAwardMapper.selectByExample(example);
    }

    @Override
    @Transactional(rollbackFor=Exception.class,propagation= Propagation.REQUIRES_NEW,timeout = 300)
    public void editHighActivateAward(HighActivityAward highActivityAward, List<HighActivityPrize> highActivityPrizes)  throws Exception  {

        // 增加奖励
        if (highActivityAward.getId() == null) {
            highActivityAward.setCreateTime(new Date());
            highActivityAward.setUpdateTime(new Date());
            highActivityAward.setStatus(ActivityAwardStatus.status1.getNumber());
            highActivityAwardMapper.insert(highActivityAward);
        } else {
            highActivityAward.setUpdateTime(new Date());
            highActivityAwardMapper.updateByPrimaryKey(highActivityAward);
        }

        // 增加奖品
        for (HighActivityPrize list : highActivityPrizes) {
            list.setActivityAwardId(highActivityAward.getId());
            list.setCreateTime(new Date());
            list.setUpdateTime(new Date());
            list.setStatus(1);
            highActivityPrizeMapper.insert(list);
        }



        HighActivityAwardSerialNum serialNum;
        HighActivityAwardActualPrize actualPrize;

        // 生成中奖编号
        for (int i = 0; i < highActivityAward.getNum(); i++) {
            serialNum = new HighActivityAwardSerialNum();
            serialNum.setActivityInfoId(highActivityAward.getActivityInfoId());
            serialNum.setActivityAwardId(highActivityAward.getId());
            serialNum.setSerialNum("HFA" + DateUtil.date2String(new Date(),"yyyyMMddHHmmss") + IDGenerator.nextId(5));
            serialNum.setStatus(1);
            serialNum.setCreateTime(new Date());
            highActivityAwardSerialNumM.insert(serialNum);

            // 循环奖品
            for (HighActivityPrize list : highActivityPrizes) {


                // 奖品类型 1:卡券 2：优惠券
                if (list.getPrizeType() == 2) {
                    int startNum = 0;
                    int endNum = 0;
                    endNum += list.getPrizeNum();
                    HighDiscountAgentRel discountAgentRel = highDiscountAgentRelService.getRelByDiscountAgent(list.getPrizeId() , Long.valueOf(list.getExt1()));

                    List<HighDiscountAgentCode> codeList = highDiscountAgentCodeService.getDiscountCodeByDiscountAgentId(discountAgentRel.getId());

                    if (list.getPrizeNum() > codeList.size()) {
                        throw ErrorHelp.genException(SysCode.System, ErrorCode.ADD_DATA_ERROR, "");
                    }

                    for (HighDiscountAgentCode agentCode:  codeList.subList(startNum, endNum)) {

                        agentCode.setStatus(5);
                        actualPrize = new HighActivityAwardActualPrize();
                        actualPrize.setActivityAwardPrizeId(list.getId());
                        actualPrize.setActivityAwardSerialNumId(serialNum.getId());
                        actualPrize.setActualPrizeType(2);
                        actualPrize.setActualPrizeId(agentCode.getId());
                        actualPrize.setStatus(1);
                        actualPrize.setCreateTime(new Date());
                        highActivityAwardActualPrizeM.insert(actualPrize);
                        highDiscountAgentCodeService.updateCode(agentCode);
                    }
                    startNum += list.getPrizeNum();
                }



            }
        }

    }

    @Override
    public HighActivityAward getDetailById(Long id) {
        return highActivityAwardMapper.selectByPrimaryKey(id);
    }

    @Override
    @Transactional(rollbackFor=Exception.class,propagation= Propagation.REQUIRES_NEW)
    public void deleteActivateAward(Long id) {
        HighActivityPrizeExample example = new HighActivityPrizeExample();
        example.createCriteria().andActivityAwardIdEqualTo(id);
        List<HighActivityPrize> prize =  highActivityPrizeMapper.selectByExample(example);

        for (HighActivityPrize p : prize) {

            HighActivityAwardActualPrizeExample prizeExample = new HighActivityAwardActualPrizeExample();
            prizeExample.createCriteria().andActivityAwardPrizeIdEqualTo(p.getId());

            List<HighActivityAwardActualPrize> actualPrizeList = highActivityAwardActualPrizeM.selectByExample(prizeExample);
            HighDiscountAgentCode code;
            for (HighActivityAwardActualPrize actualPrize: actualPrizeList ) {
                code = highDiscountAgentCodeService.getCodeById(actualPrize.getActualPrizeId());

                code.setStatus(1);

                highDiscountAgentCodeService.updateCode(code);

                highActivityAwardActualPrizeM.deleteByPrimaryKey(actualPrize.getId());
            }

            highActivityPrizeMapper.deleteByPrimaryKey(p.getId());
        }

        HighActivityAwardSerialNumExample numExample   = new HighActivityAwardSerialNumExample();
        numExample.createCriteria().andActivityAwardIdEqualTo(id);

        List<HighActivityAwardSerialNum> serialNumList = highActivityAwardSerialNumM.selectByExample(numExample);

        for (HighActivityAwardSerialNum serialNum: serialNumList) {

            highActivityAwardSerialNumM.deleteByPrimaryKey(serialNum.getId());
        }

        highActivityAwardMapper.deleteByPrimaryKey(id);
    }

    @Override
    public List<HighActivityAward> checkNumberPrizes(Long id) {

        HighActivityAwardExample example = new HighActivityAwardExample();
        example.createCriteria().andActivityInfoIdEqualTo(id);

        List<HighActivityAward> activityAwardList = highActivityAwardMapper.selectByExample(example);

        for (HighActivityAward activityAward : activityAwardList) {
            HighActivityAwardSerialNumExample numExample = new HighActivityAwardSerialNumExample();
            numExample.createCriteria().andActivityAwardIdEqualTo(activityAward.getId()).andStatusEqualTo(1);
            activityAward.setExt3(String.valueOf(highActivityAwardSerialNumM.countByExample(numExample)));
        }

        return activityAwardList;

    }

    @Override
    @Transactional(rollbackFor=Exception.class,propagation= Propagation.REQUIRES_NEW)
    public void numberOfNewRewards(Long activityAwardId , Integer num)   throws Exception {

        HighActivityAward highActivityAward = highActivityAwardMapper.selectByPrimaryKey(activityAwardId);

        HighActivityAwardSerialNum serialNum;
        HighActivityAwardActualPrize actualPrize;

        HighActivityPrizeExample prizeExample = new HighActivityPrizeExample();
        prizeExample.createCriteria().andActivityAwardIdEqualTo(activityAwardId);

        List<HighActivityPrize> highActivityPrizes = highActivityPrizeMapper.selectByExample(prizeExample);

        highActivityAward.setNum(highActivityAward.getNum() + num);

        highActivityAwardMapper.updateByPrimaryKey(highActivityAward);

        // 生成中奖编号
        for (int i = 0; i < num; i++) {
            serialNum = new HighActivityAwardSerialNum();
            serialNum.setActivityInfoId(highActivityAward.getActivityInfoId());
            serialNum.setActivityAwardId(highActivityAward.getId());
            serialNum.setSerialNum("HFA" + DateUtil.date2String(new Date(),"yyyyMMddHHmmss") + IDGenerator.nextId(5));
            serialNum.setStatus(1);
            serialNum.setCreateTime(new Date());
            highActivityAwardSerialNumM.insert(serialNum);

            // 循环奖品
            for (HighActivityPrize list : highActivityPrizes) {


                // 奖品类型 1:卡券 2：优惠券
                if (list.getPrizeType() == 2) {
                    int startNum = 0;
                    int endNum = 0;
                    endNum += list.getPrizeNum();
                    HighDiscountAgentRel discountAgentRel = highDiscountAgentRelService.getRelByDiscountAgent(list.getPrizeId() , Long.valueOf(list.getExt1()));

                    List<HighDiscountAgentCode> codeList = highDiscountAgentCodeService.getDiscountCodeByDiscountAgentId(discountAgentRel.getId());

                    if (list.getPrizeNum() > codeList.size()) {
                        throw ErrorHelp.genException(SysCode.System, ErrorCode.ADD_DATA_ERROR, "");
                    }

                    for (HighDiscountAgentCode agentCode:  codeList.subList(startNum, endNum)) {

                        agentCode.setStatus(5);

                        actualPrize = new HighActivityAwardActualPrize();
                        actualPrize.setActivityAwardPrizeId(list.getId());
                        actualPrize.setActivityAwardSerialNumId(serialNum.getId());
                        actualPrize.setActualPrizeType(2);
                        actualPrize.setActualPrizeId(agentCode.getId());
                        actualPrize.setStatus(1);
                        actualPrize.setCreateTime(new Date());
                        highActivityAwardActualPrizeM.insert(actualPrize);
                        highDiscountAgentCodeService.updateCode(agentCode);
                    }
                    startNum += list.getPrizeNum();
                }



            }
        }
    }
}
