package org.eco.als.service.impl;

import java.util.Arrays;
import java.util.List;

import cn.hutool.core.util.ObjectUtil;
import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.eco.common.core.core.domain.model.LoginUser;
import org.eco.common.excel.entity.ExcelResultRes;
import org.eco.common.excel.service.IExcelService;
import org.eco.common.core.utils.bean.BeanUtils;
import org.eco.common.core.utils.MapstructUtils;
import org.eco.system.service.IImportExportService;
import org.eco.common.core.utils.StringUtils;
import org.eco.system.domain.bo.ImportExportBo;
import org.eco.common.orm.core.page.PageQuery;
import org.eco.common.core.core.page.PageResult;
import org.eco.common.orm.core.service.impl.BaseServiceImpl;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;
import java.io.IOException;
import org.eco.als.mapper.LifetimeSparePartsMapper;
import org.eco.als.domain.LifetimeSpareParts;
import org.eco.als.domain.bo.LifetimeSparePartsBo;
import org.eco.als.domain.vo.LifetimeSparePartsVo;
import org.eco.als.domain.vo.LifetimeSparePartsImportVo;
import org.eco.als.listener.LifetimeSparePartsImportListener;
import org.eco.als.service.ILifetimeSparePartsService;
import static org.eco.als.domain.table.LifetimeSparePartsTableDef.LIFETIME_SPARE_PARTS;

/**
 * 有寿件Service业务层处理
 *
 * @author wgk
 * @date 2024-12-14
 */
@Service
@Slf4j
public class LifetimeSparePartsServiceImpl extends BaseServiceImpl<LifetimeSparePartsMapper, LifetimeSpareParts> implements ILifetimeSparePartsService {
    @Resource
    private LifetimeSparePartsMapper lifetimeSparePartsMapper;

    @Resource
    private IExcelService excelService;

    @Resource
    private IImportExportService importExportService;

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

    private QueryWrapper buildQueryWrapper(LifetimeSparePartsBo lifetimeSparePartsBo) {
        QueryWrapper queryWrapper = super.buildBaseQueryWrapper();
        queryWrapper.and(LIFETIME_SPARE_PARTS.AIR_ID.eq
        (lifetimeSparePartsBo.getAirId()));
        queryWrapper.and(LIFETIME_SPARE_PARTS.AIR_MODEL_ID.eq
        (lifetimeSparePartsBo.getAirModelId()));
        queryWrapper.and(LIFETIME_SPARE_PARTS.CONFIG_ID.eq
        (lifetimeSparePartsBo.getConfigId()));
        queryWrapper.and(LIFETIME_SPARE_PARTS.COMPONENT_NAME.like
        (lifetimeSparePartsBo.getComponentName()));

        return queryWrapper;
    }

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

    }


    /**
     * 查询有寿件列表
     *
     * @param lifetimeSparePartsBo 有寿件Bo
     * @return 有寿件集合
     */
    @Override
    public List<LifetimeSparePartsVo> selectList(LifetimeSparePartsBo lifetimeSparePartsBo) {
        QueryWrapper queryWrapper = buildQueryWrapper(lifetimeSparePartsBo);
            return this.listAs(queryWrapper, LifetimeSparePartsVo.class);
    }

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

    /**
     * 新增有寿件
     *
     * @param lifetimeSparePartsBo 有寿件Bo
     * @return 结果:true 操作成功，false 操作失败
     */
    @Override
    public boolean insert(LifetimeSparePartsBo lifetimeSparePartsBo) {
    LifetimeSpareParts lifetimeSpareParts =MapstructUtils.convert(lifetimeSparePartsBo, LifetimeSpareParts. class);

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

    /**
     * 新增有寿件，前台提供主键值，一般用于导入的场合
     *
     * @param lifetimeSparePartsBo 有寿件Bo
     * @return 结果:true 操作成功，false 操作失败
     */
    @Override
    public boolean insertWithPk(LifetimeSparePartsBo lifetimeSparePartsBo)
    {
        LifetimeSpareParts lifetimeSpareParts = MapstructUtils.convert(lifetimeSparePartsBo, LifetimeSpareParts.class);


            return lifetimeSparePartsMapper.insertWithPk(lifetimeSpareParts) > 0;//前台传来主键值
    }

    /**
     * 修改有寿件
     *
     * @param lifetimeSparePartsBo 有寿件Bo
     * @return 结果:true 更新成功，false 更新失败
     */
    @Override
    public boolean update(LifetimeSparePartsBo lifetimeSparePartsBo) {
        LifetimeSpareParts lifetimeSpareParts =MapstructUtils.convert(lifetimeSparePartsBo, LifetimeSpareParts. class);
        if (ObjectUtil.isNotNull(lifetimeSpareParts) && ObjectUtil.isNotNull(lifetimeSpareParts.getId())){
            boolean updated = this.updateById(lifetimeSpareParts);
                return updated;
        }
        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, LifetimeSparePartsImportVo.class, new LifetimeSparePartsImportListener(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<LifetimeSparePartsVo> listVo, String sheetName, LoginUser loginUser) {
        ExcelResultRes result = excelService.exportExcel(listVo, sheetName, LifetimeSparePartsVo.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));
    }

}
