package com.chaozhanggui.admin.system.task;

import cn.hutool.core.util.ObjectUtil;
import com.chaozhanggui.dao.system.dao.TbPlussMerchantOrderMapper;
import com.chaozhanggui.dao.system.dao.TbPlussUserPromotionMapper;
import com.chaozhanggui.dao.system.entity.TbPlussUserPromotion;
import com.chaozhanggui.dao.system.model.TaskOrderSum;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

@Component
@Slf4j
public class CustomerTask {


    @Autowired
    private TbPlussMerchantOrderMapper tbPlussMerchantOrderMapper;

    @Autowired
    private TbPlussUserPromotionMapper tbPlussUserPromotionMapper;


    public void customerTask(){
        List<TaskOrderSum> list= tbPlussMerchantOrderMapper.selectByUserId();
        if(ObjectUtil.isEmpty(list)||list.size()<=0){
            log.warn("没有待升级的创客信息");
            return;
        }
        for (TaskOrderSum taskOrderSum : list) {
            deal(taskOrderSum);
        }
    }

    private void deal(TaskOrderSum orderSum){


        TbPlussUserPromotion userPromotion= tbPlussUserPromotionMapper.selectByPrimaryKey(Integer.valueOf(orderSum.getUserId()));
        if(ObjectUtil.isEmpty(userPromotion)){
            log.warn("小微商户不存在或者状态异常");
            return;
        }

        if(orderSum.getConsumeFee().compareTo(new BigDecimal(20000))>=0&&orderSum.getConsumeFee().compareTo(new BigDecimal(50000))<0){
            userPromotion.setIsExtend("1");
        }

        if(orderSum.getConsumeFee().compareTo(new BigDecimal(50000))>=0){
            userPromotion.setIsExtend("2");
        }

        userPromotion.setUpdateTime(new Date());

        tbPlussUserPromotionMapper.updateByPrimaryKey(userPromotion);
    }
}
