package yem.wmc.imc.utils;

import com.alibaba.fastjson.JSONObject;
import kd.bos.dataentity.OperateOption;
import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.entity.operate.result.OperationResult;
import kd.bos.servicehelper.BusinessDataServiceHelper;
import kd.bos.servicehelper.operation.OperationServiceHelper;

import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @author 金亮
 * @date 2024/1/17 10:01
 * @description 全电自主登录工具类
 * @versions 1.0
 */
public class AllElectricLoginUtils {

    /**
     * 同步接口-全电同步自主登录
     * @param loginType 0（短信验证码） ;  1（税务APP扫码登录）
     * @return
     */
    public static JSONObject allElectricLogIn(Integer loginType) {
        JSONObject errorJson = new JSONObject();
        if (loginType != 0 && loginType != 1) {
            errorJson.put("code", "500");
            errorJson.put("message", "参数范围超出，登录类型参数仅设置0（短信验证码）和1（税务APP扫码登录）");
        }
        final int businessType = 2;  //进项
        Map<String, String> headMap = new HashMap<>();//请求头Map
        headMap.put("UserAgent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/107.0.0.0 Safari/537.36 Edg/107.0.1418.24");
        headMap.put("ContentType", "application/json;charset=UTF-8");
        String Token = CommonUtils.getToken();
        headMap.put("X-Access-Token", Token);
        String url = "https://huisuiyun.com/api/v2/agent/cdk/allElectric/autonomous/sync/login";
        JSONObject paramJson = new JSONObject(); //参数
        paramJson.put("businessType", businessType);
        paramJson.put("loginType", loginType);
        String param = String.valueOf(paramJson);

        String TokenStr = null;
        try {
            TokenStr = HttpUtils.doPostWithHeader(url, param, headMap);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        JSONObject returnJson = JSONObject.parseObject(TokenStr);
        return returnJson;
    }

    /**
     * 同步接口-全电同步自主登录提交短信验证码
     * @param sid
     * @param smsCode 短信验证码
     * @return
     */
    public static JSONObject allElectricSyncSubmitSms( Long sid,String smsCode) {
        Map<String, String> headMap = new HashMap<>();//请求头Map
        headMap.put("UserAgent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/107.0.0.0 Safari/537.36 Edg/107.0.1418.24");
        headMap.put("ContentType", "application/json;charset=UTF-8");
        String Token = CommonUtils.getToken();
        headMap.put("X-Access-Token", Token);
        String url = "https://huisuiyun.com/api/v2/agent/cdk/allElectric/autonomous/sync/submitSms";
        JSONObject paramJson = new JSONObject(); //参数
//        sid = 933384077174636544L;
        paramJson.put("sid", sid);
        paramJson.put("smsCode", smsCode);
        String param = String.valueOf(paramJson);
        String TokenStr = null;
        try {
            TokenStr = HttpUtils.doPostWithHeader(url, param, headMap);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        JSONObject returnJson = JSONObject.parseObject(TokenStr);
            return returnJson;
    }



    /**
     * 同步接口-全电同步自主登录获取二维码扫码结果
     * @param sid
     * @return
     */
   public static JSONObject allElectricSyncGetQrCodeStatus(Long sid) {
       Map<String, String> headMap = new HashMap<>();//请求头Map
       headMap.put("UserAgent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/107.0.0.0 Safari/537.36 Edg/107.0.1418.24");
       headMap.put("ContentType", "application/json;charset=UTF-8");
       String Token = CommonUtils.getToken();
       headMap.put("X-Access-Token", Token);
       String url = "https://huisuiyun.com/api/v2/agent/cdk/allElectric/autonomous/sync/getQrCodeStatus";
       JSONObject paramJson = new JSONObject(); //参数
       paramJson.put("sid", sid);
       String param = String.valueOf(paramJson);
       String TokenStr = null;
       try {
           TokenStr = HttpUtils.doPostWithHeader(url, param, headMap);
       } catch (IOException e) {
           throw new RuntimeException(e);
       }
       JSONObject returnJson = JSONObject.parseObject(TokenStr);
       return returnJson;

   }


    /**
     * 新增全电同步自主登录信息表（基础资料）
     * @return
     */
    public static Boolean newAllElectricLoginInfo(){
        String entityName = "yem_logininfo";
        DynamicObject bill = BusinessDataServiceHelper.newDynamicObject(entityName);
        bill.set("status","C"); //单据状态
        bill.set("yem_dutyparagraph","cs002"); //税号
        bill.set("yem_user", CommonUtils.getCurrentUser()); //人员
        bill.set("yem_lastlogintime", new Date()); //上次登录时间
        bill.set("enable", "1"); //使用状态
        // 保存
        OperationResult operationResult = OperationServiceHelper.executeOperate("save", entityName, new DynamicObject[]{bill},
                OperateOption.create());
        return operationResult.isSuccess();
    }
}
