package com.chinaweal.aiccs.aiccs.business.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.chinaweal.aiccs.aiccs.auditing.entity.TSOpinion;
import com.chinaweal.aiccs.aiccs.business.entity.BizProblemReport;
import com.chinaweal.aiccs.aiccs.business.entity.TSTaskList;
import com.chinaweal.aiccs.aiccs.business.mapper.BizProblemReportMapper;
import com.chinaweal.aiccs.aiccs.business.service.BizProblemReportService;
import com.chinaweal.aiccs.aiccs.business.service.TSTaskListService;
import com.chinaweal.aiccs.common.util.FlowUtil;
import com.chinaweal.aiccs.common.util.HandleWorkFlowUtil;
import com.chinaweal.aiccs.common.util.StringUtils;
import com.chinaweal.aicorg.model.AICUser;
import com.chinaweal.youfool.framework.springboot.exception.custom.BusinessException;
import com.chinaweal.youfool.framework.springboot.mybatis.plus.BaseServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 问题上报业务记录表 服务实现类
 * </p>
 *
 * @author <a href="https://blog.lroyia.top">lroyia</a>
 * @since 2021-04-21
 */
@Service
public class BizProblemReportServiceImpl extends BaseServiceImpl<BizProblemReportMapper, BizProblemReport> implements BizProblemReportService {

    @Autowired
    private TSTaskListService taskListService;

    @Override
    public IPage<BizProblemReport> simplePage(IPage<BizProblemReport> page, Map<String, Object> param) {
        return baseMapper.simplePage(page, param);
    }

    @Override
    public TSTaskList feedbackProblem(AICUser curUser, TSTaskList taskList, BizProblemReport... problems) {
        if (StringUtils.isBlank(taskList.getTasklistid())) {
            for (BizProblemReport info : problems) {
                info.setBizseq(taskList.getBizseqid()); // 流程启动人(主办人)
                info.setState("2"); // 流程启动把状态设置为 "处理中"
                saveOrUpdate(info);
            }
            String definedname = FlowUtil.PROBLEM_FEEDBACK_WORKFLOW;//问题上报模版
            //新待办
            taskList = HandleWorkFlowUtil.createTaskList(taskList, curUser);
            //开始工作流
            taskListService.startWorkFlow(taskList, curUser, definedname);
        }
        return taskList;
    }

    @Override
    public TSTaskList nextProcess(AICUser user, TSTaskList taskList, TSOpinion opinionModel, String nextNodeID, String[] nextPerformerids, String[] nextPerformerNames, BizProblemReport... problem) {
        try {
            String state = "2";
            if(FlowUtil.PROBLEM_FEEDBACK_WORKFLOW_OVERFEEDBACK.equals(nextNodeID)){
                // 不通过
                state = "4";
            }else if(FlowUtil.PROBLEM_FEEDBACK_WORKFLOW_ENDFEEDBACK.equals(nextNodeID)){
                // 通过
                state = "3";
            }
            for (BizProblemReport each : problem) {
                each.setState(state);
                updateById(each);
            }
            taskListService.updateWorkFlow(user, opinionModel, taskList, nextNodeID, nextPerformerids, nextPerformerNames);
        } catch (Exception e) {
            e.printStackTrace();
            throw new BusinessException("流程扭转出错!");
        }
        return taskList;
    }

    @Override
    public BizProblemReport getProblemById(String id) {
        return baseMapper.selectProblemById(id);
    }

    @Override
    public List<BizProblemReport> selectbizProblemReportList() {
        return baseMapper.selectbizProblemReportList();
    }
}
