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.Task;
import org.eco.als.domain.bo.TaskBo;
import org.eco.als.domain.vo.TaskVo;
import org.eco.als.mapper.TaskMapper;
import org.eco.als.service.ITaskService;
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 java.util.Arrays;
import java.util.List;

import static org.eco.als.domain.table.TaskTableDef.TASK;

/**
 * 任务信息Service业务层处理
 *
 * @author wgk
 * @date 2024-08-07
 */
@Service
@Slf4j
public class TaskServiceImpl extends BaseServiceImpl<TaskMapper, Task> implements ITaskService {
    @Resource
    private TaskMapper taskMapper;

    @Resource
    private IExcelService excelService;

    @Resource
    private IImportExportService importExportService;

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

    private QueryWrapper buildQueryWrapper(TaskBo taskBo) {
        QueryWrapper queryWrapper = super.buildBaseQueryWrapper();
        queryWrapper.and(TASK.NAME.like
            (taskBo.getName()));
        queryWrapper.and(TASK.SOURCE.eq
            (taskBo.getSource()));
        queryWrapper.and(TASK.SORTIE_NO.eq
            (taskBo.getSortieNo()));

        return queryWrapper;
    }

    /**
     * 查询任务信息
     *
     * @param id 任务信息主键
     * @return 任务信息
     */
    @Override
    public TaskVo selectById(Long id) {
        return this.getOneAs(query().where(TASK.ID.eq(id)), TaskVo.class);

    }


    /**
     * 查询任务信息列表
     *
     * @param taskBo 任务信息Bo
     * @return 任务信息集合
     */
    @Override
    public List<TaskVo> selectList(TaskBo taskBo) {
        QueryWrapper queryWrapper = buildQueryWrapper(taskBo);
        return this.listAs(queryWrapper, TaskVo.class);
    }

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

    /**
     * 新增任务信息
     *
     * @param taskBo 任务信息Bo
     * @return 结果:true 操作成功，false 操作失败
     */
    @Override
    public boolean insert(TaskBo taskBo) {
        Task task = MapstructUtils.convert(taskBo, Task.class);

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

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


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

    /**
     * 修改任务信息
     *
     * @param taskBo 任务信息Bo
     * @return 结果:true 更新成功，false 更新失败
     */
    @Override
    public boolean update(TaskBo taskBo) {
        Task task = MapstructUtils.convert(taskBo, Task.class);
        if (ObjectUtil.isNotNull(task) && ObjectUtil.isNotNull(task.getId())) {
            return this.updateById(task);
        }
        return false;
    }

    @Override
    public void asyncExport(List<TaskVo> listVo, String sheetName, LoginUser loginUser) {
        ExcelResultRes result = excelService.exportExcel(listVo, sheetName, TaskVo.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));
    }

}
