package com.boman.web.core.service.toQianShan;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.boman.common.core.utils.StringUtils;
import com.boman.common.core.utils.sql.SqlUtil;
import com.boman.common.core.web.page.PageDomain;
import com.boman.common.core.web.page.TableSupport;
import com.boman.domain.Czrk;
import com.boman.domain.dto.AjaxResult;
import com.boman.web.core.domain.ToQianshanPersonner;
import com.boman.web.core.domain.vo.AccountingDataVo;
import com.boman.web.core.mapper.AccountingDataMapper;
import com.boman.web.core.mapper.CzrkMapper;
import com.boman.web.core.mapper.StandardlyMapper;
import com.boman.web.core.mapper.ToQianshanPersonnerMapper;
import com.boman.web.core.utils.HttpClientUtils;
import com.github.pagehelper.PageHelper;
import org.apache.ibatis.session.ExecutorType;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Service
public class ToQianshanPersonnerServiceImpl implements ToQianshanPersonnerService {

    @Resource
    private SqlSessionFactory sqlSessionFactory;

    @Autowired
    private ToQianshanPersonnerMapper toQianshanPersonnerMapper;

    @Resource
    private CzrkMapper czrkMapper;


    @Override
    public List<Map<String,Object>> toQianshan(ToQianshanPersonner toQianshanPersonner) {
        startPage();
        List<Map<String, Object>> qianShanMap = toQianshanPersonnerMapper.toQianshanList(toQianshanPersonner);
        return qianShanMap;
    }




        /*djsjqs = djsjqs.replace(" ", "%20");
        djsjjs = djsjjs.replace(" ", "%20");

        //查询前先获取tocken
        String access_token = null;
        Map<String, String> paramMapT = new HashMap<>();
        paramMapT.put("client_id", "acdf50bd13be4901b64c62b1fee862c0");
        paramMapT.put("client_secret", "a3650d67fc034b2d8ea259182b3d99f3");
        String httpt = "http://60.171.171.235:9090/oauth/token";
        try {
            String datas = HttpClientUtils.doGet(httpt, paramMapT);
            JSONObject jsonT = JSON.parseObject(datas);
            access_token = (String) jsonT.get("access_token");
        } catch (IOException e) {
            e.printStackTrace();
        }


        Map<String, String> paramMap = new HashMap<>();
        String http = null;
        //当区划为空的时候根据姓名，身份证查询
        if (StringUtils.isNotEmpty(sfzhm)) {
            //潜山来宜人员数据(姓名 身份证 时间段)
            paramMap.put("client_id", "acdf50bd13be4901b64c62b1fee862c0");
            paramMap.put("access_token", access_token);
            paramMap.put("name", name);
            paramMap.put("sfzhm", sfzhm);
            paramMap.put("djsjqs", djsjqs);
            paramMap.put("djsjjs", djsjjs);

            http = "http://60.171.171.235:9090/service/api/rkk/qslyrysjcx";
        } else {
            //潜山来宜人员数据(区划 时间段)
            paramMap.put("client_id", "acdf50bd13be4901b64c62b1fee862c0");
            paramMap.put("access_token", access_token);
            if (StringUtils.isEmpty(rc)) {
                rc = "潜山市";
            }
            paramMap.put("rc", rc);
            paramMap.put("djsjqs", djsjqs);
            paramMap.put("djsjjs", djsjjs);

            http = "http://60.171.171.235:9090/service/api/rkk/qslyrysj";
        }

        try {
            if (StringUtils.isEmpty(http)) {
                return AjaxResult.error("参数错误");
            }
            String data = HttpClientUtils.doGet(http, paramMap);

            //List list = new ArrayList();
            List<JSONObject> list = JSON.parseArray(data, JSONObject.class);
            for (JSONObject jsonObject : list) {
                jsonObject.put("SFZ_ID",jsonObject.get("hi_icd"));
            }

            return AjaxResult.success("数据返回成功", list);
        } catch (IOException e) {
            e.printStackTrace();
            return AjaxResult.error("数据返回失败");
        }*/

    private void startPage() {
        PageDomain pageDomain = TableSupport.buildPageRequest();
        Integer pageNum = pageDomain.getPageNum();
        Integer pageSize = pageDomain.getPageSize();
        if (StringUtils.isNotNull(pageNum) && StringUtils.isNotNull(pageSize)) {
            String orderBy = SqlUtil.escapeOrderBySql(pageDomain.getOrderBy());
            PageHelper.startPage(pageNum, pageSize, orderBy);
        }
    }


    @Override
    public void insertToQianshanPersonner(List<ToQianshanPersonner> list) {
        SqlSession sqlSession = sqlSessionFactory.openSession(ExecutorType.BATCH, false);
        ToQianshanPersonnerMapper mapper = sqlSession.getMapper(ToQianshanPersonnerMapper.class);


        Czrk czrk = new Czrk();

        for (ToQianshanPersonner toQianshanPersonner : list) {
            String zjhm = toQianshanPersonner.getHiIcd();
            if (StringUtils.isNotBlank(zjhm)) {
                czrk.setIdCard(zjhm);
                czrk.setToAppropriatePhone(toQianshanPersonner.getHiPhone());
                czrk.setToAppropriateTime(toQianshanPersonner.getHiDate());
                //去更新常住人口表中数据
                czrkMapper.updateCzrkByToQianshanPersonner(czrk);
            }

            mapper.insertToQianshanPersonner(toQianshanPersonner);

        }
        sqlSession.flushStatements();

    }

    @Override
    public ToQianshanPersonner getNewInfo() {
        return toQianshanPersonnerMapper.getNewInfo();
    }
}
