package cn.iocoder.yudao.module.bpm.service.oa;

import cn.iocoder.yudao.module.bpm.api.task.BpmProcessInstanceApi;
import cn.iocoder.yudao.module.bpm.api.task.dto.BpmProcessInstanceCreateReqDTO;
import cn.iocoder.yudao.module.bpm.controller.admin.oa.vo.incentive.BpmOAIncentiveCreateReqVO;
import cn.iocoder.yudao.module.bpm.controller.admin.upload.UploadUserFile;
import cn.iocoder.yudao.module.bpm.convert.oa.BpmOAIncentiveConvert;
import cn.iocoder.yudao.module.bpm.dal.dataobject.oa.BpmOAIncentiveDO;
import cn.iocoder.yudao.module.bpm.dal.mysql.oa.BpmOAIncentiveMapper;
import cn.iocoder.yudao.module.bpm.enums.task.BpmProcessInstanceResultEnum;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.bpm.enums.ErrorCodeConstants.OA_INCENTIVE_NOT_EXISTS;

/**
 * OA 奖惩申请 Service 实现类
 *
 * @author 符溶馨

 */
@Service
@Validated
public class BpmOAIncentiveServiceImpl extends BpmOABaseService implements BpmOAIncentiveService{

    /**
     * OA 奖惩对应的流程定义 KEY
     */
    public static final String PROCESS_KEY = "oa_incentive";

    @Resource
    private BpmOAIncentiveMapper incentiveMapper;

    @Resource
    private BpmProcessInstanceApi processInstanceApi;

    @Override
    public Long createIncentive(Long userId, BpmOAIncentiveCreateReqVO createReqVO) {

        //插入OA 奖惩申请
        BpmOAIncentiveDO incentive = BpmOAIncentiveConvert.INSTANCE.convert(createReqVO).setUserId(userId)
                .setResult(BpmProcessInstanceResultEnum.PROCESS.getResult());
        incentiveMapper.insert(incentive) ;

        // 发起 BPM 流程
        Map<String, Object> processInstanceVariables = new HashMap<>();
        String processInstanceId = processInstanceApi.createProcessInstance(userId,
                new BpmProcessInstanceCreateReqDTO().setProcessDefinitionKey(PROCESS_KEY)
                        .setVariables(processInstanceVariables).setBusinessKey(String.valueOf(incentive.getId()))).getCheckedData();

        // 将工作流的编号，更新到 OA 奖惩单中
        incentiveMapper.updateById(new BpmOAIncentiveDO().setId(incentive.getId()).setProcessInstanceId(processInstanceId));

        List<UploadUserFile> fileItems = createReqVO.getFileItems() ;
        //这里的逻辑，如果fileItems不为空，且有数据，那么说明是上传了附件的，则需要更工作流文件表对应的实例Id
        if (fileItems != null && !fileItems.isEmpty()) {
            uploadBpmFileProcessInstanceId(processInstanceId,fileItems) ;
        }
        return incentive.getId();
    }

    @Override
    public void updateIncentiveResult(Long id, Integer result) {

        validateLeaveExists(id);
        incentiveMapper.updateById(new BpmOAIncentiveDO().setId(id).setResult(result));
    }

    private void validateLeaveExists(Long id) {
        if (incentiveMapper.selectById(id) == null) {
            throw exception(OA_INCENTIVE_NOT_EXISTS);
        }
    }

    @Override
    public BpmOAIncentiveDO getIncentive(Long id) {

        return incentiveMapper.selectById(id);
    }
}
