package test.cn.estsh.i3plus.core.apiservice.serviceimpl.busi;

import cn.estsh.i3plus.core.api.iservice.busi.*;
import cn.estsh.i3plus.core.apiservice.serviceimpl.busi.TestBase;
import cn.estsh.i3plus.core.apiservice.util.FileUtil;
import cn.estsh.i3plus.platform.common.convert.ConvertBean;
import cn.estsh.i3plus.pojo.base.bean.BaseBean;
import cn.estsh.i3plus.pojo.base.codemaker.SnowflakeIdMaker;
import cn.estsh.i3plus.pojo.base.enumutil.CommonEnumUtil;
import cn.estsh.i3plus.pojo.base.enumutil.ImppEnumUtil;
import cn.estsh.i3plus.pojo.model.platform.UserDetailModel;
import cn.estsh.i3plus.pojo.platform.bean.*;
import cn.estsh.i3plus.pojo.platform.sqlpack.CoreHqlPack;
import cn.estsh.impp.framework.boot.auth.AuthUtil;
import cn.estsh.impp.framework.boot.util.ValidatorBean;
import com.alibaba.fastjson.JSON;
import org.apache.commons.lang3.RandomStringUtils;
import org.apache.commons.lang3.RandomUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;

import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.UUID;

/**
 * @Description :
 * @Reference :
 * @Author : Adair Peng
 * @CreateDate : 2019-02-25 14:39
 * @Modify:
 **/
public class TestUser extends TestBase{

    SnowflakeIdMaker snowflakeIdMaker = new SnowflakeIdMaker();

    @Autowired
    private ISysPositionService positionService;

    @Autowired
    private ISysOrganizeService organizeService;

    @Autowired
    private ISysDepartmentService departmentService;

    @Autowired
    private ISysMenuService menuService;

    @Autowired
    private ISysRoleService roleService;

    String TEXT = "飓风黑兹尔是1954年大西洋飓风季期间造成死亡人数最多、经济损失最惨重的飓风。这场风暴夺走了海地多达1000人的生命，然后又以四级飓风强度从南、北卡罗莱纳州边境附近袭击美国，造成95人丧生。之后黑兹尔转变成温带风暴，但仍然致使81人遇难，其中大部分来自多伦多。由于风暴造成了重大人员伤亡和财产损失，其名称“黑兹尔”因此从大西洋风暴命名名单上退役。黑兹尔摧毁了海地40%的咖啡树和50%的可可作物，对当地经济的影响持续了数年之久。飓风在南、北卡罗莱纳州登陆，冲击点附近的大部分海滨住宅被毁。气旋从北卡罗莱纳州沿大西洋海岸北上，先后影响了弗吉尼亚州、西弗吉尼亚州、马里兰州、特拉华州、新泽西州、宾夕法尼亚州和纽约州，产生的阵风时速有近160公里，造成的经济损失高达3.08亿美元。多伦多市内及周边有许多溪流溢出，导致严重的洪涝灾害。加拿大遭受的损失达到1.35亿加拿大元。由于缺少应对热带气旋的经验，加上风暴强度的保持时间异常之长，多伦多所受影响的严重程度是前所未有的";

    @Test
    public void testSysPosition() throws Exception{
        List<SysPosition> dataBase = new ArrayList<>();

        for (int i = 0; i < 15; i++) {
            SysPosition position = new SysPosition();
            position.setName(RandomStringUtils.random(10, TEXT));
            position.setPositionCode(RandomStringUtils.random(10, true, false));
            position.setParentId(CommonEnumUtil.PARENT.DEFAULT.getValue());
            init(position);
            dataBase.add(position);

            for (int j = 0; j < 10; j++) {

                SysPosition position2 = new SysPosition();
                position2.setName(RandomStringUtils.random(10, TEXT));
                position2.setPositionCode(RandomStringUtils.random(10, true, false));
                position2.setParentId(position.getId());
                position2.setParentNameRdd(position.getName());
                init(position2);
                dataBase.add(position2);

                for (int k = 0; k < 5; k++) {
                    SysPosition position3 = new SysPosition();
                    position3.setName(RandomStringUtils.random(10, TEXT));
                    position3.setPositionCode(RandomStringUtils.random(10, true, false));
                    position3.setParentId(position2.getId());
                    position3.setParentNameRdd(position2.getName());
                    init(position3);
                    dataBase.add(position3);
                }
            }
        }

        System.out.println("打印SQL Start ");
        String txt = null;

        for (SysPosition position : dataBase) {
            txt = new String("INSERT INTO sys_position (id, create_date, create_user, is_deleted, is_valid, modify_date, modify_user, organize_code, name, parent_id, parent_name_rdd, position_code, position_description) VALUES" +
                    " ("+position.getId()+", '"+position.getCreateDatetime()+"', '"+position.getCreateUser()+"', "+position.getIsDeleted()+", "+position.getIsValid()+"," +
                    " '"+position.getModifyDatetime()+"', '"+position.getModifyUser()+"', null, '"+position.getName()+"', "+position.getParentId()+", '"+position.getParentNameRdd()+"', '"+position.getPositionCode()+"', null);");
            fileWriter("E:\\test\\SysPosition.sql",txt);
        }

        System.out.println("打印SQL Start End ");

    }

