package com.mosty.yjzl.remote;

import com.alibaba.fastjson.JSON;
import com.mosty.base.feign.service.MostyRwzxFeignService;
import com.mosty.base.model.dto.rwzx.TbRwTaskDto;
import com.mosty.base.model.entity.rwzx.TbRwTask;
import com.mosty.base.model.entity.yjzl.TbZlxx;
import com.mosty.base.model.query.rwzx.TbRwTaskByYwidQuery;
import com.mosty.common.base.domain.ResponseResult;
import com.mosty.common.base.exception.BusinessException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * @author dw
 * @since 2022/10/05
 * 外部调用任务中心接口
 **/
@Service
@Slf4j
public class TbRwzxAdaptRemoteService {

    @Resource
    private MostyRwzxFeignService mostyRwzxFeignService;

    /**
     * 发送任务给各个用户
     *
     * @return
     */
    public Integer addRw(TbRwTaskDto dto) {
        ResponseResult<Integer> responseResult = mostyRwzxFeignService.addRw(dto);
        if (responseResult == null || !responseResult.isSuccess()) {
            log.error("任务下发失败 responseResult = {}", JSON.toJSONString(responseResult));
            return 0;
        }
        return responseResult.getData();
    }

    /**
     * 发送任务给各个用户
     *
     * @return
     */
    public TbRwTask getRwByYwid(TbRwTaskByYwidQuery dto) {
        ResponseResult<TbRwTask> responseResult = mostyRwzxFeignService.getRwByYwid(dto);
        if (responseResult == null || !responseResult.isSuccess()) {
            log.error("任务下发失败 responseResult = {}", JSON.toJSONString(responseResult));
            throw new BusinessException("任务查询报错！！");
        }
        return responseResult.getData();
    }

    /**
     * 修改旧的指令任务状态为未读
     * @return
     */
    public void changeTaskStateByZlid(TbZlxx zlxx) {
        ResponseResult<Void> responseResult = mostyRwzxFeignService.changeTaskStateByZlid(zlxx);
        if (responseResult == null || !responseResult.isSuccess()) {
            log.error("修改任务状态失败 responseResult = {}", JSON.toJSONString(responseResult));
            throw new BusinessException("修改任务状态失败！！");
        }
    }
}
