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.FlightSplitTask;
import org.eco.als.domain.bo.FlightSplitTaskBo;
import org.eco.als.domain.vo.FlightSplitTaskImportVo;
import org.eco.als.domain.vo.FlightSplitTaskVo;
import org.eco.als.listener.FlightSplitTaskImportListener;
import org.eco.als.mapper.FlightSplitTaskMapper;
import org.eco.als.service.IFlightSplitTaskService;
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 org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Arrays;
import java.util.List;

import static org.eco.als.domain.table.FlightSplitTaskTableDef.FLIGHT_SPLIT_TASK;

/**
 * 飞行分解任务Service业务层处理
 *
 * @author wgk
 * @date 2024-07-24
 */
@Service
@Slf4j
public class FlightSplitTaskServiceImpl extends BaseServiceImpl<FlightSplitTaskMapper, FlightSplitTask> implements IFlightSplitTaskService {
    @Resource
    private FlightSplitTaskMapper flightSplitTaskMapper;

    @Resource
    private IExcelService excelService;

    @Resource
    private IImportExportService importExportService;

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

    private QueryWrapper buildQueryWrapper(FlightSplitTaskBo flightSplitTaskBo) {
        QueryWrapper queryWrapper = super.buildBaseQueryWrapper();
        queryWrapper.and(FLIGHT_SPLIT_TASK.AIRCRAFT_MODEL.eq
            (flightSplitTaskBo.getAircraftModel()));
        queryWrapper.and(FLIGHT_SPLIT_TASK.AIRCRAFT_GROUP.eq
            (flightSplitTaskBo.getAircraftGroup()));
        queryWrapper.and(FLIGHT_SPLIT_TASK.DURATION.eq
            (flightSplitTaskBo.getDuration()));
        queryWrapper.and(FLIGHT_SPLIT_TASK.DEVICE.eq
            (flightSplitTaskBo.getDevice()));
        queryWrapper.and(FLIGHT_SPLIT_TASK.DEVICE_COUNT.eq
            (flightSplitTaskBo.getDeviceCount()));
        queryWrapper.and(FLIGHT_SPLIT_TASK.UNIT.eq
            (flightSplitTaskBo.getUnit()));

        return queryWrapper;
    }

    /**
     * 查询飞行分解任务
     *
     * @param id 飞行分解任务主键
     * @return 飞行分解任务
     */
    @Override
    public FlightSplitTaskVo selectById(Long id) {
        return this.getOneAs(query().where(FLIGHT_SPLIT_TASK.ID.eq(id)), FlightSplitTaskVo.class);

    }


    /**
     * 查询飞行分解任务列表
     *
     * @param flightSplitTaskBo 飞行分解任务Bo
     * @return 飞行分解任务集合
     */
    @Override
    public List<FlightSplitTaskVo> selectList(FlightSplitTaskBo flightSplitTaskBo) {
        QueryWrapper queryWrapper = buildQueryWrapper(flightSplitTaskBo);
        return this.listAs(queryWrapper, FlightSplitTaskVo.class);
    }

    /**
     * 分页查询飞行分解任务列表
     *
     * @param flightSplitTaskBo 飞行分解任务Bo
     * @return 分页飞行分解任务集合
     */
    @Override
    public PageResult<FlightSplitTaskVo> selectPage(FlightSplitTaskBo flightSplitTaskBo) {
        QueryWrapper queryWrapper = buildQueryWrapper(flightSplitTaskBo);
        Page<FlightSplitTaskVo> page = this.pageAs(PageQuery.build(), queryWrapper, FlightSplitTaskVo.class);
        return PageResult.build(page);
    }

    /**
     * 新增飞行分解任务
     *
     * @param flightSplitTaskBo 飞行分解任务Bo
     * @return 结果:true 操作成功，false 操作失败
     */
    @Override
    public boolean insert(FlightSplitTaskBo flightSplitTaskBo) {
        FlightSplitTask flightSplitTask = MapstructUtils.convert(flightSplitTaskBo, FlightSplitTask.class);

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

    /**
     * 新增飞行分解任务，前台提供主键值，一般用于导入的场合
     *
     * @param flightSplitTaskBo 飞行分解任务Bo
     * @return 结果:true 操作成功，false 操作失败
     */
    @Override
    public boolean insertWithPk(FlightSplitTaskBo flightSplitTaskBo) {
        FlightSplitTask flightSplitTask = MapstructUtils.convert(flightSplitTaskBo, FlightSplitTask.class);


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

    /**
     * 修改飞行分解任务
     *
     * @param flightSplitTaskBo 飞行分解任务Bo
     * @return 结果:true 更新成功，false 更新失败
     */
    @Override
    public boolean update(FlightSplitTaskBo flightSplitTaskBo) {
        FlightSplitTask flightSplitTask = MapstructUtils.convert(flightSplitTaskBo, FlightSplitTask.class);
        if (ObjectUtil.isNotNull(flightSplitTask) && ObjectUtil.isNotNull(flightSplitTask.getId())) {
            return this.updateById(flightSplitTask);
        }
        return false;
    }

    @Override
    public void asyncImportData(MultipartFile file, boolean updateSupport, LoginUser loginUser) {
        ExcelResultRes result;
        try {
            String name = file.getOriginalFilename();
            result = excelService.importExcel(file.getInputStream(), name, FlightSplitTaskImportVo.class, new FlightSplitTaskImportListener(updateSupport, loginUser));
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        boolean flag = importExportService.saveInfo(result, loginUser, "0");
        if (flag) {
            log.info("异步导入日志写入成功");
        }
    }

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

}
