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.FalseAlarmResult;
import org.eco.als.domain.bo.FalseAlarmResultBo;
import org.eco.als.domain.vo.FalseAlarmResultVo;
import org.eco.als.mapper.FalseAlarmResultMapper;
import org.eco.als.service.IFalseAlarmResultService;
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.FalseAlarmResultTableDef.FALSE_ALARM_RESULT;

/**
 * 虚警抑制结果Service业务层处理
 *
 * @author wgk
 * @date 2024-07-23
 */
@Service
@Slf4j
public class FalseAlarmResultServiceImpl extends BaseServiceImpl<FalseAlarmResultMapper, FalseAlarmResult> implements IFalseAlarmResultService {
    @Resource
    private FalseAlarmResultMapper falseAlarmResultMapper;

    @Resource
    private IExcelService excelService;

    @Resource
    private IImportExportService importExportService;

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

    private QueryWrapper buildQueryWrapper(FalseAlarmResultBo falseAlarmResultBo) {
        QueryWrapper queryWrapper = super.buildBaseQueryWrapper();
        queryWrapper.and(FALSE_ALARM_RESULT.FALSE_ALARM_ID.eq
            (falseAlarmResultBo.getFalseAlarmId()));
        queryWrapper.and(FALSE_ALARM_RESULT.SORTIE_NO.eq
            (falseAlarmResultBo.getSortieNo()));
        queryWrapper.and(FALSE_ALARM_RESULT.AIRCRAFT_ID.eq
            (falseAlarmResultBo.getAircraftId()));
        queryWrapper.and(FALSE_ALARM_RESULT.CODE.eq
            (falseAlarmResultBo.getCode()));
        queryWrapper.and(FALSE_ALARM_RESULT.NAME.like
            (falseAlarmResultBo.getName()));
        queryWrapper.and(FALSE_ALARM_RESULT.DESCRIBE.eq
            (falseAlarmResultBo.getDescribe()));
        queryWrapper.and(FALSE_ALARM_RESULT.STATUS.eq
            (falseAlarmResultBo.getStatus()));

        return queryWrapper;
    }

    /**
     * 查询虚警抑制结果
     *
     * @param id 虚警抑制结果主键
     * @return 虚警抑制结果
     */
    @Override
    public FalseAlarmResultVo selectById(Long id) {
        return this.getOneAs(query().where(FALSE_ALARM_RESULT.ID.eq(id)), FalseAlarmResultVo.class);

    }


    /**
     * 查询虚警抑制结果列表
     *
     * @param falseAlarmResultBo 虚警抑制结果Bo
     * @return 虚警抑制结果集合
     */
    @Override
    public List<FalseAlarmResultVo> selectList(FalseAlarmResultBo falseAlarmResultBo) {
        QueryWrapper queryWrapper = buildQueryWrapper(falseAlarmResultBo);
        return this.listAs(queryWrapper, FalseAlarmResultVo.class);
    }

    /**
     * 分页查询虚警抑制结果列表
     *
     * @param falseAlarmResultBo 虚警抑制结果Bo
     * @return 分页虚警抑制结果集合
     */
    @Override
    public PageResult<FalseAlarmResultVo> selectPage(FalseAlarmResultBo falseAlarmResultBo) {
        QueryWrapper queryWrapper = buildQueryWrapper(falseAlarmResultBo);
        Page<FalseAlarmResultVo> page = this.pageAs(PageQuery.build(), queryWrapper, FalseAlarmResultVo.class);
        return PageResult.build(page);
    }

    /**
     * 新增虚警抑制结果
     *
     * @param falseAlarmResultBo 虚警抑制结果Bo
     * @return 结果:true 操作成功，false 操作失败
     */
    @Override
    public boolean insert(FalseAlarmResultBo falseAlarmResultBo) {
        FalseAlarmResult falseAlarmResult = MapstructUtils.convert(falseAlarmResultBo, FalseAlarmResult.class);

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

    /**
     * 新增虚警抑制结果，前台提供主键值，一般用于导入的场合
     *
     * @param falseAlarmResultBo 虚警抑制结果Bo
     * @return 结果:true 操作成功，false 操作失败
     */
    @Override
    public boolean insertWithPk(FalseAlarmResultBo falseAlarmResultBo) {
        FalseAlarmResult falseAlarmResult = MapstructUtils.convert(falseAlarmResultBo, FalseAlarmResult.class);


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

    /**
     * 修改虚警抑制结果
     *
     * @param falseAlarmResultBo 虚警抑制结果Bo
     * @return 结果:true 更新成功，false 更新失败
     */
    @Override
    public boolean update(FalseAlarmResultBo falseAlarmResultBo) {
        FalseAlarmResult falseAlarmResult = MapstructUtils.convert(falseAlarmResultBo, FalseAlarmResult.class);
        if (ObjectUtil.isNotNull(falseAlarmResult) && ObjectUtil.isNotNull(falseAlarmResult.getId())) {
            return this.updateById(falseAlarmResult);
        }
        return false;
    }

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

}
