package cn.huge.module.knowledge.controller.web; import cn.huge.base.common.utils.ReturnFailUtils; import cn.huge.base.common.utils.ReturnSucUtils; import cn.huge.module.knowledge.domain.po.Dictionary; import cn.huge.module.knowledge.service.DictionaryService; import com.google.common.collect.Maps; import lombok.extern.slf4j.Slf4j; import org.apache.commons.lang3.StringUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.domain.Page; import org.springframework.data.domain.PageRequest; import org.springframework.data.domain.Sort; import org.springframework.web.bind.annotation.*; import javax.servlet.http.HttpServletRequest; import java.util.Map; import java.util.stream.Collectors; import java.util.stream.Stream; /** * @title: ai服务字典接口api-web端 * @description: ai服务字典接口api-web端 * @company: hugeinfo * @author: huangh * @time: 2024年12月11日 * @version: 1.0.0 */ @Slf4j @RestController @RequestMapping("/api/web/dictionary") public class DictionaryWebController { @Autowired(required = false) private HttpServletRequest request; @Autowired private DictionaryService service; /** * 获取请求URL参数 * @return Map */ private Map getParameter(){ Map terms = Maps.newHashMap(); // 字典编号,主键,唯一id String code = request.getParameter("code"); if (StringUtils.isNotBlank(code)){ terms.put("code", code); } // 字典名称 String name = request.getParameter("name"); if (StringUtils.isNotBlank(name)){ terms.put("name", name); } // 字典代号/值 String value = request.getParameter("value"); if (StringUtils.isNotBlank(value)){ terms.put("value", value); } // 父节点代号 String parentCode = request.getParameter("parentCode"); if (StringUtils.isNotBlank(parentCode)){ terms.put("parentCode", parentCode); } // 创建时间区间 String createStart = request.getParameter("createStart"); String createEnd = request.getParameter("createEnd"); if(StringUtils.isNotBlank(createStart) && StringUtils.isNotBlank(createEnd)) { terms.put("createStart", createStart); terms.put("createEnd", createEnd); } // 更新时间区间 String updateStart = request.getParameter("updateStart"); String updateEnd = request.getParameter("updateEnd"); if(StringUtils.isNotBlank(updateStart) && StringUtils.isNotBlank(updateEnd)) { terms.put("updateStart", updateStart); terms.put("updateEnd", updateEnd); } return terms; } /** * 条件查询多个 * @url {ctx}/api/web/dictionary/listQuery * @return Object */ @GetMapping("/listQuery") public Object listQuery() { try { Map terms = getParameter(); return ReturnSucUtils.getRepInfo(service.listTerms(terms)); } catch (Exception e) { return ReturnFailUtils.getRepInfo(); } } /** * 条件分页查询 * @url {ctx}/api/web/dictionary/pageQuery * @param page 页码 * @param size 每页数量 * @return Object */ @GetMapping("/pageQuery") public Object pageQuery(@RequestParam(value = "page") int page, @RequestParam(value = "size") int size) { try { Map terms = getParameter(); Sort sort = Sort.by(Sort.Direction.DESC, "create_time"); PageRequest pageRequest = PageRequest.of(page-1, size, sort); Page dictionaryPage = service.pageQuery(pageRequest, terms); return ReturnSucUtils.getRepInfo( "处理成功", dictionaryPage); } catch (Exception e) { return ReturnFailUtils.getRepInfo(); } } /** * 根据编号查询单个 * @url {ctx}/api/web/dictionary/getById * @param id 主键编号 * @return Object */ @GetMapping("/getById") public Object getById(@RequestParam(value = "id") String id) { try { return ReturnSucUtils.getRepInfo(service.getById(id)); } catch (Exception e) { return ReturnFailUtils.getRepInfo(); } } /** * 根据主键单个 * @url {ctx}/api/web/dictionary/deleteById * @param id 主键编号 * @return Object */ @GetMapping("/deleteById") public Object deleteById(@RequestParam(value = "id") String id) { try { service.removeById(id); return ReturnSucUtils.getRepInfo(); } catch (Exception e) { return ReturnFailUtils.getRepInfo(); } } /** * 新增或更新对象 * @url {ctx}/api/web/dictionary/saveDictionary * @param dictionary 实体对象 * @return Object */ @PostMapping("/saveDictionary") public Object saveDictionary(@RequestBody Dictionary dictionary) { try { service.saveDictionary(dictionary); return ReturnSucUtils.getRepInfo(); } catch (Exception e) { return ReturnFailUtils.getRepInfo(); } } }