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

import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.apache.shiro.SecurityUtils;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.modules.flowpath.entity.ItdmRunFlowPath;
import org.jeecg.modules.flowpath.entity.ItdmRunFlowPathLog;
import org.jeecg.modules.flowpath.entity.ItdmRunFlowPathStep;
import org.jeecg.modules.flowpath.mapper.ItdmRunFlowPathLogMapper;
import org.jeecg.modules.flowpath.mapper.ItdmRunFlowPathMapper;
import org.jeecg.modules.flowpath.mapper.ItdmRunFlowPathStepMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Description: 运行流程步骤表
 * @Author: jeecg-boot
 * @Date:   2023-07-20
 * @Version: V1.0
 */
@Service
public class ItdmFZRunFlowPathLogService {

    @Autowired
    @SuppressWarnings("all")
    private ItdmRunFlowPathLogMapper logMapper;
    @Autowired
    @SuppressWarnings("all")
    private ItdmRunFlowPathStepMapper stepMapper;

    public void insert(String runFlowPath, String dqRunflowpathSept, String nextstep, String type,String miaoshu) {

        List<ItdmRunFlowPathLog> list = logMapper.selectList(Wrappers.lambdaQuery(ItdmRunFlowPathLog.class)
                .eq(ItdmRunFlowPathLog::getRunFlowPath, runFlowPath));

        int sort = CollectionUtils.isEmpty(list) ? 1 : list.stream().mapToInt(i -> Integer.parseInt(i.getSort())).max().orElse(1) + 1;
        LoginUser user = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        ItdmRunFlowPathLog itdmRunFlowPathLog = new ItdmRunFlowPathLog();


        ItdmRunFlowPathStep dqSept = stepMapper.selectById(dqRunflowpathSept);




        ItdmRunFlowPathStep nextSept = stepMapper.selectById(nextstep);

        itdmRunFlowPathLog.setRunFlowPath(runFlowPath);
        itdmRunFlowPathLog.setRunFlowPathSetp(dqRunflowpathSept);
        itdmRunFlowPathLog.setRunFlowPathSetpName(dqSept.getName());
        itdmRunFlowPathLog.setMiaoshu(miaoshu);
        itdmRunFlowPathLog.setSort(sort + "");
        itdmRunFlowPathLog.setFqUser(user.getUsername());
        itdmRunFlowPathLog.setXiaRunFlowPathSetp(nextstep);
        itdmRunFlowPathLog.setXiaRunFlowPathSetpName(nextSept.getName());
        itdmRunFlowPathLog.setType(type);

        logMapper.insert(itdmRunFlowPathLog);
    }

}