    @Test
    public void testOrganize()throws Exception{
        System.out.println(positionService);
        List<SysOrganize> organizeList = new ArrayList<>();
        List<SysDepartment> departmentList = new ArrayList<>();

        for (int i = 0; i < 10; i++) {
            SysOrganize organize = new SysOrganize();
            organize.setName(RandomStringUtils.random(10, TEXT));
            organize.setOrganizeCode(RandomStringUtils.random(10, true, false));
            organize.setParentId(CommonEnumUtil.PARENT.DEFAULT.getValue());
            organize.setParentNameRdd("顶级组织");
            organize.setOrganizeType(ImppEnumUtil.ORGANIZE_YTPE.TYPE_GROUP.getValue());

            init(organize);
            organizeList.add(organize);

            for (int j = 0; j < 5 ; j++) {
                SysDepartment department = new SysDepartment();
                department.setName(RandomStringUtils.random(10, TEXT));
                department.setDepartmentCode(RandomStringUtils.random(10, true, false));
                department.setParentId(CommonEnumUtil.PARENT.DEFAULT.getValue());
                department.setParentNameRdd("顶级部门");
                department.setOrganizeId(organize.getId());
                department.setOrganizeNameRdd(organize.getName());
                department.setOrganizeCode(organize.getOrganizeCode());

                init(department);
                departmentList.add(department);

                for (int k = 0; k < 5; k++) {
                    SysDepartment department1 = new SysDepartment();
                    department1.setName(RandomStringUtils.random(10, TEXT));
                    department1.setDepartmentCode(RandomStringUtils.random(10, true, false));
                    department1.setParentId(department.getId());
                    department1.setParentNameRdd(department.getParentNameRdd());
                    department1.setOrganizeId(organize.getId());
                    department1.setOrganizeNameRdd(organize.getName());
                    department1.setOrganizeCode(organize.getOrganizeCode());

                    init(department1);
                    departmentList.add(department1);

                    for (int l = 0; l < 5; l++) {
                        SysDepartment department2 = new SysDepartment();
                        department2.setName(RandomStringUtils.random(10, TEXT));
                        department2.setDepartmentCode(RandomStringUtils.random(10, true, false));
                        department2.setParentId(department1.getId());
                        department2.setParentNameRdd(department1.getParentNameRdd());
                        department2.setOrganizeId(organize.getId());
                        department2.setOrganizeNameRdd(organize.getName());
                        department2.setOrganizeCode(organize.getOrganizeCode());

                        init(department2);
                        departmentList.add(department2);
                    }
                }
            }

            for (int j = 0; j < 5; j++) {

                SysOrganize organize1 = new SysOrganize();
                organize1.setName(RandomStringUtils.random(10, TEXT));
                organize1.setOrganizeCode(RandomStringUtils.random(10, true, false));
                organize1.setParentId(organize.getParentId());
                organize1.setParentNameRdd(organize.getName());
                organize1.setOrganizeType(ImppEnumUtil.ORGANIZE_YTPE.TYPE_FACTORY.getValue());

                init(organize1);
                organizeList.add(organize1);

                for (int k = 0; k < 5; k++) {
                    SysDepartment department = new SysDepartment();
                    department.setName(RandomStringUtils.random(10, TEXT));
                    department.setDepartmentCode(RandomStringUtils.random(10, true, false));
                    department.setParentId(CommonEnumUtil.PARENT.DEFAULT.getValue());
                    department.setParentNameRdd("顶级部门");
                    department.setOrganizeId(organize1.getId());
                    department.setOrganizeNameRdd(organize1.getName());
                    department.setOrganizeCode(organize1.getOrganizeCode());

                    init(department);
                    departmentList.add(department);
                    for (int l = 0; l < 5; l++) {
                        SysDepartment department1 = new SysDepartment();
                        department1.setName(RandomStringUtils.random(10, TEXT));
                        department1.setDepartmentCode(RandomStringUtils.random(10, true, false));
                        department1.setParentId(department.getId());
                        department1.setParentNameRdd(department.getParentNameRdd());
                        department1.setOrganizeId(organize1.getId());
                        department1.setOrganizeNameRdd(organize1.getName());
                        department1.setOrganizeCode(organize1.getOrganizeCode());

                        init(department1);
                        departmentList.add(department1);
                        for (int m = 0; m < 5; m++) {
                            SysDepartment department2 = new SysDepartment();
                            department2.setName(RandomStringUtils.random(10, TEXT));
                            department2.setDepartmentCode(RandomStringUtils.random(10, true, false));
                            department2.setParentId(department1.getId());
                            department2.setParentNameRdd(department1.getParentNameRdd());
                            department2.setOrganizeId(organize1.getId());
                            department2.setOrganizeNameRdd(organize1.getName());
                            department2.setOrganizeCode(organize1.getOrganizeCode());

                            init(department2);
                            departmentList.add(department2);
                        }
                    }
                }

                for (int k = 0; k < 5; k++) {

                    SysOrganize organize2 = new SysOrganize();
                    organize2.setName(RandomStringUtils.random(10, TEXT));
                    organize2.setOrganizeCode(RandomStringUtils.random(10, true, false));
                    organize2.setParentId(organize1.getParentId());
                    organize2.setParentNameRdd(organize1.getName());
                    organize2.setOrganizeType(ImppEnumUtil.ORGANIZE_YTPE.TYPE_FACTORY.getValue());

                    init(organize2);
                    organizeList.add(organize2);

                    for (int l = 0; l < 5; l++) {
                        SysDepartment department = new SysDepartment();
                        department.setName(RandomStringUtils.random(10, TEXT));
                        department.setDepartmentCode(RandomStringUtils.random(10, true, false));
                        department.setParentId(CommonEnumUtil.PARENT.DEFAULT.getValue());
                        department.setParentNameRdd("顶级部门");
                        department.setOrganizeId(organize2.getId());
                        department.setOrganizeNameRdd(organize2.getName());
                        department.setOrganizeCode(organize2.getOrganizeCode());

                        init(department);
                        departmentList.add(department);
                        for (int m = 0; m < 5; m++) {
                            SysDepartment department1 = new SysDepartment();
                            department1.setName(RandomStringUtils.random(10, TEXT));
                            department1.setDepartmentCode(RandomStringUtils.random(10, true, false));
                            department1.setParentId(department.getId());
                            department1.setParentNameRdd(department.getParentNameRdd());
                            department1.setOrganizeId(organize2.getId());
                            department1.setOrganizeNameRdd(organize2.getName());
                            department1.setOrganizeCode(organize2.getOrganizeCode());

                            init(department1);
                            departmentList.add(department1);
                            for (int n = 0; n < 5; n++) {
                                SysDepartment department2 = new SysDepartment();
                                department2.setName(RandomStringUtils.random(10, TEXT));
                                department2.setDepartmentCode(RandomStringUtils.random(10, true, false));
                                department2.setParentId(department1.getId());
                                department2.setParentNameRdd(department1.getParentNameRdd());
                                department2.setOrganizeId(organize2.getId());
                                department2.setOrganizeNameRdd(organize2.getName());
                                department2.setOrganizeCode(organize2.getOrganizeCode());

                                init(department2);
                                departmentList.add(department2);
                            }
                        }
                    }
                }
            }
        }

        for (SysOrganize organize : organizeList) {
            String txt = new String(" INSERT INTO sys_organize (" +
                    "id, create_date, create_user, is_deleted, is_valid, modify_date, " +
                    "modify_user, organize_code, name, organize_type, parent_id, parent_name_rdd) VALUES " +
                    "("+organize.getId()+", '"+organize.getCreateDatetime()+"', '"+organize.getCreateUser()+"', " +
                    " "+organize.getIsDeleted()+", "+organize.getIsValid()+", '"+organize.getModifyDatetime()+"'," +
                    " '"+organize.getModifyUser()+"', '"+organize.getOrganizeCode()+"', '"+organize.getParentNameRdd()+"'," +
                    " "+organize.getOrganizeType()+", "+organize.getParentId()+", '"+organize.getParentNameRdd()+"');");
            fileWriter("E:\\test\\SysOrganize.sql",txt);
        }

        for (SysDepartment department : departmentList) {
            String txt = new String("INSERT INTO sys_department (id, create_date, create_user, is_deleted," +
                    " is_valid, modify_date, modify_user, department_code, name, organize_id, organize_name_rdd," +
                    " parent_id, parent_name_rdd) VALUES (" +
                    " "+department.getId()+", '"+department.getCreateDatetime()+"'," +
                    " '"+department.getCreateUser()+"', "+department.getIsDeleted()+", "+department.getIsValid()+"," +
                    " '"+department.getModifyDatetime()+"', '"+department.getModifyUser()+"', '"+department.getDepartmentCode()+"'," +
                    " '"+department.getName()+"', "+department.getOrganizeId()+", '"+department.getOrganizeNameRdd()+"', " +
                    "  "+department.getParentId()+", ':"+department.getParentNameRdd()+"');");
            fileWriter("E:\\test\\SysDepartment.sql",txt);
        }

    }

