package com.kxs.user.biz.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.LocalDateTimeUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.kxs.common.core.constant.SecurityConstants;
import com.kxs.common.core.util.AssertUtil;
import com.kxs.stat.api.feign.RemoteKxsStatService;
import com.kxs.system.api.feign.RemoteOldService;
import com.kxs.user.api.vo.kxsapp.shareholder.ShareholderGetShareholderAmountVO;
import com.kxs.user.biz.mapper.KxsShdTradeAmtMapper;
import com.kxs.user.api.model.KxsShdTradeAmt;
import com.kxs.user.biz.service.KxsShdTradeAmtService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.time.LocalDate;
import java.time.LocalDateTime;

/**
 * 股东大盘分红交易表(KxsShdTradeAmt)表服务实现类
 *
 * @author 系统
 * @since 2024-05-07 09:11:27
 */
@Service("kxsShdTradeAmtService")
@RequiredArgsConstructor
public class KxsShdTradeAmtServiceImpl extends ServiceImpl<KxsShdTradeAmtMapper, KxsShdTradeAmt> implements KxsShdTradeAmtService {
    private final KxsShdTradeAmtMapper kxsShdTradeAmtMapper;
    private final RemoteOldService remoteOldService;
    private final RemoteKxsStatService remoteKxsStatService;

    @Override
    public Object getShareholderAmount(String tradeMonth) {
        //格式校验
        AssertUtil.isSimpleMonthPattern(tradeMonth);
        tradeMonth = tradeMonth.split("-")[0] + tradeMonth.split("-")[1];
        KxsShdTradeAmt kxsShdTradeAmt = kxsShdTradeAmtMapper.selectOne(Wrappers.<KxsShdTradeAmt>query().lambda().eq(KxsShdTradeAmt::getTradeMonth, tradeMonth));
        ShareholderGetShareholderAmountVO shareholderGetShareholderAmountVO = new ShareholderGetShareholderAmountVO();
        if (kxsShdTradeAmt != null) {
            BeanUtil.copyProperties(kxsShdTradeAmt, shareholderGetShareholderAmountVO);
        }
        return shareholderGetShareholderAmountVO;
    }

    @Override
    public void prizePoolSynchronization(String para) {
        String month;
        //如果定时任务传了参数就使用定时任务的参数
        if(StrUtil.isNotBlank(para)){
            month = para;
        }else{
            month = LocalDateTimeUtil.format(LocalDate.now(), DatePattern.SIMPLE_MONTH_PATTERN);
        }


        //调用老系统 已弃用
//        HashMap<String, Object> param = new HashMap<>();
//        HashMap<String, Object> data = new HashMap<>();
//        data.put("UserId",1);
//        data.put("TradeMonthOrDate",month);
//        data.put("Kind","team");
//        param.put("value",JSON.toJSONString(data));
////        String param = "{UserId:1,TradeMonthOrDate:"+ month +",Kind:team}";
//        R result = remoteOldService.GetUserTradeAmout(param);
//        if(result.getStatus() == 1){
//            Map<String,Object> resultData = (Map<String, Object>) result.getData();
//            BigDecimal totalPosAmt = BigDecimal.valueOf((Double) resultData.get("TotalPosAmt"));
//        }

        BigDecimal statAmount = remoteKxsStatService.getStatAmount(month, SecurityConstants.FROM_IN);
        //有效交易额  稳定期交易额 * 1.5
        BigDecimal totalAmount = statAmount.multiply(new BigDecimal("1.5"));
        //累计奖金池   总交易额 * 万分之1.5  保留俩位小数
        BigDecimal bonusPool = totalAmount.multiply(new BigDecimal("1.5")).divide(new BigDecimal("10000"), 2, RoundingMode.HALF_UP);
        //高中低奖金池计算规则 总奖金池 / 3
        BigDecimal bonus = bonusPool.divide(new BigDecimal("3"), 2, RoundingMode.HALF_UP);
        KxsShdTradeAmt kxsShdTradeAmt = kxsShdTradeAmtMapper.selectOne(Wrappers.<KxsShdTradeAmt>query().lambda().eq(KxsShdTradeAmt::getTradeMonth, month));
        if (kxsShdTradeAmt == null) {
            kxsShdTradeAmt = new KxsShdTradeAmt();
            kxsShdTradeAmt.setCreateTime(LocalDateTime.now());
            kxsShdTradeAmt.setDelFlag("0");
            kxsShdTradeAmt.setTradeAmt(totalAmount);//有效交易额
            kxsShdTradeAmt.setTradeMonth(month);//月份
            kxsShdTradeAmt.setBonusPool(bonusPool);//总奖池
            kxsShdTradeAmt.setFirstBonus(bonus);//初级奖池
            kxsShdTradeAmt.setCenterBonus(bonus);//中级奖池
            kxsShdTradeAmt.setHighBonus(bonus);//高级奖池
            kxsShdTradeAmtMapper.insert(kxsShdTradeAmt);
        } else {
            kxsShdTradeAmt.setTradeAmt(totalAmount);//有效交易额
            kxsShdTradeAmt.setTradeMonth(month);//月份
            kxsShdTradeAmt.setBonusPool(bonusPool);//总奖池
            kxsShdTradeAmt.setFirstBonus(bonus);//初级奖池
            kxsShdTradeAmt.setCenterBonus(bonus);//中级奖池
            kxsShdTradeAmt.setHighBonus(bonus);//高级奖池
            kxsShdTradeAmt.setUpdateTime(LocalDateTime.now());
            kxsShdTradeAmtMapper.updateById(kxsShdTradeAmt);
        }
    }
}

