package com.zjrs.ggfw.sggfw.bpo.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.yinhai.bcpcs.exceptions.ClientException;
import io.micrometer.core.instrument.util.StringUtils;
import org.mohrss.leaf.core.framework.domain.bpo.impl.BPOImpl;
import org.mohrss.leaf.core.framework.web.controller.AjaxResponse;
import com.zjrs.ggfw.sggfw.bpo.QueryBusinessBPO;
import com.zjrs.ggfw.sggfw.dto.GgcxDTO;
import com.zjrs.ggfw.sggfw.dto.TywtGrjbxxGgcx01DTO;
import com.zjrs.ggfw.sggfw.util.SggfwConfig;
import com.zjrs.ggfw.sggfw.util.SggfwInterface;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.*;

@Service
public class QueryBusinessBPOImpl extends BPOImpl implements QueryBusinessBPO {
    private static final Logger LOG = LoggerFactory.getLogger(QueryBusinessBPOImpl.class);
    @Autowired
    SggfwConfig config;
    @Override
    public AjaxResponse tywtGrjbxxGgcx01(TywtGrjbxxGgcx01DTO dto) throws ClientException, IOException {
        AjaxResponse ajaxResponse = new AjaxResponse();
        Map<String, Object> p = new HashMap<String, Object>();
        Map<String,String> map=new HashMap();
        if(StringUtils.isBlank(dto.getRylx())) {
            ajaxResponse.setAppcode("1");
            ajaxResponse.setMsg("人员类型不能为空");
            return ajaxResponse;
        }
        if("3".equals(dto.getRylx())){
            //调用居保接口
            p.put("businessID","UCA001");
            p.put("loginID",config.getLoginID());
            p.put("aac058","01");
            p.put("aac147", dto.getAac002());//身份证
            p.put("aac003", dto.getAac003());//姓名
            Map<Object,Object> m=SggfwInterface.qqjk(p,config,"NB_JMYL_GGYW_001");
            if(m.get("errorCode").equals("0")){
                map.put("sfcg","0");
                map.put("msg",m.get("message").toString());
                ajaxResponse.setData("data",map);
                return ajaxResponse;
            }else if(m.get("errorCode").equals("1")){
                List<Map<String,String>> list= (List<Map<String, String>>) m.get("resultList");
                Map<String,String> map1=list.get(0);
                String AAC084=map1.get("AAC084");
                if(AAC084.equals("0")){
                    map.put("sfcg","0");
                    map.put("msg","暂不符合待遇认证资格");
                }else if(AAC084.equals("1")){
                    map.put("sfcg","1");
                    map.put("msg","");
                }
                ajaxResponse.setData("data",map);
                return ajaxResponse;
            }
            ajaxResponse.setData("data",map);
            return ajaxResponse;
        }
        p.put("aac147", dto.getAac002());//身份证
        p.put("aac003", dto.getAac003());//姓名
        p.put("startrow",1);
        p.put("endrow",30);
        Map<Object,Object> m=SggfwInterface.qqjk(p,config,"TYWT_GRJBXX_GGCX_01");
        JSONObject jsonObject = JSONObject.parseObject(m.get("output").toString());
        String recordcount=jsonObject.get("recordcount").toString();
        jsonObject = JSONObject.parseObject(jsonObject.get("resultset").toString());
        LOG.info("领取待遇资格认证，个人基本信息查询，respond="+jsonObject);
        if(recordcount.equals("1")){
            jsonObject = JSONObject.parseObject(jsonObject.get("row").toString());
            if(jsonObject.get("aac084").equals("是")){
                map.put("sfcg","1");
                map.put("msg","");
                ajaxResponse.setData("data",map);
                return ajaxResponse;
            }else{
                map.put("sfcg","0");
                map.put("msg","暂不符合待遇认证资格");
            }
        }else if(recordcount.equals("0")){
            //不存在对应数据
            map.put("sfcg","0");
            map.put("msg","人员基本信息查询结果为空");
        }else{
            JSONArray data=jsonObject.getJSONArray("row");
            //循环
            for(int i=0;i<data.size();i++){
                jsonObject = JSONObject.parseObject(data.get(i).toString());
                if(jsonObject.get("aac084").equals("是")){
                    map.put("sfcg","1");
                    map.put("msg","");
                    ajaxResponse.setData("data",map);
                    return ajaxResponse;
                }else{
                    map.put("sfcg","0");
                    map.put("msg","暂不符合待遇认证资格");
                }
            }
        }
        ajaxResponse.setData("data",map);
        return ajaxResponse;
    }

