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.MaintenanceTask;
import org.eco.als.domain.bo.MaintenanceTaskBo;
import org.eco.als.domain.vo.MaintenanceTaskImportVo;
import org.eco.als.domain.vo.MaintenanceTaskVo;
import org.eco.als.listener.MaintenanceTaskImportListener;
import org.eco.als.mapper.MaintenanceTaskMapper;
import org.eco.als.service.IMaintenanceTaskService;
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 org.springframework.web.multipart.MultipartFile;

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

import static org.eco.als.domain.table.MaintenanceTaskTableDef.MAINTENANCE_TASK;

/**
 * 维修信息Service业务层处理
 *
 * @author wgk
 * @date 2024-11-14
 */
@Service
@Slf4j
public class MaintenanceTaskServiceImpl extends BaseServiceImpl<MaintenanceTaskMapper, MaintenanceTask> implements IMaintenanceTaskService {
    @Resource
    private MaintenanceTaskMapper maintenanceTaskMapper;

    @Resource
    private IExcelService excelService;

    @Resource
    private IImportExportService importExportService;

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

    private QueryWrapper buildQueryWrapper(MaintenanceTaskBo maintenanceTaskBo) {
        QueryWrapper queryWrapper = super.buildBaseQueryWrapper();
        queryWrapper.and(MAINTENANCE_TASK.TASK_ID.eq
            (maintenanceTaskBo.getTaskId()));
        queryWrapper.and(MAINTENANCE_TASK.AIRCRAFT_CODE.eq
            (maintenanceTaskBo.getAircraftCode()));
        if (StringUtils.isNotBlank(maintenanceTaskBo.getAircraftType())) {
            String[] airTypeIds = maintenanceTaskBo.getAircraftType().split(",");
            queryWrapper.and(MAINTENANCE_TASK.AIRCRAFT_TYPE.in
                (airTypeIds));
        }
        queryWrapper.and(MAINTENANCE_TASK.DATA_TYPE.eq
            (maintenanceTaskBo.getDataType()));
        queryWrapper.and(MAINTENANCE_TASK.FAULT_CODE.eq
            (maintenanceTaskBo.getFaultCode()));
        queryWrapper.and(MAINTENANCE_TASK.FAULT_NAME.like
            (maintenanceTaskBo.getFaultName()));
        queryWrapper.and(MAINTENANCE_TASK.LOCATION.eq
            (maintenanceTaskBo.getLocation()));
        queryWrapper.and(MAINTENANCE_TASK.LEVEL.eq
            (maintenanceTaskBo.getLevel()));
        queryWrapper.and(MAINTENANCE_TASK.FAULT_DATE.eq
            (maintenanceTaskBo.getFaultDate()));
        queryWrapper.and(MAINTENANCE_TASK.FAULT_DESCRIPTION.eq
            (maintenanceTaskBo.getFaultDescription()));
        queryWrapper.and(MAINTENANCE_TASK.SYMPTOMS.eq
            (maintenanceTaskBo.getSymptoms()));
        queryWrapper.and(MAINTENANCE_TASK.FAULT_CAUSE.eq
            (maintenanceTaskBo.getFaultCause()));
        queryWrapper.and(MAINTENANCE_TASK.STATUS.eq
            (maintenanceTaskBo.getStatus()));
        queryWrapper.and(MAINTENANCE_TASK.REMARKS.eq
            (maintenanceTaskBo.getRemarks()));

        return queryWrapper;
    }

    /**
     * 查询维修信息
     *
     * @param id 维修信息主键
     * @return 维修信息
     */
    @Override
    public MaintenanceTaskVo selectById(Long id) {
        return this.getOneAs(query().where(MAINTENANCE_TASK.ID.eq(id)), MaintenanceTaskVo.class);

    }


    /**
     * 查询维修信息列表
     *
     * @param maintenanceTaskBo 维修信息Bo
     * @return 维修信息集合
     */
    @Override
    public List<MaintenanceTaskVo> selectList(MaintenanceTaskBo maintenanceTaskBo) {
        QueryWrapper queryWrapper = buildQueryWrapper(maintenanceTaskBo);
        return this.listAs(queryWrapper, MaintenanceTaskVo.class);
    }

    /**
     * 分页查询维修信息列表
     *
     * @param maintenanceTaskBo 维修信息Bo
     * @return 分页维修信息集合
     */
    @Override
    public PageResult<MaintenanceTaskVo> selectPage(MaintenanceTaskBo maintenanceTaskBo) {
        QueryWrapper queryWrapper = buildQueryWrapper(maintenanceTaskBo);
        Page<MaintenanceTaskVo> page = this.pageAs(PageQuery.build(), queryWrapper, MaintenanceTaskVo.class);
        return PageResult.build(page);
    }

    /**
     * 新增维修信息
     *
     * @param maintenanceTaskBo 维修信息Bo
     * @return 结果:true 操作成功，false 操作失败
     */
    @Override
    public boolean insert(MaintenanceTaskBo maintenanceTaskBo) {
        MaintenanceTask maintenanceTask = MapstructUtils.convert(maintenanceTaskBo, MaintenanceTask.class);

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

    /**
     * 新增维修信息，前台提供主键值，一般用于导入的场合
     *
     * @param maintenanceTaskBo 维修信息Bo
     * @return 结果:true 操作成功，false 操作失败
     */
    @Override
    public boolean insertWithPk(MaintenanceTaskBo maintenanceTaskBo) {
        MaintenanceTask maintenanceTask = MapstructUtils.convert(maintenanceTaskBo, MaintenanceTask.class);


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

    /**
     * 修改维修信息
     *
     * @param maintenanceTaskBo 维修信息Bo
     * @return 结果:true 更新成功，false 更新失败
     */
    @Override
    public boolean update(MaintenanceTaskBo maintenanceTaskBo) {
        MaintenanceTask maintenanceTask = MapstructUtils.convert(maintenanceTaskBo, MaintenanceTask.class);
        if (ObjectUtil.isNotNull(maintenanceTask) && ObjectUtil.isNotNull(maintenanceTask.getId())) {
            boolean updated = this.updateById(maintenanceTask);
            return updated;
        }
        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, MaintenanceTaskImportVo.class, new MaintenanceTaskImportListener(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<MaintenanceTaskVo> listVo, String sheetName, LoginUser loginUser) {
        ExcelResultRes result = excelService.exportExcel(listVo, sheetName, MaintenanceTaskVo.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));
    }

}
