package com.zfire.mall.manager.logic.external;

import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSONObject;
import com.zfire.mall.manager.exception.RemoteServiceException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * 第三方系统/外部系统对接
 *  工程机系统服务
 */
@Slf4j
@Service
public class ExternalEnginLogic {

//    @Value("${engin.user.check.url}")
    private String enginUserCheckUrl;
//    @Value("${engin.user.login.url}")
    private String enginUserLoginUrl;

    /**
     * 检查工程机帐号密码是否正确
     * @param userName
     * @param password
     * @return
     */
    public Integer checkEnginUser(String userName, String password){
        Map<String,Object> map = new HashMap<>();
        map.put("username",userName);
        map.put("password",password);
        log.info("【工程机系统帐号验证】request:"+JSONObject.toJSONString(map));
        String post = HttpUtil.post(enginUserCheckUrl, map);
        JSONObject result = JSONObject.parseObject(post);
        log.info("【工程机系统帐号验证】response:"+result);
        if(!result.get("code").toString().equals("200")){
            throw new RemoteServiceException(4444,"工程机系统认证失败");
        }
        return result.getInteger("data");
    }


    public String getLoginUrl(){
        return this.enginUserLoginUrl;
    }

}
