package com.bonus.system.basic.controller;

import com.bonus.common.core.utils.poi.ExcelUtil;
import com.bonus.common.core.web.controller.BaseController;
import com.bonus.common.core.web.domain.AjaxResult;
import com.bonus.common.core.web.page.TableDataInfo;
import com.bonus.common.log.annotation.Log;
import com.bonus.common.log.enums.BusinessType;
import com.bonus.common.security.annotation.RequiresPermissions;
import com.bonus.common.security.utils.SecurityUtils;
import com.bonus.system.api.domain.SysDictType;
import com.bonus.system.basic.service.ISysDictTypeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

/**
 * 数据字典信息
 * 
 * @author bonus
 */
@RestController
@RequestMapping("/dict/type")
@Slf4j
public class SysDictTypeController extends BaseController
{
    @Autowired
    private ISysDictTypeService dictTypeService;

    @RequiresPermissions("system:dict:list")
    @GetMapping("/list")
    @Log(title = "系统管理->字典管理", businessType = BusinessType.QUERY)
    public TableDataInfo list(SysDictType dictType) {
        try{
            startPage();
            List<SysDictType> list = dictTypeService.selectDictTypeList(dictType);
            return getDataTable(list);
        }catch (Exception e){
            log.error(e.toString(),e);
        }
        return  getDataTableError(new ArrayList<>());
    }

    @Log(title = "系统管理->字典管理->导出", businessType = BusinessType.EXPORT)
    @RequiresPermissions("system:dict:export")
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysDictType dictType) {
       try{
           List<SysDictType> list = dictTypeService.selectDictTypeList(dictType);
           ExcelUtil<SysDictType> util = new ExcelUtil<SysDictType>(SysDictType.class);
           util.exportExcel(response, list, "字典类型");
       }catch (Exception e){
           log.error(e.toString(),e);
       }
    }

    /**
     * 查询字典类型详细
     */
    @RequiresPermissions("system:dict:query")
    @GetMapping(value = "/{dictId}")

    public AjaxResult getInfo(@PathVariable Long dictId) {
        try{
            return success(dictTypeService.selectDictTypeById(dictId));
        }catch (Exception e){
            log.error(e.toString(),e);
        }
        return error("系统错误");
    }

    /**
     * 新增字典类型
     */
    @RequiresPermissions("system:dict:add")
    @PostMapping
    @Log(title = "系统管理->字典管理->新增字典类型", businessType = BusinessType.INSERT)
    public AjaxResult add(@Validated @RequestBody SysDictType dict) {
        try{
            if (!dictTypeService.checkDictTypeUnique(dict)) {
                return error("新增字典'" + dict.getDictName() + "'失败，字典类型已存在");
            }
            dict.setCreateBy(SecurityUtils.getUsername());
            return toAjax(dictTypeService.insertDictType(dict));
        }catch (Exception e){
            log.error(e.toString(),e);
        }
        return error("系统错误");
    }

    /**
     * 修改字典类型
     */
    @RequiresPermissions("system:dict:edit")
    @PutMapping
    @Log(title = "系统管理->字典管理->修改字典类型", businessType = BusinessType.UPDATE)
    public AjaxResult edit(@Validated @RequestBody SysDictType dict) {
        try{
            if (!dictTypeService.checkDictTypeUnique(dict)) {
                return error("修改字典'" + dict.getDictName() + "'失败，字典类型已存在");
            }
            dict.setUpdateBy(SecurityUtils.getUsername());
            return toAjax(dictTypeService.updateDictType(dict));
        }catch (Exception e){
            log.error(e.toString(),e);
        }
        return error("系统错误");

    }

    /**
     * 删除字典类型
     */
    @RequiresPermissions("system:dict:remove")
    @DeleteMapping("/{dictIds}")
    @Log(title = "系统管理->字典管理->删除字典类型", businessType = BusinessType.DELETE)
    public AjaxResult remove(@PathVariable Long[] dictIds) {
        try{
            dictTypeService.deleteDictTypeByIds(dictIds);
            return success();
        }catch (Exception e){
            log.error(e.toString(),e);
        }
        return error("系统错误");


    }

    /**
     * 刷新字典缓存
     */
    @RequiresPermissions("system:dict:remove")
    @DeleteMapping("/refreshCache")
    @Log(title = "系统管理->字典管理->刷新字典缓存", businessType = BusinessType.OTHER)
    public AjaxResult refreshCache() {
        try{
            dictTypeService.resetDictCache();
            return success();
        }catch (Exception e){
            log.error(e.toString(),e);
        }
        return error("系统错误");

    }

    /**
     * 获取字典选择框列表
     */
    @GetMapping("/optionselect")
    public AjaxResult optionselect() {
        try{
            List<SysDictType> dictTypes = dictTypeService.selectDictTypeAll();
            return success(dictTypes);
        }catch (Exception e){
            log.error(e.toString(),e);
        }
        return error("系统错误");
    }
}
