package com.chushang.task.service;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.IService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.chushang.common.mq.produce.MqProduceService;
import com.chushang.common.mybatis.page.CommonParam;
import com.chushang.common.mybatis.utils.PageResult;
import com.chushang.common.mybatis.utils.WrapperUtils;
import com.chushang.datascope.annotation.DataScope;
import com.chushang.datascope.constants.ScopeConstants;
import com.chushang.task.entity.TaskInfo;
import com.chushang.task.enums.TaskStatusEnum;
import com.chushang.task.mapper.TaskInfoMapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * @auther: zhao
 * @date: 2024/6/7 18:19
 */
@Slf4j
@Service
public class TaskInfoService extends ServiceImpl<TaskInfoMapper, TaskInfo> implements IService<TaskInfo> {

    @Resource
    MqProduceService<TaskInfo> mqProduceService;

    @DataScope
    public PageResult pagePostList(TaskInfo sysPost, CommonParam commonParam){
        LambdaQueryWrapper<TaskInfo> sql = WrapperUtils.builder(sysPost, commonParam);
        IPage<TaskInfo> page = this.page(
                new com.baomidou.mybatisplus.extension.plugins.pagination.Page<>(commonParam.getPage(), commonParam.getLimit()),
                sql
        );
        return new PageResult(page);
    }

    public void joinQueue(TaskInfo taskInfo){
        mqProduceService.send(taskInfo.getApplicationName(), taskInfo);

        taskInfo.setTaskStatus(TaskStatusEnum.DISPATCH.getCode());
        updateById(taskInfo);
    }

}
