package com.fjhx.mes.service.production.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.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fjhx.file.entity.ObsFile;
import com.fjhx.file.service.FileInfoService;
import com.fjhx.file.utils.ObsFileUtil;
import com.fjhx.mes.entity.applicable.po.ApplicableProducts;
import com.fjhx.mes.entity.production.dto.ProductionProcessesDto;
import com.fjhx.mes.entity.production.dto.ProductionProcessesSelectDto;
import com.fjhx.mes.entity.production.po.ProductionProcesses;
import com.fjhx.mes.entity.production.vo.ProductionProcessesVo;
import com.fjhx.mes.entity.technology.po.Technology;
import com.fjhx.mes.entity.work.po.WorkOrder;
import com.fjhx.mes.mapper.production.ProductionProcessesMapper;
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.WorkOrderProductionProcessesService;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.wrapper.IWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;


/**
 * <p>
 * 工序 服务实现类
 * </p>
 *
 * @author
 * @since 2023-03-28
 */
@Service
public class ProductionProcessesServiceImpl extends ServiceImpl<ProductionProcessesMapper, ProductionProcesses> implements ProductionProcessesService {

    @Autowired
    private FileInfoService fileInfoService;
    @Autowired
    private WorkOrderProductionProcessesService workOrderProductionProcessesService;
    @Autowired
    private ApplicableProductsService applicableProductsService;
    @Autowired
    private TechnologyService technologyService;


    @Override
    public Page<ProductionProcessesVo> getPage(ProductionProcessesSelectDto dto) {
        IWrapper<ProductionProcesses> wrapper = getWrapper();
        wrapper.like(ProductionProcesses::getName, dto.getKeyword());

        wrapper.orderByAsc("pp", ProductionProcesses::getCreateTime);
        wrapper.orderByAsc("pp", ProductionProcesses::getId);
        Page<ProductionProcessesVo> page = this.baseMapper.getPage(dto.getPage(), wrapper);
        return page;
    }

    @Override
    public ProductionProcessesVo detail(Long id) {
        ProductionProcesses ProductionProcesses = this.getById(id);
        ProductionProcessesVo result = BeanUtil.toBean(ProductionProcesses, ProductionProcessesVo.class);
        return result;
    }

    @DSTransactional
    @Override
    public void add(ProductionProcessesDto productionProcessesDto) {
//        Assert.notEmpty(productionProcessesDto.getRoleId(),"角色id不能为空");
        List<ObsFile> fileList = productionProcessesDto.getFileList();
        if (ObjectUtil.isNotEmpty(fileList)) {
            ObsFile obsFile = productionProcessesDto.getFileList().get(0);
            productionProcessesDto.setFileName(obsFile.getFileName());
        }
        this.save(productionProcessesDto);
        ObsFileUtil.saveFile(productionProcessesDto.getFileList(), productionProcessesDto.getId());
    }

    @DSTransactional
    @Override
    public void edit(ProductionProcessesDto productionProcessesDto) {
//        Assert.notEmpty(productionProcessesDto.getRoleId(),"角色id不能为空");
        List<ObsFile> fileList = productionProcessesDto.getFileList();
        if (fileList.size() > 0) {
            ObsFile obsFile = fileList.get(0);
            productionProcessesDto.setFileName(obsFile.getFileName());
        } else {
            productionProcessesDto.setFileName(null);
        }
        this.updateById(productionProcessesDto);
        baseMapper.updateFileName(productionProcessesDto.getId(), productionProcessesDto.getFileName());
        ObsFileUtil.editFile(productionProcessesDto.getFileList(), productionProcessesDto.getId());
    }

    @DSTransactional
    @Override
    public void delete(Long id) {
        this.removeById(id);
        ObsFileUtil.removeFile(id);
    }

    /**
     * 根据工单详细获取基本工序
     */
    @Override
    public List<ProductionProcesses> getBaseProductionProcessesList(WorkOrder workOrder) {
        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(",");
        //查询工序并自定义排序
        IWrapper<ProductionProcesses> wrapper = IWrapper.getWrapper();
        wrapper.in(ProductionProcesses::getId, split);
        wrapper.orderByAsc("FIELD( id, " + processRoute + ")");
        return this.list(wrapper);
    }

}
