package org.jeecg.modules.apiService.impl;

import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.jeecg.common.api.vo.Result;
import org.jeecg.modules.apiBean.*;
import org.jeecg.modules.apiService.TemplateService;
import org.jeecg.modules.workorderGeneralStepfour.entity.WorkorderGeneralStepfour;
import org.jeecg.modules.workorderGeneralStepfour.service.IWorkorderGeneralStepfourService;
import org.jeecg.modules.workorderGeneralStepone.entity.WorkorderGeneralStepone;
import org.jeecg.modules.workorderGeneralStepone.service.IWorkorderGeneralSteponeService;
import org.jeecg.modules.workorderGeneralStepthree.entity.WorkorderGeneralStepthree;
import org.jeecg.modules.workorderGeneralStepthree.service.IWorkorderGeneralStepthreeService;
import org.jeecg.modules.workorderGeneralSteptwo.entity.WorkorderGeneralSteptwo;
import org.jeecg.modules.workorderGeneralSteptwo.service.IWorkorderGeneralSteptwoService;
import org.jeecg.modules.workorderItemCheck.entity.WorkorderItemCheck;
import org.jeecg.modules.workorderItemCheck.service.IWorkorderItemCheckService;
import org.jeecg.modules.workorderParamStepfour.entity.WorkorderParamStepfour;
import org.jeecg.modules.workorderParamStepfour.service.IWorkorderParamStepfourService;
import org.jeecg.modules.workorderParamStepone.entity.WorkorderParamStepone;
import org.jeecg.modules.workorderParamStepone.service.IWorkorderParamSteponeService;
import org.jeecg.modules.workorderParamStepthree.entity.WorkorderParamStepthree;
import org.jeecg.modules.workorderParamStepthree.service.IWorkorderParamStepthreeService;
import org.jeecg.modules.workorderParamSteptwo.entity.WorkorderParamSteptwo;
import org.jeecg.modules.workorderParamSteptwo.service.IWorkorderParamSteptwoService;
import org.jeecg.modules.workorderProcess.entity.WorkorderProcess;
import org.jeecg.modules.workorderProcess.service.IWorkorderProcessService;
import org.jeecg.modules.workorderStatus.entity.WorkorderStatus;
import org.jeecg.modules.workorderStatus.service.IWorkorderStatusService;
import org.jeecg.modules.workorderStep.entity.WorkorderStep;
import org.jeecg.modules.workorderStep.service.IWorkorderStepService;
import org.jeecg.modules.workorderStepfour.entity.WorkorderStepfour;
import org.jeecg.modules.workorderStepfour.service.IWorkorderStepfourService;
import org.jeecg.modules.workorderStepone.entity.WorkorderStepone;
import org.jeecg.modules.workorderStepone.service.IWorkorderSteponeService;
import org.jeecg.modules.workorderStepthree.entity.WorkorderStepthree;
import org.jeecg.modules.workorderStepthree.service.IWorkorderStepthreeService;
import org.jeecg.modules.workorderSteptwo.entity.WorkorderSteptwo;
import org.jeecg.modules.workorderSteptwo.service.IWorkorderSteptwoService;
import org.jeecg.modules.workorderTemplate.entity.WorkorderTemplate;
import org.jeecg.modules.workorderTemplate.service.IWorkorderTemplateService;
import org.springframework.stereotype.Service;

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

@Service
public class TemplateServiceImpl implements TemplateService {

    //工单状态
    @Resource
    private IWorkorderStatusService workorderStatusService;

    //工单信息
    @Resource
    private IWorkorderTemplateService workorderTemplateService;

    //工序卡信息
    @Resource
    private IWorkorderStepService workorderStepService;

    //工序流程信息
    @Resource
    private IWorkorderProcessService workorderProcessService;

    //检查项目信息
    @Resource
    private IWorkorderItemCheckService workorderItemCheckService;

    //工序卡常规参数
    @Resource
    private IWorkorderGeneralSteponeService workorderGeneralSteponeService;
    @Resource
    private IWorkorderGeneralSteptwoService workorderGeneralSteptwoService;
    @Resource
    private IWorkorderGeneralStepthreeService workorderGeneralStepthreeService;
    @Resource
    private IWorkorderGeneralStepfourService workorderGeneralStepfourService;

    //工序卡列表参数
    @Resource
    private IWorkorderParamSteponeService workorderParamSteponeService;
    @Resource
    private IWorkorderParamSteptwoService workorderParamSteptwoService;
    @Resource
    private IWorkorderParamStepthreeService workorderParamStepthreeService;
    @Resource
    private IWorkorderParamStepfourService workorderParamStepfourService;