    @Test
    public void testSysMenu() throws Exception {
        List<SysMenu> list = menuService.findSysMenuAll();

        for (SysMenu menu : list) {
            String txt = new String("INSERT INTO sys_menu (id, create_date, create_user, is_deleted, is_valid," +
                    " modify_date, modify_user, menu_class_path, menu_code, menu_css, menu_description, menu_grade, menu_icon, " +
                    "menu_sort, menu_status, menu_type, menu_url, name, parent_id, parent_name_rdd, name_zh_shortening) VALUES (" +
                    " "+menu.getId()+", null, 'System Init', null, null, '2018-12-20 12:39:56', '系统管理员', '', '101010000', '', null, null, '', '0', 1, 2, '/impp-base', '基础信息管理', 100002, '设置', 'jcxxgl');");
            fileWriter("E:\\test\\SysRefRoleMenu.sql",txt);
        }
    }

    @Test
    public void testSysRole() throws Exception{
        List<SysMenu> list = menuService.findSysMenuAll();

        List<SysRole> roleList = new ArrayList<>();
        List<SysRefRoleMenu> refRoleMenuList = new ArrayList<>();

        for (int i = 0; i < 100 ; i++) {
            List<String> moduleListRdd = new ArrayList<>();
            List<String> featuresListRdd = new ArrayList<>();           // 冗余

            SysRole role = new SysRole();
            role.setRoleStatus(CommonEnumUtil.DATA_STATUS.ENABLE.getValue());
            role.setMenuNumber(0);
            role.setModuleNumber(0);
            role.setName(RandomStringUtils.random(10, TEXT));
            role.setRoleDescription(RandomStringUtils.random(50, TEXT));

            for (SysMenu menu : list) {
                SysRefRoleMenu ref = new SysRefRoleMenu();
                ref.setRoleId(role.getId());
                ref.setRoleNameRdd(role.getName());
                ref.setMenuNameRdd(menu.getName());
                ref.setMenuId(menu.getId());
                ref.setIsValid(CommonEnumUtil.IS_VAILD.VAILD.getValue());
                ref.setMenuTypeRdd(menu.getMenuType());

                // 冗余信息封装
                if(CommonEnumUtil.METHOD_LEVEL.MODULE.getValue() == menu.getMenuType().intValue()){
                    moduleListRdd.add(menu.getName());
                }
                if(CommonEnumUtil.METHOD_LEVEL.METHOD.getValue() == menu.getMenuType().intValue()){
                    featuresListRdd.add(menu.getName());
                }

                init(ref);
                refRoleMenuList.add(ref);
            }

            role.setModuleNumber(moduleListRdd.size());
            role.setModuleNamesRdd(String.join(",",moduleListRdd));
            role.setMenuNumber(featuresListRdd.size());
            role.setMenuNamesRdd(String.join(",",featuresListRdd));

            init(role);
            roleList.add(role);
        }

        for (SysRole role : roleList) {
            String txt = new String(" INSERT INTO sys_role (id, create_date, create_user, is_deleted, is_valid," +
                    " modify_date, modify_user, menu_names_rdd, menu_number_rdd, module_names_rdd, module_number, name," +
                    " role_description, role_status) VALUES " +
                    "( "+role.getId()+", '"+role.getCreateDatetime()+"', '"+role.getCreateUser()+"', "+role.getIsDeleted()+"," +
                    " "+role.getIsValid()+", '"+role.getModifyDatetime()+"', '"+role.getModifyUser()+"', '"+role.getMenuNamesRdd()+"', "+role.getMenuNumber()+"," +
                    " '"+role.getModuleNamesRdd()+"', "+role.getModuleNumber()+", '"+role.getName()+"', null, 1);");
            fileWriter("E:\\test\\SysRole.sql",txt);
        }

        for (SysRefRoleMenu ref : refRoleMenuList) {
            String txt = new String("INSERT INTO sys_ref_role_menu (id, create_date, create_user, is_deleted," +
                    " is_valid, modify_date, modify_user, menu_id, menu_name_rdd, menu_type_rdd, role_id, role_name_rdd) VALUES (" +
                    " "+ref.getId()+", '"+ref.getCreateDatetime()+"', '"+ref.getCreateUser()+"', "+ref.getIsDeleted()+", "+ref.getIsValid()+"," +
                    " '"+ref.getModifyDatetime()+"', '"+ref.getModifyUser()+"', "+ref.getMenuId()+", '"+ref.getMenuNameRdd()+"'," +
                    " '"+ref.getMenuTypeRdd()+"', "+ref.getRoleId()+", '"+ref.getRoleNameRdd()+"');");
            fileWriter("E:\\test\\SysRefRoleMenu.sql",txt);
        }



    }

