package ${parentPackage}.${controllerPackage};

import org.springframework.web.bind.annotation.RequestBody;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.greenorange.promotion.model.dto.CommonRequest;
import jakarta.validation.Valid;


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

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

    @Resource
    private CommonService commonService;

    /**
     * web端管理员添加${entityComment}
     * @param ${entityNameLower}AddRequest ${entityComment}添加请求体
     * @return 是否添加成功
     */
    @PostMapping("add")
    @Operation(summary = "web端管理员添加${entityComment}", description = "参数：${entityComment}添加请求体，权限：管理员，方法名：add${entityName}")
    @RequiresPermission(mustRole = UserConstant.ADMIN_ROLE)
    @SysLog(title = "${entityComment}管理", content = "web端管理员添加${entityComment}")
    public BaseResponse<Long> add${entityName}(@Valid @RequestBody ${entityName}AddRequest ${entityNameLower}AddRequest) {
        ${entityName} ${entityNameLower} = commonService.copyProperties(${entityNameLower}AddRequest, ${entityName}.class);
        ${entityNameLower}Service.save(${entityNameLower});
        return ResultUtils.success(${entityNameLower}.getId());
    }

    /**
     * web端管理员根据id修改${entityComment}信息
     * @param ${entityNameLower}UpdateRequest ${entityComment}更新请求体
     * @return 是否更新成功
     */
    @PostMapping("update")
    @Operation(summary = "web端管理员根据id修改${entityComment}信息", description = "参数：${entityComment}更新请求体，权限：管理员，方法名：update${entityName}")
    @RequiresPermission(mustRole = UserConstant.ADMIN_ROLE)
    @SysLog(title = "${entityComment}管理", content = "web端管理员根据id修改${entityComment}信息")
    public BaseResponse<Boolean> update${entityName}(@Valid @RequestBody ${entityName}UpdateRequest ${entityNameLower}UpdateRequest) {
        ${entityName} ${entityNameLower} = commonService.copyProperties(${entityNameLower}UpdateRequest, ${entityName}.class);
        ${entityNameLower}Service.updateById(${entityNameLower});
        return ResultUtils.success(true);
    }

    /**
     * web端管理员根据id删除${entityComment}
     * @param commonRequest ${entityComment}删除请求体
     * @return 是否删除成功
     */
    @PostMapping("delete")
    @Operation(summary = "web端管理员根据id删除${entityComment}", description = "参数：${entityComment}删除请求体，权限：管理员，方法名：del${entityName}")
    @RequiresPermission(mustRole = UserConstant.ADMIN_ROLE)
    @SysLog(title = "${entityComment}管理", content = "web端管理员根据id删除${entityComment}")
    public BaseResponse<Boolean> del${entityName}(@Valid @RequestBody CommonRequest commonRequest) {
        Long id = commonRequest.getId();
        ${entityNameLower}Service.removeById(id);
        return ResultUtils.success(true);
    }

    /**
     * web端管理员批量删除${entityComment}
     * @param commonBatchRequest ${entityComment}批量删除请求体
     * @return 是否删除成功
     */
    @PostMapping("delBatch")
    @Operation(summary = "web端管理员批量删除${entityComment}", description = "参数：${entityComment}批量删除请求体，权限：管理员，方法名：delBatch${entityName}")
    @RequiresPermission(mustRole = UserConstant.ADMIN_ROLE)
    @SysLog(title = "${entityComment}管理", content = "web端管理员批量删除${entityComment}")
    public BaseResponse<Boolean> delBatch${entityName}(@Valid @RequestBody CommonBatchRequest commonBatchRequest) {
        List<Long> ids = commonBatchRequest.getIds();
        ${entityNameLower}Service.removeByIds(ids);
        return ResultUtils.success(true);
    }

    /**
     * web端管理员根据id查询${entityComment}
     * @param commonRequest ${entityComment}查询请求体
     * @return ${entityComment}信息
     */
    @PostMapping("queryById")
    @Operation(summary = "web端管理员根据id查询${entityComment}", description = "参数：${entityComment}查询请求体，权限：管理员，方法名：query${entityName}ById")
    @RequiresPermission(mustRole = UserConstant.ADMIN_ROLE)
    @SysLog(title = "${entityComment}管理", content = "web端管理员根据id查询${entityComment}")
    public BaseResponse<${entityName}VO> query${entityName}ById(@Valid @RequestBody CommonRequest commonRequest) {
        Long id = commonRequest.getId();
        ${entityName} ${entityNameLower} = ${entityNameLower}Service.getById(id);
        ${entityName}VO ${entityNameLower}VO = commonService.copyProperties(${entityNameLower}, ${entityName}VO.class);
        return ResultUtils.success(${entityNameLower}VO);
    }

    /**
     * Web端管理员分页查询${entityComment}
     * @param ${entityNameLower}QueryRequest ${entityComment}查询请求体
     * @return ${entityComment}列表
     */
    @PostMapping("page")
    @Operation(summary = "Web端管理员分页查询${entityComment}", description = "参数：${entityComment}查询请求体，权限：管理员，方法名：list${entityName}ByPage")
    @RequiresPermission(mustRole = UserConstant.ADMIN_ROLE)
    @SysLog(title = "${entityComment}管理", content = "Web端管理员分页查询${entityComment}")
    public BaseResponse<Page<${entityName}VO>> list${entityName}ByPage(@Valid @RequestBody ${entityName}QueryRequest ${entityNameLower}QueryRequest) {
        long current = ${entityNameLower}QueryRequest.getCurrent();
        long pageSize = ${entityNameLower}QueryRequest.getPageSize();
        QueryWrapper<${entityName}> queryWrapper = ${entityNameLower}Service.getQueryWrapper(${entityNameLower}QueryRequest);
        Page<${entityName}> page = ${entityNameLower}Service.page(new Page<>(current, pageSize), queryWrapper);
        List<${entityName}> ${entityNameLower}List = page.getRecords();
        List<${entityName}VO> ${entityNameLower}VOList = commonService.convertList(${entityNameLower}List, ${entityName}VO.class);
        Page<${entityName}VO> voPage = new Page<>(current, pageSize);
        voPage.setRecords(${entityNameLower}VOList);
        voPage.setPages(page.getPages());
        voPage.setTotal(page.getTotal());
        return ResultUtils.success(voPage);
    }
}