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.core.utils.StringUtils;
import org.eco.common.excel.entity.ExcelResultRes;
import org.eco.common.excel.service.IExcelService;
import org.eco.common.core.utils.MapstructUtils;
import org.eco.system.service.IImportExportService;
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.PartReplacementMapper;
import org.eco.als.domain.PartReplacement;
import org.eco.als.domain.bo.PartReplacementBo;
import org.eco.als.domain.vo.PartReplacementVo;
import org.eco.als.domain.vo.PartReplacementImportVo;
import org.eco.als.listener.PartReplacementImportListener;
import org.eco.als.service.IPartReplacementService;

import static org.eco.als.domain.table.AirConfigurationTableDef.AIR_CONFIGURATION;
import static org.eco.als.domain.table.PartReplacementTableDef.PART_REPLACEMENT;

/**
 * 换件记录Service业务层处理
 *
 * @author wgk
 * @date 2024-10-25
 */
@Service
@Slf4j
public class PartReplacementServiceImpl extends BaseServiceImpl<PartReplacementMapper, PartReplacement> implements IPartReplacementService {
    @Resource
    private PartReplacementMapper partReplacementMapper;

    @Resource
    private IExcelService excelService;

    @Resource
    private IImportExportService importExportService;

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

    private QueryWrapper buildQueryWrapper(PartReplacementBo partReplacementBo) {
        QueryWrapper queryWrapper = super.buildBaseQueryWrapper();
        if (StringUtils.isNotBlank(partReplacementBo.getAircraftType())) {
            String[] airTypeIds = partReplacementBo.getAircraftType().split(",");
            queryWrapper.and(PART_REPLACEMENT.AIRCRAFT_TYPE.in(airTypeIds));
        }
        if (StringUtils.isNotBlank(partReplacementBo.getAircraftCode())) {
            String[] airTypeIds = partReplacementBo.getAircraftCode().split(",");
            queryWrapper.and(PART_REPLACEMENT.AIRCRAFT_CODE.in(airTypeIds));
        }
        queryWrapper.and(PART_REPLACEMENT.BEFORE_EQUIPMENT_CODE.eq
            (partReplacementBo.getBeforeEquipmentCode()));
        queryWrapper.and(PART_REPLACEMENT.BEFORE_EQUIPMENT_NAME.like
            (partReplacementBo.getBeforeEquipmentName()));
        queryWrapper.and(PART_REPLACEMENT.BEFORE_MODEL_NUMBER.eq
            (partReplacementBo.getBeforeModelNumber()));
        queryWrapper.and(PART_REPLACEMENT.AFTER_EQUIPMENT_CODE.eq
            (partReplacementBo.getAfterEquipmentCode()));
        queryWrapper.and(PART_REPLACEMENT.AFTER_EQUIPMENT_NAME.eq
            (partReplacementBo.getAfterEquipmentName()));
        queryWrapper.and(PART_REPLACEMENT.AFTER_MODEL_NUMBER.eq
            (partReplacementBo.getAfterModelNumber()));
        queryWrapper.and(PART_REPLACEMENT.QUANTITY.eq
            (partReplacementBo.getQuantity()));
        queryWrapper.and(PART_REPLACEMENT.REMARKS.eq
            (partReplacementBo.getRemarks()));

        return queryWrapper;
    }

    /**
     * 查询换件记录
     *
     * @param id 换件记录主键
     * @return 换件记录
     */
    @Override
    public PartReplacementVo selectById(Long id) {
        return this.getOneAs(query().where(PART_REPLACEMENT.ID.eq(id)), PartReplacementVo.class);

    }


    /**
     * 查询换件记录列表
     *
     * @param partReplacementBo 换件记录Bo
     * @return 换件记录集合
     */
    @Override
    public List<PartReplacementVo> selectList(PartReplacementBo partReplacementBo) {
        QueryWrapper queryWrapper = buildQueryWrapper(partReplacementBo);
        return this.listAs(queryWrapper, PartReplacementVo.class);
    }

    /**
     * 分页查询换件记录列表
     *
     * @param partReplacementBo 换件记录Bo
     * @return 分页换件记录集合
     */
    @Override
    public PageResult<PartReplacementVo> selectPage(PartReplacementBo partReplacementBo) {
        QueryWrapper queryWrapper = buildQueryWrapper(partReplacementBo);
        Page<PartReplacementVo> page = this.pageAs(PageQuery.build(), queryWrapper, PartReplacementVo.class);
        return PageResult.build(page);
    }

    /**
     * 新增换件记录
     *
     * @param partReplacementBo 换件记录Bo
     * @return 结果:true 操作成功，false 操作失败
     */
    @Override
    public boolean insert(PartReplacementBo partReplacementBo) {
        PartReplacement partReplacement = MapstructUtils.convert(partReplacementBo, PartReplacement.class);

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

    /**
     * 新增换件记录，前台提供主键值，一般用于导入的场合
     *
     * @param partReplacementBo 换件记录Bo
     * @return 结果:true 操作成功，false 操作失败
     */
    @Override
    public boolean insertWithPk(PartReplacementBo partReplacementBo) {
        PartReplacement partReplacement = MapstructUtils.convert(partReplacementBo, PartReplacement.class);


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

    /**
     * 修改换件记录
     *
     * @param partReplacementBo 换件记录Bo
     * @return 结果:true 更新成功，false 更新失败
     */
    @Override
    public boolean update(PartReplacementBo partReplacementBo) {
        PartReplacement partReplacement = MapstructUtils.convert(partReplacementBo, PartReplacement.class);
        if (ObjectUtil.isNotNull(partReplacement) && ObjectUtil.isNotNull(partReplacement.getId())) {
            boolean updated = this.updateById(partReplacement);
            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, PartReplacementImportVo.class, new PartReplacementImportListener(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<PartReplacementVo> listVo, String sheetName, LoginUser loginUser) {
        ExcelResultRes result = excelService.exportExcel(listVo, sheetName, PartReplacementVo.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));
    }

}
