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.FalseAlarm;
import org.eco.als.domain.bo.FalseAlarmBo;
import org.eco.als.domain.vo.FalseAlarmVo;
import org.eco.als.mapper.FalseAlarmMapper;
import org.eco.als.service.IFalseAlarmService;
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.FalseAlarmTableDef.FALSE_ALARM;

/**
 * 虚警抑制Service业务层处理
 *
 * @author wgk
 * @date 2024-07-23
 */
@Service
@Slf4j
public class FalseAlarmServiceImpl extends BaseServiceImpl<FalseAlarmMapper, FalseAlarm> implements IFalseAlarmService {
    @Resource
    private FalseAlarmMapper falseAlarmMapper;

    @Resource
    private IExcelService excelService;

    @Resource
    private IImportExportService importExportService;

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

    private QueryWrapper buildQueryWrapper(FalseAlarmBo falseAlarmBo) {
        QueryWrapper queryWrapper = super.buildBaseQueryWrapper();
        queryWrapper.and(FALSE_ALARM.SORTIE_NO.eq
            (falseAlarmBo.getSortieNo()));
        queryWrapper.and(FALSE_ALARM.AIRCRAFT_ID.eq
            (falseAlarmBo.getAircraftId()));
        queryWrapper.and(FALSE_ALARM.STATUS.eq
            (falseAlarmBo.getStatus()));

        return queryWrapper;
    }

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

    }


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

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

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

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

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


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

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

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

}
