package com.ruoyi.web.work.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.bean.BeanUtils;
import com.ruoyi.web.work.api.util.AppUtil;
import com.ruoyi.web.work.domain.ResumeDeliver;
import com.ruoyi.web.work.domain.TaskList;
import com.ruoyi.web.work.domain.dto.TaskListDto;
import com.ruoyi.web.work.mapper.TaskListMapper;
import com.ruoyi.web.work.service.IResumeDeliverService;
import com.ruoyi.web.work.service.ITaskListService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author lsw
 * @date 2024-07-03
 */
@Service
public class TaskListServiceImpl extends ServiceImpl<TaskListMapper, TaskList> implements ITaskListService {
    @Autowired
    private TaskListMapper taskListMapper;

    @Autowired
    private IResumeDeliverService resumeDeliverService;

    @Override
    public List<TaskList> selectList(TaskList taskList) {
        return taskListMapper.selectList(taskList);
    }

    @Override
    public AjaxResult add(TaskListDto dto) {
        ResumeDeliver resumeDeliver = resumeDeliverService.getById(dto.getTaskId());
        if (resumeDeliver == null || !resumeDeliver.getUserId().equals(AppUtil.getUser().getId())) {
            return AjaxResult.error("兼职任务不存在或非法操作");
        }
        TaskList taskList = new TaskList();
        BeanUtils.copyProperties(dto, taskList);
        taskList.setUserId(resumeDeliver.getUserId());
        taskList.setEnterpriseId(resumeDeliver.getEnterpriseId());
        if (!save(taskList)) {
            throw new ServiceException("保存兼职任务进度失败,请联系平台");
        }
        return AjaxResult.success();
    }

    @Override
    public AjaxResult remove(Long id) {
        TaskList taskList = getById(id);
        if (taskList == null || !taskList.getUserId().equals(AppUtil.getUser().getId())) {
            return AjaxResult.error("任务进度不存在或非法操作");
        }
        if (!removeById(id)) {
            throw new ServiceException("删除任务进度失败,请联系平台");
        }
        return AjaxResult.success();
    }
}
