package com.boman.web.core.utils;

import com.boman.common.core.exception.BaseException;
import com.boman.common.core.utils.StringUtils;
import com.boman.domain.*;
import com.boman.web.core.domain.vo.AccountingDataVo;

import java.util.List;
import java.util.Map;

import static com.boman.common.core.utils.StringUtils.isNotEmpty;
import static com.boman.common.core.utils.obj.ObjectUtils.isEmpty;
import static com.google.common.base.Strings.nullToEmpty;

/**
 * @author tjf
 * @Date: 2022/03/23/17:23
 */
public class CzrkUtils {
    /**
     * 拼接出前端需要的户籍地或者现居住地
     */
    public static void packAddr(List<Czrk> czrks) {
        if (isEmpty(czrks)) return;
        for (Czrk crk : czrks) {
            // 户籍地
            String province = nullToEmpty(crk.getProvince()), city = nullToEmpty(crk.getCity()), region = nullToEmpty(crk.getRegion()), villageTowns = nullToEmpty(crk.getVillageTowns()), village = nullToEmpty(crk.getVillage()), villageGroup = nullToEmpty(crk.getVillagerGroup()), nowIn = nullToEmpty(crk.getNowIn());
            crk.setDomicileSelect(province + city + region + villageTowns + village + villageGroup);

            // 现居住地
            String provinceCz = nullToEmpty(crk.getProvinceXjd()), cityCz = nullToEmpty(crk.getCityXjd()), regionCz = nullToEmpty(crk.getRegionXjd()), villageTownsCz = nullToEmpty(crk.getVillageTownsXjd()), villageCz = nullToEmpty(crk.getVillageXjd()), nowInCz = nullToEmpty(crk.getDomicile());
            crk.setNowInSelect(provinceCz + cityCz + regionCz + villageTownsCz + villageCz);
        }
    }

    /**
     * 拼接出前端需要的户籍地或者现居住地(核酸检测使用)
     */
    public static void packAddrNucleicAcid(List<AccountingDataVo> accountingDataVos) {
        if (isEmpty(accountingDataVos)) return;
        for (AccountingDataVo accountingDataVo : accountingDataVos) {
            // 户籍地
            String province = nullToEmpty(accountingDataVo.getProvince()), city = nullToEmpty(accountingDataVo.getCity()), region = nullToEmpty(accountingDataVo.getRegion()), villageTowns = nullToEmpty(accountingDataVo.getVillageTowns()), village = nullToEmpty(accountingDataVo.getVillage());
            accountingDataVo.setDomicileSelect(province + city + region + villageTowns + village);

            // 现居住地
            String provinceCz = nullToEmpty(accountingDataVo.getProvinceXjd()), cityCz = nullToEmpty(accountingDataVo.getCityXjd()), regionCz = nullToEmpty(accountingDataVo.getRegionXjd()), villageTownsCz = nullToEmpty(accountingDataVo.getVillageTownsXjd()), villageCz = nullToEmpty(accountingDataVo.getVillageXjd());
            accountingDataVo.setNowInSelect(provinceCz + cityCz + regionCz + villageTownsCz + villageCz);
        }
    }


    /**
     * 人员信息拆分查询参数年龄
     *
     * @param czrk
     */
    public static void setAgeScope(Czrk czrk) {
        Map<String, Object> params = czrk.getParams();
        String ageScope = (String) params.get("age");
        if (isNotEmpty(ageScope)) {
            ageScope = ageScope.trim();
            String[] split = ageScope.split("-");
            params.put("startAge", Integer.parseInt(split[0]));
            if (split.length == 2) {
                params.put("endAge", Integer.parseInt(split[1]));
            }
        }
    }


    /**
     * 查询使用
     * 根据当前登录人判断该用户是什么角色权限字符,来进行判断该用户能查询到什么层级的数据
     *
     * @param czrk
     * @param sysUser
     * @param type    查询常住还是户籍
     */
    public static void setQueryRole(Czrk czrk, SysUser sysUser, String type) {
        SysDept dept = sysUser.getDept();
        if (dept == null) {
            throw new BaseException("对不起，未获相关部门信息");
        }
        // 从部门中获取行政区域规划id
        String areaId = dept.getAreaId();
        if (StringUtils.isBlank(areaId)) {
            throw new BaseException(dept.getDeptName() + "当前未获取到部门下行政区域id");
        }
        //判断该用户是什么角色权限字符
        List<SysRole> roles = sysUser.getRoles();
        if (roles != null && roles.size() > 0) {
            for (SysRole role : roles) {
                if (role != null) {
                    String roleKey = role.getRoleKey();
                    if (StringUtils.isNotBlank(roleKey)) {
                        if ("province".equals(roleKey)) {
                            if (type.equals(Czrk.HJ)) {
                                czrk.setProvinceId(areaId);
                                break;
                            } else {
                                czrk.setProvinceIdXjd(areaId);
                                break;
                            }
                        } else if ("city".equals(roleKey)) {
                            if (type.equals(Czrk.HJ)) {
                                czrk.setCityId(areaId);
                                break;
                            } else {
                                czrk.setCityIdXjd(areaId);
                                break;
                            }
                        } else if ("region".equals(roleKey)) {
                            if (type.equals(Czrk.HJ)) {
                                czrk.setRegionId(areaId);
                                break;
                            } else {
                                czrk.setRegionIdXjd(areaId);
                                break;
                            }
                        } else if ("villageTowns".equals(roleKey)) {
                            if (type.equals(Czrk.HJ)) {
                                czrk.setVillageTownsId(areaId);
                                break;
                            } else {
                                czrk.setVillageTownsIdXjd(areaId);
                                break;
                            }
                        } else if ("village".equals(roleKey)) {
                            if (type.equals(Czrk.HJ)) {
                                czrk.setVillageId(areaId);
                                break;
                            } else {
                                czrk.setVillageIdXjd(areaId);
                                break;
                            }
                        }
                    }
                }
            }
        }
    }



