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.MaintenanceDecision;
import org.eco.als.domain.bo.MaintenanceDecisionBo;
import org.eco.als.domain.vo.MaintenanceDecisionVo;
import org.eco.als.mapper.MaintenanceDecisionMapper;
import org.eco.als.service.IMaintenanceDecisionService;
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.MaintenanceDecisionTableDef.MAINTENANCE_DECISION;

/**
 * 辅助维修决策Service业务层处理
 *
 * @author wgk
 * @date 2024-07-24
 */
@Service
@Slf4j
public class MaintenanceDecisionServiceImpl extends BaseServiceImpl<MaintenanceDecisionMapper, MaintenanceDecision> implements IMaintenanceDecisionService {
    @Resource
    private MaintenanceDecisionMapper maintenanceDecisionMapper;

    @Resource
    private IExcelService excelService;

    @Resource
    private IImportExportService importExportService;

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

    private QueryWrapper buildQueryWrapper(MaintenanceDecisionBo maintenanceDecisionBo) {
        QueryWrapper queryWrapper = super.buildBaseQueryWrapper();
        queryWrapper.and(MAINTENANCE_DECISION.SORTIE_NO.eq
            (maintenanceDecisionBo.getSortieNo()));
        queryWrapper.and(MAINTENANCE_DECISION.AIRCRAFT_ID.eq
            (maintenanceDecisionBo.getAircraftId()));
        queryWrapper.and(MAINTENANCE_DECISION.CODE.eq
            (maintenanceDecisionBo.getCode()));
        queryWrapper.and(MAINTENANCE_DECISION.SYSTEM_NAME.like
            (maintenanceDecisionBo.getSystemName()));
        queryWrapper.and(MAINTENANCE_DECISION.UNIT_NAME.like
            (maintenanceDecisionBo.getUnitName()));
        queryWrapper.and(MAINTENANCE_DECISION.STATUS.eq
            (maintenanceDecisionBo.getStatus()));

        return queryWrapper;
    }

    /**
     * 查询辅助维修决策
     *
     * @param id 辅助维修决策主键
     * @return 辅助维修决策
     */
    @Override
    public MaintenanceDecisionVo selectById(Long id) {
        return this.getOneAs(query().where(MAINTENANCE_DECISION.ID.eq(id)), MaintenanceDecisionVo.class);

    }


    /**
     * 查询辅助维修决策列表
     *
     * @param maintenanceDecisionBo 辅助维修决策Bo
     * @return 辅助维修决策集合
     */
    @Override
    public List<MaintenanceDecisionVo> selectList(MaintenanceDecisionBo maintenanceDecisionBo) {
        QueryWrapper queryWrapper = buildQueryWrapper(maintenanceDecisionBo);
        return this.listAs(queryWrapper, MaintenanceDecisionVo.class);
    }

    /**
     * 分页查询辅助维修决策列表
     *
     * @param maintenanceDecisionBo 辅助维修决策Bo
     * @return 分页辅助维修决策集合
     */
    @Override
    public PageResult<MaintenanceDecisionVo> selectPage(MaintenanceDecisionBo maintenanceDecisionBo) {
        QueryWrapper queryWrapper = buildQueryWrapper(maintenanceDecisionBo);
        Page<MaintenanceDecisionVo> page = this.pageAs(PageQuery.build(), queryWrapper, MaintenanceDecisionVo.class);
        return PageResult.build(page);
    }

    /**
     * 新增辅助维修决策
     *
     * @param maintenanceDecisionBo 辅助维修决策Bo
     * @return 结果:true 操作成功，false 操作失败
     */
    @Override
    public boolean insert(MaintenanceDecisionBo maintenanceDecisionBo) {
        MaintenanceDecision maintenanceDecision = MapstructUtils.convert(maintenanceDecisionBo, MaintenanceDecision.class);

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

    /**
     * 新增辅助维修决策，前台提供主键值，一般用于导入的场合
     *
     * @param maintenanceDecisionBo 辅助维修决策Bo
     * @return 结果:true 操作成功，false 操作失败
     */
    @Override
    public boolean insertWithPk(MaintenanceDecisionBo maintenanceDecisionBo) {
        MaintenanceDecision maintenanceDecision = MapstructUtils.convert(maintenanceDecisionBo, MaintenanceDecision.class);


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

    /**
     * 修改辅助维修决策
     *
     * @param maintenanceDecisionBo 辅助维修决策Bo
     * @return 结果:true 更新成功，false 更新失败
     */
    @Override
    public boolean update(MaintenanceDecisionBo maintenanceDecisionBo) {
        MaintenanceDecision maintenanceDecision = MapstructUtils.convert(maintenanceDecisionBo, MaintenanceDecision.class);
        if (ObjectUtil.isNotNull(maintenanceDecision) && ObjectUtil.isNotNull(maintenanceDecision.getId())) {
            return this.updateById(maintenanceDecision);
        }
        return false;
    }

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

}
