package com.inspect.system.controller;

import com.inspect.base.core.utils.StringUtils;
import com.inspect.base.core.utils.poi.ExcelUtil;
import com.inspect.base.core.web.controller.BaseController;
import com.inspect.base.core.web.domain.AjaxResult;
import com.inspect.base.core.web.page.TableDataInfo;
import com.inspect.common.log.annotation.Log;
import com.inspect.common.log.enums.BizType;
import com.inspect.common.security.annotation.RequiresPermissions;
import com.inspect.common.security.utils.SecurityUtils;
import com.inspect.system.base.domain.SysDictData;
import com.inspect.system.service.ISysDictDataService;
import com.inspect.system.service.ISysDictTypeService;

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

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
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.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping({"/dict/data"})
public class SysDictDataController extends BaseController {
    private final ISysDictDataService dictDataService;
    private final ISysDictTypeService dictTypeService;

    public SysDictDataController(ISysDictDataService dictDataService, ISysDictTypeService dictTypeService) {
        this.dictDataService = dictDataService;
        this.dictTypeService = dictTypeService;
    }

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

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

    @RequiresPermissions({"system:dict:query"})
    @GetMapping({"/{dictCode}"})
    public AjaxResult getInfo(@PathVariable Long dictCode) {
        return AjaxResult.success(this.dictDataService.selectDictDataById(dictCode));
    }

    @GetMapping({"/type/{dictType}"})
    public AjaxResult dictType(@PathVariable String dictType) {
        List<SysDictData> data = this.dictTypeService.selectDictDataByType(dictType);
        if (StringUtils.isNull(data)) {
            data = new ArrayList<>();
        }

        return AjaxResult.success(data);
    }

    @RequiresPermissions({"system:dict:add"})
    @Log(
            title = "字典数据",
            businessType = BizType.INSERT
    )
    @PostMapping
    public AjaxResult add(@Validated @RequestBody SysDictData dict) {
        dict.setCreateBy(SecurityUtils.getUsername());
        return this.toAjax(this.dictDataService.insertDictData(dict));
    }

    @RequiresPermissions({"system:dict:edit"})
    @Log(
            title = "字典数据",
            businessType = BizType.UPDATE
    )
    @PostMapping({"/edit"})
    public AjaxResult edit(@Validated @RequestBody SysDictData dict) {
        dict.setUpdateBy(SecurityUtils.getUsername());
        return this.toAjax(this.dictDataService.updateDictData(dict));
    }

    @RequiresPermissions({"system:dict:remove"})
    @Log(
            title = "字典类型",
            businessType = BizType.DELETE
    )
    @PostMapping({"/remove/{dictCodes}"})
    public AjaxResult remove(@PathVariable Long[] dictCodes) {
        this.dictDataService.deleteDictDataByIds(dictCodes);
        return this.success();
    }
}