    //工序卡1
    @Resource
    private IWorkorderSteponeService workorderSteponeService;

    //工序卡2
    @Resource
    private IWorkorderSteptwoService workorderSteptwoService;

    //工序卡3
    @Resource
    private IWorkorderStepthreeService workorderStepthreeService;

    //工序卡4
    @Resource
    private IWorkorderStepfourService workorderStepfourService;
    /*************************************************************************************/

    //工单信息-查询工单列表
    @Override
    public Result<?> queryTemplateList(QueryTemplateBean templateBean, PageBean pageBean) {
        //分页
        Page<WorkorderTemplate> page = new Page<WorkorderTemplate>(pageBean.getPageNo(), pageBean.getPageSize());
        LambdaQueryChainWrapper<WorkorderTemplate> query = workorderTemplateService
                .lambdaQuery();

        //组装查询条件
        if(null != templateBean.getStatusId()){
            //工单状态
            query.eq(WorkorderTemplate::getStatusId, templateBean.getStatusId());
        }
        if(null != templateBean.getIsEmergency()){
            //工单是否紧急
            query.eq(WorkorderTemplate::getIsEmergency, templateBean.getIsEmergency());
        }
        if(null != templateBean.getResponsibler()){
            //负责人
            query.like(WorkorderTemplate::getResponsibler, templateBean.getResponsibler());
        }
        if(null != templateBean.getTaskNo()){
            //任务号
            query.like(WorkorderTemplate::getTaskNo, templateBean.getTaskNo());
        }

        //获取账本信息
        Page<WorkorderTemplate> pageList = query.page(page);

        return Result.OK("工单列表", pageList);
    }

    //工单信息-查询工单详情
//    @Override
//    public Result<?> queryTemplateById(String templateId) {
//        //查询工单信息详情
//        WorkorderTemplate template = workorderTemplateService
//                .lambdaQuery()
//                .eq(WorkorderTemplate::getId, templateId)
//                .one();
//
//        //工序卡1
//        //常规参数
//        WorkorderGeneralStepone generalStepone = workorderGeneralSteponeService
//                .lambdaQuery()
//                .eq(WorkorderGeneralStepone::getTemplateId, templateId)
//                .one();
//
//        //列表参数
//        List<WorkorderParamStepone> paramStepone = workorderParamSteponeService
//                .lambdaQuery()
//                .eq(WorkorderParamStepone::getTemplateId, templateId)
//                .list();
//
//        return Result.OK("工单详情", template);
//    }

    //工单信息-修改工单
    @Override
    public Result<?> updateTemplate(WorkorderTemplate workorderTemplate) {
        boolean flag = workorderTemplateService.updateById(workorderTemplate);
        if(flag){
            return Result.OK("修改工单信息成功！");
        }else {
            return Result.error("修改工单信息失败！");
        }
    }

    //工单信息-工序卡1(选配)详情
    @Override
    public Result<?> queryStepOne(String templateId) {
        //查询工序卡1(选配)详情
        //常规参数
        WorkorderGeneralStepone generalStepone = workorderGeneralSteponeService
                .lambdaQuery()
                .eq(WorkorderGeneralStepone::getTemplateId, templateId)
                .one();

        //列表参数
        List<WorkorderParamStepone> paramStepone = workorderParamSteponeService
                .lambdaQuery()
                .eq(WorkorderParamStepone::getTemplateId, templateId)
                .list();

        StepOneBean stepOne = new StepOneBean();
        stepOne.setWorkorderGeneralStepone(generalStepone);
        stepOne.setWorkorderParamSteponeList(paramStepone);

        return Result.OK("工序卡1(选配)详情", stepOne);
    }

    //工单信息-工序卡2详情
    @Override
    public Result<?> queryStepTwo(String templateId) {
        //查询工序卡1(选配)详情
        //常规参数
        WorkorderGeneralSteptwo generalStepone = workorderGeneralSteptwoService
                .lambdaQuery()
                .eq(WorkorderGeneralSteptwo::getTemplateId, templateId)
                .one();

        //列表参数
        List<WorkorderParamSteptwo> paramStepone = workorderParamSteptwoService
                .lambdaQuery()
                .eq(WorkorderParamSteptwo::getTemplateId, templateId)
                .list();

        StepTwoBean stepTwo = new StepTwoBean();
        stepTwo.setWorkorderGeneralSteptwo(generalStepone);
        stepTwo.setWorkorderParamSteptwoList(paramStepone);

        return Result.OK("工序卡2详情", stepTwo);
    }

