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

import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
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.entry.BpmOAEntryCreateReqVO;
import cn.iocoder.yudao.module.bpm.convert.oa.BpmOAEntryConvert;
import cn.iocoder.yudao.module.bpm.dal.dataobject.oa.BpmOAEntryDO;
import cn.iocoder.yudao.module.bpm.dal.mysql.oa.BpmOAEntryMapper;
import cn.iocoder.yudao.module.bpm.enums.task.BpmProcessInstanceResultEnum;
import cn.iocoder.yudao.module.bpm.service.task.BpmHistoryProcessInstanceService;
import cn.iocoder.yudao.module.system.api.dept.DeptApi;
import cn.iocoder.yudao.module.system.api.dept.dto.DeptRespDTO;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.time.LocalDate;
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.framework.security.core.util.SecurityFrameworkUtils.getLoginUserId;
import static cn.iocoder.yudao.module.bpm.enums.ErrorCodeConstants.OA_ENTRY_NOT_EXISTS;

/**
 * OA 入职申请 Service 实现类
 *
 * @author 符溶馨
 */
@Service
@Validated
public class BpmOAEntryServiceImpl implements BpmOAEntryService {

    /**
     * OA 入职申请对应的流程定义 KEY
     */
    public static final String PROCESS_KEY = "oa_entry_2";

    @Resource
    private BpmOAEntryMapper entryMapper;

    @Resource
    private BpmProcessInstanceApi processInstanceApi;

    @Resource
    private BpmHistoryProcessInstanceService historyProcessInstanceService;

    @Resource
    private DeptApi deptApi;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Long createEntry(BpmOAEntryCreateReqVO createReqVO) {

        //插入OA 入职申请
        BpmOAEntryDO entry = BpmOAEntryConvert.INSTANCE.convert(createReqVO)
                .setUserId(getLoginUserId())
                .setResult(BpmProcessInstanceResultEnum.PROCESS.getResult());
        entryMapper.insert(entry);

        // 获取入职部门信息
        DeptRespDTO dto = deptApi.getDept(entry.getEntryDeptId()).getCheckedData();

        Map<String, Object> processInstanceVariables = new HashMap<>();
        //配置入职部门flag
        processInstanceVariables.put("entry_dept_flag", dto.getFlag());
        // 发起 BPM 流程
        String processInstanceId = processInstanceApi.createProcessInstance(entry.getUserId(),
                new BpmProcessInstanceCreateReqDTO().setProcessDefinitionKey(PROCESS_KEY)
                        .setVariables(processInstanceVariables).setBusinessKey(String.valueOf(entry.getId()))).getCheckedData();

        // 判断是否为重新发起的流程
        if (createReqVO.getProcessInstanceId() != null && createReqVO.getResult() == 3) {

            historyProcessInstanceService.createHistoryProcessInstance(processInstanceId, createReqVO.getProcessInstanceId());
        }

        // 将工作流的编号，更新到 OA 入职申请单中
        entryMapper.updateById(new BpmOAEntryDO().setId(entry.getId()).setProcessInstanceId(processInstanceId));

        return entry.getId();
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateEntryResult(String processInstanceId, Long id, Integer result) {

        validateLeaveExists(id);

        BpmOAEntryDO updateVO = new BpmOAEntryDO().setId(id).setResult(result);

        // 更新入职申请状态
        entryMapper.updateById(updateVO);
    }

    private void validateLeaveExists(Long id) {

        BpmOAEntryDO entry = entryMapper.selectById(id);
        if (entry == null) {
            throw exception(OA_ENTRY_NOT_EXISTS);
        }
    }

    @Override
    public BpmOAEntryDO getEntry(Long id) {

        return entryMapper.selectById(id);
    }

    @Override
    public BpmOAEntryDO getEntryByProcessInstanceId(String processInstanceId) {

        return entryMapper.selectOne(BpmOAEntryDO::getProcessInstanceId, processInstanceId);
    }

    @Override
    public List<BpmOAEntryDO> getEntryListByTime(LocalDate entryDate) {

        return entryMapper.selectList(new LambdaQueryWrapperX<BpmOAEntryDO>()
                .eq(BpmOAEntryDO::getEntryDate, entryDate)
                .eq(BpmOAEntryDO::getResult, BpmProcessInstanceResultEnum.APPROVE.getResult()));
    }
}
