package com.younike.admin.controller.system;

import com.younike.admin.utils.ShiroUtils;
import com.younike.common.bean.APIResult;
import com.younike.common.bean.ResultGenerator;
import com.younike.common.constant.UserConstants;
import com.younike.common.core.controller.BaseController;
import com.younike.common.core.page.TableResult;
import com.younike.service.pojo.SysDictType;
import com.younike.service.service.ISysDictTypeService;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 字典信息
 */
@RestController
@RequestMapping("/system/dict/type")
public class SysDictTypeController extends BaseController {

    @Autowired
    private ISysDictTypeService sysDictTypeService;

    /**
     * 获取字典列表
     */
    @GetMapping("/list")
    public TableResult list(SysDictType dictType) {
        startPage();
        List<SysDictType> dictTypes = sysDictTypeService.selectDictTypeList(dictType);
        return getDataTable(dictTypes);
    }

    /**
     * 新增字典类型
     */
    @PostMapping("")
    public APIResult addDictType(@RequestBody SysDictType dictType) {
        //检查唯一性
        if (!sysDictTypeService.checkDictTypeUnique(dictType).equals(UserConstants.DICT_TYPE_UNIQUE)) {
            return ResultGenerator.getFailResult("该字典类型已存在");
        }
        dictType.setCreateBy(ShiroUtils.getLoginName());
        sysDictTypeService.insertDictType(dictType);
        return ResultGenerator.getSuccessResult();
    }

    /**
     * 根据id查找字典类型
     */
    @GetMapping(value = "/{dictTypeId}")
    public SysDictType getDictType(@PathVariable Long dictTypeId) {
        return sysDictTypeService.selectDictTypeById(dictTypeId);
    }

    /**
     * 更新字典类型
     */
    @PutMapping("")
    public APIResult updateDictType(@RequestBody SysDictType dictType) {
        //检查唯一性
        if (!sysDictTypeService.checkDictTypeUnique(dictType).equals(UserConstants.DICT_TYPE_UNIQUE)) {
            return ResultGenerator.getFailResult("该字典类型已存在");
        }
        dictType.setUpdateBy(ShiroUtils.getLoginName());
        sysDictTypeService.updateDictType(dictType);
        return ResultGenerator.getSuccessResult();
    }

    /**
     * 删除字典类型
     */
    @DeleteMapping(value = "/{dictTypeIds}")
    public APIResult deleteDictTypes(@PathVariable String dictTypeIds) {
        try {
            sysDictTypeService.deleteDictTypeByIds(dictTypeIds);
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
        return ResultGenerator.getSuccessResult();
    }
}
