package com.hzs.activity.work.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hzs.activity.base.service.IAcApprovalLogService;
import com.hzs.activity.base.service.IAcBaseConfigService;
import com.hzs.activity.people.vo.AcBaseConfigVO;
import com.hzs.activity.work.mapper.AcSuperWorkRuleConfigMapper;
import com.hzs.activity.work.service.IAcSuperWorkRuleConfigService;
import com.hzs.common.core.constant.msg.ConfigMsgConstants;
import com.hzs.common.core.domain.R;
import com.hzs.common.core.enums.EAgentApprovalStatus;
import com.hzs.common.core.enums.EApprovalBusiness;
import com.hzs.common.core.enums.EOrderPrefix;
import com.hzs.common.core.enums.ESignType;
import com.hzs.common.core.utils.CommonUtil;
import com.hzs.common.core.utils.TransactionUtils;
import com.hzs.common.domain.activity.base.AcApprovalLog;
import com.hzs.common.domain.activity.work.AcSuperWorkRuleConfig;
import com.hzs.system.sys.IApprovalServiceApi;
import com.hzs.system.sys.dto.ApprovalBusinessResultDTO;
import com.hzs.system.sys.dto.ApprovalSubmitDTO;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * <p>
 * 超级工作室赠送 服务实现类
 * </p>
 *
 * @author hzs
 * @since 2023-04-21
 */
@Service
public class AcSuperWorkRuleConfigServiceImpl extends ServiceImpl<AcSuperWorkRuleConfigMapper, AcSuperWorkRuleConfig> implements IAcSuperWorkRuleConfigService {


    @DubboReference
    private IApprovalServiceApi approvalServiceApi;
    @Autowired
    private IAcApprovalLogService iAcApprovalLogService;
    @Autowired
    private IAcBaseConfigService iAcBaseConfigService;

    /**
     * @description: 添加超级工作室赠品发起签呈
     * @author: zhang jing
     * @date: 2023/4/22 10:23
     * @param: [supVO]
     * @return: boolean
     **/
    @Override
    public boolean petition(AcBaseConfigVO supVO) {
        //业务单号
        String code = CommonUtil.createSerialNumber(EOrderPrefix.ACTIVITY_CODE.getValue());
        Date date = new Date();
        //添加日志信息
        AcApprovalLog acLog = new AcApprovalLog();
        acLog.setApprovalType(EApprovalBusiness.SUPE_RWORK.getValue());
        acLog.setAfterData(JSONObject.toJSONString(supVO));
        acLog.setApprovalStatus(EAgentApprovalStatus.UNAPPROVED.getValue());
        acLog.setBusinessCode(code);
        acLog.setPkCreator(supVO.getLoginUser().getUserId());
        acLog.setCreationTime(date);
        acLog.setPkCountry(supVO.getPkCountry());
        acLog.setControlType(supVO.getControlType());
        iAcApprovalLogService.save(acLog);
        //生成签呈
        ApprovalSubmitDTO approvalSubmitDTO = ApprovalSubmitDTO.builder().eApprovalBusiness(EApprovalBusiness.SUPE_RWORK)
                .businessCode(code)
                .signType(ESignType.getEnumByValue(supVO.getSignType()))
                .remark(supVO.getRemark())
                .userIdList(supVO.getUserIdList()).build();
        R<String> submit = approvalServiceApi.submit(approvalSubmitDTO, supVO.getLoginUser());
        if (!submit.isSuccess()) {
            log.error("====" + submit.getData());
            throw new RuntimeException(TransactionUtils.getContent(ConfigMsgConstants.SUPE_RWORK_SAVE_PETITION));
        }
        return true;
    }

    /**
     * @description: 超级工作室审批
     * @author: zhang jing
     * @date: 2023/4/26 16:42
     * @param: [abrDTO]
     * @return: java.lang.String
     **/
    @Override
    public String approval(ApprovalBusinessResultDTO abrDTO) {
        return null;
    }


}
