|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162 |
- package svc
-
- import (
- enum2 "applet/app/admin/enum"
- "applet/app/admin/md"
- "applet/app/db"
- "applet/app/db/model"
- )
-
- func EnterpriseList(admin model.Admin, req md.EnterpriseListReq) (m []model.Enterprise, total int64, err error) {
- sess := db.Db.Desc("id")
- if admin.IsSuperAdministrator != enum2.IsSuperAdministratorTure {
- adminWithEnterpriseDb := db.AdminWithEnterpriseDb{}
- adminWithEnterpriseDb.Set()
- adminWithEnterprise, err1 := adminWithEnterpriseDb.FindAdminWithEnterprise(admin.AdmId)
- if err1 != nil {
- return nil, 0, err1
- }
- var enterPriseIds []int
- for _, v1 := range *adminWithEnterprise {
- enterPriseIds = append(enterPriseIds, v1.EnterpriseId)
- }
- if len(enterPriseIds) == 0 {
- return
- }
- sess.In("id", enterPriseIds)
- }
- sess.Limit(req.Limit, (req.Page-1)*req.Limit)
- if req.Name != "" {
- sess.And("name like ?", "%"+req.Name+"%")
- }
- if req.Kind != 0 {
- sess.And("kind = ?", req.Kind)
- }
- total, err = sess.FindAndCount(&m)
- if err != nil {
- return
- }
- return
- }
-
- func EnterpriseDetail(enterpriseId int) (data md.EnterpriseDetailResp, err error) {
- //1、查找所在单位
- enterpriseDb := db.EnterpriseDb{}
- enterpriseDb.Set()
- enterprise, err := enterpriseDb.GetEnterprise(enterpriseId)
- if err != nil {
- return
- }
- data.Enterprise = *enterprise
-
- //2、查找对应学段
- periodDb := db.PeriodDb{}
- periodDb.Set(enterpriseId)
- gradeDb := db.GradeDb{}
- gradeDb.Set(enterpriseId)
- classDb := db.ClassDb{}
-
- periodList, err := periodDb.FindPeriod()
- if err != nil {
- return
- }
-
- for _, v := range *periodList {
- //3、查找对应年级
- gradeList, err1 := gradeDb.FindGradeByPeriodId(v.Id)
- if err1 != nil {
- return
- }
-
- var tmpGradeListStruct []md.GradeListStruct
- for _, v1 := range *gradeList {
- //3、查找对应班级
- classDb.Set(v1.Id)
- classes, err2 := classDb.FindClass()
- if err2 != nil {
- err = err2
- return
- }
- tmpGradeListStruct = append(tmpGradeListStruct, md.GradeListStruct{
- Grade: v1,
- ClassList: *classes,
- })
- }
-
- data.PeriodList = append(data.PeriodList, md.PeriodListStruct{
- Period: v,
- GradeList: tmpGradeListStruct,
- })
- }
-
- return
- }
-
- func EnterpriseDetailV2(enterpriseId int) (data md.EnterpriseDetailRespV2, err error) {
- //1、查找所在单位
- enterpriseDb := db.EnterpriseDb{}
- enterpriseDb.Set()
- enterprise, err := enterpriseDb.GetEnterprise(enterpriseId)
- if err != nil {
- return
- }
- data.Enterprise = *enterprise
-
- //2、查找对应年级
- gradeDb := db.GradeDb{}
- gradeDb.Set(enterpriseId)
- gradeList, err := gradeDb.FindGrade()
- if err != nil {
- return
- }
-
- for _, v := range *gradeList {
- //3、查找对应班级
- classDb := db.ClassDb{}
- classDb.Set(v.Id)
- classes, err1 := classDb.FindClass()
- if err1 != nil {
- err = err1
- return
- }
- data.GradeList = append(data.GradeList, md.GradeListStructV2{
- Grade: v,
- ClassList: *classes,
- })
- }
- return
- }
-
- func EnterpriseDelete(enterpriseIds []int) (err error) {
- session := db.Db.NewSession()
- defer session.Close()
- session.Begin()
- //1、删除所在单位
- enterpriseDb := db.EnterpriseDb{}
- enterpriseDb.Set()
- _, err = enterpriseDb.EnterpriseDeleteBySession(session, enterpriseIds)
- if err != nil {
- _ = session.Rollback()
- return
- }
-
- //2、删除年级
- gradeDb := db.GradeDb{}
- gradeDb.Set(0)
- _, err = gradeDb.ClassDeleteBySessionForEnterprise(session, enterpriseIds)
- if err != nil {
- _ = session.Rollback()
- return
- }
-
- //3、删除班级
- classDb := db.ClassDb{}
- classDb.Set(0)
- _, err = classDb.ClassDeleteBySessionForEnterprise(session, enterpriseIds)
- if err != nil {
- _ = session.Rollback()
- return
- }
-
- return session.Commit()
- }
|