package com.org.system.controller;

import cn.hutool.core.lang.Dict;
import com.org.system.annotation.Log;
import com.org.system.config.validation.Update;
import com.org.system.entity.SysDataDictionaryItem;
import com.org.system.enums.BusinessType;
import com.org.system.service.SysDataDictionaryItemService;
import com.org.utils.AjaxResult;
import com.org.utils.StringUtils;
import com.org.utils.page.TableDataInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@RestController
@RequestMapping("sys/dictionary/item")
public class SysDataDictionaryItemController extends BaseController {

    @Autowired
    private SysDataDictionaryItemService sysDataDictionaryItemService;

    // 新增
    @PostMapping("/add")
    @PreAuthorize("@ss.hasPermi('sys:dictionary:item:edit')")
    @Log(title = "词典项新增", businessType = BusinessType.INSERT)
    public AjaxResult add(@RequestBody @Validated SysDataDictionaryItem sysDataDictionaryItem){
        log.info("SysDataDictionaryItemController - add sysDataDictionaryItem:{}", sysDataDictionaryItem);

        return toAjax(sysDataDictionaryItemService.saveOrUpdate(sysDataDictionaryItem));
    }

    // 详情
    @PostMapping("/get")
    @PreAuthorize("@ss.hasPermi('sys:dictionary:item:get')")
    @Log(title = "词典项详情", businessType = BusinessType.UPDATE)
    public AjaxResult get(@RequestParam Long id){
        log.info("SysDataDictionaryItemController - get id:{}", id);

        return AjaxResult.success(sysDataDictionaryItemService.getById(id));
    }

    // 修改
    @PostMapping("/edit")
    @PreAuthorize("@ss.hasPermi('sys:dictionary:item:edit')")
    @Log(title = "词典项修改", businessType = BusinessType.UPDATE)
    public AjaxResult edit(@RequestBody @Validated(Update.class) SysDataDictionaryItem sysDataDictionaryItem){
        log.info("SysDataDictionaryItemController - edit sysDataDictionaryItem:{}", sysDataDictionaryItem);

        return toAjax(sysDataDictionaryItemService.saveOrUpdate(sysDataDictionaryItem));
    }

    // 删除
    @PostMapping("/del")
    @PreAuthorize("@ss.hasPermi('sys:dictionary:item:del')")
    @Log(title = "词典项删除", businessType = BusinessType.DELETE)
    public AjaxResult del(@RequestParam String ids, @RequestParam String deleteReason){
        log.info("SysDataDictionaryItemController - del ids:{}", ids);
        log.info("SysDataDictionaryItemController - del deleteReason:{}", deleteReason);

        if(StringUtils.isEmpty(ids)) return AjaxResult.success();

        List<Long> idList = Arrays.asList(ids.split(",")).stream().map(idStr -> Long.parseLong(idStr)).collect(Collectors.toList());

        return toAjax(sysDataDictionaryItemService.del(idList, deleteReason));
    }

    // 列表
    @GetMapping("/pageList")
    @PreAuthorize("@ss.hasPermi('sys:dictionary:item:list')")
    @Log(title = "词典项列表", businessType = BusinessType.SELECT)
    public TableDataInfo pageList(SysDataDictionaryItem sysDataDictionaryItem){
        log.info("SysDataDictionaryItemController - pageList sysDataDictionaryItem:{}", sysDataDictionaryItem);

        startPage();
        List<SysDataDictionaryItem> list = sysDataDictionaryItemService.findList(sysDataDictionaryItem);

        return getDataTable(list);
    }

    // 列表
    @GetMapping("/list")
    @PreAuthorize("@ss.hasPermi('sys:dictionary:item:list')")
    @Log(title = "词典项列表", businessType = BusinessType.SELECT)
    public AjaxResult list(SysDataDictionaryItem sysDataDictionaryItem){
        log.info("SysDataDictionaryItemController - list sysDataDictionaryItem:{}", sysDataDictionaryItem);

        List<SysDataDictionaryItem> list = sysDataDictionaryItemService.findList(sysDataDictionaryItem);

        return AjaxResult.success(list);
    }

    // 查询最大词典项值
    @GetMapping("/findMaxValue")
    @PreAuthorize("@ss.hasPermi('sys:dictionary:item:list')")
    @Log(title = "查询最大词典项值", businessType = BusinessType.SELECT)
    public AjaxResult findMaxValue(SysDataDictionaryItem sysDataDictionaryItem){
        log.info("SysDataDictionaryItemController - findMaxValue sysDataDictionaryItem:{}", sysDataDictionaryItem);

        if(StringUtils.isEmpty(sysDataDictionaryItem.getDictionaryCode())) return AjaxResult.error("词典项标识不能为空");

        Integer maxValue = sysDataDictionaryItemService.findMaxValueByCode(sysDataDictionaryItem.getDictionaryCode());
        log.info("SysDataDictionaryItemController - findMaxValue sysDataDictionaryItem:{},maxValue:{}", sysDataDictionaryItem, maxValue);

        return AjaxResult.success(maxValue);
    }

    // 词典项下拉选
    @GetMapping("/dropDown")
    @Log(title = "词典项下拉选", businessType = BusinessType.SELECT)
    public AjaxResult dropDown(String dictionaryCode){
        log.info("SysDataDictionaryItemController - dropDown dictionaryCode:{}", dictionaryCode);

        List<Dict> list = sysDataDictionaryItemService.listByCode(dictionaryCode);

        return AjaxResult.success(list);
    }

    // 词典项下拉选
    @GetMapping("/mobile/dropDown")
    @Log(title = "词典项下拉选", businessType = BusinessType.SELECT)
    public AjaxResult dropDownNoToken(String dictionaryCode){
        log.info("SysDataDictionaryItemController - dropDown dictionaryCode:{}", dictionaryCode);

        List<Dict> list = sysDataDictionaryItemService.listByCode(dictionaryCode);

        return AjaxResult.success(list);
    }

}
