package com.hzs.member.address.provider;

import com.alibaba.fastjson2.JSONObject;
import com.hzs.common.core.domain.R;
import com.hzs.common.core.enums.EApprovalBusiness;
import com.hzs.common.core.enums.EApproveStatus;
import com.hzs.common.core.utils.StringUtils;
import com.hzs.common.domain.member.address.CuMemberGift;
import com.hzs.common.domain.member.address.CuMemberGiftRecord;
import com.hzs.common.security.utils.SecurityUtils;
import com.hzs.member.account.dto.MemberAccountAudit;
import com.hzs.member.address.ICuMemberGiftServiceApi;
import com.hzs.member.address.service.ICuMemberGiftRecordService;
import com.hzs.member.address.service.ICuMemberGiftService;
import com.hzs.system.sys.dto.ApprovalBusinessResultDTO;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Date;

/**
 * @Description: 会员礼包修改收货地址审批回调
 * @Author: ljc
 * @Time: 2022/9/24 13:53
 * @Classname: IMemberAccountAuditServiceProvider
 * @Package_name: com.hzs.member.account.provider
 */
@Slf4j
@DubboService
public class CuMemberGiftServiceProvider implements ICuMemberGiftServiceApi {

    @Autowired
    ICuMemberGiftRecordService cuMemberGiftRecordService;
    @Autowired
    ICuMemberGiftService cuMemberGiftService;

    @Override
    public R auditProcess(ApprovalBusinessResultDTO approvalBusinessResultDTO) {
        //审核状态(1=待提交,2=已提交,3=审核中,4=已驳回,5=已完成,6=已归档)
        EApproveStatus eApproveStatus = approvalBusinessResultDTO.getEApproveStatus();
        EApprovalBusiness eApprovalBusiness = approvalBusinessResultDTO.getEApprovalBusiness();
        String businessCode = approvalBusinessResultDTO.getBusinessCode();

        if (StringUtils.isEmpty(businessCode) || null == eApproveStatus || null == eApprovalBusiness) {
            return R.fail("提交审核缺少参数！");
        }
        int approveState = eApproveStatus.getValue();
        try {
            //会员礼包收货地址修改审批
            if (approveState == (EApproveStatus.FINISH.getValue())) {
                CuMemberGiftRecord cuMemberGiftRecord =  cuMemberGiftRecordService.getById(Integer.parseInt(businessCode));
                String newValue=cuMemberGiftRecord.getNewValue();
                if(StringUtils.isNotEmpty(newValue)){
                    JSONObject root = new JSONObject().parseObject(newValue);
                    Long pkId =root.getLong("pkId");
                    String giftAddress =root.getString("giftAddress");
                    String giftName =root.getString("giftName");
                    String giftPhone =root.getString("giftPhone");
                    Long giftProvince =root.getLong("giftProvince");
                    Long giftCity =root.getLong("giftCity");
                    Long giftCounty =root.getLong("giftCounty");
                    CuMemberGift cuMemberGift= new CuMemberGift();
                    cuMemberGift.setPkId(pkId);
                    cuMemberGift.setGiftAddress(giftAddress);
                    cuMemberGift.setGiftName(giftName);
                    cuMemberGift.setGiftPhone(giftPhone);
                    cuMemberGift.setGiftProvince(giftProvince);
                    cuMemberGift.setGiftCity(giftCity);
                    cuMemberGift.setGiftCounty(giftCounty);
                    cuMemberGift.setPkCountry(SecurityUtils.getPkCountry());
                    cuMemberGift.setPkModified(SecurityUtils.getUserId());
                    cuMemberGift.setModifiedTime(new Date());
                    cuMemberGiftService.updateById(cuMemberGift);
                }else{
                    return R.fail("提交审核缺少参数！");
                }
            }
            return R.ok();
        } catch (Exception e) {
            //会员礼包修改收货地址业务发生异常时，提醒审批流程事务回滚
            log.error("审核状态为： {} 的审核出错", approveState, e);
            return R.fail("会员礼包修改收货地址业务流程出错！");
        }
    }


    /**
     * TDO转业务参数
     *
     * @param approvalBusinessResultDTO 业务审批操作DTO
     * @param approveState              审核状态
     * @param module                    模块
     * @param businessCode              业务代码
     * @return MemberAccountAudit
     */
    private MemberAccountAudit dtoToParam(ApprovalBusinessResultDTO approvalBusinessResultDTO, Integer approveState, Integer module, String businessCode) {
        return MemberAccountAudit.builder()
                .approver(approvalBusinessResultDTO.getLoginUser().getUserId())
                .pkModified(approvalBusinessResultDTO.getLoginUser().getUserId())
                .approveState(approveState)
                .approveTime(approvalBusinessResultDTO.getOperationTime())
                .businessCode(businessCode).module(module)
                .pkRole(approvalBusinessResultDTO.getSysUser().getUserId())
                .roleName(approvalBusinessResultDTO.getSysUser().getUserName())
                .build();
    }


}
