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.SmTask;
import org.eco.als.domain.bo.SmTaskBo;
import org.eco.als.domain.vo.SmTaskImportVo;
import org.eco.als.domain.vo.SmTaskVo;
import org.eco.als.listener.SmTaskImportListener;
import org.eco.als.mapper.SmTaskMapper;
import org.eco.als.service.ISmTaskService;
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.SmTaskTableDef.SM_TASK;

/**
 * 定检任务Service业务层处理
 *
 * @author wgk
 * @date 2024-07-25
 */
@Service
@Slf4j
public class SmTaskServiceImpl extends BaseServiceImpl<SmTaskMapper, SmTask> implements ISmTaskService {
    @Resource
    private SmTaskMapper smTaskMapper;

    @Resource
    private IExcelService excelService;

    @Resource
    private IImportExportService importExportService;

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

    private QueryWrapper buildQueryWrapper(SmTaskBo smTaskBo) {
        QueryWrapper queryWrapper = super.buildBaseQueryWrapper();
        queryWrapper.and(SM_TASK.AIRCRAFT_ID.eq
            (smTaskBo.getAircraftId()));
        queryWrapper.and(SM_TASK.AIRCRAFT_MODEL.eq
            (smTaskBo.getAircraftModel()));
        queryWrapper.and(SM_TASK.NAME.like
            (smTaskBo.getName()));
        queryWrapper.and(SM_TASK.CODE.eq
            (smTaskBo.getCode()));
        queryWrapper.and(SM_TASK.END_TIME.eq
            (smTaskBo.getEndTime()));
        queryWrapper.and(SM_TASK.REMAIN_TIME.eq
            (smTaskBo.getRemainTime()));
        queryWrapper.and(SM_TASK.SPECIALTY.eq
            (smTaskBo.getSpecialty()));
        queryWrapper.and(SM_TASK.ISSUE_DATE.eq
            (smTaskBo.getIssueDate()));
        queryWrapper.and(SM_TASK.FINISH_DATE.eq
            (smTaskBo.getFinishDate()));
        queryWrapper.and(SM_TASK.HEAD_NAME.like
            (smTaskBo.getHeadName()));
        queryWrapper.and(SM_TASK.UNIT_NAME.like
            (smTaskBo.getUnitName()));

        return queryWrapper;
    }

    /**
     * 查询定检任务
     *
     * @param id 定检任务主键
     * @return 定检任务
     */
    @Override
    public SmTaskVo selectById(Long id) {
        return this.getOneAs(query().where(SM_TASK.ID.eq(id)), SmTaskVo.class);

    }


    /**
     * 查询定检任务列表
     *
     * @param smTaskBo 定检任务Bo
     * @return 定检任务集合
     */
    @Override
    public List<SmTaskVo> selectList(SmTaskBo smTaskBo) {
        QueryWrapper queryWrapper = buildQueryWrapper(smTaskBo);
        return this.listAs(queryWrapper, SmTaskVo.class);
    }

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

    /**
     * 新增定检任务
     *
     * @param smTaskBo 定检任务Bo
     * @return 结果:true 操作成功，false 操作失败
     */
    @Override
    public boolean insert(SmTaskBo smTaskBo) {
        SmTask smTask = MapstructUtils.convert(smTaskBo, SmTask.class);

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

    /**
     * 新增定检任务，前台提供主键值，一般用于导入的场合
     *
     * @param smTaskBo 定检任务Bo
     * @return 结果:true 操作成功，false 操作失败
     */
    @Override
    public boolean insertWithPk(SmTaskBo smTaskBo) {
        SmTask smTask = MapstructUtils.convert(smTaskBo, SmTask.class);


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

    /**
     * 修改定检任务
     *
     * @param smTaskBo 定检任务Bo
     * @return 结果:true 更新成功，false 更新失败
     */
    @Override
    public boolean update(SmTaskBo smTaskBo) {
        SmTask smTask = MapstructUtils.convert(smTaskBo, SmTask.class);
        if (ObjectUtil.isNotNull(smTask) && ObjectUtil.isNotNull(smTask.getId())) {
            return this.updateById(smTask);
        }
        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, SmTaskImportVo.class, new SmTaskImportListener(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<SmTaskVo> listVo, String sheetName, LoginUser loginUser) {
        ExcelResultRes result = excelService.exportExcel(listVo, sheetName, SmTaskVo.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));
    }

}