    @Test
    public void testUserInfo() throws Exception {
        List<SysRole> roleList = roleService.findSysRoleAll();
        List<SysPosition> positionList = positionService.listSysPosition();
        List<SysDepartment> departmentList = departmentService.listSysDepartment();

        for (int y = 0; y < 10; y++) {
            String PATH = new String("E:\\test\\user\\" + y);
            List<SysUser> userList = new ArrayList<>();
            List<SysUserInfo> userInfoList = new ArrayList<>();
            List<SysRefUserRole> refUserRoleList = new ArrayList<>();
            List<SysRefUserDepartment> refUserDepartmentList = new ArrayList<>();
            List<SysRefUserInfoDepartment> refUserInfoDepartmentList = new ArrayList<>();
            List<SysRefUserPosition> refUserPositionList = new ArrayList<>();

            List<String> refUserRoleNameList ;
            List<String> refUserPositionNameList ;
            List<String> refUserDepartmentNameList ;

            SysUser user;
            SysUserInfo userInfo;
            SysRefUserRole refUserRole;
            SysRefUserDepartment refUserDepartment;
            SysRefUserInfoDepartment refUserInfoDepartment;
            SysRefUserPosition refUserPosition;
            SysDepartment department = null;
            SysPosition position = null;

            for (int i = 0; i < 10000; i++) {
                user = new SysUser();
                userInfo = new SysUserInfo();
                refUserRoleNameList = new ArrayList<>();
                refUserPositionNameList = new ArrayList<>();
                refUserDepartmentNameList = new ArrayList<>();

                init(user);
                init(userInfo);

                for (int j = 0; j < 2; j++) {
                    SysRole role = roleList.get(new Random().nextInt(roleList.size() - 1));
                    if(role != null){
                        refUserRole = new SysRefUserRole();
                        init(refUserRole);

                        refUserRole.setRoleId(role.getId());
                        refUserRole.setRoleNameRdd(role.getName());
                        refUserRole.setUserId(user.getId());
                        refUserRole.setUserNameRdd(user.getUserName());

                        refUserRoleNameList.add(role.getName());
                        refUserRoleList.add(refUserRole);
                    }
                }

                for (int j = 0; j < 3; j++) {
                    position = positionList.get(new Random().nextInt(positionList.size() - 1));
                    if(position != null){
                        refUserPosition = new SysRefUserPosition();
                        init(refUserPosition);

                        refUserPosition.setUserId(userInfo.getId());
                        refUserPosition.setUserNameRdd(userInfo.getName());
                        refUserPosition.setPositionId(position.getId());
                        refUserPosition.setPositionNameRdd(position.getName());

                        refUserPositionNameList.add(position.getName());

                        refUserPositionList.add(refUserPosition);
                    }
                }

                for (int j = 0; j < 3; j++) {
                    department = departmentList.get(new Random().nextInt(departmentList.size() - 1));

                    if(department != null){
                        refUserDepartment= new SysRefUserDepartment();
                        refUserInfoDepartment = new SysRefUserInfoDepartment();

                        init(refUserDepartment);
                        init(refUserInfoDepartment);

                        refUserDepartment.setDepartmentId(department.getId());
                        refUserDepartment.setDepartmentNameRdd(department.getName());
                        refUserDepartment.setOrganizeNameRdd(department.getOrganizeNameRdd());
                        refUserDepartment.setOrganizeId(department.getOrganizeId());
                        refUserDepartment.setOrganizeCode(department.getOrganizeCode());
                        refUserDepartment.setUserId(user.getId());
                        refUserDepartment.setUserNameRdd(user.getUserName());

                        refUserInfoDepartment.setDepartmentId(department.getId());
                        refUserInfoDepartment.setDepartmentNameRdd(department.getName());
                        refUserInfoDepartment.setOrganizeNameRdd(department.getOrganizeNameRdd());
                        refUserInfoDepartment.setOrganizeId(department.getOrganizeId());
                        refUserInfoDepartment.setOrganizeCode(department.getOrganizeCode());
                        refUserInfoDepartment.setUserId(user.getId());
                        refUserInfoDepartment.setUserNameRdd(user.getUserName());

                        refUserDepartmentNameList.add(department.getName());

                        refUserDepartmentList.add(refUserDepartment);
                        refUserInfoDepartmentList.add(refUserInfoDepartment);
                    }
                }

                userInfo.setDepartmentNamesRdd(StringUtils.join(refUserDepartmentNameList));
                userInfo.setDepartmentId(department.getId());
                userInfo.setDepartmentNameRdd(department.getName());
                userInfo.setOrganizeId(department.getOrganizeId());
                userInfo.setOrganizeNameRdd(department.getOrganizeNameRdd());
                userInfo.setOrganizeCode(department.getOrganizeCode());
                userInfo.setPositionId(position.getId());
                userInfo.setPositionNameRdd(position.getName());
                userInfo.setPositionNamesRdd(StringUtils.join(refUserPositionNameList,","));
                userInfo.setName(RandomStringUtils.random(5, TEXT));
                userInfo.setUserEmpNo(RandomStringUtils.random(10, true, true));
                userInfo.setUserInfoStatus(CommonEnumUtil.USER_INFO_STATUS.INCUMBENT.getValue());
                userInfo.setUserLoginNum(0);
                userInfo.setIsValid(CommonEnumUtil.IS_VAILD.VAILD.getValue());
                userInfo.setUserId(user.getId());

                user.setUserType(CommonEnumUtil.USER_TYPE.USER.getValue());
                user.setUserStatus(CommonEnumUtil.USER_STATUS.ENABLE.getValue());
                user.setIsValid(CommonEnumUtil.IS_VAILD.VAILD.getValue());
                user.setUserLoginNum(0);
                user.setDepartmentNamesRdd(StringUtils.join(refUserDepartmentNameList,","));
                user.setDepartmentId(department.getId());
                user.setDepartmentNameRdd(department.getName());
                user.setUserPhone(RandomStringUtils.random(11, false, true));
                user.setUserEmail(RandomStringUtils.random(15, true, true));
                user.setUserLoginName(RandomStringUtils.random(20, true, true));
                user.setUserLoginPassword("123456");
                user.setUserEmpNo(userInfo.getUserEmpNo());

                userList.add(user);
                userInfoList.add(userInfo);
            }

            for (SysRefUserPosition ref : refUserPositionList) {
                String txt = new String("INSERT INTO sys_ref_user_position (id, create_date, create_user, is_deleted," +
                        " is_valid, modify_date, modify_user, position_id, position_name_rdd, user_id, user_name_rdd) VALUES (" +
                        "  "+ref.getId()+", '"+ref.getCreateDatetime()+"', '"+ref.getCreateUser()+"', "+ref.getIsDeleted()+"," +
                        " "+ref.getIsValid()+", '"+ref.getModifyUser()+"', '"+ref.getModifyDatetime()+"', "+ref.getPositionId()+"," +
                        " '"+ref.getPositionNameRdd()+"', "+ref.getUserId()+", '"+ref.getUserNameRdd()+"');");
                fileWriter(PATH+"\\SysRefUserPosition.sql",txt);
            }

            for (SysRefUserRole ref : refUserRoleList) {
                String txt = new String("INSERT INTO sys_ref_user_role (id, create_date, create_user, is_deleted," +
                        " is_valid, modify_date, modify_user, role_id, role_name_rdd, user_id, user_name_rdd) VALUES (" +
                        " "+ref.getId()+", '"+ref.getCreateDatetime()+"', '"+ref.getCreateUser()+"', "+ref.getIsDeleted()+"," +
                        " "+ref.getIsValid()+", '"+ref.getModifyDatetime()+"', '"+ref.getModifyUser()+"', "+ref.getRoleId()+"," +
                        " '"+ref.getRoleNameRdd()+"', "+ref.getUserId()+", '"+ref.getUserNameRdd()+"');");
                fileWriter(PATH+"\\SysRefUserRole.sql",txt);
            }

            for (SysRefUserDepartment ref : refUserDepartmentList) {
                String txt = new String("INSERT INTO sys_ref_user_department (id, create_date, create_user, is_deleted," +
                        " is_valid, modify_date, modify_user, department_id, department_name_rdd, organize_id, organize_name_rdd, user_id, user_name) VALUES (" +
                        " "+ref.getId()+", '"+ref.getCreateDatetime()+"', '"+ref.getCreateUser()+"', "+ref.getIsDeleted()+"," +
                        " "+ref.getIsValid()+", '"+ref.getModifyDatetime()+"', '"+ref.getModifyUser()+"', "+ref.getDepartmentId()+"," +
                        " '"+ref.getDepartmentNameRdd()+"', "+ref.getOrganizeId()+", '"+ref.getOrganizeNameRdd()+"'," +
                        " "+ref.getUserId()+", '"+ref.getUserNameRdd()+"');");
                fileWriter(PATH+"\\SysRefUserDepartment.sql",txt);
            }

            for (SysRefUserInfoDepartment ref : refUserInfoDepartmentList) {
                String txt = new String("INSERT INTO sys_ref_user_info_department (id, create_date, create_user, is_deleted," +
                        " is_valid, modify_date, modify_user, department_id, department_name_rdd, organize_id, organize_name_rdd, user_id, user_name) VALUES (" +
                        " "+ref.getId()+", '"+ref.getCreateDatetime()+"', '"+ref.getCreateUser()+"', "+ref.getIsDeleted()+"," +
                        " "+ref.getIsValid()+", '"+ref.getModifyDatetime()+"', '"+ref.getModifyUser()+"', "+ref.getDepartmentId()+"," +
                        " '"+ref.getDepartmentNameRdd()+"', "+ref.getOrganizeId()+", '"+ref.getOrganizeNameRdd()+"'," +
                        " "+ref.getUserId()+", '"+ref.getUserNameRdd()+"');");
                fileWriter(PATH+"\\SysRefUserInfoDepartment.sql",txt);
            }

            for (SysUser su : userList) {
                String txt = new String("INSERT INTO sys_user (id, create_date, create_user, is_deleted, is_valid," +
                        " modify_date, modify_user, organize_code, department_id, department_name_rdd, department_names_rdd, language_code," +
                        " organize_id, organize_name_rdd, organize_names_rdd, role_names_rdd, user_email, user_emp_no, user_info_id," +
                        " user_login_last_date_time, user_login_name, user_login_num, user_login_password, user_name_rdd," +
                        " user_phone, user_status, user_type) VALUES (" +
                        " "+su.getId()+", '"+su.getCreateDatetime()+"', '"+su.getCreateUser()+"', "+su.getIsDeleted()+", "+su.getIsValid()+"," +
                        " '"+su.getModifyDatetime()+"', '"+su.getModifyUser()+"', '"+su.getOrganizeCode()+"', " +
                        " "+su.getDepartmentId()+", '"+su.getDepartmentNameRdd()+"', '"+su.getDepartmentNamesRdd()+"', ''," +
                        " "+su.getOrganizeId()+", '"+su.getOrganizeNameRdd()+"', null, '"+su.getRoleNamesRdd()+"', '"+su.getUserEmail()+"'," +
                        " '"+su.getUserEmpNo()+"', "+su.getUserInfoId()+", null, '"+su.getUserLoginName()+"', 0, '2637a5c30af69a7bad877fdb65fbd78b'," +
                        " '"+su.getUserName()+"', '"+su.getUserPhone()+"', 1, 1);");
                fileWriter(PATH+"\\SysUser.sql",txt);
            }

            for (SysUserInfo ui : userInfoList) {
                String txt = new String("INSERT INTO sys_user_info (id, create_date, create_user, is_deleted, is_valid," +
                        " modify_date, modify_user, organize_code, department_id, department_name_rdd, department_names_rdd, name," +
                        " organize_id, organize_name_rdd, position_id, position_name_rdd, position_names_rdd, user_emp_no, user_id," +
                        " user_info_status, user_join_date, user_login_last_date_time, user_login_num) VALUES (" +
                        " "+ui.getId()+", '"+ui.getCreateDatetime()+"', '"+ui.getCreateUser()+"', "+ui.getIsDeleted()+", "+ui.getIsValid()+"," +
                        " '"+ui.getModifyDatetime()+"', '"+ui.getModifyUser()+"', '"+ui.getOrganizeCode()+"'," +
                        " "+ui.getDepartmentId()+", '"+ui.getDepartmentNameRdd()+"', '"+ui.getDepartmentNamesRdd()+"'," +
                        " '"+ui.getName()+"', "+ui.getOrganizeId()+", '"+ui.getOrganizeNameRdd()+"'," +
                        " "+ui.getPositionId()+", '"+ui.getPositionNameRdd()+"', '"+ui.getPositionNamesRdd()+"'," +
                        " '"+ui.getUserEmpNo()+"', "+ui.getUserId()+", 1, '2019-02-26', null , 0);");
                fileWriter(PATH+"\\SysUserInfo.sql",txt);
            }

            System.gc();
        }
    }