    @Override
    public AjaxResponse tywtZgsbGgcx01(GgcxDTO dto) throws ClientException, IOException {
        AjaxResponse ajaxResponse = new AjaxResponse();
        Map<String, Object> p = new HashMap<String, Object>();
        if(StringUtils.isBlank(dto.getAaa027())) {
            dto.setAaa027("440800");
        }
        p.put("aaa027", dto.getAaa027());//地市
        p.put("aae140", "110");
        p.put("aac147", dto.getAac002());//身份证
        p.put("aac003", dto.getAac003());//姓名
        p.put("aae041",dto.getAae041());//起始年度
        p.put("aae042",dto.getAae042());//结束年度
        long startrow=(dto.getPagenum()-1)*dto.getPagesize()+1;
        if(startrow<1){
            startrow=1;
        }
        p.put("startrow",startrow);
        p.put("endrow",dto.getPagenum()*dto.getPagesize());
        Map<Object,Object> m=SggfwInterface.qqjk(p,config,"TYWT_ZGSB_GGCX_01");
        JSONObject jsonObject = JSONObject.parseObject(m.get("output").toString());
        String recordcount=jsonObject.get("recordcount").toString();
        if(recordcount.equals("1")||(!recordcount.equals("0")&&dto.getPagesize()==1)){
            jsonObject = JSONObject.parseObject(jsonObject.get("resultset").toString());
            jsonObject = JSONObject.parseObject(jsonObject.get("row").toString());
            JSONArray data = new JSONArray();
            data.add(jsonObject);
            ajaxResponse.setData("total",1);
            ajaxResponse.setData("data",data);
        }else if(!recordcount.equals("0")){
            jsonObject = JSONObject.parseObject(jsonObject.get("resultset").toString());
            JSONArray data = jsonObject.getJSONArray("row");
            ajaxResponse.setData("total",data.size());
            ajaxResponse.setData("data",data);
        }else{
			ajaxResponse.setData("total",0);
            ajaxResponse.setData("data",new JSONArray());
		}
        return ajaxResponse;
    }

    @Override
    public AjaxResponse tywtZgsbGgcx02(GgcxDTO dto) throws ClientException, IOException {
        AjaxResponse ajaxResponse = new AjaxResponse();
        Map<String, Object> p = new HashMap<String, Object>();
        if(StringUtils.isBlank(dto.getAaa027())) {
            dto.setAaa027("440800");
        }
        p.put("aaa027", dto.getAaa027());//地市
        p.put("aae140", "210");
        p.put("aac147", dto.getAac002());//身份证
        p.put("aac003", dto.getAac003());//姓名
        p.put("aae041",dto.getAae041());//起始年度
        p.put("aae042",dto.getAae042());//结束年度
        long startrow=(dto.getPagenum()-1)*dto.getPagesize()+1;
        if(startrow<1){
            startrow=1;
        }
        p.put("startrow",startrow);
        p.put("endrow",dto.getPagenum()*dto.getPagesize());
        Map<Object,Object> m=SggfwInterface.qqjk(p,config,"TYWT_ZGSB_GGCX_02");
        JSONObject jsonObject = JSONObject.parseObject(m.get("output").toString());
        String recordcount=jsonObject.get("recordcount").toString();
        if(recordcount.equals("1")||(!recordcount.equals("0")&&dto.getPagesize()==1)){
            jsonObject = JSONObject.parseObject(jsonObject.get("resultset").toString());
            jsonObject = JSONObject.parseObject(jsonObject.get("row").toString());
            JSONArray data = new JSONArray();
            data.add(jsonObject);
            ajaxResponse.setData("total",1);
            ajaxResponse.setData("data",data);
        }else if(!recordcount.equals("0")){
            jsonObject = JSONObject.parseObject(jsonObject.get("resultset").toString());
            JSONArray data = jsonObject.getJSONArray("row");
            ajaxResponse.setData("total",data.size());
            ajaxResponse.setData("data",data);
        }else{
			ajaxResponse.setData("total",0);
            ajaxResponse.setData("data",new JSONArray());
		}
        return ajaxResponse;
    }

