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.FaultCase;
import org.eco.als.domain.bo.FaultCaseBo;
import org.eco.als.domain.vo.FaultCaseImportVo;
import org.eco.als.domain.vo.FaultCaseVo;
import org.eco.als.listener.FaultCaseImportListener;
import org.eco.als.mapper.FaultCaseMapper;
import org.eco.als.service.IFaultCaseService;
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.FaultCaseTableDef.FAULT_CASE;

/**
 * 故障案例Service业务层处理
 *
 * @author wgk
 * @date 2024-11-14
 */
@Service
@Slf4j
public class FaultCaseServiceImpl extends BaseServiceImpl<FaultCaseMapper, FaultCase> implements IFaultCaseService {
    @Resource
    private FaultCaseMapper faultCaseMapper;

    @Resource
    private IExcelService excelService;

    @Resource
    private IImportExportService importExportService;

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

    private QueryWrapper buildQueryWrapper(FaultCaseBo faultCaseBo) {
        QueryWrapper queryWrapper = super.buildBaseQueryWrapper();
        queryWrapper.and(FAULT_CASE.AIRCRAFT_CODE.eq(faultCaseBo.getAircraftCode()));
        if (StringUtils.isNotBlank(faultCaseBo.getAircraftType())) {
            String[] airTypeIds = faultCaseBo.getAircraftType().split(",");
            queryWrapper.and(FAULT_CASE.AIRCRAFT_TYPE.in(airTypeIds));
        }
        queryWrapper.and(FAULT_CASE.FAULT_CODE.eq(faultCaseBo.getFaultCode()));
        queryWrapper.and(FAULT_CASE.FAULT_NAME.like(faultCaseBo.getFaultName()));
        queryWrapper.and(FAULT_CASE.OSS_ID.eq(faultCaseBo.getOssId()));
        queryWrapper.and(FAULT_CASE.FAULT_DATE.eq(faultCaseBo.getFaultDate()));
        queryWrapper.and(FAULT_CASE.SYSTEM_ITEM.eq(faultCaseBo.getSystemItem()));
        queryWrapper.and(FAULT_CASE.SPECIALTY.eq(faultCaseBo.getSpecialty()));
        queryWrapper.and(FAULT_CASE.LOCATION.eq(faultCaseBo.getLocation()));
        queryWrapper.and(FAULT_CASE.LEVEL.eq(faultCaseBo.getLevel()));
        queryWrapper.and(FAULT_CASE.SYMPTOMS.eq(faultCaseBo.getSymptoms()));
        queryWrapper.and(FAULT_CASE.METHOD_RECTIFICATION.eq(faultCaseBo.getMethodRectification()));
        queryWrapper.and(FAULT_CASE.FAULT_CAUSE_TYPE.eq(faultCaseBo.getFaultCauseType()));
        queryWrapper.and(FAULT_CASE.FAULT_CAUSE.eq(faultCaseBo.getFaultCause()));
        queryWrapper.and(FAULT_CASE.REMARKS.eq(faultCaseBo.getRemarks()));

        return queryWrapper;
    }

    /**
     * 查询故障案例
     *
     * @param id 故障案例主键
     * @return 故障案例
     */
    @Override
    public FaultCaseVo selectById(Long id) {
        return this.getOneAs(query().where(FAULT_CASE.ID.eq(id)), FaultCaseVo.class);

    }


    /**
     * 查询故障案例列表
     *
     * @param faultCaseBo 故障案例Bo
     * @return 故障案例集合
     */
    @Override
    public List<FaultCaseVo> selectList(FaultCaseBo faultCaseBo) {
        QueryWrapper queryWrapper = buildQueryWrapper(faultCaseBo);
        return this.listAs(queryWrapper, FaultCaseVo.class);
    }

    /**
     * 分页查询故障案例列表
     *
     * @param faultCaseBo 故障案例Bo
     * @return 分页故障案例集合
     */
    @Override
    public PageResult<FaultCaseVo> selectPage(FaultCaseBo faultCaseBo) {
        QueryWrapper queryWrapper = buildQueryWrapper(faultCaseBo);
        Page<FaultCaseVo> page = this.pageAs(PageQuery.build(), queryWrapper, FaultCaseVo.class);
        return PageResult.build(page);
    }

    /**
     * 新增故障案例
     *
     * @param faultCaseBo 故障案例Bo
     * @return 结果:true 操作成功，false 操作失败
     */
    @Override
    public boolean insert(FaultCaseBo faultCaseBo) {
        FaultCase faultCase = MapstructUtils.convert(faultCaseBo, FaultCase.class);

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

    /**
     * 新增故障案例，前台提供主键值，一般用于导入的场合
     *
     * @param faultCaseBo 故障案例Bo
     * @return 结果:true 操作成功，false 操作失败
     */
    @Override
    public boolean insertWithPk(FaultCaseBo faultCaseBo) {
        FaultCase faultCase = MapstructUtils.convert(faultCaseBo, FaultCase.class);


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

    /**
     * 修改故障案例
     *
     * @param faultCaseBo 故障案例Bo
     * @return 结果:true 更新成功，false 更新失败
     */
    @Override
    public boolean update(FaultCaseBo faultCaseBo) {
        FaultCase faultCase = MapstructUtils.convert(faultCaseBo, FaultCase.class);
        if (ObjectUtil.isNotNull(faultCase) && ObjectUtil.isNotNull(faultCase.getId())) {
            boolean updated = this.updateById(faultCase);
            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, FaultCaseImportVo.class, new FaultCaseImportListener(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<FaultCaseVo> listVo, String sheetName, LoginUser loginUser) {
        ExcelResultRes result = excelService.exportExcel(listVo, sheetName, FaultCaseVo.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));
    }

}
