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

import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson.JSONObject;
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.bonus.risk.CuMemberRiskControl;
import com.hzs.common.domain.member.detail.CuMemberServiceLog;
import com.hzs.common.security.utils.SecurityUtils;
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.stereotype.Service;

import java.util.List;

/**
 * 会员奖金-K值风控设置 服务实现类
 */
@Service
public class CuMemberRiskControlServiceImpl extends ServiceImpl<CuMemberRiskControlMapper, CuMemberRiskControl> implements ICuMemberRiskControlService {

    @DubboReference
    IApprovalServiceApi approvalServiceApi;
    @DubboReference
    ICuMemberServiceLogServiceApi memberServiceLogServiceApi;

    @Override
    public List<MemberRiskControlVo> queryByList(MemberRiskControlParam memberRiskControlParam) {
        return baseMapper.queryByList(memberRiskControlParam);
    }

    @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);
    }

    /**
     * 提交审批
     *
     * @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;

    }

}