    /**
     * 查询使用(核酸检测)
     * 根据当前登录人判断该用户是什么角色权限字符,来进行判断该用户能查询到什么层级的数据
     *
     * @param accountingDataVo
     * @param sysUser
     * @param type    查询常住还是户籍
     */
    public static void setQueryRole(AccountingDataVo accountingDataVo, SysUser sysUser, String type) {
        SysDept dept = sysUser.getDept();
        if (dept == null) {
            throw new BaseException("对不起，未获相关部门信息");
        }
        // 从部门中获取行政区域规划id
        String areaId = dept.getAreaId();
        if (StringUtils.isBlank(areaId)) {
            throw new BaseException(dept.getDeptName() + "当前未获取到部门下行政区域id");
        }
        //判断该用户是什么角色权限字符
        List<SysRole> roles = sysUser.getRoles();
        if (roles != null && roles.size() > 0) {
            for (SysRole role : roles) {
                if (role != null) {
                    String roleKey = role.getRoleKey();
                    if (StringUtils.isNotBlank(roleKey)) {
                        if ("province".equals(roleKey)) {
                            if (type.equals(Czrk.HJ)) {
                                accountingDataVo.setProvinceId(areaId);
                                break;
                            } else {
                                accountingDataVo.setProvinceIdXjd(areaId);
                                break;
                            }
                        } else if ("city".equals(roleKey)) {
                            if (type.equals(Czrk.HJ)) {
                                accountingDataVo.setCityId(areaId);
                                break;
                            } else {
                                accountingDataVo.setCityIdXjd(areaId);
                                break;
                            }
                        } else if ("region".equals(roleKey)) {
                            if (type.equals(Czrk.HJ)) {
                                accountingDataVo.setRegionId(areaId);
                                break;
                            } else {
                                accountingDataVo.setRegionIdXjd(areaId);
                                break;
                            }
                        } else if ("villageTowns".equals(roleKey)) {
                            if (type.equals(Czrk.HJ)) {
                                accountingDataVo.setVillageTownsId(areaId);
                                break;
                            } else {
                                accountingDataVo.setVillageTownsIdXjd(areaId);
                                break;
                            }
                        } else if ("village".equals(roleKey)) {
                            if (type.equals(Czrk.HJ)) {
                                accountingDataVo.setVillageId(areaId);
                                break;
                            } else {
                                accountingDataVo.setVillageIdXjd(areaId);
                                break;
                            }
                        }
                    }
                }
            }
        }
    }


    /**
     * 查询使用(死亡导入人员权限)
     * 根据当前登录人判断该用户是什么角色权限字符,来进行判断该用户能查询到什么层级的数据
     *
     * @param thirdPartInfo
     * @param sysUser
     */
    public static void setQueryRoleDie(ThirdPartInfo thirdPartInfo, SysUser sysUser) {
        SysDept dept = sysUser.getDept();
        if (dept == null) {
            throw new BaseException("对不起，未获相关部门信息");
        }
        // 从部门中获取行政区域规划id
        String areaId = dept.getAreaId();
        if (StringUtils.isBlank(areaId)) {
            throw new BaseException(dept.getDeptName() + "当前未获取到部门下行政区域id");
        }
        //判断该用户是什么角色权限字符
        List<SysRole> roles = sysUser.getRoles();
        if (roles != null && roles.size() > 0) {
            for (SysRole role : roles) {
                if (role != null) {
                    String roleKey = role.getRoleKey();
                    if (StringUtils.isNotBlank(roleKey)) {
                        if ("province".equals(roleKey)) {
                            thirdPartInfo.setProvinceId(areaId);

                        } else if ("city".equals(roleKey)) {
                            thirdPartInfo.setCityId(areaId);

                        } else if ("region".equals(roleKey)) {
                            thirdPartInfo.setRegionId(areaId);

                        } else if ("villageTowns".equals(roleKey)) {
                            thirdPartInfo.setVillageTownsId(areaId);

                        } else if ("village".equals(roleKey)) {
                            thirdPartInfo.setVillageId(areaId);

                        }
                    }
                }
            }
        }
    }

