package main import ( "dsBaseRpc/Const" "dsBaseRpc/Utils/CommonUtil" "dsBaseRpc/Utils/DateUtil" "dsBaseRpc/Utils/DbUtil" "dsBaseRpc/Utils/LdapUtil" "dsBaseRpc/Utils/PinYinUtil" "dsBaseRpc/Utils/RedisUtil" "dsBaseRpc/models" "fmt" "github.com/mritd/chinaid" "log" "strings" "time" ) //市及县区下教辅单位数量 var bureauCount = 20 //每个市及县区下学校数量 var schoolCount = 20 //教师数量 var teacherCount = 100 //班级数量 var classCount = 20 //学生数量 var studentCount = 40 var db = DbUtil.Engine //声明结构体数组,最终提交 var arrayTBaseOrganization = make([]models.TBaseOrganization, 0) var arrayTBaseTeacher = make([]models.TBaseTeacher, 0) var arrayTSysLoginperson = make([]models.TSysLoginperson, 0) var arrayTBaseRolePerson = make([]models.TBaseRolePerson, 0) var arrayTBaseClass = make([]models.TBaseClass, 0) var arrayTBaseStudent = make([]models.TBaseStudent, 0) var arrayTBaseParent = make([]models.TBaseParent, 0) type schoolStruct struct { SchoolId string SchoolName string } type LoginAccount struct { LoginName string //登录名 Pwd string //密码,支持ldap OriginalPwd string //初始密码 } func GetLoginIdIntMax(count int64) int64 { return RedisUtil.IncrBy(Const.LoginIdIntMax, count) } /** 功能:生成登录账号 作者:黄海 时间:2020-06-11 */ func GenerateLoginAccount(identityId int32, count int64) []LoginAccount { var loginPrefix = "" switch identityId { case 1: loginPrefix = "sys" break case 2: loginPrefix = "tea" break case 3: loginPrefix = "stu" break case 4: loginPrefix = "par" break } //获取最大ID loginIdInt := GetLoginIdIntMax(count) //循环生成 result := make([]LoginAccount, 0) for i := 0; i < int(count); i++ { var loginAccount LoginAccount loginAccount.LoginName = loginPrefix + CommonUtil.ConvertInt64ToString(loginIdInt-count+int64(i)+1) time.Sleep(6 * time.Microsecond) loginAccount.OriginalPwd = CommonUtil.GenValidateCode(6) loginAccount.Pwd = LdapUtil.GetLdapPassword(loginAccount.OriginalPwd) result = append(result, loginAccount) } return result } //增加登录人员 func addLoginPerson(identityId int32, personId string, personName string, loginBean LoginAccount) { //调用dao model := new(models.TSysLoginperson) model.Id = CommonUtil.GetUUID() model.LoginName = loginBean.LoginName model.Pwd = loginBean.Pwd model.OriginalPwd = loginBean.OriginalPwd model.IdentityId = identityId model.PersonId = personId model.PersonName = personName model.BUse = 1 arrayTSysLoginperson = append(arrayTSysLoginperson, *model) } /** 功能:在指定区域下添加教辅单位 作者:黄海 时间:2020-06-10 */ func addBureau(areaCode string, jiaoYuJuId string, areaName string, provinceCode string, cityCode string, districtCode string, bureauCount int) []string { bureauList := make([]string, 0) accountArray := GenerateLoginAccount(1, int64(bureauCount)) for i := 0; i < bureauCount; i++ { model := new(models.TBaseOrganization) model.OrgId = CommonUtil.GetUUID() bureauList = append(bureauList, model.OrgId) //是市,还是区? model.ProvinceCode = provinceCode model.CityCode = cityCode model.DistrictCode = districtCode //组织机构类型 1:教育局 2:学校 3:部门 4:院 5:系 6:专业 7:教辅单位,8:大学区 model.OrgType = 7 model.ParentId = Const.ZeroGuid //默认是一组0的GUID号 model.BureauId = model.OrgId //默认是自己的单位号 //如果是单位找到上一级教育局 //model.ManageOrgId = jiaoYuJuId model.OrgCode = strings.Replace(CommonUtil.GetUUID()[0:30],"-","",-1) model.OrgName = areaName + "第" + CommonUtil.ConvertIntToString(i+1) + "教辅单位" model.AreaCode = areaCode model.EduAssistType = 1 model.MainSchoolId = model.BureauId //指教育局,教辅单位或部门时 model.MainSchoolType = 100 //如果不传递排序号 model.SortId = 1 model.BUse = 1 arrayTBaseOrganization = append(arrayTBaseOrganization, *model) //生成登录账号 addManager(model.OrgId, model.OrgName, model.OrgType, model.ProvinceCode, model.CityCode, model.DistrictCode, accountArray[i]) fmt.Println("准备教辅单位数据:" + model.OrgName) } return bureauList } /** 功能:添加单位管理员 作者:黄海 时间:2020-06-11 */ func addManager(bureauId string, bureauName string, orgType int32, ProvinceCode string, CityCode string, DistrictCode string, loginBean LoginAccount) { //(1)插入基本人员信息 model := new(models.TBaseTeacher) model.PersonId = CommonUtil.GetUUID() model.IdentityId = 1 model.Xm = bureauName + "管理员" model.Xmpy = PinYinUtil.PinYin(model.Xm) model.Cym = model.Xm model.Xbm = "男" model.Csrq = chinaid.RandDate() model.Mzm = "01" model.Zzmmm = "13" model.Sfzjlxm = "4" model.Sfzjh = chinaid.IDNo() model.Xlm = "90" model.Xwm = "0" model.Zcm = "5" model.Bzlbm = "9" model.Cjny = DateUtil.ConvertDate("1999-09-01") model.StageId = "Z" model.SubjectId = "99" model.Gwzym = "80" model.Lxdh = "18888888888" model.Dzxx = "xx@dsideal.com" model.BUse = 1 arrayTBaseTeacher = append(arrayTBaseTeacher, *model) //(2)生成登录名和密码 modelLoginPerson := new(models.TSysLoginperson) modelLoginPerson.Id = CommonUtil.GetUUID() modelLoginPerson.LoginName = loginBean.LoginName modelLoginPerson.Pwd = loginBean.Pwd modelLoginPerson.OriginalPwd = loginBean.OriginalPwd modelLoginPerson.IdentityId = 1 modelLoginPerson.PersonId = model.PersonId modelLoginPerson.PersonName = model.Xm modelLoginPerson.BUse = 1 arrayTSysLoginperson = append(arrayTSysLoginperson, *modelLoginPerson) //(3)、t_base_role modelBaseRolePerson := new(models.TBaseRolePerson) modelBaseRolePerson.Id = CommonUtil.GetUUID() var RoleId string if orgType == 2 { //学校 RoleId = Const.SchoolManager } else { RoleId = Const.EduOrgManager } modelBaseRolePerson.RoleId = RoleId modelBaseRolePerson.IdentityId = 1 modelBaseRolePerson.PersonId = model.PersonId modelBaseRolePerson.RuleId = bureauId //具体管辖是哪个省、市、区或大学区ID,单位ID modelBaseRolePerson.BUse = 1 arrayTBaseRolePerson = append(arrayTBaseRolePerson, *modelBaseRolePerson) } /** 功能:在指定区域下添加学校 作者:黄海 时间:2020-06-10 */ func addSchool(areaCode string, jiaoYuJuId string, areaName string, provinceCode string, cityCode string, districtCode string, schoolCount int) []schoolStruct { schoolList := make([]schoolStruct, 0) accountArray := GenerateLoginAccount(1, int64(schoolCount)) for i := 0; i < schoolCount; i++ { model := new(models.TBaseOrganization) model.OrgId = CommonUtil.GetUUID() model.ProvinceCode = provinceCode model.CityCode = cityCode model.DistrictCode = districtCode model.OrgType = 2 model.ParentId = Const.ZeroGuid //默认是一组0的GUID号 model.BureauId = model.OrgId //默认是自己的单位号 //model.ManageOrgId = jiaoYuJuId model.OrgCode = strings.Replace(CommonUtil.GetUUID()[0:30],"-","",-1) model.OrgName = areaName + "第" + CommonUtil.ConvertIntToString(i+1) + "学校" model.AreaCode = areaCode model.EduAssistType = 1 model.MainSchoolId = model.BureauId model.SortId = 1 model.BUse = 1 model.Xxbxlxm = "312" //九年一贯制 arrayTBaseOrganization = append(arrayTBaseOrganization, *model) model.MainSchoolType = 1 //model.DirectlyUnderType=1 var c schoolStruct c.SchoolId = model.OrgId c.SchoolName = model.OrgName schoolList = append(schoolList, c) //添加管理员 addManager(model.OrgId, model.OrgName, model.OrgType, model.ProvinceCode, model.CityCode, model.DistrictCode, accountArray[i]) fmt.Println("准备学校数据:" + model.OrgName) } return schoolList } /** 功能:在指定单位下添加教师 作者:黄海 时间:2020-06-10 */ func addTeacher(bureauId string, provinceCode string, cityCode string, districtCode string, mainSchoolId string, teacherCount int) { //生成登录名和密码 accountArray := GenerateLoginAccount(2, int64(teacherCount)) for i := 0; i < teacherCount; i++ { //调用dao model := new(models.TBaseTeacher) model.PersonId = CommonUtil.GetUUID() model.Xm = chinaid.Name() model.Xmpy = PinYinUtil.PinYin(model.Xm) model.Cym = model.Xm model.Sfzjlxm = "1" model.Sfzjh = chinaid.IDNo() //如果不是身份证号,那就用个默认值吧 model.Csrq = chinaid.RandDate() model.Xbm = "2" model.Mzm = "01" model.Zzmmm = "13" model.Xlm = "90" model.Xwm = "0" model.Zcm = "5" model.StateId = 1 model.Bzlbm = "9" model.Cjny = DateUtil.ConvertDate("1900-01-01") model.StageId = "Z" model.SubjectId = "99" model.Gwzym = "80" model.Lxdh=chinaid.Mobile() model.Dzxx=chinaid.Email() model.BUse = 1 //教师的身份ID model.IdentityId = 2 //调用DAO保存 arrayTBaseTeacher = append(arrayTBaseTeacher, *model) // 生成教师账号 addLoginPerson(2, model.PersonId, model.Xm, accountArray[i]) } } /** 功能:在指定学校下添加班级 作者:黄海 时间:2020-06-10 */ func addClass(schoolBean schoolStruct, provinceCode string, cityCode string, districtCode string, classCount int) []string { //声明实体数组 classList := make([]string, 0) //循环添加班级 for i := 1; i <= classCount; i++ { model := new(models.TBaseClass) model.ClassId = CommonUtil.GetUUID() classList = append(classList, model.ClassId) //班号 model.Bh = int32(i) //入学年份 model.Rxnf = 2019 //ClassCode是我来规定的,规则是两位学段+ 入学年份+两位班号,前补零 model.ClassCode =CommonUtil.ConvertInt32ToString(2019) +fmt.Sprintf("%01d", 2)+ fmt.Sprintf("%02d", i) //ClassName --->入学年份+班号 model.ClassName = CommonUtil.ConvertInt32ToString(2019) + "级" + CommonUtil.ConvertIntToString(i) + "班" //ClassAlias --->入学年份+班号 model.ClassAlias = model.ClassName //入学季节 model.Rxjj = 9 //暂时使用秋季表示的月份 //学制 model.SchoolingLength = 3 //学段 model.StageId = 3 //这个不能给,这个是其它接口再补的 //model.TeacherId = in.TeacherId //这个用于职业学校扩展,暂不提供 //model.OrgId = in.OrgId //单位ID model.BureauId = schoolBean.SchoolId //主校ID model.MainSchoolId = schoolBean.SchoolId //是否启用 model.BUse = 1 //扩展省,市,县区码 model.ProvinceCode = provinceCode model.CityCode = cityCode model.DistrictCode = districtCode arrayTBaseClass = append(arrayTBaseClass, *model) fmt.Println("准备班级数据:" + schoolBean.SchoolName + model.ClassName) } return classList } /** 功能:在指定班级下添加学生 作者:黄海 时间:2020-06-10 */ func addStudent(bureauId string, classId string, provinceCode string, cityCode string, districtCode string, studentCount int) { //生成登录名和密码 studentAccountArray := GenerateLoginAccount(3, int64(studentCount)) for i := 0; i < studentCount; i++ { //调用dao model := new(models.TBaseStudent) //学生ID model.PersonId = CommonUtil.GetUUID() //学生姓名 model.Xm = chinaid.Name() //姓名拼音 model.Xmpy = PinYinUtil.PinYin(model.Xm) //曾用名 model.Cym = model.Xm //身份证件号 model.Sfzjh = chinaid.IDNo() model.Sfzjlxm = "1" //如果不是身份证号,那就用个默认值吧 model.Csrq = chinaid.RandDate() model.Xbm = "1" //民族 (有字典) model.Mzm = "1" //政治面貌 (有字典) model.Zzmmm = "13" //独生子女标志 -1:不是 1:是 model.Dszybz = -1 //随迁子女标志 -1:不是 1:是 model.Sqznbz = -1 //进城务工人员随迁子女标志 -1:不是 1:是 model.Jcwgrysqznbz = -1 //孤儿标志 -1:不是 1:是 model.Gebz = -1 //留守儿童标志 -1:不是 1:是 model.Lsetbz = -1 //残疾标志 -1:不是 1:是 model.Cjbz = -1 //班级ID model.ClassId = classId //是否启用 0:未启用 1:启用 model.BUse = 1 //状态码 model.StateId = 1 //省市县区 model.ProvinceCode = provinceCode model.CityCode = cityCode model.DistrictCode = districtCode model.MainSchoolId = bureauId //学校ID model.BureauId = bureauId //添加学生 arrayTBaseStudent = append(arrayTBaseStudent, *model) //为学生创建账号 addLoginPerson(3, model.PersonId, model.Xm, studentAccountArray[i]) //为学生创建对应的家长 modelParent := new(models.TBaseParent) modelParent.PersonId = CommonUtil.GetUUID() modelParent.Xm = model.Xm + "家长" modelParent.Lxdh = "-1" modelParent.StudentId = model.PersonId modelParent.ClassId = model.ClassId modelParent.BureauId = model.BureauId modelParent.ProvinceCode = model.ProvinceCode modelParent.CityCode = model.CityCode modelParent.DistrictCode = model.DistrictCode modelParent.MainSchoolId = model.MainSchoolId modelParent.BUse = 1 arrayTBaseParent = append(arrayTBaseParent, *modelParent) } } func main() { //禁用外键约束 sql := "SET foreign_key_checks = 0" db.Exec(sql) //城市:长春 areaCode := "220100" sql = `select t1.*,t2.org_name,t2.org_id from t_gov_area as t1 inner join t_base_organization as t2 on t1.area_code=t2.area_code and t2.org_type =1 where t1.area_code=? or t1.master_code=?` list, _ := db.SQL(sql, areaCode, areaCode).Query().List() //遍历城市与城市下的县区 for k := range list { //1、在市下面添加教辅单位 areaCode := list[k]["area_code"].(string) areaName := list[k]["area_name"].(string) provinceCode := list[k]["area_code"].(string)[0:2] + "0000" cityCode := list[k]["area_code"].(string)[0:4] + "00" jiaoYuJuId := list[k]["org_id"].(string) var districtCode string if cityCode == list[k]["area_code"].(string) { districtCode = "-1" } else { districtCode = list[k]["area_code"].(string) } //需要对area_code---->jiaoYuJuId bureauList := addBureau(areaCode, jiaoYuJuId, areaName, provinceCode, cityCode, districtCode, bureauCount) //2、在教辅单位下添加人员 for i := range bureauList { addTeacher(bureauList[i], provinceCode, cityCode, districtCode, bureauList[i], teacherCount) } //3、在市+县区下面添加学校 schoolList := addSchool(areaCode, jiaoYuJuId, areaName, provinceCode, cityCode, districtCode, schoolCount) //4、在学校下 for i := range schoolList { //添加人员 addTeacher(schoolList[i].SchoolId, provinceCode, cityCode, districtCode, schoolList[i].SchoolId, teacherCount) //添加班级 classList := addClass(schoolList[i], provinceCode, cityCode, districtCode, classCount) for j := range classList { //添加学生 addStudent(schoolList[i].SchoolId, classList[j], provinceCode, cityCode, districtCode, studentCount) } } } var err error var batchSize =50 //1、组织机构 var t1 = make([]models.TBaseOrganization, 0) for i := range arrayTBaseOrganization { t1 = append(t1, arrayTBaseOrganization[i]) if (i > 0 && (i+1)%batchSize == 0) || i == len(arrayTBaseOrganization)-1 { _, err = db.Insert(t1) if err != nil { log.Fatalln(err.Error()) } fmt.Println("成功插入组织机构" + CommonUtil.ConvertIntToString(len(t1)) + "条!") //清空数组 t1 = t1[0:0] } } //2、教师 var t2 = make([]models.TBaseTeacher, 0) for i := range arrayTBaseTeacher { t2 = append(t2, arrayTBaseTeacher[i]) if (i > 0 && (i+1)%batchSize == 0) || i == len(arrayTBaseTeacher)-1 { _, err = db.Insert(t2) if err != nil { log.Fatalln(err.Error()) } fmt.Println("成功插入教师" + CommonUtil.ConvertIntToString(len(t2)) + "条!") //清空数组 t2 = t2[0:0] } } //3、人员角色 var t3 = make([]models.TBaseRolePerson, 0) for i := range arrayTBaseRolePerson { t3 = append(t3, arrayTBaseRolePerson[i]) if (i > 0 && (i+1)%batchSize == 0) || i == len(arrayTBaseRolePerson)-1 { _, err = db.Insert(t3) if err != nil { log.Fatalln(err.Error()) } fmt.Println("成功插入角色人员" + CommonUtil.ConvertIntToString(len(t3)) + "条!") //清空数组 t3 = t3[0:0] } } //4、班级 var t4 = make([]models.TBaseClass, 0) for i := range arrayTBaseClass { t4 = append(t4, arrayTBaseClass[i]) if (i > 0 && (i+1)%batchSize == 0) || i == len(arrayTBaseClass)-1 { _, err = db.Insert(t4) if err != nil { log.Fatalln(err.Error()) } fmt.Println("成功插入班级数据" + CommonUtil.ConvertIntToString(len(t4)) + "条!") //清空数组 t4 = t4[0:0] } } //5、学生 var t5 = make([]models.TBaseStudent, 0) for i := range arrayTBaseStudent { t5 = append(t5, arrayTBaseStudent[i]) if (i > 0 && (i+1)%batchSize == 0) || i == len(arrayTBaseStudent)-1 { _, err = db.Insert(t5) if err != nil { log.Fatalln(err.Error()) } fmt.Println("成功插入学生数据" + CommonUtil.ConvertIntToString(len(t5)) + "条!") //清空数组 t5 = t5[0:0] } } //6、家长 var t6 = make([]models.TBaseParent, 0) for i := range arrayTBaseParent { t6 = append(t6, arrayTBaseParent[i]) if (i > 0 && (i+1)%batchSize == 0) || i == len(arrayTBaseParent)-1 { _, err = db.Insert(t6) if err != nil { log.Fatalln(err.Error()) } fmt.Println("成功插入家长数据" + CommonUtil.ConvertIntToString(len(t6)) + "条!") //清空数组 t6 = t6[0:0] } } //7、登录信息 var t7 = make([]models.TSysLoginperson, 0) for i := range arrayTSysLoginperson { t7 = append(t7, arrayTSysLoginperson[i]) if (i > 0 && (i+1)%batchSize == 0) || i == len(arrayTSysLoginperson)-1 { _, err = db.Insert(t7) if err != nil { log.Fatalln(err.Error()) } fmt.Println("成功插入登录人员" + CommonUtil.ConvertIntToString(len(t7)) + "条!") //清空数组 t7 = t7[0:0] } } //恢复外键约束 sql = "SET foreign_key_checks = 1" db.Exec(sql) fmt.Println("恭喜,所有模拟数据生成成功!") }