package com.mortals.xhx.common.utils;

import cn.hutool.core.util.IdcardUtil;
import com.mortals.xhx.common.code.ApplicantTypeEnums;
import com.mortals.xhx.modules.supplement.model.SupplementEntity;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * @Title:
 * @Package: com.atm.affair.util
 * @Description: (用一句话描述该文件做什么)
 * @Author: tinker
 * @Date: 4/10/21
 * @Version: V1.0
 */
public class FilterSuppDataUtils {
    public static boolean isCorrectSupp(SupplementEntity supplement) {
        StringBuffer resonMsg = new StringBuffer();
        boolean flag = true;
        //判断对象基本情况，直接在supplement对象中加入成功失败
        //判断人员属性
        if (null==supplement.getApplicationType()|| StringUtils.isEmpty(supplement.getApplicationType())) {
            flag = false;
            resonMsg.append("Err1:申请人属性为空 ");
        }
        //实施清单编码*	申请人属性*	申请人证件类型*	申请人姓名*	申请人证件号码*	申请人手机号码*
        if (StringUtils.isEmpty(supplement.getImplCode())) {
            flag = false;
            resonMsg.append("Err2:实施清单编码为空 ");
        }
        if (StringUtils.isEmpty(supplement.getApplicationCardType())) {
            flag = false;
            resonMsg.append("Err3:证件类型为空 ");
        }
        if(!StringUtils.isEmpty(supplement.getApplicationCardType())){
            if(supplement.getApplicationCardType().equals("414")){
                if (StringUtils.isEmpty(supplement.getApplicationId())) {
                    flag = false;
                    resonMsg.append("Err4:护照为空或者证件号码不正确 ");
                }
            }
            if(supplement.getApplicationCardType().equals("111")){
                if (StringUtils.isEmpty(supplement.getApplicationId()) || !IdcardUtil.isValidCard(supplement.getApplicationId().trim())) {
                    flag = false;
                    resonMsg.append("Err4:证件号码为空或者证件号码不正确 ");
                }
            }
        }
        if(StringUtils.isEmpty(supplement.getApplicationCardType())){
            flag = false;
            resonMsg.append("Err00:证件类型为空");
        }


        if (StringUtils.isEmpty(supplement.getApplicationPhone()) || supplement.getApplicationPhone().trim().length() != 11 ) {
            flag = false;
            resonMsg.append("Err5:手机号码为空或者手机号码不正确 ");
        }
        if (null==supplement.getApplicationName()|| StringUtils.isEmpty(supplement.getApplicationName())) {
            flag = false;
            resonMsg.append("Err6:申请人姓名为空 ");
        }
        if (String.valueOf(ApplicantTypeEnums.LEGAL.getCode()).equals(supplement.getApplicationType())) {
            //如果是法人，还需要在校验下
            //机构名称*	机构地址	机构性质*	证件类型*	机构编码*	法人姓名*
            if (StringUtils.isEmpty(supplement.getMechanismName())) {
                flag = false;
                resonMsg.append("Err21:机构名称为空 ");
            }
            if (StringUtils.isEmpty(supplement.getMechanismType())) {
                flag = false;
                resonMsg.append("Err22:机构性质为空 ");
            }
            if (StringUtils.isEmpty(supplement.getMechanismIdType())) {
                flag = false;
                resonMsg.append("Err23:机构证件类型为空 ");
            }
            if (StringUtils.isEmpty(supplement.getMechanismId())) {
                flag = false;
                resonMsg.append("Err24:机构编码为空 ");
            }
            if (StringUtils.isEmpty(supplement.getLegalPerson())) {
                flag = false;
                resonMsg.append("Err25:法人姓名为空 ");
            }
        }
        supplement.setFailReason(resonMsg.toString());
        return flag;

    }
    public static String parseGender(String cid) {
        String gender = null;
        char c = cid.charAt(cid.length() - 2);
        int sex = Integer.parseInt(String.valueOf(c));
        if (sex % 2 == 0) {
            gender = "2";
        } else {
            gender = "1";
        }
        return gender;
    }

    /**
     * List数据拆分
     *
     * @param sourceList
     * @param size       指定大小
     * @return
     */
    public static List<List<SupplementEntity>> splitList(List<SupplementEntity> sourceList, int size) {
        List<List<SupplementEntity>> listArr = new ArrayList<List<SupplementEntity>>();
        //获取被拆分的数组个数
        int arrSize = sourceList.size() % size == 0 ? sourceList.size() / size : sourceList.size() / size + 1;
        for (int i = 0; i < arrSize; i++) {
            List<SupplementEntity> sub = new ArrayList<SupplementEntity>();
            //把指定索引数据放入到list中
            for (int j = i * size; j <= size * (i + 1) - 1; j++) {
                if (j <= sourceList.size() - 1) {
                    sub.add(sourceList.get(j));
                }
            }
            listArr.add(sub);
        }
        return listArr;
    }

    public static void main(String[] args) {
        List<SupplementEntity> supplementList = new ArrayList<>();
        for (int i = 0; i < 9878; i++) {
            SupplementEntity supplement = new SupplementEntity();
            supplement.setImplName(String.valueOf("Test-" + i));
            supplementList.add(supplement);
        }
        Long a = System.currentTimeMillis();
        List<List<SupplementEntity>> aar = splitList(supplementList, 1000);
        System.out.println(System.currentTimeMillis() - a);
        System.out.println(aar);

    }

}