    /**
     * 新增用户的时候,判断登录用户是否能新增
     *
     * @param czrk
     * @param sysUser
     */
    public static Boolean setInsertRole(Czrk czrk, SysUser sysUser) {
        boolean flag = true;
        //从部门中获取行政区域规划id
        SysDept dept = sysUser.getDept();
        if (dept == null) {
            throw new BaseException("对不起，未获相关部门信息");
        }
        String areaId = dept.getAreaId();
        if (StringUtils.isBlank(areaId)) {
            throw new BaseException(dept.getDeptName() + "当前未获取到部门下行政区域id");
        }
        //判断该用户是什么角色权限字符
        List<SysRole> roles = sysUser.getRoles();
        if (roles != null && roles.size() > 0) {
            for (SysRole role : roles) {
                if (role != null) {
                    String roleKey = role.getRoleKey();
                    if (StringUtils.isNotBlank(roleKey)) {
                        if ("province".equals(roleKey)) {
                            String provinceId = czrk.getProvinceId();
                            //获取居住地址
                            if (StringUtils.isNotBlank(provinceId) && provinceId.equals(areaId)) {
                                flag = false;
                                break;
                            } else {
                                List<CzrkJzdz> czrkJzdzList = czrk.getCzrkJzdzList();
                                for (CzrkJzdz czrkJzdz : czrkJzdzList) {
                                    String provinceIdJzdz = czrkJzdz.getProvinceId();
                                    if (StringUtils.isNotBlank(provinceIdJzdz) && provinceIdJzdz.equals(areaId)) {
                                        flag = false;
                                        break;
                                    }
                                }
                            }
                        } else if ("city".equals(roleKey)) {
                            String cityId = czrk.getCityId();
                            if (StringUtils.isNotBlank(cityId) && cityId.equals(areaId)) {
                                flag = false;
                                break;
                            } else {
                                List<CzrkJzdz> czrkJzdzList = czrk.getCzrkJzdzList();
                                for (CzrkJzdz czrkJzdz : czrkJzdzList) {
                                    String cityJzdz = czrkJzdz.getCityId();
                                    if (StringUtils.isNotBlank(cityJzdz) && cityJzdz.equals(areaId)) {
                                        flag = false;
                                        break;
                                    }
                                }
                            }
                        } else if ("region".equals(roleKey)) {
                            String regionId = czrk.getRegionId();
                            if (StringUtils.isNotBlank(regionId) && regionId.equals(areaId)) {
                                flag = false;
                                break;
                            } else {
                                List<CzrkJzdz> czrkJzdzList = czrk.getCzrkJzdzList();
                                for (CzrkJzdz czrkJzdz : czrkJzdzList) {
                                    String regoinJzdz = czrkJzdz.getRegionId();
                                    if (StringUtils.isNotBlank(regoinJzdz) && regoinJzdz.equals(areaId)) {
                                        flag = false;
                                        break;
                                    }
                                }
                            }
                        } else if ("villageTowns".equals(roleKey)) {
                            String villageTownsId = czrk.getVillageTownsId();
                            if (StringUtils.isNotBlank(villageTownsId) && villageTownsId.equals(areaId)) {
                                flag = false;
                                break;
                            } else {
                                List<CzrkJzdz> czrkJzdzList = czrk.getCzrkJzdzList();
                                for (CzrkJzdz czrkJzdz : czrkJzdzList) {
                                    String townIdJzdz = czrkJzdz.getTownId();
                                    if (StringUtils.isNotBlank(townIdJzdz) && townIdJzdz.equals(areaId)) {
                                        flag = false;
                                        break;
                                    }
                                }
                            }
                        } else if ("village".equals(roleKey)) {
                            String villageId = czrk.getVillageId();
                            if (StringUtils.isNotBlank(villageId) && villageId.equals(areaId)) {
                                flag = false;
                                break;
                            } else {
                                List<CzrkJzdz> czrkJzdzList = czrk.getCzrkJzdzList();
                                for (CzrkJzdz czrkJzdz : czrkJzdzList) {
                                    String villageIdJzdz = czrkJzdz.getVillageId();
                                    if (StringUtils.isNotBlank(villageIdJzdz) && villageIdJzdz.equals(areaId)) {
                                        flag = false;
                                        break;
                                    }
                                }
                            }
                        } else if ("admin".equals(roleKey)) {
                            flag = false;
                            break;
                        }
                    }
                }
            }
        }
        return flag;
    }



}
