package com.qiaoba.module.system.controller;

import com.qiaoba.api.system.entity.SysDictType;
import com.qiaoba.api.system.entity.param.SysDictTypeParam;
import com.qiaoba.common.base.result.AjaxResult;
import com.qiaoba.common.base.validate.AddGroup;
import com.qiaoba.common.base.validate.EditGroup;
import com.qiaoba.common.database.entity.PageQuery;
import com.qiaoba.common.database.entity.TableDataInfo;
import com.qiaoba.common.poi.util.ExcelUtil;
import com.qiaoba.module.system.service.SysDictDataService;
import com.qiaoba.module.system.service.SysDictTypeService;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.Size;
import java.util.List;

/**
 * 字典类型管理 Web层
 *
 * @author ailanyin
 * @version 1.0
 * @since 2022-09-22 04:20:28
 */
@Validated
@RestController
@RequestMapping("/system/dict/type")
@RequiredArgsConstructor
public class SysDictTypeController {

    private final SysDictTypeService sysDictTypeService;
    private final SysDictDataService sysDictDataService;


    @PreAuthorize("hasAuthority('system:dict:list')")
    @GetMapping("/list")
    public TableDataInfo list(@Validated SysDictTypeParam param, PageQuery pageQuery) {
        return sysDictTypeService.selectPageList(param, pageQuery);
    }

    @PreAuthorize("hasAuthority('system:dict:export')")
    @PostMapping("/export")
    public void export(HttpServletResponse response, @Validated SysDictTypeParam param) {
        List<SysDictType> list = sysDictTypeService.selectList(param);
        ExcelUtil.exportExcel(list, SysDictType.class, "字典类型", response);
    }

    @PreAuthorize("hasAuthority('system:dict:query')")
    @GetMapping(value = "/{dictId}")
    public AjaxResult getInfo(@Size(min = 19, max = 20, message = "ID不合法") @PathVariable String dictId) {
        return AjaxResult.success(sysDictTypeService.selectById(dictId));
    }

    @PreAuthorize("hasAuthority('system:dict:add')")
    @PostMapping
    public AjaxResult add(@Validated(AddGroup.class) @RequestBody SysDictType sysDictType) {
        return AjaxResult.toAjax(sysDictTypeService.insert(sysDictType));
    }

    @PreAuthorize("hasAuthority('system:dict:edit')")
    @PutMapping
    public AjaxResult edit(@Validated(EditGroup.class) @RequestBody SysDictType sysDictType) {
        return AjaxResult.toAjax(sysDictTypeService.update(sysDictType));
    }

    @PreAuthorize("hasAuthority('system:dict:remove')")
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable List<String> ids) {
        return AjaxResult.toAjax(sysDictTypeService.deleteByIds(ids));
    }

    @PreAuthorize("hasAuthority('system:dict:remove')")
    @DeleteMapping("/refreshCache")
    public AjaxResult refreshCache() {
        sysDictDataService.resetDictCache();
        return AjaxResult.success();
    }

    /**
     * 获取字典选择框列表
     */
    @GetMapping("/option-select")
    public AjaxResult optionSelect() {
        List<SysDictType> dictTypes = sysDictTypeService.selectList(new SysDictTypeParam());
        return AjaxResult.success(dictTypes);
    }
}
