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.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.mapper.production.ProductionProcessesMapper;
import com.fjhx.mes.service.production.ProductionProcessesService;
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
    FileInfoService fileInfoService;

    @Override
    public Page<ProductionProcessesVo> getPage(ProductionProcessesSelectDto dto) {
        IWrapper<ProductionProcesses> wrapper = getWrapper();
        wrapper.orderByDesc("pp", ProductionProcesses::getId);
        wrapper.like(ProductionProcesses::getName, dto.getKeyword());
        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) {
        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) {
        ObsFile obsFile = productionProcessesDto.getFileList().get(0);
        productionProcessesDto.setFileName(obsFile.getFileName());
        this.updateById(productionProcessesDto);
        ObsFileUtil.editFile(productionProcessesDto.getFileList(), productionProcessesDto.getId());
    }

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

}
