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.LifePredictionResult;
import org.eco.als.domain.bo.LifePredictionResultBo;
import org.eco.als.domain.vo.LifePredictionResultVo;
import org.eco.als.mapper.LifePredictionResultMapper;
import org.eco.als.service.ILifePredictionResultService;
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.LifePredictionResultTableDef.LIFE_PREDICTION_RESULT;

/**
 * 寿命预测结果Service业务层处理
 *
 * @author wgk
 * @date 2024-07-26
 */
@Service
@Slf4j
public class LifePredictionResultServiceImpl extends BaseServiceImpl<LifePredictionResultMapper, LifePredictionResult> implements ILifePredictionResultService {
    @Resource
    private LifePredictionResultMapper lifePredictionResultMapper;

    @Resource
    private IExcelService excelService;

    @Resource
    private IImportExportService importExportService;

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

    private QueryWrapper buildQueryWrapper(LifePredictionResultBo lifePredictionResultBo) {
        QueryWrapper queryWrapper = super.buildBaseQueryWrapper();
        queryWrapper.and(LIFE_PREDICTION_RESULT.LIFE_ID.eq
            (lifePredictionResultBo.getLifeId()));
        queryWrapper.and(LIFE_PREDICTION_RESULT.AIRCRAFT_ID.eq
            (lifePredictionResultBo.getAircraftId()));
        queryWrapper.and(LIFE_PREDICTION_RESULT.NAME.like
            (lifePredictionResultBo.getName()));
        queryWrapper.and(LIFE_PREDICTION_RESULT.MODEL.eq
            (lifePredictionResultBo.getModel()));
        queryWrapper.and(LIFE_PREDICTION_RESULT.SYSTEM.eq
            (lifePredictionResultBo.getSystem()));
        queryWrapper.and(LIFE_PREDICTION_RESULT.METHOD_TYPE.eq
            (lifePredictionResultBo.getMethodType()));
        queryWrapper.and(LIFE_PREDICTION_RESULT.REMAIN_TIEM.eq
            (lifePredictionResultBo.getRemainTiem()));

        return queryWrapper;
    }

    /**
     * 查询寿命预测结果
     *
     * @param id 寿命预测结果主键
     * @return 寿命预测结果
     */
    @Override
    public LifePredictionResultVo selectById(Long id) {
        return this.getOneAs(query().where(LIFE_PREDICTION_RESULT.ID.eq(id)), LifePredictionResultVo.class);

    }


    /**
     * 查询寿命预测结果列表
     *
     * @param lifePredictionResultBo 寿命预测结果Bo
     * @return 寿命预测结果集合
     */
    @Override
    public List<LifePredictionResultVo> selectList(LifePredictionResultBo lifePredictionResultBo) {
        QueryWrapper queryWrapper = buildQueryWrapper(lifePredictionResultBo);
        return this.listAs(queryWrapper, LifePredictionResultVo.class);
    }

    /**
     * 分页查询寿命预测结果列表
     *
     * @param lifePredictionResultBo 寿命预测结果Bo
     * @return 分页寿命预测结果集合
     */
    @Override
    public PageResult<LifePredictionResultVo> selectPage(LifePredictionResultBo lifePredictionResultBo) {
        QueryWrapper queryWrapper = buildQueryWrapper(lifePredictionResultBo);
        Page<LifePredictionResultVo> page = this.pageAs(PageQuery.build(), queryWrapper, LifePredictionResultVo.class);
        return PageResult.build(page);
    }

    /**
     * 新增寿命预测结果
     *
     * @param lifePredictionResultBo 寿命预测结果Bo
     * @return 结果:true 操作成功，false 操作失败
     */
    @Override
    public boolean insert(LifePredictionResultBo lifePredictionResultBo) {
        LifePredictionResult lifePredictionResult = MapstructUtils.convert(lifePredictionResultBo, LifePredictionResult.class);

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

    /**
     * 新增寿命预测结果，前台提供主键值，一般用于导入的场合
     *
     * @param lifePredictionResultBo 寿命预测结果Bo
     * @return 结果:true 操作成功，false 操作失败
     */
    @Override
    public boolean insertWithPk(LifePredictionResultBo lifePredictionResultBo) {
        LifePredictionResult lifePredictionResult = MapstructUtils.convert(lifePredictionResultBo, LifePredictionResult.class);


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

    /**
     * 修改寿命预测结果
     *
     * @param lifePredictionResultBo 寿命预测结果Bo
     * @return 结果:true 更新成功，false 更新失败
     */
    @Override
    public boolean update(LifePredictionResultBo lifePredictionResultBo) {
        LifePredictionResult lifePredictionResult = MapstructUtils.convert(lifePredictionResultBo, LifePredictionResult.class);
        if (ObjectUtil.isNotNull(lifePredictionResult) && ObjectUtil.isNotNull(lifePredictionResult.getId())) {
            return this.updateById(lifePredictionResult);
        }
        return false;
    }

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

}