    //工单信息-工序卡3详情
    @Override
    public Result<?> queryStepThree(String templateId) {
        //查询工序卡1(选配)详情
        //常规参数
        WorkorderGeneralStepthree generalStepthree = workorderGeneralStepthreeService
                .lambdaQuery()
                .eq(WorkorderGeneralStepthree::getTemplateId, templateId)
                .one();

        //列表参数
        List<WorkorderParamStepthree> paramStepthree = workorderParamStepthreeService
                .lambdaQuery()
                .eq(WorkorderParamStepthree::getTemplateId, templateId)
                .list();

        StepThreeBean stepThree = new StepThreeBean();
        stepThree.setWorkorderGeneralStepthree(generalStepthree);
        stepThree.setWorkorderParamStepthreeList(paramStepthree);

        return Result.OK("工序卡3详情", stepThree);
    }

    //工单信息-工序卡4(总成)详情
    @Override
    public Result<?> queryStepFour(String templateId) {
        //查询工序卡1(选配)详情
        //常规参数
        WorkorderGeneralStepfour generalStepfour = workorderGeneralStepfourService
                .lambdaQuery()
                .eq(WorkorderGeneralStepfour::getTemplateId, templateId)
                .one();

        //列表参数
        List<WorkorderParamStepfour> paramStepfour = workorderParamStepfourService
                .lambdaQuery()
                .eq(WorkorderParamStepfour::getTemplateId, templateId)
                .list();

        StepFourBean stepFourBean = new StepFourBean();
        stepFourBean.setWorkorderGeneralStepfour(generalStepfour);
        stepFourBean.setWorkorderParamStepfourList(paramStepfour);

        return Result.OK("工序卡4(总成)详情", stepFourBean);
    }

    //工单信息-修改常规参数-工序卡1(选配)
    @Override
    public Result<?> updateGeneralStepOne(WorkorderGeneralStepone generalStepone) {
        boolean flag = workorderGeneralSteponeService.saveOrUpdate(generalStepone);
        if(flag){
            return Result.OK("新增/修改常规参数-工序卡1成功！");
        }else {
            return Result.error("新增/修改常规参数-工序卡1失败！");
        }
    }

    //工单信息-修改常规参数-工序卡2
    @Override
    public Result<?> updateGeneralStepTwo(WorkorderGeneralSteptwo generalSteptwo) {
        boolean flag = workorderGeneralSteptwoService.saveOrUpdate(generalSteptwo);
        if(flag){
            return Result.OK("新增/修改常规参数-工序卡2成功！");
        }else {
            return Result.error("新增/修改常规参数-工序卡2失败！");
        }
    }

    //工单信息-修改常规参数-工序卡3
    @Override
    public Result<?> updateGeneralStepThree(WorkorderGeneralStepthree generalStepthree) {
        boolean flag = workorderGeneralStepthreeService.saveOrUpdate(generalStepthree);
        if(flag){
            return Result.OK("新增/修改常规参数-工序卡3成功！");
        }else {
            return Result.error("新增/修改常规参数-工序卡3失败！");
        }
    }

    //工单信息-修改常规参数-工序卡4(总成)
    @Override
    public Result<?> updateGeneralStepFour(WorkorderGeneralStepfour generalStepfour) {
        boolean flag = workorderGeneralStepfourService.saveOrUpdate(generalStepfour);
        if(flag){
            return Result.OK("新增/修改常规参数-工序卡4成功！");
        }else {
            return Result.error("新增/修改常规参数-工序卡4失败！");
        }
    }

    //工单信息-修改工序参数-工序卡1(选配)
    @Override
    public Result<?> updateParamStepOne(WorkorderParamStepone paramStepone) {
        boolean flag = workorderParamSteponeService.saveOrUpdate(paramStepone);
        if(flag){
            return Result.OK("新增/修改工序参数-工序卡1成功！");
        }else {
            return Result.error("新增/修改工序参数-工序卡1失败！");
        }
    }

    //工单信息-修改工序参数-工序卡2
    @Override
    public Result<?> updateParamStepTwo(WorkorderParamSteptwo paramSteptwo) {
        boolean flag = workorderParamSteptwoService.saveOrUpdate(paramSteptwo);
        if(flag){
            return Result.OK("新增/修改工序参数-工序卡2成功！");
        }else {
            return Result.error("新增/修改工序参数-工序卡2失败！");
        }
    }

