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.SmTaskSub;
import org.eco.als.domain.bo.SmTaskSubBo;
import org.eco.als.domain.vo.SmTaskSubImportVo;
import org.eco.als.domain.vo.SmTaskSubVo;
import org.eco.als.listener.SmTaskSubImportListener;
import org.eco.als.mapper.SmTaskSubMapper;
import org.eco.als.service.ISmTaskSubService;
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.SmTaskSubTableDef.SM_TASK_SUB;

/**
 * 定检任务子Service业务层处理
 *
 * @author wgk
 * @date 2024-07-25
 */
@Service
@Slf4j
public class SmTaskSubServiceImpl extends BaseServiceImpl<SmTaskSubMapper, SmTaskSub> implements ISmTaskSubService {
    @Resource
    private SmTaskSubMapper smTaskSubMapper;

    @Resource
    private IExcelService excelService;

    @Resource
    private IImportExportService importExportService;

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

    private QueryWrapper buildQueryWrapper(SmTaskSubBo smTaskSubBo) {
        QueryWrapper queryWrapper = super.buildBaseQueryWrapper();
        queryWrapper.and(SM_TASK_SUB.AIRCRAFT_ID.eq
            (smTaskSubBo.getAircraftId()));
        queryWrapper.and(SM_TASK_SUB.AIRCRAFT_MODEL.eq
            (smTaskSubBo.getAircraftModel()));
        queryWrapper.and(SM_TASK_SUB.NAME.like
            (smTaskSubBo.getName()));
        queryWrapper.and(SM_TASK_SUB.CODE.eq
            (smTaskSubBo.getCode()));
        queryWrapper.and(SM_TASK_SUB.END_TIME.eq
            (smTaskSubBo.getEndTime()));
        queryWrapper.and(SM_TASK_SUB.REMAIN_TIME.eq
            (smTaskSubBo.getRemainTime()));
        queryWrapper.and(SM_TASK_SUB.SPECIALTY.eq
            (smTaskSubBo.getSpecialty()));
        queryWrapper.and(SM_TASK_SUB.ISSUE_DATE.eq
            (smTaskSubBo.getIssueDate()));
        queryWrapper.and(SM_TASK_SUB.FINISH_DATE.eq
            (smTaskSubBo.getFinishDate()));
        queryWrapper.and(SM_TASK_SUB.STAFF.eq
            (smTaskSubBo.getStaff()));
        queryWrapper.and(SM_TASK_SUB.REVIEW_BY.eq
            (smTaskSubBo.getReviewBy()));
        queryWrapper.and(SM_TASK_SUB.UNIT_NAME.like
            (smTaskSubBo.getUnitName()));
        queryWrapper.and(SM_TASK_SUB.CONTENTS.eq
            (smTaskSubBo.getContents()));

        return queryWrapper;
    }

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

    }


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

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

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

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

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


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

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

}
