/*
 * Decompiled with CFR 0.152.
 * 
 * Could not load the following classes:
 *  com.google.gson.Gson
 *  org.apache.commons.lang.StringUtils
 *  org.apache.http.HttpEntity
 *  org.apache.http.client.ClientProtocolException
 *  org.apache.http.client.entity.UrlEncodedFormEntity
 *  org.apache.http.client.methods.CloseableHttpResponse
 *  org.apache.http.client.methods.HttpPost
 *  org.apache.http.client.methods.HttpUriRequest
 *  org.apache.http.impl.client.CloseableHttpClient
 *  org.apache.http.impl.client.HttpClients
 *  org.apache.http.message.BasicNameValuePair
 *  org.apache.http.util.EntityUtils
 *  org.springframework.beans.factory.annotation.Autowired
 *  org.springframework.stereotype.Service
 *  org.springframework.transaction.annotation.Propagation
 *  org.springframework.transaction.annotation.Transactional
 */
package com.hst.taxService.service.imp;

import com.google.gson.Gson;
import com.hst.common.service.imp.BaseBusinessServiceImp;
import com.hst.framework.constants.WebConstants;
import com.hst.framework.util.HstDateUtil;
import com.hst.taxService.dao.TaxWxGzzListAppDao;
import com.hst.taxService.domain.GzzListBackDomain;
import com.hst.taxService.domain.GzzXxDomain;
import com.hst.taxService.domain.WxZhgxLogDomain;
import com.hst.taxService.domain.WxZhxxDomain;
import com.hst.taxService.service.TaxWxGzzListAppService;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import org.apache.commons.lang.StringUtils;
import org.apache.http.HttpEntity;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

@Service
public class TaxWxGzzListAppServiceImp
extends BaseBusinessServiceImp
implements TaxWxGzzListAppService {
    @Autowired
    private TaxWxGzzListAppDao dao;

    @Override
    @Transactional(readOnly=false, propagation=Propagation.REQUIRED)
    public void doSaveWxYhxx(WxZhxxDomain zhxxDom) throws Exception {
        GzzListBackDomain returnDom = this.doYhxxCl(zhxxDom.getAppid(), zhxxDom.getAppsecret(), "");
        if ("0".equals(returnDom.getBackCode())) {
            this.dao.saveWxYhxx(zhxxDom.getAppid());
        }
    }

    @Override
    public List<WxZhxxDomain> queryZhxxList() throws Exception {
        return this.dao.queryWxZhxxList();
    }

    @Override
    public GzzListBackDomain doYhxxCl(String appid, String arrsecret, String nextOpenid) throws Exception {
        String kssj = HstDateUtil.toDateString(new Date(), "yyyy-MM-dd HH:mm:ss");
        WxZhgxLogDomain logDom = new WxZhgxLogDomain();
        String jssj = "";
        GzzListBackDomain backDom = this.sendMess(appid, arrsecret, nextOpenid);
        if (backDom == null) {
            return new GzzListBackDomain();
        }
        if (!backDom.getBackCode().equals("0") || backDom.getBean().getCount().equals("0")) {
            jssj = HstDateUtil.toDateString(new Date(), "yyyy-MM-dd HH:mm:ss");
            logDom.setAppid(appid);
            logDom.setBackcode(backDom.getBackCode());
            logDom.setErrormess(backDom.getErrorMess());
            if (backDom.getBean() != null && backDom.getBean().getCount().equals("0")) {
                logDom.setTotal(backDom.getBean().getTotal());
                logDom.setBc_count(backDom.getBean().getCount());
            }
            logDom.setKssj(kssj);
            logDom.setJssj(jssj);
            this.dao.saveWxZhgxLog(logDom);
            return backDom;
        }
        this.dao.saveLsbWxYhxx(backDom.getBean().getData(), appid);
        jssj = HstDateUtil.toDateString(new Date(), "yyyy-MM-dd HH:mm:ss");
        logDom.setAppid(appid);
        logDom.setBackcode(backDom.getBackCode());
        logDom.setBc_count(backDom.getBean().getCount());
        logDom.setErrormess(backDom.getErrorMess());
        logDom.setKssj(kssj);
        logDom.setJssj(jssj);
        logDom.setNext_openid(backDom.getBean().getNext_openid());
        logDom.setTotal(backDom.getBean().getTotal());
        this.dao.saveWxZhgxLog(logDom);
        if (backDom.getBean().getTotal().equals(backDom.getBean().getCount())) {
            return backDom;
        }
        if (StringUtils.isNotBlank((String)backDom.getBean().getNext_openid())) {
            this.doYhxxCl(appid, arrsecret, backDom.getBean().getNext_openid());
        }
        return backDom;
    }

    public GzzListBackDomain sendMess(String openId, String appSecrect, String nextOpenId) {
        String url = WebConstants.WX_GZZLIST_URL;
        HttpPost httppost = new HttpPost(url);
        String strResult = "";
        CloseableHttpClient httpClient = HttpClients.createDefault();
        GzzListBackDomain dom = new GzzListBackDomain();
        try {
            httppost.addHeader("Content-type", "application/x-www-form-urlencoded;charset=gbk");
            ArrayList<BasicNameValuePair> par = new ArrayList<BasicNameValuePair>();
            par.add(new BasicNameValuePair("openId", openId));
            par.add(new BasicNameValuePair("appSecrect", appSecrect));
            par.add(new BasicNameValuePair("nextOpenId", nextOpenId));
            httppost.setEntity((HttpEntity)new UrlEncodedFormEntity(par));
            CloseableHttpResponse response = httpClient.execute((HttpUriRequest)httppost);
            if (response.getStatusLine().getStatusCode() != 200) {
                String err = String.valueOf(response.getStatusLine().getStatusCode());
                strResult = String.valueOf(strResult) + "\u8bf7\u6c42\u5931\u8d25\uff0c\u9519\u8bef\u4ee3\u7801\uff1a" + err;
                dom.setBackCode("-1");
                dom.setErrorMess(strResult);
                throw new RuntimeException(strResult);
            }
            strResult = EntityUtils.toString((HttpEntity)response.getEntity());
            HashMap<String, Class<GzzXxDomain>> classMap = new HashMap<String, Class<GzzXxDomain>>();
            classMap.put("data", GzzXxDomain.class);
            Gson gson = new Gson();
            dom = (GzzListBackDomain)gson.fromJson(strResult, GzzListBackDomain.class);
        }
        catch (ClientProtocolException e) {
            dom.setBackCode("-1");
            dom.setErrorMess(e.toString());
            _logger.error((Object)e.toString());
        }
        catch (IOException e) {
            dom.setBackCode("-1");
            dom.setErrorMess(e.toString());
            _logger.error((Object)e.toString());
        }
        return dom;
    }

    @Override
    public void doWxYhdygxCl() throws Exception {
        this.dao.doWxYhdygxCl();
    }
}
