package com.red.circle.console.adapter.app.sys;

import com.red.circle.console.app.dto.clienobject.sys.SysPushRecordLogCO;
import com.red.circle.console.app.dto.clienobject.sys.SysPushTaskCO;
import com.red.circle.console.app.service.app.sys.SysPushService;
import com.red.circle.console.infra.annotations.OpsOperationReqLog;
import com.red.circle.framework.dto.PageResult;
import com.red.circle.framework.web.controller.BaseController;
import com.red.circle.other.inner.model.cmd.sys.CreateRecordCmd;
import com.red.circle.other.inner.model.cmd.sys.SysPushRecordLogQryCmd;
import com.red.circle.other.inner.model.cmd.sys.SysPushTaskCmd;
import com.red.circle.other.inner.model.cmd.sys.SysPushTaskQryCmd;
import lombok.RequiredArgsConstructor;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 后台推送 前端控制器
 *
 * @author pengliang
 * @since 2020-02-07
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping(value = "/push", produces = MediaType.APPLICATION_JSON_VALUE)
public class PushRestController extends BaseController {

  private final SysPushService sysPushService;

  /**
   * 后台推送分页列表.
   */
  @GetMapping("/page")
  public PageResult<SysPushRecordLogCO> pageSysPushBack(SysPushRecordLogQryCmd query) {
    return sysPushService.pageLogs(query);
  }

  @OpsOperationReqLog("发送推送信息")
  @PostMapping
  public void saveAndPush(@RequestBody CreateRecordCmd param) {
    param.setCreateUser(param.getReqUserId());
    sysPushService.create(param);
  }

  @OpsOperationReqLog("保存定时消息推送任务")
  @PostMapping("/task/save")
  public void saveTask(@RequestBody @Validated SysPushTaskCmd param) {
    param.setCreateUser(param.getReqUserId());
    sysPushService.saveTask(param);
  }

  /**
   * 定时推送任务列表.
   */
  @GetMapping("/task/page")
  public PageResult<SysPushTaskCO> pageTask(SysPushTaskQryCmd query) {
    return sysPushService.pageTask(query);
  }

  /**
   * 删除定时推送任务.
   */
  @GetMapping("/task/delete/{id}")
  public void deletePushTask(@PathVariable("id") Long id) {
    sysPushService.deletePushTask(id);
  }

}