    @Override
    public AjaxResponse tywtZgsbGgcx04(GgcxDTO dto) throws ClientException, IOException {
        AjaxResponse ajaxResponse = new AjaxResponse();
        Map<String, Object> p = new HashMap<String, Object>();
        if(StringUtils.isBlank(dto.getAaa027())) {
            dto.setAaa027("440800");
        }
        p.put("aaa027", dto.getAaa027());//地市
        p.put("aae140", "410");
        p.put("aac147", dto.getAac002());//身份证
        p.put("aac003", dto.getAac003());//姓名
        p.put("aae041",dto.getAae041());//起始年度
        p.put("aae042",dto.getAae042());//结束年度
        long startrow=(dto.getPagenum()-1)*dto.getPagesize()+1;
        if(startrow<1){
            startrow=1;
        }
        p.put("startrow",startrow);
        p.put("endrow",dto.getPagenum()*dto.getPagesize());
        Map<Object,Object> m=SggfwInterface.qqjk(p,config,"TYWT_ZGSB_GGCX_04");
        JSONObject jsonObject = JSONObject.parseObject(m.get("output").toString());
        String recordcount=jsonObject.get("recordcount").toString();
        if(recordcount.equals("1")||(!recordcount.equals("0")&&dto.getPagesize()==1)){
            jsonObject = JSONObject.parseObject(jsonObject.get("resultset").toString());
            jsonObject = JSONObject.parseObject(jsonObject.get("row").toString());
            JSONArray data = new JSONArray();
            data.add(jsonObject);
            ajaxResponse.setData("total",1);
            ajaxResponse.setData("data",data);
        }else if(!recordcount.equals("0")){
            jsonObject = JSONObject.parseObject(jsonObject.get("resultset").toString());
            JSONArray data = jsonObject.getJSONArray("row");
            ajaxResponse.setData("total",data.size());
            ajaxResponse.setData("data",data);
        }else{
			ajaxResponse.setData("total",0);
            ajaxResponse.setData("data",new JSONArray());
		}
        return ajaxResponse;
    }

    @Override
    public AjaxResponse tywtZgylDycx01(GgcxDTO dto) throws ClientException, IOException {
        AjaxResponse ajaxResponse = new AjaxResponse();
        Map<String, Object> p = new HashMap<String, Object>();
        if(StringUtils.isBlank(dto.getAaa027())) {
            dto.setAaa027("440800");
        }
        p.put("aaa027", dto.getAaa027());//地市
        p.put("aac147", dto.getAac002());//身份证
        p.put("aac003", dto.getAac003());//姓名
        p.put("aae041",dto.getAae041());//起始年度
        p.put("aae042",dto.getAae042());//结束年度
        long startrow=(dto.getPagenum()-1)*dto.getPagesize()+1;
        if(startrow<1){
            startrow=1;
        }
        p.put("startrow",startrow);
        p.put("endrow",dto.getPagenum()*dto.getPagesize());
        Map<Object,Object> m=SggfwInterface.qqjk(p,config,"TYWT_ZGYL_DYCX_01");
        JSONObject jsonObject = JSONObject.parseObject(m.get("output").toString());
        String recordcount=jsonObject.get("recordcount").toString();
        if(recordcount.equals("1")||(!recordcount.equals("0")&&dto.getPagesize()==1)){
            jsonObject = JSONObject.parseObject(jsonObject.get("resultset").toString());
            jsonObject = JSONObject.parseObject(jsonObject.get("row").toString());
            JSONArray data = new JSONArray();
            data.add(jsonObject);
            ajaxResponse.setData("total",1);
            ajaxResponse.setData("data",data);
        }else if(!recordcount.equals("0")){
            jsonObject = JSONObject.parseObject(jsonObject.get("resultset").toString());
            JSONArray data = jsonObject.getJSONArray("row");
            ajaxResponse.setData("total",data.size());
            ajaxResponse.setData("data",data);
        }else{
			ajaxResponse.setData("total",0);
            ajaxResponse.setData("data",new JSONArray());
		}
        return ajaxResponse;
    }

