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.LifePrediction;
import org.eco.als.domain.bo.LifePredictionBo;
import org.eco.als.domain.vo.LifePredictionVo;
import org.eco.als.mapper.LifePredictionMapper;
import org.eco.als.service.ILifePredictionService;
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.core.utils.StringUtils;
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.LifePredictionTableDef.LIFE_PREDICTION;

/**
 * 寿命预测Service业务层处理
 *
 * @author wgk
 * @date 2024-07-26
 */
@Service
@Slf4j
public class LifePredictionServiceImpl extends BaseServiceImpl<LifePredictionMapper, LifePrediction> implements ILifePredictionService {
    @Resource
    private LifePredictionMapper lifePredictionMapper;

    @Resource
    private IExcelService excelService;

    @Resource
    private IImportExportService importExportService;

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

    private QueryWrapper buildQueryWrapper(LifePredictionBo lifePredictionBo) {
        QueryWrapper queryWrapper = super.buildBaseQueryWrapper();
        if (StringUtils.isNotBlank(lifePredictionBo.getAircraftId())) {
            queryWrapper.and(LIFE_PREDICTION.AIRCRAFT_ID.in
                (lifePredictionBo.getAircraftId().split(",")));
        }
        queryWrapper.and(LIFE_PREDICTION.NAME.like
            (lifePredictionBo.getName()));
        queryWrapper.and(LIFE_PREDICTION.MODEL.eq
            (lifePredictionBo.getModel()));
        queryWrapper.and(LIFE_PREDICTION.SYSTEM.eq
            (lifePredictionBo.getSystem()));
        queryWrapper.and(LIFE_PREDICTION.METHOD_TYPE.eq
            (lifePredictionBo.getMethodType()));
        queryWrapper.and(LIFE_PREDICTION.REMAIN_TIEM.eq
            (lifePredictionBo.getRemainTiem()));

        return queryWrapper;
    }

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

    }


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

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

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

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

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


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

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

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

}
