package com.ycwl.basic.service.pc.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ycwl.basic.mapper.TaskMapper;
import com.ycwl.basic.model.pc.task.entity.TaskEntity;
import com.ycwl.basic.model.pc.task.req.TaskReqQuery;
import com.ycwl.basic.model.pc.task.resp.TaskRespVO;
import com.ycwl.basic.repository.VideoTaskRepository;
import com.ycwl.basic.service.pc.TaskService;
import com.ycwl.basic.utils.ApiResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Author：longbinbin
 * @Date：2024/12/3 16:04
 */
@Service
public class TaskServiceImpl implements TaskService {

    @Autowired
    private TaskMapper taskMapper;
    @Autowired
    private VideoTaskRepository videoTaskRepository;

    @Override
    public ApiResponse<PageInfo<TaskRespVO>> pageQuery(TaskReqQuery taskReqQuery) {
        PageHelper.startPage(taskReqQuery.getPageNum(), taskReqQuery.getPageSize());
        List<TaskRespVO> list = taskMapper.list(taskReqQuery);
        PageInfo<TaskRespVO> pageInfo = new PageInfo<>(list);
        return ApiResponse.success(pageInfo);
    }

    @Override
    public ApiResponse<List<TaskRespVO>> list(TaskReqQuery taskReqQuery) {
        return ApiResponse.success(taskMapper.list(taskReqQuery));
    }

    @Override
    public ApiResponse<TaskRespVO> getById(Long id) {
        return ApiResponse.success(taskMapper.getById(id));
    }

    @Override
    public ApiResponse<Boolean> deleteById(Long id) {
        int i = taskMapper.deleteById(id);
        videoTaskRepository.clearTaskCache(id);
        if (i > 0) {
            return ApiResponse.success(true);
        }else {
            return ApiResponse.fail("删除失败");
        }
    }

    @Override
    public ApiResponse<Boolean> update(TaskEntity task) {
        int i = taskMapper.update(task);
        videoTaskRepository.clearTaskCache(task.getId());
        if (i > 0) {
            return ApiResponse.success(true);
        }else {
            return ApiResponse.fail("修改失败");
        }
    }

    @Override
    public ApiResponse<Boolean> updateStatus(Long id, Integer status) {
        int i = taskMapper.updateStatus(id, status);
        videoTaskRepository.clearTaskCache(id);
        if (i > 0) {
            return ApiResponse.success(true);
        }else {
            return ApiResponse.fail("修改状态失败");
        }
    }
}
