package com.bonus.system.controller;

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

import com.bonus.common.log.annotation.SysLog;
import com.bonus.common.log.enums.OperaType;
import com.bonus.common.security.annotation.InnerAuth;
import com.bonus.common.security.annotation.RequiresPermissionsOrInnerAuth;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
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.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
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.security.annotation.RequiresPermissions;
import com.bonus.common.security.utils.SecurityUtils;
import com.bonus.system.api.domain.SysDictType;
import com.bonus.system.service.ISysDictTypeService;

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

    @RequiresPermissionsOrInnerAuth(innerAuth = @InnerAuth, requiresPermissions = @RequiresPermissions("system:dict:list"))
    @GetMapping("/list")
    @SysLog(title = "字典管理", businessType = OperaType.QUERY,logType = 0,module = "系统管理->字典管理")
    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<>());
    }

    @SysLog(title = "字典管理", businessType = OperaType.EXPORT,logType = 0,module = "系统管理->字典管理")
    @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);
       }
    }

    /**
     * 查询字典类型详细
     */
    @RequiresPermissionsOrInnerAuth(innerAuth = @InnerAuth, requiresPermissions = @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("系统错误");
    }

    /**
     * 新增字典类型
     */
    @RequiresPermissionsOrInnerAuth(innerAuth = @InnerAuth, requiresPermissions = @RequiresPermissions("system:dict:add"))
    @PostMapping
    @SysLog(title = "字典管理", businessType = OperaType.INSERT,logType = 0,module = "系统管理->字典管理")
    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("系统错误");
    }

    /**
     * 修改字典类型
     */
    @RequiresPermissionsOrInnerAuth(innerAuth = @InnerAuth, requiresPermissions = @RequiresPermissions("system:dict:edit"))
   @PostMapping("/edit")
    @SysLog(title = "字典管理", businessType = OperaType.UPDATE,logType = 0,module = "系统管理->字典管理")
    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("系统错误");

    }

    /**
     * 删除字典类型
     */
    @RequiresPermissionsOrInnerAuth(innerAuth = @InnerAuth, requiresPermissions = @RequiresPermissions("system:dict:remove"))
    @PostMapping("/delete/{dictIds}")
    @SysLog(title = "字典管理", businessType = OperaType.DELETE,logType = 0,module = "系统管理->字典管理")
    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")
    @PostMapping("/refreshCache")
    @SysLog(title = "字典管理", businessType = OperaType.FLASH,logType = 0,module = "系统管理->字典管理",details = "刷新字典缓存")
    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("系统错误");
    }
}
