package com.adi.airopt.controller;

import com.adi.airopt.vo.AiroptUser;
import com.adi.common.core.controller.BaseController;
import com.adi.common.core.domain.AjaxResult;
import com.adi.common.core.page.PageDomain;
import com.adi.common.core.page.TableSupport;
import com.adi.common.core.redis.RedisCache;
import com.adi.config.AiroptHttpConfig;
import com.adi.util.HttpUtils;
import com.alibaba.fastjson2.JSONObject;
import com.mysql.cj.util.StringUtils;
import org.apache.http.HttpResponse;
import org.apache.http.util.EntityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.*;

/**
 *  ariopt 用户数据数据查询
 *
 * @author adi
 */
@RestController
@RequestMapping("/buss/user")
public class UserController extends BaseController {
    public final static String UserKeyPre = "USER_";
    /** 系统基础配置 */
    @Autowired
    private AiroptHttpConfig airConfig;
    @Autowired
    private RedisCache redisCache;
    /**
     * 获取在线用户
     */
    @RequestMapping("/online")
    public AjaxResult online() throws IOException

    {
        Collection<String> keys = redisCache.keys(airConfig.getUserKeyPre() + "*");
        Map<String, Object> result = new HashMap<>(2);
        result.put("count", keys.size());
        List<String> uids = new ArrayList<>();
        for (String key : keys)
        {
            String uid = key.replace(airConfig.getUserKeyPre(),"");
            uids.add(uid);
        }
        result.put("uids", uids);
        return AjaxResult.success(result);
    }
    /**
     * 获取用户信息
     */
    @RequestMapping("/detail/{uid}")
    public AjaxResult userdetil(@PathVariable String uid) throws IOException
    {
        String host =airConfig.getUrl();
        Map<String, String> bodys =new HashMap<>();
        bodys.put("channelNo",airConfig.getChannelNo());
        bodys.put("clientToken",airConfig.getClientToken());
        bodys.put("transCode","B00031");
        bodys.put("userId",airConfig.getUserId());
        bodys.put("uid",uid);
        try {
            HttpResponse response= HttpUtils.doPost(host,bodys);
            int stat = response.getStatusLine().getStatusCode();
            if (stat != 200) {
                return AjaxResult.error("业务服务链接失败");
            }
            String res = EntityUtils.toString(response.getEntity());
            JSONObject res_obj = JSONObject.parseObject(res);
            return AjaxResult.success(res_obj);
        } catch (Exception e) {
            e.printStackTrace();
           return AjaxResult.error("业务服务链接失败");
        }

    }

    /**
     * 获取用户信息
     */
    @RequestMapping("/list")
    public AjaxResult userlist(@RequestParam String searchtag) throws IOException
    {
        PageDomain pageDomain = TableSupport.buildPageRequest();
        Integer pageNum = pageDomain.getPageNum();
        Integer pageSize = pageDomain.getPageSize();
        String host =airConfig.getUrl();
        Map<String, String> bodys =new HashMap<>();
        bodys.put("channelNo",airConfig.getChannelNo());
        bodys.put("clientToken",airConfig.getClientToken());
        bodys.put("transCode","B00030");
        bodys.put("userId",airConfig.getUserId());
        if(!StringUtils.isNullOrEmpty(searchtag)){
            bodys.put("searchtag",searchtag);
        }

        bodys.put("count", pageSize.toString());
        bodys.put("page",pageNum.toString());
        try {
            HttpResponse response= HttpUtils.doPost(host,bodys);
            int stat = response.getStatusLine().getStatusCode();
            if (stat != 200) {
                return AjaxResult.error("业务服务链接失败");
            }
            String res = EntityUtils.toString(response.getEntity());
            JSONObject res_obj = JSONObject.parseObject(res);
            return AjaxResult.success(res_obj);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("业务服务链接失败");
        }

    }
    /**
     * 重置密码
     */
    @RequestMapping("repwd/{uid}")
    public AjaxResult repwd(@PathVariable String uid) throws IOException
    {
        String host =airConfig.getUrl();
        Map<String, String> bodys =new HashMap<>();
        bodys.put("channelNo",airConfig.getChannelNo());
        bodys.put("clientToken",airConfig.getClientToken());
        bodys.put("transCode","B00032");
        bodys.put("userId",airConfig.getUserId());
        bodys.put("uid",uid);
        try {
            HttpResponse response= HttpUtils.doPost(host,bodys);
            int stat = response.getStatusLine().getStatusCode();
            if (stat != 200) {
                return AjaxResult.error("业务服务链接失败");
            }
            String res = EntityUtils.toString(response.getEntity());
            JSONObject res_obj = JSONObject.parseObject(res);
            return AjaxResult.success(res_obj);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("业务服务链接失败");
        }

    }
    /**
     * 用户信息修改
     */
    @RequestMapping("update")
    public AjaxResult update(@Validated @RequestBody AiroptUser user) throws IOException
    {
        String host =airConfig.getUrl();
        Map<String, String> bodys =new HashMap<>();
        bodys.put("channelNo",airConfig.getChannelNo());
        bodys.put("clientToken",airConfig.getClientToken());
        bodys.put("transCode","B00033");
        bodys.put("userId",airConfig.getUserId());
        bodys.put("uid",user.getUid());
        bodys.put("nickName",user.getNickname());
        bodys.put("username",user.getUsername());
        bodys.put("mobileNo",user.getMobileNo());
        bodys.put("headProfile",user.getHeadProfile());
        bodys.put("company",user.getCompany());
        bodys.put("email",user.getEmail());
        try {
            HttpResponse response= HttpUtils.doPost(host,bodys);
            int stat = response.getStatusLine().getStatusCode();
            if (stat != 200) {
                return AjaxResult.error("业务服务链接失败");
            }
            String res = EntityUtils.toString(response.getEntity());
            JSONObject res_obj = JSONObject.parseObject(res);
            return AjaxResult.success(res_obj);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("业务服务链接失败");
        }

    }
}


