package com.hzs.system.sys.controller.manage;

import com.hzs.common.core.enums.ESystemType;
import com.hzs.common.core.enums.EYesNo;
import com.hzs.common.core.web.domain.AjaxResult;
import com.hzs.common.core.web.domain.EnumEntity;
import com.hzs.common.security.service.UserTokenService;
import com.hzs.system.sys.dto.LoginUser;
import com.hzs.system.sys.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * @Description: 系统类型控制器
 * @Author: jiang chao
 * @Time: 2024/12/9 15:48
 * @Classname: SystemTypeController
 * @PackageName: com.hzs.system.sys.controller.manage
 */
@RestController
@RequestMapping("/manage/system-type")
public class SystemTypeController {

    @Autowired
    private ISysUserService iSysUserService;
    @Autowired
    private UserTokenService userTokenService;

    /**
     * 获取用户系统类型列表
     *
     * @return
     */
    @GetMapping("/list")
    public AjaxResult list() {
        List<EnumEntity> list = new ArrayList<>();
        for (ESystemType value : ESystemType.values()) {
            if (EYesNo.YES.getIntValue() == value.getEnable()) {
                list.add(new EnumEntity(value.getValue(), value.getLabel()));
            }
        }

        return AjaxResult.success(list);
    }


    @GetMapping("/all-list")
    public AjaxResult sysTypeAllList() {
        List<EnumEntity> list = new ArrayList<>();
        for (ESystemType value : ESystemType.values()) {
            list.add(new EnumEntity(value.getValue(), value.getLabel()));
        }
        return AjaxResult.success(list);
    }

    /**
     * 修改用户系统类型
     *
     * @param dataType 系统类型
     * @return
     */
    @PostMapping("/change/{dataType}")
    public AjaxResult change(@PathVariable("dataType") Integer dataType) {
        LoginUser loginUser = userTokenService.getLoginUser();
        loginUser.setSystemType(dataType);

        iSysUserService.updateLastSystemType(loginUser.getUserId(), dataType);

        // 修改地区数据权限，重新刷新缓存token内容
        userTokenService.refreshToken(loginUser);
        return AjaxResult.success();
    }

}
