package cn.besteam.modules.system.controller; import cn.besteam.common.api.vo.Result; import cn.besteam.common.aspect.annotation.AutoLog; import cn.besteam.common.util.oConvertUtils; import cn.besteam.modules.system.entity.SysDictItem; import cn.besteam.modules.system.service.ISysDictItemService; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.baomidou.mybatisplus.core.metadata.IPage; import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.*; import javax.servlet.http.HttpServletRequest; import java.util.Arrays; /** * @Title: Controller * @Description: 字典值 * @author: besteam * @date: 2020-04-17 * @version: V1.0 */ @RestController @RequestMapping("/system/sysDictItem") @Slf4j @Api(description = "字典值") public class SysDictItemController { @Autowired private ISysDictItemService sysDictItemService; /** * 分页列表查询 * * @param sysDictItem * @param pageNo * @param pageSize * @param req * @return */ @GetMapping(value = "/list") @ApiOperation(value = "获取字典值数据列表", notes = "获取所有字典值数据列表", produces = "application/json") public Result> queryPageList(SysDictItem sysDictItem, @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo, @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize, HttpServletRequest req) { Result> result = new Result>(); QueryWrapper queryWrapper = new QueryWrapper(sysDictItem); Page page = new Page(pageNo, pageSize); //排序逻辑 处理 String column = req.getParameter("column"); String order = req.getParameter("order"); if (oConvertUtils.isNotEmpty(column) && oConvertUtils.isNotEmpty(order)) { if ("asc".equals(order)) { queryWrapper.orderByAsc(oConvertUtils.camelToUnderline(column)); } else { queryWrapper.orderByDesc(oConvertUtils.camelToUnderline(column)); } } IPage pageList = sysDictItemService.page(page, queryWrapper); //log.debug("查询当前页:"+pageList.getCurrent()); //log.debug("查询当前页数量:"+pageList.getSize()); //log.debug("查询结果数量:"+pageList.getRecords().size()); //log.debug("数据总数:"+pageList.getTotal()); result.setSuccess(true); result.setResult(pageList); return result; } /** * 添加 * * @param sysDictItem * @return */ @PostMapping(value = "/add") @AutoLog(value = "添加字典值") @ApiOperation(value = "添加字典值", notes = "添加字典值", produces = "application/json") public Result add(@RequestBody SysDictItem sysDictItem) { Result result = new Result(); try { sysDictItemService.save(sysDictItem); result.success("添加成功!"); } catch (Exception e) { log.error("", e); result.error500("操作失败"); } return result; } /** * 编辑 * * @param sysDictItem * @return */ @PostMapping(value = "/edit") @AutoLog(value = "编辑字典值") @ApiOperation(value = "编辑字典值", notes = "编辑字典值", produces = "application/json") public Result edit(@RequestBody SysDictItem sysDictItem) { Result result = new Result(); SysDictItem sysDictItemEntity = sysDictItemService.getById(sysDictItem.getId()); if (sysDictItemEntity == null) { result.error500("未找到对应实体"); } else { boolean ok = sysDictItemService.updateById(sysDictItem); //TODO 返回false说明什么? if (ok) { result.success("修改成功!"); } } return result; } /** * 通过id删除 * * @param id * @return */ @PostMapping(value = "/delete") @AutoLog(value = "删除字典值") @ApiOperation(value = "删除字典值", notes = "删除字典值", produces = "application/json") public Result delete(@RequestParam(name = "id", required = true) String id) { Result result = new Result(); SysDictItem sysDictItem = sysDictItemService.getById(id); if (sysDictItem == null) { result.error500("未找到对应实体"); } else { boolean ok = sysDictItemService.removeById(id); if (ok) { result.success("删除成功!"); } } return result; } /** * 批量删除 * * @param ids * @return */ @PostMapping(value = "/deleteBatch") @AutoLog(value = "批量删除字典值") @ApiOperation(value = "批量删除字典值", notes = "批量删除字典值", produces = "application/json") public Result deleteBatch(@RequestParam(name = "ids", required = true) String ids) { Result result = new Result(); if (ids == null || "".equals(ids.trim())) { result.error500("参数不识别!"); } else { this.sysDictItemService.removeByIds(Arrays.asList(ids.split(","))); result.success("删除成功!"); } return result; } /** * 通过id查询 * * @param id * @return */ @GetMapping(value = "/queryById") @ApiOperation(value = "通过id查询字典值", notes = "通过id查询字典值", produces = "application/json") public Result queryById(@RequestParam(name = "id", required = true) String id) { Result result = new Result(); SysDictItem sysDictItem = sysDictItemService.getById(id); if (sysDictItem == null) { result.error500("未找到对应实体"); } else { result.setResult(sysDictItem); result.setSuccess(true); } return result; } }