package com.ruoyi.web.controller.tool;

import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.Dangan;
import com.ruoyi.system.domain.SysUserOnline;
import com.ruoyi.system.domain.UserRelate;
import com.ruoyi.system.service.IDanganService;
import com.ruoyi.system.service.ISysUserOnlineService;
import com.ruoyi.system.service.IUserRelateService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
@Api("用户信息管理")
@RestController
@RequestMapping("/api/advalidateciphertext")
public class ActionApi {


    @Autowired
    private IUserRelateService userRelateService;
    @Autowired
    private ISysUserOnlineService onlineService;

    @Autowired
    private IDanganService danganService;


    /**
     * JSESSIONID=abbf57c4-eb7e-4ce8-9556-061a1cb9c016
     *
     *    bbccjiacbda6d0-770d-47fe-9343-576616f5ebed
     * @return
     */
    @ApiOperation("子系统调AD")
    @PostMapping()
    public AjaxResult userList(@RequestBody ValidateMessage validateMessage)
    {


        if (!StringUtils.isNotEmpty(validateMessage.getClientKey())) {
            return AjaxResult.error("请求失败!");

        }
        if (!StringUtils.isNotEmpty(validateMessage.getCid())) {
            return AjaxResult.error("请校验应用id!");
        }
//        List<UserEntity> userList = new ArrayList<UserEntity>(users.values());


        if (StringUtils.isEmpty(validateMessage.getAuthCode())) {


            return AjaxResult.error("授权码不能为空!");


        }

        SysUserOnline sysUserOnline = onlineService.selectOnlineById( validateMessage.getReqid());

        if (StringUtils.isNull(sysUserOnline)) {

            return AjaxResult.error("授权已过期!");
        }
        String authCode = validateMessage.getAuthCode();
        Dangan dangan = danganService.selectDanganByDanganName(validateMessage.getCid());
        String keyId = dangan.getKeyId();
        String auth= keyId+validateMessage.getReqid();

        auth=new Md5Hash(auth).toHex();
        if (!auth.equals(authCode)) {
            return AjaxResult.error("授权码错误!");
        }



        // bbccji7920f9df-baa7-4e9c-9e68-72c79454589a





        String loginName = sysUserOnline.getLoginName();
        UserRelate userRelate = new UserRelate();
        userRelate.setLoginName(loginName);
        userRelate.setDanganNums(validateMessage.getCid());

        List<UserRelate> userRelates = userRelateService.selectUserRelateList(userRelate);

        if (!StringUtils.isNotEmpty(userRelates)) {
            return AjaxResult.error("授权已过期!");
        }

        UserRelate userRelate1 = userRelates.get(0);
        //把用户名返回去

        //生成校验码
        String str= keyId+validateMessage.getClientKey();
        str=new Md5Hash(str).toHex();

        AjaxResult ajaxResult=AjaxResult.success();
        ajaxResult.put("loginName",userRelate1.getDanganLoginName());
        ajaxResult.put("rspid",str);

        return ajaxResult;
    }





}
