package com.yami.shop.sys.controller;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.yami.shop.bean.model.SysDictData;
import com.yami.shop.common.util.PageParam;
import com.yami.shop.common.util.R;
import com.yami.shop.service.ISysDictDataService;
import com.yami.shop.service.ISysDictTypeService;
import lombok.RequiredArgsConstructor;

import lombok.extern.java.Log;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 数据字典信息
 *
 * @author Lion Li
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/system/dict/data")
public class SysDictDataController {

    private final ISysDictDataService dictDataService;
    private final ISysDictTypeService dictTypeService;

    /**
     * 查询字典数据列表
     */

    @GetMapping("/list")
    public R<IPage<SysDictData>> list(SysDictData dictData, PageParam<SysDictData> pageQuery) {
        return R.SUCCESS(dictDataService.selectDictTypePage(dictData, pageQuery));
    }


    /**
     * 查询字典数据详细
     *
     * @param dictCode 字典code
     */
    @GetMapping(value = "/{dictCode}")
    public R<SysDictData> getInfo(@PathVariable Long dictCode) {
        return R.SUCCESS(dictDataService.selectDictDataById(dictCode));
    }

    /**
     * 根据字典类型查询字典数据信息
     *
     * @param dictType 字典类型
     */
    @GetMapping(value = "/type/{dictType}")
    public R<List<SysDictData>> dictType(@PathVariable String dictType) {
        List<SysDictData> data = dictTypeService.selectDictDataByType(dictType);
        if (ObjectUtil.isNull(data)) {
            data = new ArrayList<>();
        }
        return R.SUCCESS(data);
    }

    /**
     * 新增字典类型
     */
    @PostMapping
    public R<Void> add(@Validated @RequestBody SysDictData dict) {
        if (!dictDataService.checkDictDataUnique(dict)) {
            return R.FAIL("新增字典数据'" + dict.getDictValue() + "'失败，字典键值已存在");
        }
        dictDataService.insertDictData(dict);
        return R.SUCCESS();
    }

    /**
     * 修改保存字典类型
     */

    @PutMapping
    public R<Void> edit(@Validated @RequestBody SysDictData dict) {
        if (dict.getDictCode()==1L||dict.getDictCode()==2L||dict.getDictCode()==3L){
            return R.FAIL("修改字典数据人员属性失败，该条字典不允许修改");
        }
        if (!dictDataService.checkDictDataUnique(dict)) {
            return R.FAIL("修改字典数据'" + dict.getDictValue() + "'失败，字典键值已存在");
        }
        dictDataService.updateDictData(dict);
        return R.SUCCESS();
    }

    /**
     * 删除字典类型
     *
     * @param dictCodes 字典code串
     */
    @DeleteMapping("/{dictCodes}")
    public R<Void> remove(@PathVariable Long[] dictCodes) {
        if (dictCodes!=null
                && (Arrays.asList(dictCodes).contains(1L)
                ||Arrays.asList(dictCodes).contains(2L)
                ||Arrays.asList(dictCodes).contains(3L))){
            return R.FAIL("修改字典数据人员属性失败，该条字典不允许删除");
        }
        dictDataService.deleteDictDataByIds(dictCodes);
        return R.SUCCESS();
    }
}
