package org.jeecg.modules.flowpath.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.apache.shiro.SecurityUtils;
import org.jeecg.common.exception.JeecgBootException;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.modules.flowpath.convert.FlowPathConvert;
import org.jeecg.modules.flowpath.dto.ItdmCreateRunFlowPath;
import org.jeecg.modules.flowpath.entity.ItdmFlowPath;
import org.jeecg.modules.flowpath.entity.ItdmFlowPathStep;
import org.jeecg.modules.flowpath.entity.ItdmRunFlowPath;
import org.jeecg.modules.flowpath.entity.ItdmRunFlowPathStep;
import org.jeecg.modules.flowpath.mapper.ItdmFlowPathMapper;
import org.jeecg.modules.flowpath.mapper.ItdmFlowPathStepMapper;
import org.jeecg.modules.flowpath.mapper.ItdmRunFlowPathMapper;
import org.jeecg.modules.flowpath.mapper.ItdmRunFlowPathStepMapper;
import org.jeecg.modules.flowpath.service.IItdmRunFlowPathService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Objects;

/**
 * @Description: 运行流程表
 * @Author: jeecg-boot
 * @Date:   2023-07-20
 * @Version: V1.0
 */
@Service
public class ItdmRunFlowPathServiceImpl extends ServiceImpl<ItdmRunFlowPathMapper, ItdmRunFlowPath> implements IItdmRunFlowPathService {
    @Autowired
    @SuppressWarnings("all")
    private ItdmFlowPathMapper itdmFlowPathMapper;

    @Autowired
    @SuppressWarnings("all")
    private ItdmFlowPathStepMapper itdmFlowPathStepMapper;


    @Autowired
    @SuppressWarnings("all")
    private ItdmRunFlowPathMapper itdmRunFlowPathMapper;

    @Autowired
    @SuppressWarnings("all")
    private ItdmRunFlowPathStepMapper itdmRunFlowPathStepMapper;


    @Override
    @Transactional
    public void createRunFlowPath(ItdmCreateRunFlowPath itdmCreateRunFlowPath) {
        ItdmFlowPath itdmFlowPath = itdmFlowPathMapper.selectById(itdmCreateRunFlowPath.getFlowPathId());

        if (itdmFlowPath == null || !Objects.equals(itdmFlowPath.getStatus(), "0")) {
            throw new JeecgBootException("流程不存在/流程禁用, 发起失败");
        }

        List<ItdmFlowPathStep> list = itdmFlowPathStepMapper.selectList(Wrappers.lambdaQuery(ItdmFlowPathStep.class)
                .eq(ItdmFlowPathStep::getFlowPathId, itdmCreateRunFlowPath.getFlowPathId()).orderByAsc(ItdmFlowPathStep::getSort));

        LoginUser user = (LoginUser) SecurityUtils.getSubject().getPrincipal();


        ItdmRunFlowPath itdmRunFlowPath = new ItdmRunFlowPath();
        itdmRunFlowPath.setFlowPathId(itdmCreateRunFlowPath.getFlowPathId());
        itdmRunFlowPath.setWeituoNo(itdmCreateRunFlowPath.getWeituoNo());
        itdmRunFlowPath.setName(itdmFlowPath.getName());
        itdmRunFlowPath.setFaqiUser(user.getUsername());
        itdmRunFlowPath.setDqSetp(list.get(0).getId());
        itdmRunFlowPath.setDqSetpUserIds(list.get(0).getUserIds());
        itdmRunFlowPath.setDqSetpRoleId(list.get(0).getRoleId());



        itdmRunFlowPath.setStatus("0");
        itdmRunFlowPathMapper.insert(itdmRunFlowPath);


        for (int i = 0; i < list.size(); i++) {
            ItdmFlowPathStep itdmFlowPathStep = list.get(i);
            // todo
            ItdmRunFlowPathStep itdmRunFlowPathStep = FlowPathConvert.INSTANCE.to(itdmFlowPathStep, null,itdmRunFlowPath.getId());
            itdmRunFlowPathStepMapper.insert(itdmRunFlowPathStep);
        }


    }
}
