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.CarryChecklist;
import org.eco.als.domain.bo.CarryChecklistBo;
import org.eco.als.domain.vo.CarryChecklistImportVo;
import org.eco.als.domain.vo.CarryChecklistVo;
import org.eco.als.listener.CarryChecklistImportListener;
import org.eco.als.mapper.CarryChecklistMapper;
import org.eco.als.service.ICarryChecklistService;
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.CarryChecklistTableDef.CARRY_CHECKLIST;

/**
 * 任务携行Service业务层处理
 *
 * @author wgk
 * @date 2024-07-25
 */
@Service
@Slf4j
public class CarryChecklistServiceImpl extends BaseServiceImpl<CarryChecklistMapper, CarryChecklist> implements ICarryChecklistService {
    @Resource
    private CarryChecklistMapper carryChecklistMapper;

    @Resource
    private IExcelService excelService;

    @Resource
    private IImportExportService importExportService;

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

    private QueryWrapper buildQueryWrapper(CarryChecklistBo carryChecklistBo) {
        QueryWrapper queryWrapper = super.buildBaseQueryWrapper();
        queryWrapper.and(CARRY_CHECKLIST.AIRCRAFT_MODEL.eq
            (carryChecklistBo.getAircraftModel()));
        queryWrapper.and(CARRY_CHECKLIST.AIRCRAFT_ID.eq
            (carryChecklistBo.getAircraftId()));
        queryWrapper.and(CARRY_CHECKLIST.NAME.like
            (carryChecklistBo.getName()));
        queryWrapper.and(CARRY_CHECKLIST.MODEL.eq
            (carryChecklistBo.getModel()));
        queryWrapper.and(CARRY_CHECKLIST.FACTORY.eq
            (carryChecklistBo.getFactory()));
        queryWrapper.and(CARRY_CHECKLIST.QUANTITY.eq
            (carryChecklistBo.getQuantity()));

        return queryWrapper;
    }

    /**
     * 查询任务携行
     *
     * @param id 任务携行主键
     * @return 任务携行
     */
    @Override
    public CarryChecklistVo selectById(Long id) {
        return this.getOneAs(query().where(CARRY_CHECKLIST.ID.eq(id)), CarryChecklistVo.class);

    }


    /**
     * 查询任务携行列表
     *
     * @param carryChecklistBo 任务携行Bo
     * @return 任务携行集合
     */
    @Override
    public List<CarryChecklistVo> selectList(CarryChecklistBo carryChecklistBo) {
        QueryWrapper queryWrapper = buildQueryWrapper(carryChecklistBo);
        return this.listAs(queryWrapper, CarryChecklistVo.class);
    }

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

    /**
     * 新增任务携行
     *
     * @param carryChecklistBo 任务携行Bo
     * @return 结果:true 操作成功，false 操作失败
     */
    @Override
    public boolean insert(CarryChecklistBo carryChecklistBo) {
        CarryChecklist carryChecklist = MapstructUtils.convert(carryChecklistBo, CarryChecklist.class);
        return this.save(carryChecklist);//使用全局配置的雪花算法主键生成器生成ID值
    }

    /**
     * 新增任务携行，前台提供主键值，一般用于导入的场合
     *
     * @param carryChecklistBo 任务携行Bo
     * @return 结果:true 操作成功，false 操作失败
     */
    @Override
    public boolean insertWithPk(CarryChecklistBo carryChecklistBo) {
        CarryChecklist carryChecklist = MapstructUtils.convert(carryChecklistBo, CarryChecklist.class);
        return carryChecklistMapper.insertWithPk(carryChecklist) > 0;//前台传来主键值
    }

    /**
     * 修改任务携行
     *
     * @param carryChecklistBo 任务携行Bo
     * @return 结果:true 更新成功，false 更新失败
     */
    @Override
    public boolean update(CarryChecklistBo carryChecklistBo) {
        CarryChecklist carryChecklist = MapstructUtils.convert(carryChecklistBo, CarryChecklist.class);
        if (ObjectUtil.isNotNull(carryChecklist) && ObjectUtil.isNotNull(carryChecklist.getId())) {
            return this.updateById(carryChecklist);
        }
        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, CarryChecklistImportVo.class, new CarryChecklistImportListener(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<CarryChecklistVo> listVo, String sheetName, LoginUser loginUser) {
        ExcelResultRes result = excelService.exportExcel(listVo, sheetName, CarryChecklistVo.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));
    }

}
