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

import com.cpop.common.utils.bean.BeanUtils;
import com.cpop.core.base.R;
import com.cpop.core.base.entity.LoginUser;
import com.cpop.core.base.exception.ServiceException;
import com.cpop.core.utils.SecurityUtils;
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 lombok.extern.java.Log;
import org.springframework.security.access.prepost.PreAuthorize;
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 io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

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

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

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

    @Autowired
    private DictTypeService dictTypeService;

    /**
     * 获取系统字典类型分页
     *
     * @param dictName 字典名
     * @return R<Page < DictTypePageVo>>
     * @author DB
     * @since 2023/12/01
     */
    @PreAuthorize("@aps.hasPermission('system:dict:list')")
    @GetMapping("/getDictTypePage")
    @ApiOperation("获取系统字典类型分页")
    public R<Page<DictTypePageVo>> getDictTypePage(@ApiParam(value = "字典名") 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}")
    @ApiOperation("根据主键获取字典类型详情")
    public R<DictTypeVo> getDictTypeInfo(@PathVariable @ApiParam("字典类型主键") 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")
    @ApiOperation("新增字典类型")
    public R<Void> insertDictType(@Validated @RequestBody DictTypeBo bo) {
        if (!dictTypeService.checkDictTypeUnique(bo)) {
            throw new ServiceException("新增字典'" + bo.getDictName() + "'失败，字典类型已存在");
        }
        LoginUser loginUser = SecurityUtils.getInstance().getLoginUser();
        DictType dictType = BeanUtils.mapToClass(bo, DictType.class);
        dictType.setUserType(loginUser.getUserType().toString());
        dictTypeService.save(dictType);
        return R.ok();
    }

    /**
     * 修改字典类型
     *
     * @param bo 请求
     * @return R<Void>
     * @author DB
     * @since 2023/12/01
     */
    @PutMapping("/updateDictType")
    @ApiOperation("修改字典类型")
    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}")
    @ApiOperation("根据主键删除字典类型")
    public R<Void> removeById(@PathVariable @ApiParam("字典类型主键") String id) {
        dictTypeService.removeDataAndCache(id);
        return R.ok();
    }

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

    /**
     * 获取字典选择框列表
     * @author DB
     * @since 2023/12/01
     * @return R<List<DictTypeVo>>
     */
    @ApiOperation("获取字典选择框列表")
    @GetMapping("/optionSelect")
    public R<List<DictTypeVo>> optionSelect() {
        LoginUser loginUser = SecurityUtils.getInstance().getLoginUser();
        List<DictTypeVo> dictTypes = dictTypeService.queryChain()
                .where(DICT_TYPE.USER_TYPE.eq(loginUser.getUserType().toString()))
                .listAs(DictTypeVo.class);
        return R.ok(dictTypes);
    }

}
