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.core.controller.BaseController;
import com.younike.common.core.dto.AjaxResult;
import com.younike.common.core.page.TableResult;
import com.younike.service.pojo.SysDictData;
import com.younike.service.service.ISysDictDataService;
import com.younike.service.service.ISysDictTypeService;


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

import java.util.List;

/**
 * 字典信息
 *
 * @author Andrew
 */
@RestController
@RequestMapping("/system/dict/data")
public class SysDictDataController extends BaseController {

    @Autowired
    private ISysDictTypeService dictTypeService;

    @Autowired
    private ISysDictDataService sysDictDataService;

    /**
     * 获取字典数据列表
     */
    @GetMapping("/list")
    public TableResult list(SysDictData dictData) {
        startPage();
        List<SysDictData> dictDatas = sysDictDataService.selectDictDataList(dictData);
        return getDataTable(dictDatas);
    }

    /**
     * 根据字典类型查询字典数据信息
     */
    @GetMapping(value = "/type/{dictType}")
    public AjaxResult dictType(@PathVariable String dictType) {
        return AjaxResult.success(dictTypeService.selectDictDataByType(dictType));
    }

    /**
     * 新增字典数据
     */
    @PostMapping("")

    public APIResult addDictData(@RequestBody SysDictData dictData) {
        dictData.setCreateBy(ShiroUtils.getLoginName());
        sysDictDataService.insertDictData(dictData);
        return ResultGenerator.getSuccessResult();
    }

    /**
     * 根据id查找字典数据
     */
    @GetMapping(value = "/{dictDataId}")
    public SysDictData getDictData(@PathVariable Long dictDataId) {
        return sysDictDataService.selectDictDataById(dictDataId);
    }

    /**
     * 更新字典数据
     */
    @PutMapping("")

    public APIResult updateDictData(@RequestBody SysDictData dictData) {
        dictData.setUpdateBy(ShiroUtils.getLoginName());
        sysDictDataService.updateDictData(dictData);
        return ResultGenerator.getSuccessResult();
    }

    /**
     * 删除字典数据
     */
    @DeleteMapping(value = "/{dictDataIds}")

    public APIResult deleteDictDatas(@PathVariable String dictDataIds) {
        try {
            sysDictDataService.deleteDictDataByIds(dictDataIds);
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
        return ResultGenerator.getSuccessResult();
    }

}
