package com.bonus.system.basic.controller;

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.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.SysDictData;
import com.bonus.system.basic.service.ISysDictDataService;
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/data")
@Slf4j
public class SysDictDataController extends BaseController
{
    @Autowired
    private ISysDictDataService dictDataService;
    
    @Autowired
    private ISysDictTypeService dictTypeService;

    @RequiresPermissions("system:dict:list")
    @GetMapping("/list")
    @Log(title = "系统管理->字典管理->查询", businessType = BusinessType.QUERY)
    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")
    @Log(title = "系统管理->字典管理->导出", businessType = BusinessType.EXPORT)
    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("系统错误");

    }

    @GetMapping(value = "/type/leaveReporting")
    public AjaxResult leaveReporting() {
        try{
            List<SysDictData> data = dictTypeService.leaveReporting("");
            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")
    @Log(title = "系统管理->字典管理->新增字典类型", businessType = BusinessType.INSERT)
    @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
    @Log(title = "系统管理->字典管理->修改保存字典类型", businessType = BusinessType.UPDATE)
    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}")
    @Log(title = "系统管理->字典管理->删除字典类型", businessType = BusinessType.DELETE)
    public AjaxResult remove(@PathVariable Long[] dictCodes) {
        try{
            dictDataService.deleteDictDataByIds(dictCodes);
            return success();
        }catch (Exception e){
            log.error(e.toString(),e);
        }
        return  error("系统错误");
    }
}
