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 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.StringUtils;
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.SysDictData;
import com.bonus.system.service.ISysDictDataService;
import com.bonus.system.service.ISysDictTypeService;

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

    @RequiresPermissions("system:dict:list")
    @GetMapping("/list")
    @SysLog(title = "字典管理", businessType = OperaType.QUERY,logType = 0,module = "系统管理->字典管理")
    public TableDataInfo list(SysDictData dictData) {
        try{
            startPage();
            List<SysDictData> list = dictDataService.selectDictDataList(dictData);
            return getDataTable(list);
        }catch (Exception e){
            log.error(e.toString(),e);
        }
        return  getDataTableError(new ArrayList<>());
    }

    @RequiresPermissions("system:dict:export")
    @PostMapping("/export")
    @SysLog(title = "字典管理", businessType = OperaType.EXPORT,logType = 0,module = "系统管理->字典管理")
    public void export(HttpServletResponse response, SysDictData dictData) {
        try{
            List<SysDictData> list = dictDataService.selectDictDataList(dictData);
            ExcelUtil<SysDictData> util = new ExcelUtil<SysDictData>(SysDictData.class);
            util.exportExcel(response, list, "字典数据");
        }catch (Exception e){
            log.error(e.toString(),e);
        }
    }

    /**
     * 查询字典数据详细
     */
    @RequiresPermissions("system:dict:query")
    @GetMapping(value = "/{dictCode}")
    public AjaxResult getInfo(@PathVariable Long dictCode) {
        try{
            return success(dictDataService.selectDictDataById(dictCode));
        }catch (Exception e){
            log.error(e.toString(),e);
        }
            return  error("系统错误");
    }

    /**
     * 根据字典类型查询字典数据信息
     */
    @GetMapping(value = "/type/{dictType}")
    public AjaxResult dictType(@PathVariable String dictType) {
        try{
            List<SysDictData> data = dictTypeService.selectDictDataByType(dictType);
            if (StringUtils.isNull(data)) {
                data = new ArrayList<SysDictData>();
            }
            return success(data);
        }catch (Exception e){
            log.error(e.toString(),e);
        }
        return  error("系统错误");

    }

    /**
     * 新增字典类型
     */
    @RequiresPermissions("system:dict:add")
    @SysLog(title = "字典管理", businessType = OperaType.INSERT,logType = 0,module = "系统管理->字典管理")
    @PostMapping
    public AjaxResult add(@Validated @RequestBody SysDictData dict) {
        try{
            dict.setCreateBy(SecurityUtils.getUsername());
            return toAjax(dictDataService.insertDictData(dict));
        }catch (Exception e){
            log.error(e.toString(),e);
        }
        return  error("系统错误");

    }

    /**
     * 修改保存字典类型
     */
    @RequiresPermissions("system:dict:edit")
    @PutMapping
    @SysLog(title = "字典管理", businessType = OperaType.UPDATE,logType = 0,module = "系统管理->字典管理")
    public AjaxResult edit(@Validated @RequestBody SysDictData dict) {
        try{
            dict.setUpdateBy(SecurityUtils.getUsername());
            return toAjax(dictDataService.updateDictData(dict));
        }catch (Exception e){
            log.error(e.toString(),e);
        }
        return  error("系统错误");

    }

    /**
     * 删除字典类型
     */
    @RequiresPermissions("system:dict:remove")
    @DeleteMapping("/{dictCodes}")
    @SysLog(title = "字典管理", businessType = OperaType.DELETE,logType = 0,module = "系统管理->字典管理")
    public AjaxResult remove(@PathVariable Long[] dictCodes) {
        try{
            dictDataService.deleteDictDataByIds(dictCodes);
            return success();
        }catch (Exception e){
            log.error(e.toString(),e);
        }
        return  error("系统错误");
    }
}
