package org.nl.system.controller.dict;

import com.alibaba.fastjson.JSONObject;
import io.swagger.annotations.ApiOperation;
import org.nl.common.base.TableDataInfo;
import org.nl.common.domain.query.PageQuery;
import org.nl.common.logging.annotation.Log;
import org.nl.system.service.dict.ISysDictService;
import org.nl.system.service.dict.dao.Dict;
import org.nl.system.service.dict.dto.DictQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * <p>
 * 字典表 前端控制器
 * </p>
 *
 * @author generator
 * @since 2022-12-14
 */
@RestController
@RequestMapping("/api/dict")
public class SysDictController {

    @Autowired
    private ISysDictService dictService;

    @Log("查询字典")
    @GetMapping
    @ApiOperation("查询字典")
//    @SaCheckPermission("dict:list")
    public ResponseEntity<Object> query(@RequestParam Map whereJson, PageQuery pageable){
        return new ResponseEntity<>(TableDataInfo.build(dictService.queryAll(whereJson,pageable)), HttpStatus.OK);
    }

    @ApiOperation("查询所有字典信息")
    @GetMapping(value = "/all")
//    @SaCheckPermission("dict:list")
    public ResponseEntity<Object> queryAll(){
        return new ResponseEntity<>(dictService.queryAll(),HttpStatus.OK);
    }

    @Log("新增字典")
    @PostMapping
    @ApiOperation("新增字典")
//    @SaCheckPermission("dict:add")
    public ResponseEntity<Object> create(@RequestBody Dict dict){
        dictService.create(dict);
        return new ResponseEntity<>(HttpStatus.CREATED);
    }

    @PutMapping
    @Log("修改字典")
    @ApiOperation("修改字典")
    //@SaCheckPermission("@el.check('dict:edit')")
    public ResponseEntity<Object> updateDict(@Validated @RequestBody Dict dto){
        dictService.updateDict(dto);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @Log("删除字典")
    @ApiOperation("删除字典")
    @DeleteMapping
//    @SaCheckPermission("dict:del")
    public ResponseEntity<Object> delete(@RequestBody Set<String> ids){
        dictService.deleteBatchByIds(ids);
        return new ResponseEntity<>(HttpStatus.OK);
    }

    @GetMapping("/dictDetail")
    @ApiOperation("查询字典详情")
    public ResponseEntity<Object> queryDetails(@RequestParam Map criteria, PageQuery pageable){
        DictQuery dictQuery = JSONObject.parseObject(JSONObject.toJSONString(criteria), DictQuery.class);
        return new ResponseEntity<>(TableDataInfo.build(dictService.queryAllDetail(dictQuery,pageable)),HttpStatus.OK);
    }

    @ApiOperation("查询多个字典详情")
    @GetMapping(value = "/dictDetail/map")
    public ResponseEntity<Object> getDictDetailMaps(@RequestParam String dictName){
        String[] names = dictName.split("[,，]");
        Map<String, List<Dict>> dictMap = new HashMap<>(16);
        for (String name : names) {
            dictMap.put(name, dictService.getDictByName(name));
        }
        return new ResponseEntity<>(dictMap, HttpStatus.OK);
    }

    @Log("新增字典详情")
    @ApiOperation("新增字典详情")
    @PostMapping("/dictDetail")
//    @SaCheckPermission("dict:add")
    public ResponseEntity<Object> createDetail(@RequestBody Dict resources){
        dictService.createDetail(resources);
        return new ResponseEntity<>(HttpStatus.CREATED);
    }

    @Log("修改字典详情")
    @ApiOperation("修改字典详情")
    @PutMapping("/dictDetail")
//    @SaCheckPermission("dict:edit")
    public ResponseEntity<Object> updateDetail(@RequestBody Dict resources){
        dictService.updateDetail(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @Log("删除字典详情")
    @ApiOperation("删除字典详情")
    @DeleteMapping(value = "/dictDetail/{id}")
//    @SaCheckPermission("dict:del")
    public ResponseEntity<Object> deleteDetail(@PathVariable String id){
        dictService.deleteDetail(id);
        return new ResponseEntity<>(HttpStatus.OK);
    }

}

