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.second.BpmOASecondCreateReqVO;
import cn.iocoder.yudao.module.bpm.controller.admin.upload.UploadUserFile;
import cn.iocoder.yudao.module.bpm.convert.oa.BpmOASecondConvert;
import cn.iocoder.yudao.module.bpm.dal.dataobject.oa.BpmOASecondDO;
import cn.iocoder.yudao.module.bpm.dal.mysql.oa.BpmOASecondMapper;
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_SECOND_NOT_EXISTS;

/**
 * 功能描述
 * OA 借调申请 Service 实现类
 *
 *  @author 符溶馨
 */
@Service
@Validated
public class BpmOASecondServiceImpl extends BpmOABaseService implements BpmOASecondService{

    /**
     * OA 请假对应的流程定义 KEY
     */
    public static final String PROCESS_KEY = "oa_second";

    @Resource
    private BpmOASecondMapper secondMapper ;

    @Resource
    private BpmProcessInstanceApi processInstanceApi;

    @Override
    public Long createSecond(Long userId, BpmOASecondCreateReqVO createReqVO) {
        BpmOASecondDO second = BpmOASecondConvert.INSTANCE.convert(createReqVO).setUserId(userId)
                .setResult(BpmProcessInstanceResultEnum.PROCESS.getResult());
        secondMapper.insert(second) ;

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

        // 将工作流的编号，更新到 OA 请假单中
        secondMapper.updateById(new BpmOASecondDO().setId(second.getId()).setProcessInstanceId(processInstanceId));

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

    @Override
    public void updateSecondResult(Long id, Integer result) {
        validateLeaveExists(id);
        secondMapper.updateById(new BpmOASecondDO().setId(id).setResult(result));
    }

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

    @Override
    public BpmOASecondDO getSecond(Long id) {
        
        return secondMapper.selectById(id);
    }
}
