package org.eco.als.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryWrapper;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.eco.als.domain.SpareParts;
import org.eco.als.domain.bo.SparePartsBo;
import org.eco.als.domain.vo.SparePartsImportVo;
import org.eco.als.domain.vo.SparePartsVo;
import org.eco.als.listener.SparePartsImportListener;
import org.eco.als.mapper.SparePartsMapper;
import org.eco.als.service.ISparePartsService;
import org.eco.common.core.core.domain.model.LoginUser;
import org.eco.common.core.core.page.PageResult;
import org.eco.common.core.utils.MapstructUtils;
import org.eco.common.excel.entity.ExcelResultRes;
import org.eco.common.excel.service.IExcelService;
import org.eco.common.orm.core.page.PageQuery;
import org.eco.common.orm.core.service.impl.BaseServiceImpl;
import org.eco.system.service.IImportExportService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Arrays;
import java.util.List;

import static org.eco.als.domain.table.SparePartsTableDef.SPARE_PARTS;

/**
 * 部件Service业务层处理
 *
 * @author wgk
 * @date 2024-07-25
 */
@Service
@Slf4j
public class SparePartsServiceImpl extends BaseServiceImpl<SparePartsMapper, SpareParts> implements ISparePartsService {
    @Resource
    private SparePartsMapper sparePartsMapper;

    @Resource
    private IExcelService excelService;

    @Resource
    private IImportExportService importExportService;

    @Override
    public QueryWrapper query() {
        return super.query().from(SPARE_PARTS);
    }

    private QueryWrapper buildQueryWrapper(SparePartsBo sparePartsBo) {
        QueryWrapper queryWrapper = super.buildBaseQueryWrapper();
        queryWrapper.and(SPARE_PARTS.NAME.like
            (sparePartsBo.getName()));
        queryWrapper.and(SPARE_PARTS.MODEL.eq
            (sparePartsBo.getModel()));
        queryWrapper.and(SPARE_PARTS.SYSTEM_NAME.like
            (sparePartsBo.getSystemName()));
        queryWrapper.and(SPARE_PARTS.FACTORY.eq
            (sparePartsBo.getFactory()));
        queryWrapper.and(SPARE_PARTS.QUANTITY.eq
            (sparePartsBo.getQuantity()));

        return queryWrapper;
    }

    /**
     * 查询部件
     *
     * @param id 部件主键
     * @return 部件
     */
    @Override
    public SparePartsVo selectById(Long id) {
        return this.getOneAs(query().where(SPARE_PARTS.ID.eq(id)), SparePartsVo.class);

    }


    /**
     * 查询部件列表
     *
     * @param sparePartsBo 部件Bo
     * @return 部件集合
     */
    @Override
    public List<SparePartsVo> selectList(SparePartsBo sparePartsBo) {
        QueryWrapper queryWrapper = buildQueryWrapper(sparePartsBo);
        return this.listAs(queryWrapper, SparePartsVo.class);
    }

    /**
     * 分页查询部件列表
     *
     * @param sparePartsBo 部件Bo
     * @return 分页部件集合
     */
    @Override
    public PageResult<SparePartsVo> selectPage(SparePartsBo sparePartsBo) {
        QueryWrapper queryWrapper = buildQueryWrapper(sparePartsBo);
        Page<SparePartsVo> page = this.pageAs(PageQuery.build(), queryWrapper, SparePartsVo.class);
        return PageResult.build(page);
    }

    /**
     * 新增部件
     *
     * @param sparePartsBo 部件Bo
     * @return 结果:true 操作成功，false 操作失败
     */
    @Override
    public boolean insert(SparePartsBo sparePartsBo) {
        SpareParts spareParts = MapstructUtils.convert(sparePartsBo, SpareParts.class);

        return this.save(spareParts);//使用全局配置的雪花算法主键生成器生成ID值
    }

    /**
     * 新增部件，前台提供主键值，一般用于导入的场合
     *
     * @param sparePartsBo 部件Bo
     * @return 结果:true 操作成功，false 操作失败
     */
    @Override
    public boolean insertWithPk(SparePartsBo sparePartsBo) {
        SpareParts spareParts = MapstructUtils.convert(sparePartsBo, SpareParts.class);
        return sparePartsMapper.insertWithPk(spareParts) > 0;//前台传来主键值
    }

    /**
     * 修改部件
     *
     * @param sparePartsBo 部件Bo
     * @return 结果:true 更新成功，false 更新失败
     */
    @Override
    public boolean update(SparePartsBo sparePartsBo) {
        SpareParts spareParts = MapstructUtils.convert(sparePartsBo, SpareParts.class);
        if (ObjectUtil.isNotNull(spareParts) && ObjectUtil.isNotNull(spareParts.getId())) {
            return this.updateById(spareParts);
        }
        return false;
    }

    @Override
    public void asyncImportData(MultipartFile file, boolean updateSupport, LoginUser loginUser) {
        ExcelResultRes result;
        try {
            String name = file.getOriginalFilename();
            result = excelService.importExcel(file.getInputStream(), name, SparePartsImportVo.class, new SparePartsImportListener(updateSupport, loginUser));
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        boolean flag = importExportService.saveInfo(result, loginUser, "0");
        if (flag) {
            log.info("异步导入日志写入成功");
        }
    }

    @Override
    public void asyncExport(List<SparePartsVo> listVo, String sheetName, LoginUser loginUser) {
        ExcelResultRes result = excelService.exportExcel(listVo, sheetName, SparePartsVo.class);
        boolean flag = importExportService.saveInfo(result, loginUser, "1");
        if (flag) {
            log.info("异步导出日志写入成功");
        }
    }

    /**
     * 批量删除部件
     *
     * @param ids 需要删除的部件主键集合
     * @return 结果:true 删除成功，false 删除失败
     */
    @Transactional
    @Override
    public boolean deleteByIds(Long[] ids) {
        return this.removeByIds(Arrays.asList(ids));
    }

}
