package shop.alien.store.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import io.swagger.annotations.*;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import shop.alien.entity.result.R;
import shop.alien.entity.store.StoreDictionary;
import shop.alien.entity.store.vo.SystemConfigVo;
import shop.alien.store.service.StoreDictService;

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

/**
 * 字典Controller
 *
 * @author ssk
 * @version 1.0
 * @date 2025/2/8 10:21
 */
@Api(tags = {"二期-字典"})
@Slf4j
@CrossOrigin
@RestController
@RequestMapping("/dict")
@RequiredArgsConstructor
public class StoreDictController {

    private final StoreDictService storeDictService;

    @ApiOperation("获取字典")
    @ApiOperationSupport(order = 1)
    @ApiImplicitParams({@ApiImplicitParam(name = "dictType", value = "字典类型", dataType = "String", paramType = "query", required = true)})
    @GetMapping("/getDict")
    public R<List<StoreDictionary>> getDict(String dictType) {
        return R.data(storeDictService.getDict(dictType));
    }

    @ApiOperation("新增字典")
    @ApiOperationSupport(order = 2)
    @GetMapping("/addDict")
    public R<Boolean> addDict(StoreDictionary storeDictionary) {
        return R.data(storeDictService.addDict(storeDictionary));
    }

    @ApiOperation("删除字典")
    @ApiOperationSupport(order = 3)
    @ApiImplicitParams({@ApiImplicitParam(name = "id", value = "字典主键", dataType = "Integer", paramType = "query", required = true)})
    @GetMapping("/deleteDict")
    public R<Boolean> deleteDict(Integer id) {
        return R.data(storeDictService.deleteDict(id));
    }

    @ApiOperation("获取一级数据")
    @GetMapping(value = "getPrimaryData")
    private R<List<StoreDictionary>> getPrimaryData() {
        List<StoreDictionary> storeDictionaries = storeDictService.list(
                new LambdaQueryWrapper<StoreDictionary>().isNull(StoreDictionary::getParentId));
        return R.data(storeDictionaries);
    }

    @ApiOperation("根据上级id获取下级数据")
    @GetMapping(value = "getChildByParentId")
    private R<List<StoreDictionary>> getChildByParentId(@RequestParam(value = "parentId", required = false) String parentId) {
        List<StoreDictionary> storeDictionaries = storeDictService.list(
                new LambdaQueryWrapper<StoreDictionary>().eq(StoreDictionary::getParentId, parentId));
        return R.data(storeDictionaries);
    }

    @ApiOperation("web端新增数据")
    @PostMapping(value = "addInformation")
    private R addInformation(@RequestBody StoreDictionary storeDictionary) {
        storeDictionary.setCreatedTime(new Date());
        storeDictionary.setUpdatedTime(new Date());
        storeDictService.save(storeDictionary);
        return R.success("新增成功");
    }

    @ApiOperation("web端编辑数据")
    @PutMapping(value = "editInformation")
    private R editInformation(@RequestBody StoreDictionary storeDictionary) {
        storeDictionary.setUpdatedTime(new Date());
        storeDictService.updateById(storeDictionary);
        return R.success("编辑成功");
    }

    @ApiOperation("web端删除数据")
    @DeleteMapping(value = "deleteInformation")
    private R deleteInformation(@RequestParam(value = "id") String id) {
        storeDictService.removeById(id);
        return R.success("删除成功");
    }

    @ApiOperation("获取字典集合")
    @ApiOperationSupport(order = 1)
    @GetMapping("/getDictList")
    public R<Map<String, List<StoreDictionary>>> getDictList() {
        return R.data(storeDictService.getDictList());
    }

    @ApiOperation("获取配置")
    @ApiOperationSupport(order = 1)
    @ApiImplicitParams({@ApiImplicitParam(name = "dictType", value = "字典类型", dataType = "String", paramType = "query", required = true)})
    @GetMapping("/getConfig")
    public R<SystemConfigVo> getConfig(String dictType) {
        return R.data(storeDictService.getConfig(dictType));
    }

    @ApiOperation("修改配置")
    @PutMapping(value = "editConfig")
    private R editConfig(@RequestBody SystemConfigVo systemConfigVo) {
        storeDictService.editConfig(systemConfigVo);
        return R.success("成功");
    }
}
