package cn.pluss.platform.job;

import cn.pluss.platform.entity.*;
import cn.pluss.platform.merchant.MerchantBaseInfoService;
import cn.pluss.platform.merchantOrder.MerchantOrderService;
import cn.pluss.platform.user.UserRoleService;
import cn.pluss.platform.userApp.UserAppService;
import cn.pluss.platform.userAssess.UserAssessService;
import cn.pluss.platform.util.DateUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.Scheduled;

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

/**
 * TODO
 * 校验用户的考核状态
 * @author crystal
 * @date 2022/7/11 10:13
 */
@Slf4j
// @Configuration
//@ConditionalOnProperty(prefix = "scheduling", name = "enabled", havingValue = "true")
public class CheckUserAssessStatus {

    @Resource
    private UserAssessService userAssessService;

    @Resource
    private UserRoleService userRoleService;

    @Resource
    private UserAppService userAppService;

    @Resource
    private MerchantBaseInfoService merchantBaseInfoService;

    @Resource
    private MerchantOrderService merchantOrderService;

    private static int DEFAULT_SIZE = 200;

    private static int DEFAULT_CURRENT = 1;

    /**
     * 每天凌晨12点执行
     * @date: 2022/1/14 14:14
     */
    @Scheduled(cron = "0 0 0 * * ?")
    public void checkBstUserAssessStatusJob(){
        log.info("检查倍斯特用户考核状态开始");
        LambdaQueryWrapper<UserRole> lambdaQuery = Wrappers.lambdaQuery();
        lambdaQuery.eq(UserRole::getValidFlag,UserRole.DEFAULT_VALID_OPEN);
        List<UserRole> list = userRoleService.list(lambdaQuery);
        if(list.isEmpty()){
            return;
        }
        Date now = DateUtils.currentDate();
        for (UserRole u:list) {
            startCheck(now,u,DEFAULT_CURRENT,DEFAULT_SIZE);
        }
        log.info("检查倍斯特用户考核状态结束");
    }

    public void startCheck(Date now,UserRole r,int current,int size) {
        Page<UserAssess> page = new Page<>(current,size);
        QueryWrapper<UserAssess> queryWrapper = new QueryWrapper<UserAssess>()
                .eq("roleCode",r.getRoleCode());
        page = userAssessService.page(page, queryWrapper);
        List<UserAssess> records = page.getRecords();
        for (UserAssess assess:records) {
            List<String> list = merchantBaseInfoService.queryNextMercCodeByUserId(assess.getUserId());
            assess.setDirectMercNum(list.size());
//            BigInteger validMercNum = BigInteger.ZERO;
//            for (String merchantCode:list) {
//                MerchantBaseInfo merchant = merchantBaseInfoService.getMerchantBaseInfoByMerchantCode(merchantCode);
//                if(merchant != null){
//                    if(merchant.getValidFlag()  == 1){
//                        validMercNum = validMercNum.add(BigInteger.ONE);
//                    }
////                    else{
////                        Map<String, Double> tradeAmtMap = merchantOrderService.getCountTradeAmt(merchantCode, DateUtils.toString(now, "yyyy-MM-dd"),null,null);
////                        BigDecimal tradeAmt = BigDecimal.valueOf(tradeAmtMap.get("tradeAmtCount"));
////                        boolean amtFlag = tradeAmt.compareTo(r.getValidTransAmt()) >= 0;
////                        int num = merchantOrderService.getValidNumByMerchantCode(merchantCode);
////                        boolean numFlag = num >= r.getMinNum();
////                        if(amtFlag && numFlag){
////                            validMercNum = validMercNum.add(BigInteger.ONE);
////                            MerchantBaseInfo update = new MerchantBaseInfo();
////                            update.setId(merchant.getId());
////                            update.setValidFlag(1);
////                            merchantBaseInfoService.updateById(update);
////                        }
////                    }
//                }
//            }
            //计算用户达标数量
        }
        
        if(page.getSize() < size){
            this.startCheck(now,r,current++,size);
        }
    }
}
