package com.bonus.material.work.service.impl;

import com.bonus.common.biz.enums.TmTaskTypeEnum;
import com.bonus.common.core.exception.ServiceException;
import com.bonus.common.core.utils.DateUtils;
import com.bonus.common.security.utils.SecurityUtils;
import com.bonus.material.work.domain.SysWorkflowNode;
import com.bonus.material.work.domain.SysWorkflowRecord;
import com.bonus.material.work.domain.SysWorkflowRecordHistory;
import com.bonus.material.work.domain.SysWorkflowType;
import com.bonus.material.work.mapper.SysWorkflowNodeMapper;
import com.bonus.material.work.mapper.SysWorkflowRecordHistoryMapper;
import com.bonus.material.work.mapper.SysWorkflowRecordMapper;
import com.bonus.material.work.mapper.SysWorkflowTypeMapper;
import com.bonus.material.work.service.SysWorkflowRecordService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

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

@Service
public class SysWorkflowRecordServiceImpl implements SysWorkflowRecordService {

    @Resource
    private SysWorkflowRecordMapper sysWorkflowRecordMapper;

    @Resource
    private SysWorkflowTypeMapper sysWorkflowTypeMapper;

    @Resource
    private SysWorkflowRecordHistoryMapper sysWorkflowRecordHistoryMapper;

    @Resource
    private SysWorkflowNodeMapper sysWorkflowNodeMapper;

    @Override
    @Transactional
    public int addSysWorkflowRecord(int taskId, String taskCode, int taskType) {
        //获取审批流id
        SysWorkflowType sysWorkflowType = new SysWorkflowType();
        sysWorkflowType.setTaskType(taskType);
        sysWorkflowType.setIsEnable(1);
        List<SysWorkflowType> sysWorkflowTypeList =sysWorkflowTypeMapper.selectSysWorkflowTypeList(sysWorkflowType);
        if (sysWorkflowTypeList.isEmpty()){
            throw new ServiceException("新增任务失败,请创建审核流程");
        }
        sysWorkflowType = sysWorkflowTypeList.get(0);
        SysWorkflowRecord sysWorkflowRecord = new SysWorkflowRecord();
        sysWorkflowRecord.setWorkflowId(sysWorkflowType.getId());
        sysWorkflowRecord.setTaskId(taskId);
        sysWorkflowRecord.setTaskType(taskType);
        sysWorkflowRecord.setTaskCode(taskCode);
        //创建审批任务
        int count = sysWorkflowRecordMapper.addSysWorkflowRecord(sysWorkflowRecord);
        if (0 == count) {
            throw new RuntimeException("创建审批任务失败!");
        }
        // 获取新创建的ID
        int newId = sysWorkflowRecord.getId();
        //获取当前审核流下的节点
        SysWorkflowNode  sysWorkflowNode = new SysWorkflowNode();
        sysWorkflowNode.setTypeId(sysWorkflowType.getId());
        List<SysWorkflowNode> sysWorkflowNodeList = sysWorkflowNodeMapper.selectSysWorkflowNodeList(sysWorkflowNode);

        ///  2025-06-17 syruan : 任务要查询下个节点待审核的人员存入数据库
        // 获取当前登陆用户id，查询所在的分公司
        Long thisLoginUserDeptId = SecurityUtils.getLoginUser().getSysUser().getDeptId();
        if (null == thisLoginUserDeptId) {
            thisLoginUserDeptId = 0L;
        }


        // 创建审核记录
        SysWorkflowRecordHistory sysWorkflowRecordHistory = new SysWorkflowRecordHistory();
        sysWorkflowRecordHistory.setRecordId(sysWorkflowRecord.getId());
        sysWorkflowRecordHistory.setNodeId(sysWorkflowNodeList.get(0).getId());
        sysWorkflowRecordHistory.setNextNodeId(sysWorkflowNodeList.get(1).getId());
        sysWorkflowRecordHistory.setIsAccept(1);
        sysWorkflowRecordHistory.setCreateBy(SecurityUtils.getLoginUser().getUserid().toString());
        sysWorkflowRecordHistory.setCreateTime(DateUtils.getNowDate());
        sysWorkflowRecordHistoryMapper.addSysWorkflowRecordHistory(sysWorkflowRecordHistory);
        return newId;
    }
}
