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

import cn.dev33.satoken.stp.StpUtil;
import com.cpop.core.base.entity.R;
import com.cpop.core.base.enums.UserType;
import com.cpop.core.base.exception.ServiceException;
import com.cpop.core.utils.bean.BeanUtils;
import com.cpop.system.business.bo.DictTypeBo;
import com.cpop.system.business.vo.DictTypePageVo;
import com.cpop.system.business.vo.DictTypeVo;
import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryWrapper;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
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.DictType;
import com.cpop.system.business.service.DictTypeService;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

import static com.cpop.system.business.entity.table.DictTypeTableDef.DICT_TYPE;

/**
 * 系统-字典类型表 控制层。
 *
 * @author DB
 * @since 2023-12-01
 */
@RestController
@Tag(name = "字典类型接口")
@RequestMapping("/backstage/dictType")
public class BackstageDictTypeController {

    @Autowired
    private DictTypeService dictTypeService;

    /**
     * 获取字典类型分页
     *
     * @param dictName 字典名
     * @return R<Page<DictTypePageVo>>
     * @author DB
     * @since 2023/12/01
     */
    @GetMapping("/getDictTypePage")
    @Operation(summary = "获取字典类型分页")
    public R<Page<DictTypePageVo>> getDictTypePage(@Parameter(description = "字典名") String dictName) {
        Page<DictTypePageVo> page = dictTypeService.getDictTypePage(dictName);
        return R.ok(page);
    }

    /**
     * 根据主键获取字典类型详情
     *
     * @param id 字典类型主键
     * @return R<DictTypeVo>
     * @author DB
     * @since 2023/12/01
     */
    @GetMapping("/getDictTypeInfo/{id}")
    @Operation(summary = "根据主键获取字典类型详情")
    public R<DictTypeVo> getDictTypeInfo(@PathVariable @Parameter(description = "字典类型主键") String id) {
        DictTypeVo dictType = dictTypeService.getOneAs(QueryWrapper.create().where(DICT_TYPE.ID.eq(id)), DictTypeVo.class);
        return R.ok(dictType);
    }

    /**
     * 新增字典类型
     *
     * @param bo 请求参数
     * @return R<Void>
     * @author DB
     * @since 2023/12/01
     */
    @PostMapping("/insertDictType")
    @Operation(summary = "新增字典类型")
    public R<Void> insertDictType(@Validated @RequestBody DictTypeBo bo) {
        if (!dictTypeService.checkDictTypeUnique(bo)) {
            throw new ServiceException("新增字典'" + bo.getDictName() + "'失败，字典类型已存在");
        }
        DictType dictType = BeanUtils.mapToClass(bo, DictType.class);
        dictType.setUserType((UserType) StpUtil.getSession().get("userType"));
        dictTypeService.save(dictType);
        return R.ok();
    }

    /**
     * 修改字典类型
     *
     * @param bo 请求
     * @return R<Void>
     * @author DB
     * @since 2023/12/01
     */
    @PutMapping("/updateDictType")
    @Operation(summary = "修改字典类型")
    public R<Void> updateDictType(@Validated @RequestBody DictTypeBo bo) {
        if (!dictTypeService.checkDictTypeUnique(bo)) {
            throw new ServiceException("修改字典'" + bo.getDictName() + "'失败，字典类型已存在");
        }
        dictTypeService.updateById(BeanUtils.mapToClass(bo, DictType.class));
        return R.ok();
    }

    /**
     * 根据主键删除字典类型
     *
     * @param id 主键
     * @return R<Void>
     * @author DB
     * @since 2023/12/01
     */
    @DeleteMapping("/removeById/{id}")
    @Operation(summary = "根据主键删除字典类型")
    public R<Void> removeById(@PathVariable @Parameter(description = "字典类型主键") String id) {
        dictTypeService.removeDataAndCache(id);
        return R.ok();
    }

    /**
     * 刷新字典缓存
     *
     * @return R<Void>
     * @author DB
     * @since 2023/12/01
     */
    @Operation(summary = "刷新字典缓存")
    @PutMapping("/refreshCache")
    public R<Void> refreshCache() {
        dictTypeService.resetDictCache();
        return R.ok();
    }

    /**
     * 获取字典选择框列表
     * @author DB
     * @since 2023/12/01
     * @return R<List<DictTypeVo>>
     */
    @Operation(summary = "获取字典选择框列表")
    @GetMapping("/optionSelect")
    public R<List<DictTypeVo>> optionSelect() {
        List<DictTypeVo> dictTypes = dictTypeService.queryChain()
                .where(DICT_TYPE.USER_TYPE.eq(StpUtil.getSession().get("userType")))
                .listAs(DictTypeVo.class);
        return R.ok(dictTypes);
    }

}
