package com.tax.file.utils.token;

import com.tax.file.model.FileServer;
import com.tax.file.model.TkTaxUser;
import com.yxcheng.token.utils.HttpHelper;
import com.yxcheng.token.utils.MD5Util;
import com.yxcheng.token.utils.ResponseContent;
import org.apache.http.HttpException;

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

public class GetTokenFromTaxBureau extends AbstractTokenService {

    public static class SingletonHolder{
        private static GetTokenFromTaxBureau instance = new GetTokenFromTaxBureau();
    }

    public static  GetTokenFromTaxBureau getInstance(){
        return SingletonHolder.instance;
    }
    @Override
    protected String getResult(Map<String, Object> retMap) {
        String tokenCode = null;
        String retCode = String.valueOf(retMap.get("code"));

        if(retCode.equals("0") && null != retMap.get("token")) {
            tokenCode = String.valueOf(retMap.get("token"));
        } else if(retCode.equals("4")) {
            //还在获取中什么也不做
        } else {
            //tokenEt.setStatus("3");
        }
        return tokenCode;
    }

    @Override
    protected ResponseContent invoke(TkTaxUser tkTaxUser, FileServer fileServer) throws IOException, HttpException {
        Map<String, Object> paramsMap = new HashMap<String, Object>();
        String taxno = tkTaxUser.getTaxno();
        String dqbm = tkTaxUser.getZoneCode();
        String force = "true";
        paramsMap.put("sh", taxno);
        paramsMap.put("dqbm", dqbm);
        paramsMap.put("force", force);
        paramsMap.put("username", fileServer.getUsername());
        paramsMap.put("sign", MD5Util.MD5Encode(taxno + dqbm + force + fileServer.getUsername() + fileServer.getPassword()));
        return HttpHelper.postEntity(fileServer.getTokenUrl(), paramsMap);
    }
}
