package com.fjhx.mes.service.work.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.dynamic.datasource.annotation.DSTransactional;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fjhx.file.entity.ObsFile;
import com.fjhx.file.utils.ObsFileUtil;
import com.fjhx.mes.entity.work.dto.WorkOrderProductionProcessesDto;
import com.fjhx.mes.entity.work.po.WorkOrderProductionProcesses;
import com.fjhx.mes.entity.work.vo.WorkOrderProductionProcessesVo;
import com.fjhx.mes.mapper.work.WorkOrderProductionProcessesMapper;
import com.fjhx.mes.service.work.WorkOrderProductionProcessesService;
import com.fjhx.mes.entity.applicable.po.ApplicableProducts;
import com.fjhx.mes.entity.production.po.ProductionProcesses;
import com.fjhx.mes.entity.technology.po.Technology;
import com.fjhx.mes.entity.work.po.WorkOrder;
import com.fjhx.mes.service.applicable.ApplicableProductsService;
import com.fjhx.mes.service.production.ProductionProcessesService;
import com.fjhx.mes.service.technology.TechnologyService;
import com.fjhx.mes.service.work.WorkOrderService;
import com.obs.services.internal.ServiceException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 工单工序 服务实现类
 * </p>
 *
 * @author
 * @since 2023-05-17
 */
@Service
public class WorkOrderProductionProcessesServiceImpl extends ServiceImpl<WorkOrderProductionProcessesMapper, WorkOrderProductionProcesses> implements WorkOrderProductionProcessesService {

    @Autowired
    private TechnologyService technologyService;
    @Autowired
    private ApplicableProductsService applicableProductsService;
    @Autowired
    private WorkOrderService workOrderService;
    @Autowired
    private ProductionProcessesService productionProcessesService;

    @Override
    public List<WorkOrderProductionProcessesVo> getList(WorkOrderProductionProcesses dto) {
        WorkOrder workOrder = workOrderService.getById(dto.getWorkOrderId());
        if (ObjectUtil.isEmpty(workOrder)) {
            throw new ServiceException("查询不到工单信息");
        }
        List<WorkOrderProductionProcesses> workOrderProductionProcessesList = list(q -> q
                .eq(WorkOrderProductionProcesses::getWorkOrderId, dto.getWorkOrderId())
                .orderByAsc(WorkOrderProductionProcesses::getSort));
        //如果定制工序信息为空就获取基础工艺工序信息
        if (ObjectUtil.isEmpty(workOrderProductionProcessesList)) {
            ApplicableProducts applicableProducts = applicableProductsService.getOne(q -> q.eq(ApplicableProducts::getProductId, workOrder.getProductId()));
            if (ObjectUtil.isEmpty(applicableProducts)) {
                throw new ServiceException("未找到适用于该产品的工艺信息");
            }
            Technology technology = technologyService.getById(applicableProducts.getTechnologyId());
            if (ObjectUtil.isEmpty(technology)) {
                throw new ServiceException("工艺信息不存在");
            }
            String processRoute = technology.getProcessRoute();
            if (ObjectUtil.isEmpty(processRoute)) {
                throw new ServiceException("工艺线路为空");
            }
            String[] split = processRoute.split(",");
            List<ProductionProcesses> productionProcesses = productionProcessesService.list(q -> q.in(ProductionProcesses::getId, split));
            List<WorkOrderProductionProcessesVo> workOrderProductionProcessesList0 = BeanUtil.copyToList(productionProcesses, WorkOrderProductionProcessesVo.class);
            int i = 0;
            for (WorkOrderProductionProcessesVo workOrderProductionProcesses : workOrderProductionProcessesList0) {
                workOrderProductionProcesses.setOldId(workOrderProductionProcesses.getId());//赋值带过来的id
                workOrderProductionProcesses.setId(null);
                workOrderProductionProcesses.setSort(i);
                i++;
            }
            return workOrderProductionProcessesList0;
        }
        List<WorkOrderProductionProcessesVo> workOrderProductionProcessesVos = BeanUtil.copyToList(workOrderProductionProcessesList, WorkOrderProductionProcessesVo.class);
        return workOrderProductionProcessesVos;
    }

    @DSTransactional
    @Override
    public void edit(WorkOrderProductionProcessesDto workOrderProductionProcessesDto) {
//        this.updateById(workOrderProductionProcessesDto);

        List<WorkOrderProductionProcessesVo> workOrderProductionProcessesList = workOrderProductionProcessesDto.getWorkOrderProductionProcessesList();

        int i = 0;
        for (WorkOrderProductionProcessesVo workOrderProductionProcesses : workOrderProductionProcessesList) {
            workOrderProductionProcesses.setWorkOrderId(workOrderProductionProcessesDto.getWorkOrderId());
            workOrderProductionProcesses.setSort(i);
            List<ObsFile> fileList = workOrderProductionProcesses.getFileList();
            if (ObjectUtil.isNotEmpty(fileList)) {
                workOrderProductionProcesses.setFileName(fileList.get(0).getFileName());
            }
            i++;

            //处理附件信息
            if (ObjectUtil.isEmpty(workOrderProductionProcesses.getId())) {
                ObsFileUtil.saveFile(workOrderProductionProcesses.getFileList(), workOrderProductionProcesses.getId());
            } else {
                ObsFileUtil.editFile(workOrderProductionProcesses.getFileList(), workOrderProductionProcesses.getId());
            }
        }
        List<WorkOrderProductionProcesses> workOrderProductionProcessesList1 = BeanUtil.copyToList(workOrderProductionProcessesList, WorkOrderProductionProcesses.class);
        this.editLinked(workOrderProductionProcessesList1, WorkOrderProductionProcesses::getWorkOrderId, workOrderProductionProcessesDto.getWorkOrderId());
    }

}
