/*
 * Decompiled with CFR 0.152.
 * 
 * Could not load the following classes:
 *  org.apache.commons.logging.Log
 *  org.apache.commons.logging.LogFactory
 *  org.springframework.beans.factory.annotation.Autowired
 *  org.springframework.stereotype.Service
 */
package com.hst.task.service;

import com.hst.common.util.Base64;
import com.hst.common.util.HstWxServiceUtil;
import com.hst.framework.constants.WebConstants;
import com.hst.framework.service.BaseService;
import com.hst.task.dao.NsfwSjscDaoImp;
import com.hst.task.domain.NsfwJhGt3StatusDto;
import com.hst.task.domain.ZhdtCkryImageDto;
import com.hst.taxService.domain.smz.SmzRxsfzBdAppDomain;
import com.hst.taxService.domain.smz.SmzRxsfzBdReturnDomain;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URL;
import java.net.URLConnection;
import java.util.List;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class NsfwSjscService
implements BaseService {
    public static Log _logger = LogFactory.getLog(NsfwSjscService.class);
    @Autowired
    private NsfwSjscDaoImp dao;

    public void doCkryImageDownload() {
        try {
            List<ZhdtCkryImageDto> taskList = this.dao.queryCkryImgDownloadList();
            for (ZhdtCkryImageDto dto : taskList) {
                String cgbz = "N";
                String mess = "";
                String fileName = "";
                try {
                    try {
                        int len;
                        URL url = new URL(dto.getImage());
                        URLConnection con = url.openConnection();
                        con.setConnectTimeout(5000);
                        InputStream is = con.getInputStream();
                        byte[] bs = new byte[1024];
                        int index1 = dto.getImage().lastIndexOf("/");
                        fileName = dto.getImage().substring(index1 + 1);
                        String filePath = String.valueOf(WebConstants.CKRY_IMG_PATH) + dto.getBsdtdjxh() + "/";
                        File dir = new File(filePath);
                        if (!dir.exists()) {
                            dir.mkdirs();
                        }
                        FileOutputStream os = new FileOutputStream(String.valueOf(filePath) + "/" + fileName);
                        while ((len = is.read(bs)) != -1) {
                            ((OutputStream)os).write(bs, 0, len);
                        }
                        ((OutputStream)os).close();
                        is.close();
                        cgbz = "Y";
                    }
                    catch (IOException e) {
                        mess = e.getMessage();
                        _logger.error((Object)("DownLoadCutPhotoService.download error:" + e.getMessage()));
                        dto.setTbbz(cgbz);
                        dto.setMess(mess);
                        dto.setImgname(fileName);
                        this.dao.updateCkryImgZt(dto);
                        continue;
                    }
                }
                catch (Throwable throwable) {
                    dto.setTbbz(cgbz);
                    dto.setMess(mess);
                    dto.setImgname(fileName);
                    this.dao.updateCkryImgZt(dto);
                    throw throwable;
                }
                dto.setTbbz(cgbz);
                dto.setMess(mess);
                dto.setImgname(fileName);
                this.dao.updateCkryImgZt(dto);
            }
        }
        catch (Exception e) {
            _logger.error((Object)"\u4e0b\u8f7d\u7a97\u53e3\u4eba\u5458\u51fa\u9519", (Throwable)e);
        }
    }

    public void doCheckedGt3() {
        try {
            List<NsfwJhGt3StatusDto> taskList = this.dao.queryGt3CheckList();
            for (NsfwJhGt3StatusDto dto : taskList) {
                SmzRxsfzBdAppDomain appDomain = new SmzRxsfzBdAppDomain();
                appDomain.setBsdt_djxh(dto.getBsdtdjxh());
                appDomain.setXb("1");
                appDomain.setXm("XMTEST");
                appDomain.setCsrq("1991-01-01");
                appDomain.setYxq_q("2011-01-01");
                appDomain.setYxq_z("2029-01-01");
                String path = WebConstants.HST_PROP_MAP.get("hst.GT3_PNG");
                String imgae1 = Base64.byteToString(NsfwSjscService.getBytesFromFile(path)).replaceAll("\r\n", "");
                appDomain.setHeadPic(imgae1);
                appDomain.setSfzzmPic(imgae1);
                String code = "";
                String mess = "";
                try {
                    try {
                        SmzRxsfzBdReturnDomain returnDomain = HstWxServiceUtil.sendMess(dto.getApiUrl(), appDomain, "smz.rxsfzBd", SmzRxsfzBdReturnDomain.class);
                        code = returnDomain.getCode();
                        mess = returnDomain.getMess();
                    }
                    catch (Exception e) {
                        code = "99";
                        mess = e.getMessage();
                        if (mess.length() > 2000) {
                            mess = mess.substring(0, 2000);
                        }
                        dto.setRtnCode(code);
                        dto.setRtnMess(mess);
                        this.dao.updateNSfwJhGt3Status(dto);
                        continue;
                    }
                }
                catch (Throwable throwable) {
                    dto.setRtnCode(code);
                    dto.setRtnMess(mess);
                    this.dao.updateNSfwJhGt3Status(dto);
                    throw throwable;
                }
                dto.setRtnCode(code);
                dto.setRtnMess(mess);
                this.dao.updateNSfwJhGt3Status(dto);
            }
        }
        catch (Exception e) {
            _logger.error((Object)"\u6821\u9a8c\u91d1\u4e09\u63a5\u53e3\u8054\u901a\u51fa\u9519", (Throwable)e);
        }
    }

    public static byte[] getBytesFromFile(String filePath) {
        File f = new File(filePath);
        if (f.isDirectory()) {
            return null;
        }
        try {
            int n;
            FileInputStream stream = new FileInputStream(f);
            ByteArrayOutputStream out = new ByteArrayOutputStream(1024);
            byte[] b = new byte[1024];
            while ((n = stream.read(b)) != -1) {
                out.write(b, 0, n);
            }
            stream.close();
            out.close();
            return out.toByteArray();
        }
        catch (IOException iOException) {
            return null;
        }
    }
}
