package org.wfc.user.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.wfc.common.core.utils.StringUtils;
import org.wfc.common.core.utils.poi.ExcelUtil;
import org.wfc.common.core.web.controller.BaseController;
import org.wfc.common.core.web.domain.AjaxResult;
import org.wfc.common.core.web.page.TableDataInfo;
import org.wfc.common.log.annotation.Log;
import org.wfc.common.log.enums.BusinessType;
import org.wfc.common.security.annotation.RequiresPermissions;
import org.wfc.common.security.utils.SecurityUtils;
import org.wfc.user.api.domain.UDictData;
import org.wfc.user.service.IUDictDataService;
import org.wfc.user.service.IUDictTypeService;

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

/**
 * 数据字典信息
 * 
 * @author wfc
 */
@RestController
@RequestMapping("/dict/data")
public class UDictDataController extends BaseController
{
    @Autowired
    private IUDictDataService dictDataService;
    
    @Autowired
    private IUDictTypeService dictTypeService;

    @RequiresPermissions("Utem:dict:list")
    @GetMapping("/list")
    public TableDataInfo list(UDictData dictData)
    {
        startPage();
        List<UDictData> list = dictDataService.selectDictDataList(dictData);
        return getDataTable(list);
    }

    // @Log(title = "字典数据", businessType = BusinessType.EXPORT)
    @RequiresPermissions("Utem:dict:export")
    @PostMapping("/export")
    public void export(HttpServletResponse response, UDictData dictData)
    {
        List<UDictData> list = dictDataService.selectDictDataList(dictData);
        ExcelUtil<UDictData> util = new ExcelUtil<UDictData>(UDictData.class);
        util.exportExcel(response, list, "字典数据");
    }

    /**
     * 查询字典数据详细
     */
    @RequiresPermissions("Utem:dict:query")
    @GetMapping(value = "/{dictCode}")
    public AjaxResult getInfo(@PathVariable Long dictCode)
    {
        return success(dictDataService.selectDictDataById(dictCode));
    }

    /**
     * 根据字典类型查询字典数据信息
     */
    @GetMapping(value = "/type/{dictType}")
    public AjaxResult dictType(@PathVariable String dictType)
    {
        List<UDictData> data = dictTypeService.selectDictDataByType(dictType);
        if (StringUtils.isNull(data))
        {
            data = new ArrayList<UDictData>();
        }
        return success(data);
    }

    /**
     * 新增字典类型
     */
    @RequiresPermissions("Utem:dict:add")
    // @Log(title = "字典数据", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Validated @RequestBody UDictData dict)
    {
        dict.setCreateBy(SecurityUtils.getUsername());
        return toAjax(dictDataService.insertDictData(dict));
    }

    /**
     * 修改保存字典类型
     */
    @RequiresPermissions("Utem:dict:edit")
    // @Log(title = "字典数据", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody UDictData dict)
    {
        dict.setUpdateBy(SecurityUtils.getUsername());
        return toAjax(dictDataService.updateDictData(dict));
    }

    /**
     * 删除字典类型
     */
    @RequiresPermissions("Utem:dict:remove")
    // @Log(title = "字典类型", businessType = BusinessType.DELETE)
    @DeleteMapping("/{dictCodes}")
    public AjaxResult remove(@PathVariable Long[] dictCodes)
    {
        dictDataService.deleteDictDataByIds(dictCodes);
        return success();
    }
}
