package com.hai.schedule;

import com.alibaba.fastjson.JSONObject;
import com.hai.common.exception.ErrorCode;
import com.hai.common.exception.ErrorHelp;
import com.hai.common.exception.SysCode;
import com.hai.common.utils.ResponseMsgUtil;
import com.hai.config.HuiLianTongConfig;
import com.hai.config.HuiLianTongUnionCardConfig;
import com.hai.dao.HighCouponAgentCodeMapper;
import com.hai.dao.HighCouponCodeOtherMapper;
import com.hai.dao.HighUserCouponMapper;
import com.hai.entity.*;
import com.hai.service.HighOrderService;
import com.hai.service.HighUserCouponService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.Scheduled;

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

/**
 * @Auther: 胡锐
 * @Description: 卡卷定时任务
 * @Date: 2021/3/27 15:39
 */
@Configuration
public class HighCouponSchedule {

    private static Logger log = LoggerFactory.getLogger(HighCouponSchedule.class);

    @Resource
    private HighUserCouponService highUserCouponService;

    @Resource
    private HighUserCouponMapper highUserCouponMapper;

    @Resource
    private HighCouponCodeOtherMapper highCouponCodeOtherMapper;

    @Resource
    private HighCouponAgentCodeMapper highCouponAgentCodeMapper;

    @Resource
    private HighOrderService highOrderService;

    @Resource
    private HuiLianTongConfig huiLianTongConfig;

    /**
     * @Author 胡锐
     * @Description 处理过期的卡券
     * @Date 2021/4/4 22:44
     **/
    @Scheduled(cron = "5 0 0 * * ?")   //每日凌晨12点5秒执行一次
    public void expiredCoupon() {
        // 查询过期的卡券
        List<HighUserCoupon> userCoupons = highUserCouponService.getOverdueCoupon();
        for (HighUserCoupon userCoupon : userCoupons) {
            try {
                highUserCouponService.expiredCoupon(userCoupon.getId());
            } catch (Exception e) {
                log.error("HighCouponSchedule --> expiredCoupon() error!", e);
            }
        }
    }

    /**
     * @Author 胡锐
     * @Description 处理汇联卡券状态
     * @Date 2021/4/4 22:44
     **/
    @Scheduled(cron="0 0/10 * * * ?")   //每10分钟执行一次
    public void handleCouponStatus() throws Exception {
        HighCouponCodeOtherExample example = new HighCouponCodeOtherExample();
        example.createCriteria().andStatusEqualTo(20).andTypeEqualTo(1);
        List<HighCouponCodeOther> otherList = highCouponCodeOtherMapper.selectByExample(example);

        for (HighCouponCodeOther couponCodeOther : otherList) {
            JSONObject jsonObject = HuiLianTongConfig.getPayOrderByCouNo(couponCodeOther.getCouNo());

            if (jsonObject.getString("respCode").equals("0000")) {
                JSONObject data = HuiLianTongUnionCardConfig.resolveResponse(jsonObject.getString("data"));

                if (data.getString("respCode").equals("0000") && data.getJSONObject("data").getInteger("state") != 20) {
                    couponCodeOther.setStatus(data.getJSONObject("data").getInteger("state"));
                    highCouponCodeOtherMapper.updateByPrimaryKey(couponCodeOther);

                    if (couponCodeOther.getCouponAgentCodeId() != null) {
                        HighCouponAgentCode agentCode = highCouponAgentCodeMapper.selectByPrimaryKey(couponCodeOther.getCouponAgentCodeId());
                        if (agentCode == null) {
                            agentCode.setStatus(3);
                            highCouponAgentCodeMapper.updateByPrimaryKey(agentCode);
                        }
                    }

                    if (couponCodeOther.getChildOrderId() != null) {
                        HighUserCouponExample userCouponExample = new HighUserCouponExample();
                        userCouponExample.createCriteria().andQrCodeImgEqualTo(couponCodeOther.getCouNo()).andStatusEqualTo(1);
                        List<HighUserCoupon> list = highUserCouponMapper.selectByExample(userCouponExample);
                        if (list.size() > 0) {
                            HighUserCoupon userCoupon = list.get(0);
                            if (data.getJSONObject("data").getInteger("state") == 40) {
                                userCoupon.setStatus(2);
                                userCoupon.setConsumeTime(new Date());
                            } else {
                                userCoupon.setStatus(0);
                            }
                            highUserCouponMapper.updateByPrimaryKey(userCoupon);
                        }
                        // 查询订单中的汇联通卡券全部已处理
                        HighCouponCodeOtherExample otherExample = new HighCouponCodeOtherExample();
                        otherExample.createCriteria().andChildOrderIdEqualTo(couponCodeOther.getChildOrderId()).andStatusEqualTo(20).andTypeEqualTo(1);
                        if (highCouponCodeOtherMapper.selectByExample(otherExample).size() == 0) {
                            highOrderService.childOrderComplete(couponCodeOther.getChildOrderId());
                        }
                    }

                }
            }
        }
    }

}
