DictionaryController.java 3.9 KB
package vion.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import vion.Global;
import vion.enums.ResultEnum;
import vion.model.Dictionary;
import vion.model.DictionaryType;
import vion.model.Store;
import vion.service.IDictionarySerrvice;
import vion.service.IDictionaryTypeSerrvice;
import vion.utils.ResultUtil;

import java.util.Comparator;
import java.util.List;

@RestController
@RequestMapping(Global.BASE_URL)
@Slf4j
public class DictionaryController {

    @Autowired
    private IDictionarySerrvice dictionarySerrvice;
    @Autowired
    private IDictionaryTypeSerrvice dictionaryTypeSerrvice;

    @GetMapping("/dictionarys")
    @ResponseBody
    public Object getDictionaryList(@RequestParam(name = "pageNum")Integer pageNum,
                                    @RequestParam(name = "pageSize")Integer pageSize,
                                    @RequestParam(name = "type",required=false)String type){
        //pageNum当前页,pageSize每页条数
        Page page= PageHelper.startPage(pageNum,pageSize);
        QueryWrapper<Dictionary> wrapper=new QueryWrapper<>();
        if(type!=null)
            wrapper.eq("type",type);

        List<Dictionary> dictionaryList=dictionarySerrvice.list(wrapper);

        dictionaryList.sort(Comparator.comparing(Dictionary::getId));
        PageInfo pageInfo=new PageInfo<>(page);
        pageInfo.setList(dictionaryList);

        if (dictionaryList != null) {
            return ResultUtil.success(pageInfo);
        } else {
            return ResultUtil.error(ResultEnum.SELECT_ERROR);
        }
    }

    @GetMapping("/dictionary")
    @ResponseBody
    public Object getDictionaryByID(@RequestParam(name = "id")Integer id){

        Dictionary dictionary=dictionarySerrvice.getById(id);

        if (dictionary != null) {
            return ResultUtil.success(dictionary);
        } else {
            return ResultUtil.error(ResultEnum.SELECT_ERROR);
        }
    }

    @PostMapping("/dictionarys")
    @ResponseBody
    public Object saveOrUpdateDictionary(@RequestBody Dictionary data) {

        try {
            dictionarySerrvice.saveOrUpdate(data);
            return ResultUtil.success();
        }catch (Exception e) {
            return ResultUtil.error(ResultEnum.SELECT_ERROR);
        }
    }

    @GetMapping("/dictionaryTypes")
    @ResponseBody
    public Object getDictionaryTypeList(@RequestParam(name = "pageNum")Integer pageNum,
                                        @RequestParam(name = "pageSize")Integer pageSize){
        //pageNum当前页,pageSize每页条数
        PageHelper.startPage(pageNum,pageSize);
        List<DictionaryType> dictionaryTypeList=dictionaryTypeSerrvice.list();
        PageInfo pageInfo=new PageInfo(dictionaryTypeList,10);

        if (dictionaryTypeList != null) {
            return ResultUtil.success(pageInfo);
        } else {
            return ResultUtil.error(ResultEnum.SELECT_ERROR);
        }
    }

    @GetMapping("/dictionaryType")
    @ResponseBody
    public Object getDictionaryTypeByID(@RequestParam(name = "id")Integer id){

        DictionaryType dictionaryType=dictionaryTypeSerrvice.getById(id);

        if (dictionaryType != null) {
            return ResultUtil.success(dictionaryType);
        } else {
            return ResultUtil.error(ResultEnum.SELECT_ERROR);
        }
    }

    @PostMapping("/dictionaryTypes")
    @ResponseBody
    public Object saveOrUpdateDictionaryType(@RequestBody DictionaryType data) {

        try {
            dictionaryTypeSerrvice.saveOrUpdate(data);
            return ResultUtil.success();
        }catch (Exception e) {
            return ResultUtil.error(ResultEnum.SELECT_ERROR);
        }
    }
}