package cn.pluss.platform.openAccount.impl;

import cn.pluss.platform.entity.MerchantBaseInfo;
import cn.pluss.platform.entity.OpenAccountConfig;
import cn.pluss.platform.entity.UserApp;
import cn.pluss.platform.exception.MsgException;
import cn.pluss.platform.mapper.OpenAccountConfigMapper;
import cn.pluss.platform.openAccount.OpenAccountConfigService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

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

/**
 * <p>
 * 开户奖配置表 服务实现类
 * </p>
 *
 * @author crystal
 * @since 2022-06-14
 */
@Service
@Slf4j
public class OpenAccountConfigServiceImpl extends ServiceImpl<OpenAccountConfigMapper, OpenAccountConfig> implements OpenAccountConfigService {


    @Override
    public BigDecimal getOpendAccountReward(MerchantBaseInfo merchant, UserApp userApp) {
        BigDecimal rewardAmt = BigDecimal.ZERO;
        String addressNo = merchant.getAddressNo();
        List<OpenAccountConfig> list = baseMapper.listByRoleCode(userApp.getRoleCode());
        if(list.isEmpty()){
            return BigDecimal.ZERO;
        }
        try {
            String[] areaCodeInfo = addressNo.split("-");
            for (OpenAccountConfig config :list) {
                if(OpenAccountConfig.DEFAULT_TYPE.equals(config.getType())){
                    return config.getRewardAmt();
                }else{
                    if(config.getProvinceCode().equals(areaCodeInfo[0]) &&
                            config.getCityCode().equals(areaCodeInfo[1]) && config.getAreaCode().equals(areaCodeInfo[2])){
                        rewardAmt = config.getRewardAmt();
                    }else if(config.getProvinceCode().equals(areaCodeInfo[0]) &&
                            config.getCityCode().equals(areaCodeInfo[1])){
                        rewardAmt = config.getRewardAmt();
                    }else if(config.getProvinceCode().equals(areaCodeInfo[0])){
                        rewardAmt = config.getRewardAmt();
                    }
                }
            }
        }catch (Exception e){
            e.printStackTrace();
            log.error("【开户奖励配置】数据获取异常，异常信息：{}",e.getMessage());
        }
        return rewardAmt;
    }

    /**
     *
     * @param entity
     * @return
     */
    @Override
    public boolean save(OpenAccountConfig entity) {
        entity.checkParams();
        QueryWrapper<OpenAccountConfig> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("roleCode",entity.getRoleCode()).eq("type",OpenAccountConfig.DEFAULT_TYPE);
        List<OpenAccountConfig> list = baseMapper.selectList(queryWrapper);
        if(list.isEmpty() && !entity.getType().equals(OpenAccountConfig.DEFAULT_TYPE)){
            MsgException.throwException("请先添加一个类型为默认的开户奖配置，当未匹配到该地区的开户奖时，会选取默认配置的开户奖进行发放");
        }
        if(!list.isEmpty() && entity.getType().equals(OpenAccountConfig.DEFAULT_TYPE)){
            MsgException.throwException("默认的类型的开户奖配置有且只能配置一个");
        }
        if(entity.getType().equals(OpenAccountConfig.AREA_TYPE)){
            QueryWrapper<OpenAccountConfig> checkWapper = new QueryWrapper<>();
            checkWapper.eq("type",entity.getType());
            int level = entity.getLevel();
            if(level == 1){
                checkWapper.eq("provinceCode",entity.getProvinceCode());
            }else if(level == 2){
                checkWapper.eq("provinceCode",entity.getProvinceCode());
                checkWapper.eq("cityCode",entity.getCityCode());
            }else if(level == 3){
                checkWapper.eq("provinceCode",entity.getProvinceCode());
                checkWapper.eq("cityCode",entity.getCityCode());
                checkWapper.eq("areaCode",entity.getAreaCode());
            }else{
                MsgException.throwException("等级level参数异常");
            }
            List<OpenAccountConfig> checklist = baseMapper.selectList(checkWapper);
            if(!checklist.isEmpty()){
                MsgException.throwException("已存在一个当前区域的配置");
            }
        }
        baseMapper.insert(entity);
        return true;
    }
}
