package com.inspect.system.controller;

import com.inspect.base.core.utils.poi.ExcelUtil;
import com.inspect.base.core.web.controller.BaseController;
import com.inspect.base.core.web.domain.AjaxResult;
import com.inspect.base.core.web.page.TableDataInfo;
import com.inspect.common.log.annotation.Log;
import com.inspect.common.log.enums.BizType;
import com.inspect.common.security.annotation.RequiresPermissions;
import com.inspect.common.security.utils.SecurityUtils;
import com.inspect.system.base.domain.SysDictType;
import com.inspect.system.service.ISysDictTypeService;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping({"/dict/type"})
public class SysDictTypeController extends BaseController {
    private final ISysDictTypeService dictTypeService;

    public SysDictTypeController(ISysDictTypeService dictTypeService) {
        this.dictTypeService = dictTypeService;
    }

    @RequiresPermissions({"system:dict:list"})
    @GetMapping({"/list"})
    public TableDataInfo list(SysDictType dictType) {
        this.startPage();
        List<SysDictType> list = this.dictTypeService.selectDictTypeList(dictType);
        return this.getDataTable(list);
    }

    @Log(
            title = "字典类型",
            businessType = BizType.EXPORT
    )
    @RequiresPermissions({"system:dict:export"})
    @PostMapping({"/export"})
    public void export(HttpServletResponse response, SysDictType dictType) {
        List<SysDictType> list = this.dictTypeService.selectDictTypeList(dictType);
        ExcelUtil<SysDictType> util = new ExcelUtil<>(SysDictType.class);
        util.exportExcel(response, list, "字典类型");
    }

    @RequiresPermissions({"system:dict:query"})
    @GetMapping({"/{dictId}"})
    public AjaxResult getInfo(@PathVariable Long dictId) {
        return AjaxResult.success(this.dictTypeService.selectDictTypeById(dictId));
    }

    @RequiresPermissions({"system:dict:add"})
    @Log(
            title = "字典类型",
            businessType = BizType.INSERT
    )
    @PostMapping
    public AjaxResult add(@Validated @RequestBody SysDictType dict) {
        if ("1".equals(this.dictTypeService.checkDictTypeUnique(dict))) {
            return AjaxResult.error("新增字典'" + dict.getDictName() + "'失败，字典类型已存在");
        } else {
            dict.setCreateBy(SecurityUtils.getUsername());
            return this.toAjax(this.dictTypeService.insertDictType(dict));
        }
    }

    @RequiresPermissions({"system:dict:edit"})
    @Log(
            title = "字典类型",
            businessType = BizType.UPDATE
    )
    @PostMapping({"/edit"})
    public AjaxResult edit(@Validated @RequestBody SysDictType dict) {
        if ("1".equals(this.dictTypeService.checkDictTypeUnique(dict))) {
            return AjaxResult.error("修改字典'" + dict.getDictName() + "'失败，字典类型已存在");
        } else {
            dict.setUpdateBy(SecurityUtils.getUsername());
            return this.toAjax(this.dictTypeService.updateDictType(dict));
        }
    }

    @RequiresPermissions({"system:dict:remove"})
    @Log(
            title = "字典类型",
            businessType = BizType.DELETE
    )
    @PostMapping({"/remove/{dictIds}"})
    public AjaxResult remove(@PathVariable Long[] dictIds) {
        this.dictTypeService.deleteDictTypeByIds(dictIds);
        return this.success();
    }

    @RequiresPermissions({"system:dict:remove"})
    @Log(
            title = "字典类型",
            businessType = BizType.CLEAN
    )
    @PostMapping({"/refreshCache"})
    public AjaxResult refreshCache() {
        this.dictTypeService.resetDictCache();
        return AjaxResult.success();
    }

    @GetMapping({"/optionselect"})
    public AjaxResult optionSelect() {
        List<SysDictType> dictTypes = this.dictTypeService.selectDictTypeAll();
        return AjaxResult.success(dictTypes);
    }
}
