package cn.pluss.platform.userAssess.impl;

import cn.pluss.platform.entity.MerchantBaseInfo;
import cn.pluss.platform.entity.UserApp;
import cn.pluss.platform.entity.UserAssess;
import cn.pluss.platform.entity.UserRole;
import cn.pluss.platform.enums.UserRoleEnum;
import cn.pluss.platform.mapper.UserAppMapper;
import cn.pluss.platform.mapper.UserAssessMapper;
import cn.pluss.platform.mapper.UserRoleMapper;
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 cn.pluss.platform.util.StringUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 用户考核表 服务实现类
 * </p>
 *
 * @author bzg
 * @since 2022-07-11
 */
@Service
public class UserAssessServiceImpl extends ServiceImpl<UserAssessMapper, UserAssess> implements UserAssessService {

    @Resource
    private UserAppMapper userAppMapper;

    @Resource
    private MerchantOrderService merchantOrderService;

    @Resource
    private UserRoleMapper userRoleMapper;

    @Resource
    private MerchantBaseInfoService merchantBaseInfoService;

    public static int DEFAULT_VALID_FLAG  = 1;
    /**
     * 校验该商户是否有效
     * @param mbi
     */
    @Override
    public void checkUserValid(MerchantBaseInfo mbi) {
        if(mbi.getValidFlag() == DEFAULT_VALID_FLAG){
            return;
        }
        UserApp userApp = userAppMapper.selectByUserId(mbi.getUserId());
        if(userApp == null){
            return;
        }
        if(!UserRoleEnum.ZY_BST.getCode().equals(userApp.getRoleCode())){
            return;
        }
        if(userApp.getParentId() == null){
            return;
        }
        UserApp pUser = userAppMapper.selectByUserId(userApp.getParentId());
        if(pUser == null || !UserRoleEnum.ZY_BST.getCode().equals(pUser.getRoleCode())){
            return;
        }
        if(!"agent".equals(pUser.getUserType()) || StringUtil.isEmpty(pUser.getRoleCode())){
            return;
        }
        UserRole role = userRoleMapper.getByRoleCode(pUser.getRoleCode());
        if(role == null){
            return;
        }
        Map<String, Double> countTradeAmt = merchantOrderService.getCountTradeAmt(mbi.getMerchantCode(), DateUtils.toString(DateUtils.currentDate(), "yyyy-MM-dd"), null, null);
        BigDecimal tradeAmt = BigDecimal.valueOf(countTradeAmt.get("tradeAmtCount"));
        boolean amtFlag = tradeAmt.compareTo(role.getValidTransAmt()) >= 0;
        int num = merchantOrderService.getValidNumByMerchantCode(mbi.getMerchantCode());
        boolean numFlag = num >= role.getValidTransNum();
        UserAssess assess = baseMapper.getByUserId(pUser.getUserId());
        if(assess == null){
            assess = new UserAssess(pUser,role);
        }
        List<String> codeList = merchantBaseInfoService.queryNextMercCodeByUserId(pUser.getUserId());
        assess.setDirectMercNum(codeList.size());
        if(amtFlag && numFlag){
            assess.setValidMercNum(assess.getValidMercNum() + 1);
            mbi.setValidFlag(DEFAULT_VALID_FLAG);
        }
        saveOrUpdate(assess);
    }

    /**
     * 获取用户的考核状态
     * @param userId
     * @return
     */
    @Override
    public boolean getUserValid(Long userId,UserApp userApp) {
        boolean flag = true;
        Date now = DateUtils.currentDate();
        if(userApp == null){
            userApp = userAppMapper.selectByUserId(userId);
        }
        if(!"agent".equals(userApp.getUserType())){
            return flag;
        }
        UserAssess byUserId = baseMapper.getByUserId(userId);
        try {
            if(byUserId == null){
                UserRole role = userRoleMapper.getByRoleCode(userApp.getRoleCode());
                UserAssess assess = new UserAssess(userApp,role);
                save(assess);
                return flag;
            }else{
                UserRole role = userRoleMapper.getByRoleCode(byUserId.getRoleCode());
                if(UserRole.DEFAULT_VALID_CLOSE == role.getValidFlag()){
                    return flag;
                }
                //是否白名单
                if(UserAssess.DEFAULT_IS_WHITE_YES == byUserId.getIsWhite()){
                    return flag;
                }
                if(now.compareTo(byUserId.getExpireTime()) <= 0){
                    return flag;
                }
                if(role.getMinNum() <= byUserId.getValidMercNum()){
                    return flag;
                }
                flag = false;
                return flag;
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return flag;
    }
}
