package com.cpop.system.business.controller.backstage;

import com.cpop.common.utils.bean.BeanUtils;
import com.cpop.core.base.R;
import com.cpop.core.utils.SpringUtils;
import com.cpop.system.business.bo.DictDataBo;
import com.cpop.system.business.bo.DictDataPageBo;
import com.cpop.system.business.entity.DictType;
import com.cpop.system.business.service.DictTypeService;
import com.cpop.system.business.vo.DictDataListVo;
import com.cpop.system.business.vo.DictDataPageVo;
import com.cpop.system.business.vo.DictDataVo;
import com.mybatisflex.core.paginate.Page;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.beans.factory.annotation.Autowired;
import com.cpop.system.business.entity.DictData;
import com.cpop.system.business.service.DictDataService;
import org.springframework.web.bind.annotation.RestController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

import java.io.Serializable;
import java.util.List;

/**
 * 系统-字典数据表 控制层。
 *
 * @author DB
 * @since 2023-12-01
 */
@RestController
@Api(tags = "系统-字典数据表接口")
@RequestMapping("/backstage/dictData")
public class BackstageDictDataController {

    @Autowired
    private DictDataService dictDataService;

    /**
     * 获取字典数据分页
     * @author DB
     * @since 2023/12/01
     * @param bo 请求参数
     * @return R<Page<DictData>>
     */
    @GetMapping("/getDictDataPage")
    @ApiOperation("获取字典数据分页")
    public R<Page<DictDataPageVo>> getDictDataPage(@ApiParam("查询参数") DictDataPageBo bo) {
        Page<DictDataPageVo> page = dictDataService.getDictDataPage(bo);
        return R.ok(page);
    }

    /**
     * 根据主键获取字典数据详情
     * @author DB
     * @since 2023/12/01
     * @param id 字典数据主键
     * @return DictData
     */
    @GetMapping("/getDictDataInfo/{id}")
    @ApiOperation("根据主键获取字典数据详情")
    public R<DictDataVo> getDictDataInfo(@PathVariable @ApiParam("字典数据code") String id) {
        DictDataVo vo = dictDataService.getDictDataInfo(id);
        return R.ok(vo);
    }

    /**
     * 根据字典类型获取字典数据
     * @author DB
     * @since 2023/12/01
     * @param dictType 字典类型
     * @return R<DictDataVo>
     */
    @GetMapping("/getDictDataByDictType/{dictType}")
    @ApiOperation("根据字典类型获取字典数据")
    public R<List<DictDataListVo>> getDictDataByDictType(@PathVariable @ApiParam("字典数据code") String dictType) {
        List<DictData> dataList = dictDataService.getDictDataByDictType(dictType);
        return R.ok(BeanUtils.mapToList(dataList, DictDataListVo.class));
    }

    /**
     * 新增字典数据
     * @author DB
     * @since 2023/12/01
     * @param bo 请求参数
     * @return R<Void>
     */
    @PostMapping("/insertDictData")
    @ApiOperation("新增字典数据")
    public R<Void> insertDictData(@RequestBody @ApiParam("字典数据") @Validated DictDataBo bo) {
        dictDataService.insertDictData(bo);
        return R.ok();
    }

    /**
     * 修改字典数据
     * @author DB
     * @since 2023/12/01
     * @param bo 请求
     * @return R<Void>
     */
    @PutMapping("/updateDictData")
    @ApiOperation("修改字典数据")
    public R<Void> updateDictData(@RequestBody @ApiParam("字典数据") @Validated DictDataBo bo) {
        dictDataService.updateDictData(bo);
        return R.ok();
    }

    /**
     * 删除字典数据
     * @author DB
     * @since 2023/12/01
     * @param id 主键
     * @return R<Void>
     */
    @DeleteMapping("/removeDictData/{id}")
    @ApiOperation("删除字典数据")
    public R<Void> removeDictData(@PathVariable String id) {
        dictDataService.removeDictData(id);
        return R.ok();
    }

}
