package com.sooka.system.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.JSONPObject;
import com.sooka.common.core.text.Convert;
import com.sooka.common.utils.DateUtils;
import com.sooka.common.utils.uuid.UUID;
import com.sooka.system.domain.SysUser;
import com.sooka.system.domain.TULog;
import com.sooka.system.mapper.TULogMapper;
import com.util.HttpUtil;
import com.util.UserInfoUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.sooka.system.mapper.TUSfzMapper;
import com.sooka.system.domain.TUSfz;
import com.sooka.system.service.ITUSfzService;

import javax.annotation.Resource;


/**
 * 身份证Service业务层处理
 *
 * @author lei
 * @date 2021-07-06
 */
@Service
public class TUSfzServiceImpl implements ITUSfzService {
    @Autowired
    private TUSfzMapper tUSfzMapper;

    @Resource
    private TULogMapper tuLogMapper;

    @Resource
    private UserInfoUtil userInfoUtil;

    /**
     * 查询身份证
     *
     * @param id 身份证ID
     * @return 身份证
     */
    @Override
    public TUSfz selectTUSfzById(String id) {
        return tUSfzMapper.selectTUSfzById(id);
    }

    /**
     * 查询身份证列表
     *
     * @param tUSfz 身份证
     * @return 身份证
     */
    @Override
    public List<TUSfz> selectTUSfzList(TUSfz tUSfz) {
        Map map = new HashMap();
        map.put("secretKey", "0f0806f206544597840df9cade4127bb");
        String r = HttpUtil.doGet("http://127.0.0.1:88/api/test/sfz", "", map);
        JSONObject jsonpObject = JSONObject.parseObject(r);
        if(jsonpObject != null && jsonpObject.getString("code").equals("1000")){
            JSONArray array = jsonpObject.getJSONObject("data").getJSONArray("data");
            List ls = new ArrayList();
            for (int i = 0; i < array.size(); i++) {
                JSONObject obj = array.getJSONObject(i);
                TUSfz s = new TUSfz();
                tUSfz.setId(UUID.fastUUID().toString());
                tUSfz.setLicenceIssue(obj.getString("licenceIssue"));
                tUSfz.setName(obj.getString("name"));
                tUSfz.setGender(obj.getString("gender"));
                tUSfz.setNationality(obj.getString("nationality"));
                tUSfz.setBirthDate(obj.getString("birthDate"));
                tUSfz.setIdcardNo(obj.getString("idcardNo"));
                tUSfz.setAddressPermanent(obj.getString("addressPermanent"));
                tUSfz.setPhotos(obj.getString("photos"));
                tUSfz.setPhotograph(obj.getString("photograph"));
                tUSfzMapper.insertTUSfz(tUSfz);
                ls.add(tUSfz);
            }
            //添加操作日志
            SysUser sysUser = userInfoUtil.getLoginUserInfo();
            //
            TULog tuLog = new TULog();
            tuLog.setId(UUID.fastUUID().toString());
            tuLog.setBrowser(tUSfz.getBrowser());
            tuLog.setInterfaceinfoId("1294c112-d5d9-48e2-a794-f1e8453e6516");
            tuLog.setInterfaceinfoName("吉林省-人口基础信息-数据查询接口");
            tuLog.setIpaddress(tUSfz.getIpaddress());
            tuLog.setLoginUser(sysUser.getUserId());
            tuLog.setLoginName(sysUser.getUserName());
            tuLog.setOperationQuantity(new Long(ls.size()));
            tuLog.setOs(tUSfz.getOs());
            tuLog.setBrowser(tUSfz.getBrowser());
            //共享接口
            tuLog.setPlatformInterfacetype("platform_interfacetype_1");
            tuLogMapper.insertTULog(tuLog);

            return ls;
        }else{
            return null;
        }
//        return tUSfzMapper.selectTUSfzList(tUSfz);
    }

    /**
     * 新增身份证
     *
     * @param tUSfz 身份证
     * @return 结果
     */
    @Override
    public int insertTUSfz(TUSfz tUSfz) {
        tUSfz.setCreateTime(DateUtils.getNowDate());
        return tUSfzMapper.insertTUSfz(tUSfz);
    }

    /**
     * 修改身份证
     *
     * @param tUSfz 身份证
     * @return 结果
     */
    @Override
    public int updateTUSfz(TUSfz tUSfz) {
        tUSfz.setUpdateTime(DateUtils.getNowDate());
        return tUSfzMapper.updateTUSfz(tUSfz);
    }

    /**
     * 删除身份证对象
     *
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteTUSfzByIds(String ids) {
        return tUSfzMapper.deleteTUSfzByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除身份证信息
     *
     * @param id 身份证ID
     * @return 结果
     */
    @Override
    public int deleteTUSfzById(String id) {
        return tUSfzMapper.deleteTUSfzById(id);
    }
}