    @Override
    public AjaxResponse tywtSyeDycx03(TywtGrjbxxGgcx01DTO dto) throws ClientException, IOException {
        AjaxResponse ajaxResponse = new AjaxResponse();
        Map<String, Object> p = new HashMap<String, Object>();
        if(StringUtils.isBlank(dto.getAaa027())) {
            dto.setAaa027("440800");
        }
        p.put("aaa027", dto.getAaa027());//地市
        p.put("aac147", dto.getAac002());//身份证
        p.put("aac003", dto.getAac003());//姓名
        long startrow=(dto.getPagenum()-1)*dto.getPagesize()+1;
        if(startrow<1){
            startrow=1;
        }
        p.put("startrow",startrow);
        p.put("endrow",dto.getPagenum()*dto.getPagesize());
        Map<Object,Object> m=SggfwInterface.qqjk(p,config,"TYWT_SYE_DYCX_03");
        JSONObject jsonObject = JSONObject.parseObject(m.get("output").toString());
        String recordcount=jsonObject.get("recordcount").toString();
        if(recordcount.equals("1")||(!recordcount.equals("0")&&dto.getPagesize()==1)){
            jsonObject = JSONObject.parseObject(jsonObject.get("resultset").toString());
            jsonObject = JSONObject.parseObject(jsonObject.get("row").toString());
            JSONArray data = new JSONArray();
            data.add(jsonObject);
            ajaxResponse.setData("total",1);
            ajaxResponse.setData("data",data);
        }else if(!recordcount.equals("0")){
            jsonObject = JSONObject.parseObject(jsonObject.get("resultset").toString());
            JSONArray data = jsonObject.getJSONArray("row");
            ajaxResponse.setData("total",data.size());
            ajaxResponse.setData("data",data);
        }else{
			ajaxResponse.setData("total",0);
            ajaxResponse.setData("data",new JSONArray());
		}
        return ajaxResponse;
    }

    @Override
    public AjaxResponse tywtGsDycx05(GgcxDTO dto) throws ClientException, IOException {
        AjaxResponse ajaxResponse = new AjaxResponse();
        Map<String, Object> p = new HashMap<String, Object>();
        if(StringUtils.isBlank(dto.getAaa027())) {
            dto.setAaa027("440800");
        }
        p.put("aaa027", dto.getAaa027());//地市
        p.put("aac147", dto.getAac002());//身份证
        p.put("aac003", dto.getAac003());//姓名
        p.put("aae041",dto.getAae041());//起始年度
        p.put("aae042",dto.getAae042());//结束年度
        long startrow=(dto.getPagenum()-1)*dto.getPagesize()+1;
        if(startrow<1){
            startrow=1;
        }
        p.put("startrow",startrow);
        p.put("endrow",dto.getPagenum()*dto.getPagesize());
        Map<Object,Object> m=SggfwInterface.qqjk(p,config,"TYWT_GS_DYCX_05");
        JSONObject jsonObject = JSONObject.parseObject(m.get("output").toString());
        String recordcount=jsonObject.get("recordcount").toString();
        if(recordcount.equals("1")||(!recordcount.equals("0")&&dto.getPagesize()==1)){
            jsonObject = JSONObject.parseObject(jsonObject.get("resultset").toString());
            jsonObject = JSONObject.parseObject(jsonObject.get("row").toString());
            JSONArray data = new JSONArray();
            data.add(jsonObject);
            ajaxResponse.setData("total",1);
            ajaxResponse.setData("data",data);
        }else if(!recordcount.equals("0")){
            jsonObject = JSONObject.parseObject(jsonObject.get("resultset").toString());
            JSONArray data = jsonObject.getJSONArray("row");
            ajaxResponse.setData("total",data.size());
            ajaxResponse.setData("data",data);
        }else{
			ajaxResponse.setData("total",0);
            ajaxResponse.setData("data",new JSONArray());
		}
        return ajaxResponse;
    }

