package ${package}.api;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.core.domain.Page;
import com.ruoyi.common.core.validate.AddGroup;
import com.ruoyi.common.core.validate.EditGroup;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.JsonUtils;
import ${package}.mongo.${entity.toUpperName()};
import ${package}.mongo.query.${entity.toUpperName()}Query;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;

import static com.ruoyi.common.utils.MongoUtil.*;

/**
 * ${entity.name}
${entity.toComment(0)}
 */
@Slf4j
@Validated
@RestController
@RequiredArgsConstructor
@RequestMapping("/${moduleName}/${entity.toSimpleLowerName()}/")
public class ${entity.toUpperName()}Api {

  @GetMapping("/{id}")
  @SaCheckPermission("${moduleName}:${entity.toSimpleLowerName()}:query")
  public ${entity.toUpperName()} id(@NotNull(message = "编号不能为空") @PathVariable Long id) {
    return findById(${entity.toUpperName()}.class, id);
  }

  @SaCheckPermission("${moduleName}:${entity.toSimpleLowerName()}:page")
  @GetMapping("/page")
  public Page<${entity.toUpperName()}> page(Page<${entity.toUpperName()}> page, ${entity.toUpperName()} ${entity.toLowerName()}, ${entity.toUpperName()}Query query) {
    return findPage(${entity.toUpperName()}.class, page, criteria(${entity.toLowerName()}), query.where());
  }


  @SaCheckPermission("${moduleName}:${entity.toSimpleLowerName()}:add")
  @Log(title = "${entity.name}", businessType = BusinessType.INSERT)
  @RepeatSubmit
  @PostMapping
  public void add(@Validated(AddGroup.class) @RequestBody ${entity.toUpperName()} ${entity.toLowerName()}) {
    if (log.isTraceEnabled()) {
      log.trace("${entity.name}添加参数:{}", JsonUtils.toJsonString(${entity.toLowerName()}));
    }
    fillId(${entity.toLowerName()});
    #foreach($item in $entity.cols)
      #if($item.add)
        #if($item.addDefault)
    ${entity.toLowerName()}.set${item.toUpperName()}(${item.addDefault});
        #end
      #else
    ${entity.toLowerName()}.set${item.toUpperName()}(null); 
      #end
    #end
    doInsert(${entity.toLowerName()});
  }


  @SaCheckPermission("${moduleName}:${entity.toSimpleLowerName()}:update")
  @Log(title = "${entity.name}", businessType = BusinessType.UPDATE)
  @RepeatSubmit
  @PutMapping
  public void update(@Validated(EditGroup.class) @RequestBody ${entity.toUpperName()} ${entity.toLowerName()}) {
    if (log.isTraceEnabled()) {
      log.trace("${entity.name}修改参数:{}", JsonUtils.toJsonString(${entity.toLowerName()}));
    }

    #foreach($item in $entity.cols)
      #if($item.update)
        #if($item.updateDefault)
    ${entity.toLowerName()}.set${item.toUpperName()}(${item.updateDefault});
        #end
      #else
    ${entity.toLowerName()}.set${item.toUpperName()}(null); 
      #end
    #end
    doUpdate(${entity.toLowerName()});
  }


  @SaCheckPermission("${moduleName}:${entity.toSimpleLowerName()}:delete")
  @Log(title = "${entity.name}", businessType = BusinessType.DELETE)
  @DeleteMapping("/{ids}")
  public void remove(@NotEmpty(message = "编号不能为空") @PathVariable Long[] ids) {
    doRemove(${entity.toUpperName()}.class, conditions().put("_id$in", ids).query());
  }


}
