package com.qmth.sop.business.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qmth.boot.api.exception.ApiException;
import com.qmth.sop.business.bean.result.TaskListResult;
import com.qmth.sop.business.entity.BasicAttachment;
import com.qmth.sop.business.entity.SysUser;
import com.qmth.sop.business.entity.TBTask;
import com.qmth.sop.business.mapper.TBTaskMapper;
import com.qmth.sop.business.service.BasicAttachmentService;
import com.qmth.sop.business.service.TBTaskService;
import com.qmth.sop.common.contant.SystemConstant;
import com.qmth.sop.common.enums.TaskResultEnum;
import com.qmth.sop.common.enums.TaskStatusEnum;
import com.qmth.sop.common.enums.TaskTypeEnum;
import com.qmth.sop.common.enums.UploadFileEnum;
import com.qmth.sop.common.util.ResultUtil;
import com.qmth.sop.common.util.ServletUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

/**
 * <p>
 * 导入导出任务表 服务实现类
 * </p>
 *
 * @author wangliang
 * @since 2023-07-17
 */
@Service
public class TBTaskServiceImpl extends ServiceImpl<TBTaskMapper, TBTask> implements TBTaskService {

    @Resource
    BasicAttachmentService basicAttachmentService;

    /**
     * 保存任务
     *
     * @param file
     * @param taskTypeEnum
     * @return
     */
    @Override
    @Transactional
    public Map<String, Object> saveTask(MultipartFile file, TaskTypeEnum taskTypeEnum) throws Exception {
        BasicAttachment basicAttachment = null;
        Map<String, Object> map = new HashMap<>();
        try {
            basicAttachment = basicAttachmentService.saveAttachment(file, UploadFileEnum.FILE, null);
            SysUser sysUser = (SysUser) ServletUtil.getRequestUser();
            TBTask tbTask = new TBTask(taskTypeEnum,
                    TaskStatusEnum.INIT,
                    basicAttachment,
                    Objects.nonNull(sysUser) ? sysUser.getId() : null);
            this.save(tbTask);
            map.computeIfAbsent(SystemConstant.USER, v -> sysUser);
            map.computeIfAbsent(SystemConstant.TASK, v -> tbTask);
        } catch (Exception e) {
            log.error(SystemConstant.LOG_ERROR, e);
            basicAttachmentService.deleteAttachment(basicAttachment);
            if (e instanceof ApiException) {
                ResultUtil.error((ApiException) e, ((ApiException) e).getCode(), e.getMessage());
            } else {
                ResultUtil.error(e.getMessage());
            }
        }
        return map;
    }

    /**
     * 保存任务
     *
     * @param taskTypeEnum
     * @return
     * @throws Exception
     */
    @Override
    @Transactional
    public Map<String, Object> saveTask(TaskTypeEnum taskTypeEnum) throws Exception {
        Map<String, Object> map = new HashMap<>();
        try {
            SysUser sysUser = (SysUser) ServletUtil.getRequestUser();
            TBTask tbTask = new TBTask(taskTypeEnum,
                    TaskStatusEnum.INIT,
                    Objects.nonNull(sysUser) ? sysUser.getId() : null);
            this.save(tbTask);
            map.computeIfAbsent(SystemConstant.USER, v -> sysUser);
            map.computeIfAbsent(SystemConstant.TASK, v -> tbTask);
        } catch (Exception e) {
            log.error(SystemConstant.LOG_ERROR, e);
            if (e instanceof ApiException) {
                ResultUtil.error((ApiException) e, ((ApiException) e).getCode(), e.getMessage());
            } else {
                ResultUtil.error(e.getMessage());
            }
        }
        return map;
    }

    /**
     * 查询任务列表
     *
     * @param iPage
     * @param status
     * @param type
     * @param result
     * @return
     */
    @Override
    public IPage<TaskListResult> query(IPage<Map> iPage, TaskStatusEnum status, TaskTypeEnum type, TaskResultEnum result) {
        return this.baseMapper.query(iPage, Objects.nonNull(status) ? status.name() : null, Objects.nonNull(type) ? type.name() : null, Objects.nonNull(result) ? result.name() : null);
    }
}
