package com.zhongshu.card.server.core.controller.dictionary;

import com.github.microservice.net.ResultContent;
import com.zhongshu.card.client.model.dictionary.*;
import com.zhongshu.card.client.model.pay.PeriodQueryModel;
import com.zhongshu.card.client.model.pay.RechargeParam;
import com.zhongshu.card.client.type.DictionaryUpdateStatus;
import com.zhongshu.card.server.core.service.dictionary.DictionaryService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.data.web.PageableDefault;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

@RestController
@RequestMapping("dictionary")
@Tag(name = "标签字典", description = "标签字典")
public class DictionaryController {

    @Autowired
    private DictionaryService dictionaryService;

    @Operation(summary = "添加字典", description = "添加字典")
    @RequestMapping(value = "addDictionary", method = RequestMethod.POST)
    public Object addDictionary(@Valid @RequestBody AddDictionaryParam param){
        return dictionaryService.addDictionary(param);
    }

    @Operation(summary = "修改字典", description = "修改字典")
    @RequestMapping(value = "updateDictionary", method = RequestMethod.POST)
    public ResultContent updateDictionary(@Valid @RequestBody UpdateDictionaryParam param){
        return dictionaryService.updateDictionary(param);
    }

    @Operation(summary = "删除字典", description = "修改字典")
    @RequestMapping(value = "deleteDictionary", method = RequestMethod.GET)
    public ResultContent deleteDictionary(@RequestParam("id") String id){
        return dictionaryService.deleteDictionary(id);
    }

    @Operation(summary = "分页查询字典", description = "分页查询字典")
    @RequestMapping(value = "pageDictionary", method = RequestMethod.POST)
    public Object page(@Parameter(hidden = true) @PageableDefault(page = 0, size = 10)Pageable pageable,
                       @Parameter(required = true) DictionaryQueryParam param){
        return dictionaryService.page(pageable, param);
    }

    @Operation(summary = "添加项", description = "添加项")
    @RequestMapping(value = "addItem", method = RequestMethod.POST)
    public ResultContent addItem(@RequestBody AddDictionaryItemParam param){
        return dictionaryService.addItem(param);
    }

    @Operation(summary = "修改项", description = "修改项")
    @RequestMapping(value = "updateItem", method = RequestMethod.POST)
    public ResultContent updateItem(@RequestBody AddDictionaryItemParam param){
        return dictionaryService.updateItem(param);
    }

    @Operation(summary = "删除项", description = "删除项")
    @RequestMapping(value = "deleteItem", method = RequestMethod.GET)
    public ResultContent deleteItem(@RequestParam("id") String id){
        return dictionaryService.deleteItem(id);
    }

    @Operation(summary = "分页查询项", description = "分页查询项")
    @RequestMapping(value = "pageItem", method = RequestMethod.POST)
    public Object pageItem(@Parameter(hidden = true) @PageableDefault(page = 0, size = 10)Pageable pageable,
                           @Parameter(required = true) DictionaryItemQueryParam param){
        return dictionaryService.pageItem(pageable, param);
    }

    @Operation(summary = "根据编码获取字典", description = "根据编码获取字典")
    @RequestMapping(value = "listDictionary", method = RequestMethod.POST)
    public ResultContent listDictionary(@RequestBody ListDictionaryParam param){
        return dictionaryService.listDictionary(param);
    }

    @Operation(summary = "根据编码获取项", description = "根据编码获取字典")
    @RequestMapping(value = "listItem", method = RequestMethod.POST)
    public ResultContent listItem(@RequestBody ListDictionaryParam param){
        return dictionaryService.listItem(param);
    }

    @Operation(summary = "查询字典所有项（树形）", description = "查询字典所有项（树形）")
    @RequestMapping(value = "deepTreeItem", method = RequestMethod.POST)
    public Object deepTreeItem(@RequestBody DeepTreeItemQueryParam param){
        return dictionaryService.deepTreeItem(param.getProjectId(), param.getDictionaryKey(), param.getDictionaryId());
    }

    @Operation(summary = "检查更新", description = "检查更新")
    @RequestMapping(value = "updateCheck", method = RequestMethod.GET)
    public ResultContent<DictionaryUpdateStatus> updateCheck(@RequestParam("projectId") String projectId){
        return dictionaryService.updateCheck(projectId);
    }

    @Operation(summary = "更新标签字典初始化数据", description = "更新标签字典初始化数据")
    @RequestMapping(value = "updateInit", method = RequestMethod.GET)
    public ResultContent updateInit(@RequestParam("projectId") String projectId,
                                    @RequestParam("coverName") boolean coverName,
                                    @RequestParam("coverCustomize") boolean coverCustomize){
        return dictionaryService.updateInit(projectId, coverName, coverCustomize);
    }
}
