package ${parentPackage}.${controllerPackage};

import java.util.List;

/**
 * ${entityComment} 控制器
 */
@RestController
@RequestMapping("${entityName}")
@Slf4j
@Tag(name = "${entityComment}管理")
public class ${entityName}Controller {

    @Resource
    private ${entityName}Service ${entityName}Service;

    @Resource
    private CommonService commonService;

    /**
     * web端管理员添加${entityComment}
     * @param ${entityName}AddRequest ${entityComment}添加请求体
     * @return 是否添加成功
     */
    @PostMapping("add")
    @Operation(summary = "web端管理员添加${entityComment}", description = "参数：${entityComment}添加请求体，权限：管理员（boss, admin)，方法名：add${entityName}")
    @AuthCheck(mustRole = UserConstant.ADMIN_ROLE)
    public BaseResponse<Boolean> add${entityName}(@RequestBody ${entityName}AddRequest ${entityName}AddRequest) {
        if (${entityName}AddRequest == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        ${entityName} ${entityName} = new ${entityName}();
        BeanUtils.copyProperties(${entityName}AddRequest, ${entityName});
        boolean result = ${entityName}Service.save(${entityName});
        ThrowUtils.throwIf(!result, ErrorCode.OPERATION_ERROR, "${entityComment}添加失败");
        return ResultUtils.success(true);
    }

    /**
     * web端管理员更新${entityComment}
     * @param ${entityName}UpdateRequest ${entityComment}更新请求体
     * @return 是否更新成功
     */
    @PostMapping("update")
    @Operation(summary = "web端管理员更新${entityComment}", description = "参数：${entityComment}更新请求体，权限：管理员（boss, admin)，方法名：update${entityName}")
    @AuthCheck(mustRole = UserConstant.ADMIN_ROLE)
    public BaseResponse<Boolean> update${entityName}(@RequestBody ${entityName}UpdateRequest ${entityName}UpdateRequest) {
        if (${entityName}UpdateRequest == null || ${entityName}UpdateRequest.getId() <= 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        ${entityName} ${entityName} = new ${entityName}();
        BeanUtils.copyProperties(${entityName}UpdateRequest, ${entityName});
        boolean result = ${entityName}Service.updateById(${entityName});
        ThrowUtils.throwIf(!result, ErrorCode.OPERATION_ERROR, "${entityComment}更新失败");
        return ResultUtils.success(true);
    }

    /**
     * web端管理员删除${entityComment}
     * @param commonRequest ${entityComment}删除请求体
     * @return 是否删除成功
     */
    @PostMapping("delete")
    @Operation(summary = "web端管理员删除${entityComment}", description = "参数：${entityComment}删除请求体，权限：管理员（boss, admin)，方法名：del${entityName}")
    @AuthCheck(mustRole = UserConstant.ADMIN_ROLE)
    public BaseResponse<Boolean> del${entityName}(@RequestBody CommonRequest commonRequest) {
        if (commonRequest == null || commonRequest.getId() <= 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        Long id = commonRequest.getId();
        boolean result = ${entityName}Service.removeById(id);
        ThrowUtils.throwIf(!result, ErrorCode.OPERATION_ERROR, "${entityComment}删除失败");
        return ResultUtils.success(true);
    }

    /**
     * Web端管理员分页查看${entityComment}
     * @param ${entityName}QueryRequest ${entityComment}查询请求体
     * @return ${entityComment}列表
     */
    @PostMapping("page")
    @Operation(summary = "Web端管理员分页查看${entityComment}", description = "参数：${entityComment}查询请求体，权限：管理员（boss, admin)，方法名:list${entityName}ByPage")
    public BaseResponse<Page<${entityName}VO>> list${entityName}ByPage(@RequestBody ${entityName}QueryRequest ${entityName}QueryRequest) {
        if (${entityName}QueryRequest == null) throw new BusinessException(ErrorCode.PARAMS_ERROR);
        long current = ${entityName}QueryRequest.getCurrent();
        long pageSize = ${entityName}QueryRequest.getPageSize();
        QueryWrapper<${entityName}> queryWrapper = ${entityName}Service.getQueryWrapper(${entityName}QueryRequest);
        Page<${entityName}> page = ${entityName}Service.page(new Page<>(current, pageSize), queryWrapper);
        List<${entityName}> ${entityName}List = page.getRecords();
        List<${entityName}VO> ${entityName}VOList = commonService.convertList(${entityName}List, ${entityName}VO.class);
        Page<${entityName}VO> voPage = new Page<>();
        voPage.setRecords(${entityName}VOList);
        voPage.setPages(page.getPages());
        voPage.setCurrent(page.getCurrent());
        voPage.setTotal(page.getTotal());
        voPage.setSize(page.getSize());
        return ResultUtils.success(voPage);
    }

    /**
     * web端管理员根据id查询${entityComment}
     * @param commonRequest ${entityComment}查询请求体
     * @return ${entityComment}信息
     */
    @PostMapping("queryById")
    @Operation(summary = "web端管理员根据id查询${entityComment}", description = "参数：${entityComment}查询请求体，权限：管理员（boss, admin)，方法名:query${entityName}ById")
    @AuthCheck(mustRole = UserConstant.ADMIN_ROLE)
    public BaseResponse<${entityName}VO> query${entityName}ById(@RequestBody CommonRequest commonRequest) {
        if (commonRequest == null || commonRequest.getId() <= 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        ${entityName} ${entityName} = ${entityName}Service.getById(commonRequest.getId());
        ThrowUtils.throwIf(${entityName} == null, ErrorCode.NOT_FOUND, "${entityComment}未找到");
            ${entityName}VO ${entityName}VO = commonService.convert(${entityName}, ${entityName}VO.class);
        return ResultUtils.success(${entityName}VO);
    }

    /**
     * web端管理员批量删除${entityComment}
     * @param commonRequest ${entityComment}批量删除请求体
     * @return 是否删除成功
     */
    @PostMapping("delBatch")
    @Operation(summary = "web端管理员批量删除${entityComment}", description = "参数：${entityComment}批量删除请求体，权限：管理员（boss, admin)，方法名:delBatch${entityName}")
    @AuthCheck(mustRole = UserConstant.ADMIN_ROLE)
    public BaseResponse<Boolean> delBatch${entityName}(@RequestBody CommonRequest commonRequest) {
        if (commonRequest == null || commonRequest.getIds() == null || commonRequest.getIds().isEmpty()) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        boolean result = ${entityName}Service.removeByIds(commonRequest.getIds());
        ThrowUtils.throwIf(!result, ErrorCode.OPERATION_ERROR, "${entityComment}批量删除失败");
        return ResultUtils.success(true);
    }
}
