package com.zehong.web.controller.task;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.zehong.common.core.domain.entity.SysUser;
import com.zehong.common.core.redis.RedisCache;
import com.zehong.common.exception.CustomException;
import com.zehong.common.utils.GovernmentDataCopyUtil;
import com.zehong.common.utils.StringUtils;
import com.zehong.common.utils.http.HttpClientUtils;
import com.zehong.system.domain.TProAppInforBrowse;
import com.zehong.system.domain.TUserLocation;
import com.zehong.system.domain.vo.ProAppInforBrowseVo;
import com.zehong.system.service.ISysUserService;
import com.zehong.system.service.ITUserLocationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

@Component("timeTasks")
public class TimeTasksController {
    @Value("${wjyhq.apiUrl}")
    private String apiUrl;
    @Autowired
    private RedisCache redisCache;

    @Autowired
    ITUserLocationService tUserLocationService;
    /**
     * 获取token
     * @return token
     * @throws Exception
     */
    public String getToken()throws Exception{
        String token = redisCache.getCacheObject("yhqToken");
        if(StringUtils.isEmpty(token)){
            token = getNewToken();
        }
        return token;
    }

    /**
     * 刷新token
     * @return token
     * @throws Exception
     */
    public String getNewToken()throws Exception{
        JSONObject param = new JSONObject();
        param.put("authcode","LPG-HSRQ-101807423011");
        param.put("accessKey","3D4B933297C6CA68E2FD9CEB3697FC78");
        param.put("secretKey","pu8s3Z8TsPi09fCFpOFunWqbi9TDtXTq");
        String result = HttpClientUtils.doPost(apiUrl+"/SyncDataApi/CreateToken",param.toJSONString(),null);
        if(StringUtils.isEmpty(result)) throw new CustomException("获取液化气token接口失败!");
        JSONObject resultJson = JSONObject.parseObject(result);
        redisCache.setCacheObject("yhqToken", resultJson.getJSONObject("result").getString("token"),30, TimeUnit.DAYS);
        return  resultJson.getJSONObject("result").getString("token");
    }

    /**
     * 获取钢瓶档案
     * @return token
     * @throws Exception
     */
    public void getArchivesList()throws Exception{
        //String token = getToken();
        JSONObject param = new JSONObject();
        param.put("maxReturnNum","100");
        param.put("lastUpTime","2000-03-12 00:00:00");
        Map<String,Object> head = new HashMap<>();
        head.put("authorization","token");
        String result = HttpClientUtils.doPost(apiUrl+"/SyncDataApi/ArchivesList",param.toJSONString(),head);
        if(StringUtils.isEmpty(result)) throw new CustomException("获取钢瓶档案接口失败!");
        JSONObject json = JSONObject.parseObject(result);
        //结果入库
        if(null != json && "0".equals(json.getString("resultCode")) && null != json.getJSONArray("data")){
            JSONArray data = json.getJSONArray("data");
            List<TProAppInforBrowse> proAppInforBrowseList = new ArrayList<>();
            for(int i = 0; i < data.size(); i++){
                ProAppInforBrowseVo proAppInforBrowseVo = JSONObject.toJavaObject(data.getJSONObject(i), ProAppInforBrowseVo.class);
                TProAppInforBrowse tProAppInforBrowse = new TProAppInforBrowse();
                GovernmentDataCopyUtil.copyToLocalData(proAppInforBrowseVo,tProAppInforBrowse);
                proAppInforBrowseList.add(tProAppInforBrowse);
            }
            //tProAppInforBrowseMapper.batchInsertTProAppInforBrowse(proAppInforBrowseList);
        }
    }
}