    public void testUserDetailModel(){
        List<SysRole> roleList = roleService.findSysRoleAll();
        List<SysPosition> positionList = positionService.listSysPosition();
        List<SysDepartment> departmentList = departmentService.listSysDepartment();

        UserDetailModel model = new UserDetailModel();
        List<String> roleIds = new ArrayList<>();
        List<String> positionIds = new ArrayList<>();
        List<String> departmentIds = new ArrayList<>();

        for (int i = 0; i < new Random().nextInt(10) ; i++) {
            SysRole role = roleList.get(new Random().nextInt(roleList.size() - 1));
            roleIds.add(role.getId().toString());
        }

        for (int i = 0; i < new Random().nextInt(10) ; i++) {
            SysPosition position = positionList.get(new Random().nextInt(positionList.size() - 1));
            positionIds.add(position.getId().toString());
        }

        for (int i = 0; i < new Random().nextInt(10) ; i++) {
            SysDepartment department = departmentList.get(new Random().nextInt(departmentList.size() - 1));
            departmentIds.add(department.getId().toString());
        }



        model.setUserInfoId(System.currentTimeMillis() + snowflakeIdMaker.nextId() + "");
        model.setUserId(System.currentTimeMillis() + snowflakeIdMaker.nextId() + "");
        model.setUserPhone(RandomStringUtils.random(11, false, true));
        model.setUserEmail(RandomStringUtils.random(15, true, true));
        model.setUserLoginPassword(RandomStringUtils.random(20, true, true));
        model.setUserRoleIdList(roleIds);
        model.setUserDepartmentIdList(departmentIds);
        model.setInfoEmpNo(RandomStringUtils.random(10, true, true));
        model.setInfoPositionIdList(positionIds);
        model.setInfoDepartmentIdList(departmentIds);

    }


    public void init(BaseBean bean)throws Exception{
        bean.setId(System.currentTimeMillis() + snowflakeIdMaker.nextId() + UUID.randomUUID().hashCode() + new Random().nextInt(10000000));
        bean.setIsValid(CommonEnumUtil.IS_VAILD.VAILD.getValue());
        bean.setIsDeleted(CommonEnumUtil.IS_DEAL.NO.getValue());
        bean.setCreateUser("系统管理员-测试");
        bean.setModifyUser("系统管理员-测试");
        bean.setCreateDatetime(DateFormatUtils.format(System.currentTimeMillis(),"yyyy-MM-dd HH:mm:ss"));
        bean.setModifyDatetime(DateFormatUtils.format(System.currentTimeMillis(),"yyyy-MM-dd HH:mm:ss"));
    }

    public static void fileWriter(String fileName, String content) {
        try {
            // 打开一个写文件器，构造函数中的第二个参数true表示以追加形式写文件   
            FileWriter writer = new FileWriter(fileName, true);
            writer.write(content+"\r\n");
            writer.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}
