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.PreProcessing;
import org.eco.als.domain.bo.PreProcessingBo;
import org.eco.als.domain.vo.PreProcessingVo;
import org.eco.als.mapper.PreProcessingMapper;
import org.eco.als.service.IPreProcessingService;
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 java.util.Arrays;
import java.util.List;

import static org.eco.als.domain.table.PreProcessingTableDef.PRE_PROCESSING;

/**
 * 预处理Service业务层处理
 *
 * @author wgk
 * @date 2024-07-22
 */
@Service
@Slf4j
public class PreProcessingServiceImpl extends BaseServiceImpl<PreProcessingMapper, PreProcessing> implements IPreProcessingService {
    @Resource
    private PreProcessingMapper preProcessingMapper;

    @Resource
    private IExcelService excelService;

    @Resource
    private IImportExportService importExportService;

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

    private QueryWrapper buildQueryWrapper(PreProcessingBo preProcessingBo) {
        QueryWrapper queryWrapper = super.buildBaseQueryWrapper();
        queryWrapper.and(PRE_PROCESSING.SORTIE_NO.eq
            (preProcessingBo.getSortieNo()));
        queryWrapper.and(PRE_PROCESSING.AIRCRAFT_ID.eq
            (preProcessingBo.getAircraftId()));
        queryWrapper.and(PRE_PROCESSING.OSS_ID.eq
            (preProcessingBo.getOssId()));
        queryWrapper.and(PRE_PROCESSING.STATUS.eq
            (preProcessingBo.getStatus()));

        return queryWrapper;
    }

    /**
     * 查询预处理
     *
     * @param id 预处理主键
     * @return 预处理
     */
    @Override
    public PreProcessingVo selectById(Long id) {
        return this.getOneAs(query().where(PRE_PROCESSING.ID.eq(id)), PreProcessingVo.class);

    }


    /**
     * 查询预处理列表
     *
     * @param preProcessingBo 预处理Bo
     * @return 预处理集合
     */
    @Override
    public List<PreProcessingVo> selectList(PreProcessingBo preProcessingBo) {
        QueryWrapper queryWrapper = buildQueryWrapper(preProcessingBo);
        return this.listAs(queryWrapper, PreProcessingVo.class);
    }

    /**
     * 分页查询预处理列表
     *
     * @param preProcessingBo 预处理Bo
     * @return 分页预处理集合
     */
    @Override
    public PageResult<PreProcessingVo> selectPage(PreProcessingBo preProcessingBo) {
        QueryWrapper queryWrapper = buildQueryWrapper(preProcessingBo);
        Page<PreProcessingVo> page = this.pageAs(PageQuery.build(), queryWrapper, PreProcessingVo.class);
        return PageResult.build(page);
    }

    /**
     * 新增预处理
     *
     * @param preProcessingBo 预处理Bo
     * @return 结果:true 操作成功，false 操作失败
     */
    @Override
    public boolean insert(PreProcessingBo preProcessingBo) {
        PreProcessing preProcessing = MapstructUtils.convert(preProcessingBo, PreProcessing.class);

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

    /**
     * 新增预处理，前台提供主键值，一般用于导入的场合
     *
     * @param preProcessingBo 预处理Bo
     * @return 结果:true 操作成功，false 操作失败
     */
    @Override
    public boolean insertWithPk(PreProcessingBo preProcessingBo) {
        PreProcessing preProcessing = MapstructUtils.convert(preProcessingBo, PreProcessing.class);


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

    /**
     * 修改预处理
     *
     * @param preProcessingBo 预处理Bo
     * @return 结果:true 更新成功，false 更新失败
     */
    @Override
    public boolean update(PreProcessingBo preProcessingBo) {
        PreProcessing preProcessing = MapstructUtils.convert(preProcessingBo, PreProcessing.class);
        if (ObjectUtil.isNotNull(preProcessing) && ObjectUtil.isNotNull(preProcessing.getId())) {
            return this.updateById(preProcessing);
        }
        return false;
    }

    @Override
    public void asyncExport(List<PreProcessingVo> listVo, String sheetName, LoginUser loginUser) {
        ExcelResultRes result = excelService.exportExcel(listVo, sheetName, PreProcessingVo.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));
    }

}