    @Override
    public AjaxResponse queryJbxx(TywtGrjbxxGgcx01DTO dto) throws ClientException, IOException {
        //单位名称获取职工获取最近一年的工伤保险缴费的单位名称、参保状态获取用户基本信息中的登记状态
        if(StringUtils.isBlank(dto.getAaa027())) {
            dto.setAaa027("440800");
        }
        AjaxResponse ajaxResponse = new AjaxResponse();
        Map<String,String> map=new HashMap();
        Map<String, Object> p = new HashMap<String, Object>();
        p.put("aaa027", dto.getAaa027());//统筹区编码
        p.put("aac147", dto.getAac002());//身份证
        p.put("aac003", dto.getAac003());//姓名
        p.put("startrow",1);
        p.put("endrow",30);
        Map<Object,Object> m=SggfwInterface.qqjk(p,config,"TYWT_GRJBXX_GGCX_01");
        JSONObject jsonObject = JSONObject.parseObject(m.get("output").toString());
        String recordcount=jsonObject.get("recordcount").toString();
        jsonObject = JSONObject.parseObject(jsonObject.get("resultset").toString());
        String bac062=null;
        if(recordcount.equals("1")){
            jsonObject = JSONObject.parseObject(jsonObject.get("row").toString());
            bac062=jsonObject.get("bac062").toString();
        }
        map.put("cbzt",bac062);
        ajaxResponse.setData("data",map);


        p.put("aaa027", dto.getAaa027());//地市
        p.put("aae140", "410");
        p.put("aac147", dto.getAac002());//身份证
        p.put("aac003", dto.getAac003());//姓名
        p.put("aae041",getPrevMonthDate(new Date(),12));//起始年度
        p.put("aae042",getPrevMonthDate(new Date(),0));//结束年度
        p.put("startrow","1");
        p.put("endrow","100");
        m=SggfwInterface.qqjk(p,config,"TYWT_ZGSB_GGCX_04");
        jsonObject = JSONObject.parseObject(m.get("output").toString());
        recordcount=jsonObject.get("recordcount").toString();
        if(recordcount.equals("1")||(!recordcount.equals("0")&&dto.getPagesize()==1)){
            jsonObject = JSONObject.parseObject(jsonObject.get("resultset").toString());
            jsonObject = JSONObject.parseObject(jsonObject.get("row").toString());
            map.put("cbdw",jsonObject.get("aab004").toString());
        }else if(!recordcount.equals("0")){
            jsonObject = JSONObject.parseObject(jsonObject.get("resultset").toString());
            JSONArray data = jsonObject.getJSONArray("row");
            jsonObject=data.getJSONObject(0);
            map.put("cbdw",jsonObject.get("aab004").toString());
        }else{
            map.put("cbdw",null);
        }
        ajaxResponse.setData("data",map);
        return ajaxResponse;
    }

    /**
     * 计算日期的上一个月
     * @param date
     * @param n
     * @return
     */
    public static String getPrevMonthDate(Date date, int n) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(Calendar.MONTH, calendar.get(Calendar.MONTH) - n);
        return new SimpleDateFormat("yyyyMM").format(calendar.getTime());
    }
}
