package cn.iocoder.yudao.module.jx.controller.admin;

import cn.hutool.core.util.ObjectUtil;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.security.core.LoginUser;
import cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils;
import cn.iocoder.yudao.module.jx.domain.DriveSchoolCoach;
import cn.iocoder.yudao.module.jx.domain.DriveSchoolInfo;
import cn.iocoder.yudao.module.jx.mapper.DriveSchoolCoachMapper;
import cn.iocoder.yudao.module.jx.service.IDriveSchoolInfoService;
import cn.iocoder.yudao.module.system.api.user.AdminUserApi;
import cn.iocoder.yudao.module.system.api.user.dto.AdminUserRespDTO;
import cn.iocoder.yudao.module.system.dal.dataobject.user.AdminUserDO;
import cn.iocoder.yudao.module.system.service.permission.PermissionService;
import cn.iocoder.yudao.module.system.service.user.AdminUserService;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

@Tag(name = "管理后台 - 认证")
@RestController
@RequestMapping("/jx/auth")
@Validated
@Slf4j
public class JxAuthController {


    @Resource
    private AdminUserService userService;
    @Resource
    private PermissionService permissionService;
    @Resource
    private DriveSchoolCoachMapper driveSchoolCoachMapper;
    @Resource
    private IDriveSchoolInfoService driveSchoolInfoService;
    @Resource
    private AdminUserApi userApi;




    /**
     * 获取Jx用户信息
     *
     * @return 用户信息
     */
    @GetMapping("/getJxInfo")
    public CommonResult getJxInfo()
    {
        // 获取当前用户信息
//        Long userId = SecurityFrameworkUtils.getLoginUserId();
//        AdminUserRespDTO sysUser = userApi.getUser(userId);
//        Long deptId = sysUser.getDeptId();

        LoginUser user = SecurityFrameworkUtils.getLoginUser();
        // 获取当前登录用户角色编码
        Set<String> roles = permissionService.getRolePermission(user.getId());
        //获取驾校的
        Long userId = user.getId();
        AdminUserDO sysUser = userService.getUser(userId);
        Long deptId = sysUser.getDeptId();

        String phonenumber = sysUser.getMobile();
        //根据手机号查询驾校教练
        DriveSchoolCoach driveSchoolCoach = driveSchoolCoachMapper.selectByPhonenumber(phonenumber);

        if (ObjectUtils.isNotEmpty(driveSchoolCoach)){
            sysUser.setAvatar(driveSchoolCoach.getImage());
        }
        DriveSchoolInfo driveSchoolInfo = driveSchoolInfoService.getSchoolInfoByDeptId(deptId);

        Map<String,Object> result = new HashMap<>();
        result.put("user", sysUser);
        result.put("roles", roles);
        result.put("schoolInfo", driveSchoolInfo);

        return CommonResult.success(result);
    }

    /**
     * 获取App用户信息
     *
     * @return 用户信息
     */
    @GetMapping("/getAppInfo")
    public CommonResult getAppInfo()
    {
        LoginUser user = SecurityFrameworkUtils.getLoginUser();
        // 获取当前登录用户角色编码
        Set<String> roles = permissionService.getRolePermission(user.getId());
        Map<String,Object> result = new HashMap<>();
        result.put("user", user);
        result.put("role", roles);
//        ShopConfig shopConfig = configService.selectShopConfigById(1L);
//        if (StringUtils.isEmpty(shopConfig.getOpenRz())||shopConfig.getOpenRz().equals("0")){
//            ajax.put("openRz", "0");
//        }else {
//            ajax.put("openRz", "1");
//        }
        return success(result);
    }

}
