package com.ruoyi.system.service.impl;

import com.ruoyi.common.config.Global;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.system.mapper.AppSysMapper;
import com.ruoyi.system.mapper.InfoMapper;
import com.ruoyi.system.service.IAppSysService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import static com.ruoyi.common.utils.http.HttpUtils.sendGet;

@Service
public class AppSysServiceImpl implements IAppSysService {
    @Autowired
    private AppSysMapper appSysMapper;

    @Override
    public AjaxResult login(String cardNo,String password) {
        String IDCardNo = cardNo;
        String pwd =password;
        AjaxResult result = new AjaxResult();
        List<Map<String,String>> list = appSysMapper.login(IDCardNo, pwd);
        List<Map<String,String>> czrList = appSysMapper.czrxx(IDCardNo);
        if(list.size()>0 ) {

            if(!this.CheckRz(cardNo,list.get(0).get("RenName"))){
                return AjaxResult.warn("请先在应用商店中下载\"登记注册身份验证\"APP 进行认证后方可使用");
            }
            for (int i = 0; i < list.size(); i++) {
                if (list.get(i).get("pwd").equals(pwd)) {
                   /* Map map = appSysMapper.czrlogin(IDCardNo, list.get(i).get("BsID"));
                    if(map!=null){
                        list.get(i).put("zhiwu",list.get(i).get("zhiwu")+",股东（出资人）");
                    }*/
                    list.get(i).remove("pwd");
                } else {
                    return AjaxResult.warn("密码错误");
                }
            }
            if(czrList.size()>0){
                for (int i = 0; i < czrList.size(); i++) {
                    czrList.get(i).remove("pwd");
                    list.add(czrList.get(i));
                }
            }
            return AjaxResult.success("success",list);
        }else{
            if(czrList.size()==0) {
               return AjaxResult.warn("未找到信息");
            }else {
                if(!this.CheckRz(cardNo,czrList.get(0).get("RenName"))){
                    return AjaxResult.warn("请先在应用商店中下载\"登记注册身份验证\"APP 进行认证后方可使用");
                }
                for (int i = 0; i < czrList.size(); i++) {
                    if (czrList.get(i).get("pwd").equals(pwd)) {
                        czrList.get(i).remove("pwd");
                    }
                }
                return AjaxResult.success("success",czrList);
            }
        }
    }


    private static Pattern pattern = Pattern.compile("<td( .*?)?>.*?</td>");
    private static  Pattern pattern1 = Pattern.compile("<font.*?>(.*?)</font>");
    private  Boolean CheckRz(String card, String name ){
        String url= Global.getConfig("ruoyi.gsdz")+":8005/gtdj/regApp.do";
        String param="method=validPopMultiIdCardCheck&appNo=&etpsNm=***&etpsType=9500&appTypeId=02&regOrgan=130107&validHasSpecial=&branchEtpsName=***&agentName=&agentCertNo=&contactName="+name+"&contactCertNo="+card+"&ignoreReason=";
        //  String url = "http://192.168.1.5/rz.html";
        String html=sendGet(url,param);
        List<String> ren=new ArrayList<String>();
        Matcher matcher = pattern.matcher(html);
        String html1="";
        while (matcher.find()) {
            html1=html1+matcher.group();
        }
        Matcher matcher1 = pattern1.matcher(html1);
        while (matcher1.find()) {
            ren.add(matcher1.group(1).trim());
            System.out.println("HTML111:"+matcher1.group(1));
        }
        if("成功".equals(ren.get(5))){
            return true;
        }else {
            return  false;
        }

    }
}
