package com.bocloud.ims.booter.controller.config;

import com.bocloud.cmp.model.DictionaryBean;
import com.bocloud.common.annotations.Log;
import com.bocloud.common.enums.BoCloudService;
import com.bocloud.common.model.BocloudResult;
import com.bocloud.common.utils.Common;
import com.bocloud.ims.entity.common.Dictionary;
import com.bocloud.ims.interfaces.config.DictService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 字典信息控制器
 *
 * @author dmw
 */
@RestController
@RequestMapping("/v1/dictionaries")
@Api(value = "字典信息管理", tags = "字典信息管理")
public class DictController {
    @Autowired
    private DictService dictService;

    @ApiOperation(tags = {"CMC", "CSC"}, value = "查询字典树列表",  httpMethod = "GET")
    @Log(module = BoCloudService.IMS, target = "系统字典", operation = "查询字典列表")
    @GetMapping
    public BocloudResult<List<Map<String, Object>>> list(@RequestParam(value = Common.VALUE, required = false) String value) {
        return dictService.list(value);
    }

    /**
     * 根据字典值获取子节点列表
     *
     * @param value
     * @return
     */
    @ApiOperation(tags = {"CMC", "CSC"}, value = "查询系统字典子信息",  httpMethod = "GET")
    @Log(module = BoCloudService.IMS, target = "系统字典", operation = "根据字典值获取子节点列表")
    @GetMapping("/children")
    public BocloudResult<List<Dictionary>> children(@RequestParam(value = Common.VALUE, required = false) String value) {
        return dictService.listChild(value);
    }

    /**
     * 查询字典树
     *
     * @param value
     * @return
     */
    @ApiOperation(tags = {"CMC", "CSC"}, value = "查询字典树",  httpMethod = "GET")
    @Log(module = BoCloudService.IMS, target = "系统字典", operation = "查询字典树")
    @GetMapping("/children/tree")
    public BocloudResult<List<Dictionary>> childTree(@RequestParam(value = Common.VALUE, required = false) String value) {
        return dictService.childTree(value);
    }

    /**
     * 创建
     *
     * @param dictionary
     * @return
     */
    @ApiOperation(tags = {"CMC", "CSC"}, value = "新增字典",  httpMethod = "POST")
    @Log(module = BoCloudService.IMS, target = "系统字典", operation = "创建字典")
    @PostMapping
    public BocloudResult<Dictionary> create(@RequestBody Dictionary dictionary) {
        return dictService.create(dictionary);
    }

    /**
     * 删除字典
     *
     * @param id
     * @return
     */
    @ApiOperation(tags = {"CMC", "CSC"}, value = "删除字典",  httpMethod = "DELETE")
    @Log(module = BoCloudService.IMS, target = "系统字典", operation = "删除字典")
    @DeleteMapping("/{id}")
    public BocloudResult remove(@PathVariable(value = Common.ID) Long id) {
        return dictService.remove(id);
    }

    /**
     * 修改字典
     *
     * @param id
     * @param bean
     * @return
     */
    @ApiOperation(tags = {"CMC", "CSC"}, value = "修改字典",  httpMethod = "PUT")
    @Log(module = BoCloudService.IMS, target = "系统字典", operation = "修改字典")
    @PutMapping("/{id}")
    public BocloudResult<Dictionary> modify(@PathVariable(value = Common.ID) Long id,
                                            @RequestBody DictionaryBean bean) {
        bean.setId(id);
        return dictService.modify(bean);
    }

}
