package com.hzs.bonus.risk.service.impl;

import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hzs.bonus.risk.mapper.CuMemberRiskControlMapper;
import com.hzs.bonus.risk.param.MemberRiskControlParam;
import com.hzs.bonus.risk.param.MemberRiskControlPetition;
import com.hzs.bonus.risk.service.ICuMemberRiskControlService;
import com.hzs.bonus.risk.vo.MemberRiskControlVo;
import com.hzs.common.core.domain.R;
import com.hzs.common.core.enums.*;
import com.hzs.common.core.utils.CommonUtil;
import com.hzs.common.core.utils.DateUtils;
import com.hzs.common.domain.activity.base.AcApprovalLog;
import com.hzs.common.domain.bonus.risk.CuMemberRiskControl;
import com.hzs.common.domain.member.detail.CuMemberServiceLog;
import com.hzs.common.domain.system.sys.SysApprovalBusinessMember;
import com.hzs.common.security.service.UserTokenService;
import com.hzs.common.security.utils.SecurityUtils;
import com.hzs.member.account.ICuMemberTradeServiceApi;
import com.hzs.member.detail.ICuMemberServiceLogServiceApi;
import com.hzs.system.sys.IApprovalServiceApi;
import com.hzs.system.sys.dto.ApprovalSubmitDTO;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 会员奖金-K值风控设置 服务实现类
 * </p>
 *
 * @author zhangjing
 * @since 2024-04-08
 */
@Service
public class CuMemberRiskControlServiceImpl extends ServiceImpl<CuMemberRiskControlMapper, CuMemberRiskControl> implements ICuMemberRiskControlService {
    @DubboReference
    private IApprovalServiceApi approvalServiceApi;
    @DubboReference
    private ICuMemberTradeServiceApi memberTradeServiceApi;
    @Autowired
    private UserTokenService userTokenService;
    @Autowired
    private RabbitTemplate rabbitTemplate;

    @DubboReference
    private ICuMemberServiceLogServiceApi memberServiceLogServiceApi;
    @Override
    public List<CuMemberRiskControl> queryCuMemberRiskControl(Date settleDate) {
        QueryWrapper<CuMemberRiskControl> queryWrapper = new QueryWrapper<>();
        queryWrapper.le("start_date", settleDate);
        queryWrapper.ge("end_date", settleDate);
        return baseMapper.selectList(queryWrapper);
    }
    /**
     * 列表查询
     * @param memberRiskControlParam
     * @return
     */
    @Override
    public List<MemberRiskControlVo> queryByList(MemberRiskControlParam memberRiskControlParam)
    {
        return baseMapper.queryByList(memberRiskControlParam);
    }


    /**
     * k值风控参数 审批
     * @param memberRiskControlPetition
     */
    @Override
    public void onLinePetition(MemberRiskControlPetition memberRiskControlPetition) {
        EApprovalBusiness approvalBusiness = null;
        if (memberRiskControlPetition.getControlType() == EControlType.SAVE.getValue()) {
            approvalBusiness = EApprovalBusiness.MEMBER_RISK_ADD;
        } else if (memberRiskControlPetition.getControlType() == EControlType.MODIFY.getValue()) {
            approvalBusiness = EApprovalBusiness.MEMBER_RISK_UPDATE;
        } else if (memberRiskControlPetition.getControlType() == EControlType.DELETE.getValue()) {
            approvalBusiness = EApprovalBusiness.MEMBER_RISK_REMOVE;
        }
        //业务单号
        String businessCode = CommonUtil.createSerialNumber(EOrderPrefix.RISK_CONTROL.getValue());
        Boolean b = submitApproval(memberRiskControlPetition, approvalBusiness,businessCode);
        //日志
//        MemberRiskControlParam memberRiskControlParam = memberRiskControlPetition.getMemberRiskControlParam();
//        memberRiskControlParam.setPkCountry(SecurityUtils.getPkCountry());
//        memberRiskControlParam.setPkCreator(SecurityUtils.getUserId());
//        memberRiskControlParam.setActType(EActType.PRODUCT_GIFT.getValue());
//        memberRiskControlParam.setBusinessCode(recommendParam.getActivityParam().getBusinessCode());
//        IActivityService.createBaseConfigLog(activityParam, new StringBuffer(), null, approvalBusiness.getValue(), false);
//        return b;
    }
    /**
     * 提交审批
     *
     * @param memberRiskControlPetition 抽奖参数
     */
    public Boolean submitApproval(MemberRiskControlPetition memberRiskControlPetition, EApprovalBusiness approvalBusiness,String businessCode) {
        //保存审批日志
        CuMemberServiceLog memberServiceLog = saveApprovalLog(memberRiskControlPetition, approvalBusiness,businessCode);
        ApprovalSubmitDTO approvalSubmitDTO = ApprovalSubmitDTO.builder().eApprovalBusiness(approvalBusiness)
                .businessCode(businessCode)
                .businessData(JSONObject.toJSONString(memberRiskControlPetition.getMemberRiskControlParam()))
                .signType(ESignType.getEnumByValue(memberRiskControlPetition.getSignType())).userIdList(memberRiskControlPetition.getUserIdList())
                .sendIdList(memberRiskControlPetition.getSendIdList()).remark(memberRiskControlPetition.getRemark()).fileList(memberRiskControlPetition.getFileList())
                .build();
        R<String> submit = approvalServiceApi.submit(approvalSubmitDTO, memberRiskControlPetition.getLoginUser());
        if (!submit.isSuccess()) {
            throw new RuntimeException("K值风控 提交审批失败！");
        }
        return submit.isSuccess();
    }


    /**
     * 保存活动审批日志信息
     *
     * @param memberRiskControlPetition   直推赠送参数
     * @param approvalBusiness 审批业务
     * @return boolean
     */
    public CuMemberServiceLog saveApprovalLog(MemberRiskControlPetition memberRiskControlPetition, EApprovalBusiness approvalBusiness,String businessCode) {
        Integer controlType = memberRiskControlPetition.getControlType();
        //查询变更前的数据
/*        String afterData = !controlType.equals(EControlType.SAVE.getValue()) ?
                JSONUtil.toJsonStr(showDetailInfo(recommendParam.getPkBaseId(), recommendParam.getPkCountry())) :
                JSONUtil.toJsonStr(recommendParam);*/
        String afterData = JSONUtil.toJsonStr(memberRiskControlPetition);
        String beforeData = controlType.equals(EControlType.SAVE.getValue()) ? JSONUtil.toJsonStr(memberRiskControlPetition) : null;

    //    MemberRiskControlParam memberRiskControlParam = memberRiskControlPetition.getMemberRiskControlParam();
        CuMemberServiceLog cuMemberServiceLog = new CuMemberServiceLog();
        cuMemberServiceLog.setEditType(approvalBusiness.getValue());
        cuMemberServiceLog.setPkMember(memberRiskControlPetition.getPkMember());
        cuMemberServiceLog.setNewValue(beforeData);
        cuMemberServiceLog.setBusinessNo(businessCode);
        cuMemberServiceLog.setApproveStatus(EApproveStatus.ALREADY_SUBMIT.getValue());
        cuMemberServiceLog.setApproveNumber(0);
        cuMemberServiceLog.setPkCountry(SecurityUtils.getPkCountry());
        cuMemberServiceLog.setPkCreator(SecurityUtils.getUserId());
        cuMemberServiceLog.setCreationTime(DateUtils.currentDateTime());
        memberServiceLogServiceApi.insert(cuMemberServiceLog);
        return cuMemberServiceLog;

    }

}
