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.AirConfigurationMapper;
import org.eco.als.domain.AirConfiguration;
import org.eco.als.domain.bo.AirConfigurationBo;
import org.eco.als.domain.vo.AirConfigurationVo;
import org.eco.als.domain.vo.AirConfigurationImportVo;
import org.eco.als.listener.AirConfigurationImportListener;
import org.eco.als.service.IAirConfigurationService;

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

/**
 * 飞机构型Service业务层处理
 *
 * @author wgk
 * @date 2024-11-13
 */
@Service
@Slf4j
public class AirConfigurationServiceImpl extends BaseServiceImpl<AirConfigurationMapper, AirConfiguration> implements IAirConfigurationService {
    @Resource
    private AirConfigurationMapper airConfigurationMapper;

    @Resource
    private IExcelService excelService;

    @Resource
    private IImportExportService importExportService;

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

    private QueryWrapper buildQueryWrapper(AirConfigurationBo airConfigurationBo) {
        QueryWrapper queryWrapper = super.buildBaseQueryWrapper();
        queryWrapper.and(AIR_CONFIGURATION.PARENT_ID.eq
            (airConfigurationBo.getParentId()));
        queryWrapper.and(AIR_CONFIGURATION.PARENT_NAME.like
            (airConfigurationBo.getParentName()));
        queryWrapper.and(AIR_CONFIGURATION.ANCESTORS.eq
            (airConfigurationBo.getAncestors()));
        if (StringUtils.isNotBlank(airConfigurationBo.getAircraftType())) {
            String[] airTypeIds = airConfigurationBo.getAircraftType().split(",");
            queryWrapper.and(AIR_CONFIGURATION.AIRCRAFT_TYPE.in(airTypeIds));
        }
        queryWrapper.and(AIR_CONFIGURATION.CON_CODE.eq
            (airConfigurationBo.getConCode()));
        queryWrapper.and(AIR_CONFIGURATION.NAME.like
            (airConfigurationBo.getName()));
        queryWrapper.and(AIR_CONFIGURATION.SPECS_MODEL.eq
            (airConfigurationBo.getSpecsModel()));
        queryWrapper.and(AIR_CONFIGURATION.DRAWING_NUMBER.eq
            (airConfigurationBo.getDrawingNumber()));
        queryWrapper.and(AIR_CONFIGURATION.SUPPLIER.eq
            (airConfigurationBo.getSupplier()));
        queryWrapper.and(AIR_CONFIGURATION.REMARKS.eq
            (airConfigurationBo.getRemarks()));
        queryWrapper.and(AIR_CONFIGURATION.ORDER_NUM.eq
            (airConfigurationBo.getOrderNum()));
        queryWrapper.orderBy(AIR_CONFIGURATION.ORDER_NUM.asc());

        return queryWrapper;
    }

    /**
     * 查询飞机构型
     *
     * @param id 飞机构型主键
     * @return 飞机构型
     */
    @Override
    public AirConfigurationVo selectById(Long id) {
        return this.getOneAs(query().where(AIR_CONFIGURATION.ID.eq(id)), AirConfigurationVo.class);

    }

    /**
     * 查询飞机构型
     *
     * @param name 飞机构型主键
     * @return 飞机构型
     */
    @Override
    public AirConfigurationVo selectByName(String name) {
        return this.getOneAs(query().where(AIR_CONFIGURATION.NAME.eq(name)), AirConfigurationVo.class);
    }

    /**
     * 查询飞机构型列表
     *
     * @param airConfigurationBo 飞机构型Bo
     * @return 飞机构型集合
     */
    @Override
    public List<AirConfigurationVo> selectList(AirConfigurationBo airConfigurationBo) {
        QueryWrapper queryWrapper = buildQueryWrapper(airConfigurationBo);
        return this.listAs(queryWrapper, AirConfigurationVo.class);
    }


    /**
     * 新增飞机构型
     *
     * @param airConfigurationBo 飞机构型Bo
     * @return 结果:true 操作成功，false 操作失败
     */
    @Override
    public boolean insert(AirConfigurationBo airConfigurationBo) {
        AirConfiguration airConfiguration = MapstructUtils.convert(airConfigurationBo, AirConfiguration.class);

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

    /**
     * 新增飞机构型，前台提供主键值，一般用于导入的场合
     *
     * @param airConfigurationBo 飞机构型Bo
     * @return 结果:true 操作成功，false 操作失败
     */
    @Override
    public boolean insertWithPk(AirConfigurationBo airConfigurationBo) {
        AirConfiguration airConfiguration = MapstructUtils.convert(airConfigurationBo, AirConfiguration.class);

        //获取祖级列表字段
        Long parentId = airConfiguration.getParentId();
        if (parentId == 0) {
            airConfiguration.setAncestors("0");
        } else {
            AirConfigurationVo parentAirConfiguration = selectById(airConfigurationBo.getParentId());
            if (ObjectUtil.isNotNull(parentAirConfiguration)) {
                airConfiguration.setAncestors(parentAirConfiguration.getAncestors() + "," + parentId);
            } else {
                airConfiguration.setAncestors("0");
            }
        }

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

    /**
     * 修改飞机构型
     *
     * @param airConfigurationBo 飞机构型Bo
     * @return 结果:true 更新成功，false 更新失败
     */
    @Override
    public boolean update(AirConfigurationBo airConfigurationBo) {
        AirConfiguration airConfiguration = MapstructUtils.convert(airConfigurationBo, AirConfiguration.class);
        if (ObjectUtil.isNotNull(airConfiguration) && ObjectUtil.isNotNull(airConfiguration.getId())) {
            boolean updated = this.updateById(airConfiguration);
            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, AirConfigurationImportVo.class, new AirConfigurationImportListener(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<AirConfigurationVo> listVo, String sheetName, LoginUser loginUser) {
        ExcelResultRes result = excelService.exportExcel(listVo, sheetName, AirConfigurationVo.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));
    }

}
