package {0}.api;

import {0}.service.{1}Service;
import {0}.model.{1};
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.station.core.auth.CurrentUser;
import org.station.core.log.Operation;
import org.station.core.log.UserOperation;
import org.station.core.security.ActionAccessControl;
import org.station.core.tool.api.PageQuery;
import org.station.core.tool.api.PageResult;
import org.station.core.tool.api.R;

@Slf4j
@RestController
@RequestMapping("/api/xxx/xxx")
@RequiredArgsConstructor
public class {1}Controller {

  private final {1}Service {3}Service;

  @PostMapping("/page")
  @ActionAccessControl(value = fixme, name = fixme, module = fixme)
  public R<PageResult<{1}>> get{1}Page(@RequestBody PageQuery<{1}> pageQuery) {
    return R.DATA({3}Service.pageList(pageQuery));
  }

  @PostMapping("")
  @ActionAccessControl(value = fixme, name = fixme, module = fixme)
  public R<{1}> add{1}(
    @RequestBody {1} data,
    @Operation(operate = "新增了", target = "{2}") UserOperation operation
  ) {
    String userId = CurrentUser.getUserIdNonNull();
    Integer id = {3}Service.create(data, userId);
    operation.targetId(id + "");
    return R.OK("操作成功");
  }

  @PutMapping("")
  @ActionAccessControl(value = fixme, name = fixme, module = fixme)
  public R<Integer> update{1}(
    @RequestBody {1} data,
    @Operation(operate = "更新了", target = "{2}") UserOperation operation
  ) {
    String userId = CurrentUser.getUserIdNonNull();
    {3}Service.modify(data, userId);
    operation.targetId(data.getId() + "");
    return R.OK("操作成功");
  }

  @DeleteMapping("")
  @ActionAccessControl(value = fixme, name = fixme, module = fixme)
  public R<Integer> delete{1}(
    Integer id,
    @Operation(operate = "删除了", target = "{2}") UserOperation operation
  ) {
    String userId = CurrentUser.getUserIdNonNull();
    int ret = {3}Service.remove(id, userId, {1}.class);
    Assert.isTrue(ret == 1, "数据删除失败");
    operation.targetId(id + "");
    return R.OK("操作成功");
  }
}