    //工单信息-修改工序参数-工序卡3
    @Override
    public Result<?> updateParamStepThree(WorkorderParamStepthree paramStepthree) {
        boolean flag = workorderParamStepthreeService.saveOrUpdate(paramStepthree);
        if(flag){
            return Result.OK("新增/修改工序参数-工序卡3成功！");
        }else {
            return Result.error("新增/修改工序参数-工序卡3失败！");
        }
    }

    //工单信息-修改工序参数-工序卡4(总成)
    @Override
    public Result<?> updateParamStepFour(WorkorderParamStepfour paramStepfour) {
        boolean flag = workorderParamStepfourService.saveOrUpdate(paramStepfour);
        if(flag){
            return Result.OK("新增/修改工序参数-工序卡4成功！");
        }else {
            return Result.error("新增/修改工序参数-工序卡4失败！");
        }
    }

    /*********************************************************************************************************/
    //工单信息-查询工单详情
    @Override
    public Result<?> queryTemplateById(String templateId){

        //查询工单信息
        WorkorderTemplate workorderTemplate = workorderTemplateService
                .lambdaQuery()
                .eq(WorkorderTemplate::getId, templateId)
                .one();

        //查询工序卡信息
        List<WorkorderStep> stepList = workorderStepService
                .lambdaQuery()
                .list();

        //查询工序信息
        List<WorkorderProcess> processList = workorderProcessService
                .lambdaQuery()
                .list();

        //查询检查项目信息
        List<WorkorderItemCheck> itemCheckList = workorderItemCheckService
                .lambdaQuery()
                .list();

        //根据工单id查询常规参数
        List<WorkorderGeneralStepone> GeneralSteponeList = workorderGeneralSteponeService
                .lambdaQuery()
                .eq(WorkorderGeneralStepone::getTemplateId, templateId)
                .list();
        List<WorkorderGeneralSteptwo> GeneralSteptwoList = workorderGeneralSteptwoService
                .lambdaQuery()
                .eq(WorkorderGeneralSteptwo::getTemplateId, templateId)
                .list();
        List<WorkorderGeneralStepthree> GeneralStepthreeList = workorderGeneralStepthreeService
                .lambdaQuery()
                .eq(WorkorderGeneralStepthree::getTemplateId, templateId)
                .list();
        List<WorkorderGeneralStepfour> GeneralStepfourList = workorderGeneralStepfourService
                .lambdaQuery()
                .eq(WorkorderGeneralStepfour::getTemplateId, templateId)
                .list();

        //根据工单id查询检查项目参数
        List<WorkorderParamStepone> paramSteponeList = workorderParamSteponeService
                .lambdaQuery()
                .eq(WorkorderParamStepone::getTemplateId, templateId)
                .list();
        List<WorkorderParamSteptwo> paramSteptwoList = workorderParamSteptwoService
                .lambdaQuery()
                .eq(WorkorderParamSteptwo::getTemplateId, templateId)
                .list();
        List<WorkorderParamStepthree> paramStepthreeList = workorderParamStepthreeService
                .lambdaQuery()
                .eq(WorkorderParamStepthree::getTemplateId, templateId)
                .list();
        List<WorkorderParamStepfour> paramStepfourList = workorderParamStepfourService
                .lambdaQuery()
                .eq(WorkorderParamStepfour::getTemplateId, templateId)
                .list();

        //一、将检查项目参数对象放入对应检查项目对象下
        for (WorkorderItemCheck workorderItemCheck : itemCheckList) {
            //工序卡1
            List<WorkorderParamStepone> workorderParamSteponeList = new ArrayList<>();
            for (WorkorderParamStepone workorderParamStepone : paramSteponeList) {
                if(workorderParamStepone.getProcessId().equals(workorderItemCheck.getId()) ){
                    workorderParamSteponeList.add(workorderParamStepone);
                }
            }
            workorderItemCheck.setWorkorderParamSteponeList(workorderParamSteponeList);

            //工序卡2
            List<WorkorderParamSteptwo> workorderParamSteptwoList = new ArrayList<>();
            for (WorkorderParamSteptwo workorderParamSteptwo : paramSteptwoList) {
                if(workorderParamSteptwo.getProcessId().equals(workorderItemCheck.getId())){
                    workorderParamSteptwoList.add(workorderParamSteptwo);
                }
            }
            workorderItemCheck.setWorkorderParamSteptwoList(workorderParamSteptwoList);

            //工序卡3
            List<WorkorderParamStepthree> workorderParamStepthreeList = new ArrayList<>();
            for (WorkorderParamStepthree workorderParamStepthree : paramStepthreeList) {
                if(workorderParamStepthree.getProcessId().equals(workorderItemCheck.getId())){
                    workorderParamStepthreeList.add(workorderParamStepthree);
                }
            }
            workorderItemCheck.setWorkorderParamStepthreeList(workorderParamStepthreeList);

            //工序卡4
            List<WorkorderParamStepfour> workorderParamStepfourList = new ArrayList<>();
            for (WorkorderParamStepfour workorderParamStepfour : paramStepfourList) {
                if(workorderParamStepfour.getProcessId().equals(workorderItemCheck.getId())){
                    workorderParamStepfourList.add(workorderParamStepfour);
                }
            }
            workorderItemCheck.setWorkorderParamStepfourList(workorderParamStepfourList);

        }

        //二、将检查项目对象放入对应工序流程对象下
        for (WorkorderProcess workorderProcess : processList) {
            List<WorkorderItemCheck> workorderItemCheckList = new ArrayList<>();
            for (WorkorderItemCheck workorderItemCheck : itemCheckList) {
                if(workorderItemCheck.getProcessId().equals(workorderProcess.getId())){
                    workorderItemCheckList.add(workorderItemCheck);
                }
            }
            workorderProcess.setWorkorderItemCheckList(workorderItemCheckList);
        }

        //三、将工序流程对象放入对应工序卡对象下
        for (WorkorderStep workorderStep : stepList) {
            //工序参数
            List<WorkorderProcess> workorderProcessList = new ArrayList<>();
            for (WorkorderProcess workorderProcess : processList) {
                if(workorderProcess.getStepId().equals(workorderStep.getId())){
                    workorderProcessList.add(workorderProcess);
                }
            }
            workorderStep.setWorkorderProcessList(workorderProcessList);

            //常规参数(待优化--冗余list)
            //工序卡1
            List<WorkorderGeneralStepone> workorderGeneralSteponeList = new ArrayList<>();
            for (WorkorderGeneralStepone generalStepone : GeneralSteponeList) {
                if(generalStepone.getStepId().equals(workorderStep.getId())){
                    workorderGeneralSteponeList.add(generalStepone);
                }
            }
            workorderStep.setWorkorderGeneralSteponeList(workorderGeneralSteponeList);

            //工序卡2
            List<WorkorderGeneralSteptwo> workorderGeneralSteptwoList = new ArrayList<>();
            for (WorkorderGeneralSteptwo generalSteptwo : GeneralSteptwoList) {
                if(generalSteptwo.getStepId().equals(workorderStep.getId())){
                    workorderGeneralSteptwoList.add(generalSteptwo);
                }
            }
            workorderStep.setWorkorderGeneralSteptwoList(workorderGeneralSteptwoList);

            //工序卡3
            List<WorkorderGeneralStepthree> workorderGeneralStepthreeList = new ArrayList<>();
            for (WorkorderGeneralStepthree generalStepthree : GeneralStepthreeList) {
                if(generalStepthree.getStepId().equals(workorderStep.getId())){
                    workorderGeneralStepthreeList.add(generalStepthree);
                }
            }
            workorderStep.setWorkorderGeneralStepthreeList(workorderGeneralStepthreeList);

            //工序卡4
            List<WorkorderGeneralStepfour> workorderGeneralStepfourList = new ArrayList<>();
            for (WorkorderGeneralStepfour generalStepfour : GeneralStepfourList) {
                if(generalStepfour.getStepId().equals(workorderStep.getId())){
                    workorderGeneralStepfourList.add(generalStepfour);
                }
            }
            workorderStep.setWorkorderGeneralStepfourList(workorderGeneralStepfourList);

        }

        //四、将工序卡对象放入对应工单对象下
        workorderTemplate.setWorkorderStepList(stepList);

        //补充工单状态描述
        if(null != workorderTemplate.getStatusId()){
            List<WorkorderStatus> workorderStatus = workorderStatusService
                    .lambdaQuery()
                    .list();
            for (WorkorderStatus status : workorderStatus) {
                if(workorderTemplate.getStatusId().equals(status.getId())){
                    workorderTemplate.setStatusDescribe(status.getName());
                    break;
                }
            }
        }


        return Result.OK("工单详情！", workorderTemplate);
    }

}
