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

import com.red.circle.console.app.service.app.sys.pay.SysPayApplicationCommodityService;
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.order.inner.model.cmd.SysPayApplicationCommodityCmd;
import com.red.circle.order.inner.model.cmd.SysPayApplicationCommodityQryCmd;
import com.red.circle.order.inner.model.dto.SysPayApplicationCommodityDTO;
import java.util.Objects;
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.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * 支付商品管理 前端控制器.
 *
 * @author pengliang
 * @since 2021-11-29
 */
@RequiredArgsConstructor
@RestController
@RequestMapping(value = "/sys-pay-commodity", produces = MediaType.APPLICATION_JSON_VALUE)
public class SysPayCommodityController extends BaseController {

  private final SysPayApplicationCommodityService sysPayApplicationCommodityService;

  /**
   * 商品-分页列表.
   */
  @GetMapping("/page")
  public PageResult<SysPayApplicationCommodityDTO> page(
      SysPayApplicationCommodityQryCmd query) {
    return sysPayApplicationCommodityService.pageCommodity(query);
  }

  /**
   * 商品-添加.
   */
  @OpsOperationReqLog("商品-添加")
  @PostMapping("/add-or-update")
  public void addOrUpdate(
      @RequestBody @Validated SysPayApplicationCommodityCmd param) {
    if (Objects.isNull(param.getId())) {
      param.setCreateUser(param.getReqUserId());
    }
    param.setUpdateUser(param.getReqUserId());
    sysPayApplicationCommodityService.addOrUpdateCommodity(param);
  }

  /**
   * 修改上下架状态.
   */
  @OpsOperationReqLog("商品-修改上下架状态")
  @PostMapping("/switch-shelf")
  public void switchShelf(@RequestParam Long id, @RequestParam Boolean shelf) {
    sysPayApplicationCommodityService.switchShelf(id, shelf);
  }

}
