package org.eco.als.service.impl;

import java.util.Arrays;
import java.util.List;

import cn.hutool.core.util.ObjectUtil;
import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.eco.common.core.core.domain.model.LoginUser;
import org.eco.common.excel.entity.ExcelResultRes;
import org.eco.common.excel.service.IExcelService;
import org.eco.common.core.utils.bean.BeanUtils;
import org.eco.common.core.utils.MapstructUtils;
import org.eco.system.service.IImportExportService;
import org.eco.common.core.utils.StringUtils;
import org.eco.system.domain.bo.ImportExportBo;
import org.eco.common.orm.core.page.PageQuery;
import org.eco.common.core.core.page.PageResult;
import org.eco.common.orm.core.service.impl.BaseServiceImpl;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

import org.eco.als.mapper.MaintenanceMapper;
import org.eco.als.domain.Maintenance;
import org.eco.als.domain.bo.MaintenanceBo;
import org.eco.als.domain.vo.MaintenanceVo;
import org.eco.als.domain.vo.MaintenanceImportVo;
import org.eco.als.listener.MaintenanceImportListener;
import org.eco.als.service.IMaintenanceService;

import static org.eco.als.domain.table.MaintenanceTableDef.MAINTENANCE;

/**
 * 维修信息Service业务层处理
 *
 * @author wgk
 * @date 2024-10-25
 */
@Service
@Slf4j
public class MaintenanceServiceImpl extends BaseServiceImpl<MaintenanceMapper, Maintenance> implements IMaintenanceService {
    @Resource
    private MaintenanceMapper maintenanceMapper;

    @Resource
    private IExcelService excelService;

    @Resource
    private IImportExportService importExportService;

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

    private QueryWrapper buildQueryWrapper(MaintenanceBo maintenanceBo) {
        QueryWrapper queryWrapper = super.buildBaseQueryWrapper();
        queryWrapper.and(MAINTENANCE.AIRCRAFT_CODE.eq
            (maintenanceBo.getAircraftCode()));
        queryWrapper.and(MAINTENANCE.FAULT_CODE.eq
            (maintenanceBo.getFaultCode()));
        queryWrapper.and(MAINTENANCE.FAULT_NAME.like
            (maintenanceBo.getFaultName()));
        queryWrapper.and(MAINTENANCE.LOCATION.eq
            (maintenanceBo.getLocation()));
        queryWrapper.and(MAINTENANCE.LEVEL.eq
            (maintenanceBo.getLevel()));
        queryWrapper.and(MAINTENANCE.FAULT_DATE.eq
            (maintenanceBo.getFaultDate()));
        queryWrapper.and(MAINTENANCE.SYMPTOMS.eq
            (maintenanceBo.getSymptoms()));
        queryWrapper.and(MAINTENANCE.FAULT_CAUSE.eq
            (maintenanceBo.getFaultCause()));
        queryWrapper.and(MAINTENANCE.STATUS.eq
            (maintenanceBo.getStatus()));
        queryWrapper.and(MAINTENANCE.SUPERVISOR.eq
            (maintenanceBo.getSupervisor()));
        queryWrapper.and(MAINTENANCE.MAINTENANCE_TIME.eq
            (maintenanceBo.getMaintenanceTime()));
        queryWrapper.and(MAINTENANCE.BEFORE_EQUIPMENT_CODE.eq
            (maintenanceBo.getBeforeEquipmentCode()));
        queryWrapper.and(MAINTENANCE.BEFORE_EQUIPMENT_NAME.like
            (maintenanceBo.getBeforeEquipmentName()));
        queryWrapper.and(MAINTENANCE.BEFORE_MODEL_NUMBER.eq
            (maintenanceBo.getBeforeModelNumber()));
        queryWrapper.and(MAINTENANCE.AFTER_EQUIPMENT_CODE.eq
            (maintenanceBo.getAfterEquipmentCode()));
        queryWrapper.and(MAINTENANCE.AFTER_EQUIPMENT_NAME.eq
            (maintenanceBo.getAfterEquipmentName()));
        queryWrapper.and(MAINTENANCE.AFTER_MODEL_NUMBER.eq
            (maintenanceBo.getAfterModelNumber()));
        queryWrapper.and(MAINTENANCE.QUANTITY.eq
            (maintenanceBo.getQuantity()));
        queryWrapper.and(MAINTENANCE.REMARKS.eq
            (maintenanceBo.getRemarks()));

        return queryWrapper;
    }

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

    }


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

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

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

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

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


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

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

}
