package com.gree.mall.manager.controller.common;


import cn.hutool.core.lang.TypeReference;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.gree.mall.manager.annotation.ApiNotAuth;
import com.gree.mall.manager.annotation.ZfireList;
import com.gree.mall.manager.bean.common.*;
import com.gree.mall.manager.enums.material.StateEnum;
import com.gree.mall.manager.helper.ResponseHelper;
import com.gree.mall.manager.logic.common.SysDictLogic;
import com.gree.mall.manager.plus.entity.SysDict;
import com.gree.mall.manager.plus.entity.SysDictType;
import com.gree.mall.manager.zfire.bean.ZfireParamBean;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.NotBlank;
import java.util.List;


@Validated
@RequiredArgsConstructor
@RestController
@Api(value = "字典业务配置", tags ={"字典业务配置"} )
@RequestMapping("dict")
public class OpenDictController {

    private final SysDictLogic sysDictLogic;

    @ZfireList
    @PostMapping("page")
    @ApiParam(value ="字典-分页")
    public ResponseHelper<IPage<SysDictVO>> sysDictList(@RequestBody ZfireParamBean zfireParamBean) {
        IPage<SysDictVO> sysDictIPage = sysDictLogic.sysDictIPage(zfireParamBean);
        return ResponseHelper.success(sysDictIPage, new TypeReference<SysDictVO>() {
        });
    }

    @ApiNotAuth
    @PostMapping("listDict")
    @ApiParam(value = "下拉框查询")
    public ResponseHelper<List<SysDict>> sysDictBeanList(
            @ApiParam(value = "类型", required = true) @RequestParam(required = true) String dictType,
            @ApiParam(value = "父字典类型", required = false) @RequestParam(required = false) String parentDictType,
            @ApiParam(value ="父code", required = false) @RequestParam(required = false) String parentDictCode,
            @ApiParam(value = "家用/商用", required = false) @RequestParam(required = false) String flag,
            @ApiParam(value = "服务单来源", required = false) @RequestParam(required = false) String source
    ) {

        List<SysDict> sysDicts = sysDictLogic.getSysDictList(dictType);
        return ResponseHelper.success(sysDicts);
    }
    @ApiNotAuth
    @PostMapping("type/list")
    @ApiParam(value = "字典类型")
    public ResponseHelper<List<SysDictTypeVO>> typeListAll() {
        return ResponseHelper.success(sysDictLogic.typeList(null));
    }



    @PostMapping("addType")
    @ApiParam(value = "字典类型-添加")
    public ResponseHelper addType(@Valid @RequestBody SysDictType param) {
        sysDictLogic.addType(param);
        return ResponseHelper.success();
    }

    @PostMapping("updateType")
    @ApiParam(value = "字典类型-修改")
    public ResponseHelper updateType(@Valid @RequestBody SysDictType param) {
        sysDictLogic.updateType(param);
        return ResponseHelper.success();
    }

    @PostMapping("delType")
    @ApiParam(value = "字典类型-删除")
    public ResponseHelper delType(
            @ApiParam(value  = "字典值id")
            @NotBlank(message = "字典值id不能为空")
            @RequestParam String id) {
        sysDictLogic.delType(id, true);
        return ResponseHelper.success();
    }

    @PostMapping("detailType")
    @ApiParam(value = "字典类型-详情")
    public ResponseHelper<SysDictType> detailType(
            @ApiParam(value = "字典值id")
            @NotBlank(message = "字典值id不能为空")
            @RequestParam String id) {
        SysDictType sysDict = sysDictLogic.detailType(id);
        return ResponseHelper.success(sysDict);
    }

    @PostMapping("typeList")
     @ApiParam(value = "字典类型")
    public ResponseHelper<List<SysDictTypeVO>> typeList() {
        return ResponseHelper.success(sysDictLogic.typeList(true));
    }

/*    @PostMapping("listDict")
     @ApiParam(value = "字典值列表")
    public ResponseHelper<List<SysDict>> dictList(
            @ApiParam(value = "字典类型", required = true)
            @NotBlank(message = "字典类型不能为空")

            @RequestParam String type) {
        List<SysDict> dictVOList = sysDictLogic.dictByType(type, true);
        return ResponseHelper.success(dictVOList);
    }*/

    @PostMapping("add")
     @ApiParam(value = "字典-添加")
    public ResponseHelper add(@Valid @RequestBody SysDict param) {
        sysDictLogic.saveOpenDict(param);
        return ResponseHelper.success();
    }

    @PostMapping("update")
     @ApiParam(value = "字典-修改")
    public ResponseHelper update(@Valid @RequestBody SysDict param) {
        sysDictLogic.updateOpenDict(param);
        return ResponseHelper.success();
    }

    @PostMapping("del")
     @ApiParam(value = "字典-删除")
    public ResponseHelper del(
            @ApiParam(value  = "字典值id")
            @NotBlank(message = "字典值id不能为空")
            @RequestParam String id) {
        sysDictLogic.delete(id, true);
        return ResponseHelper.success();
    }

    @PostMapping("detail")
     @ApiParam(value = "字典-详情")
    public ResponseHelper<SysDict> detail(
            @ApiParam(value = "字典值id")
            @NotBlank(message = "字典值id不能为空")
            @RequestParam String id) {
        SysDict sysDict = sysDictLogic.detail(id);
        return ResponseHelper.success(sysDict);
    }
}
