company.go 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. package service
  2. import (
  3. "cold-logistics/common/actions"
  4. cDto "cold-logistics/common/dto"
  5. "cold-logistics/common/global"
  6. "cold-logistics/common/nats/nats_server"
  7. "errors"
  8. "gogs.baozhida.cn/zoie/OAuth-core/service"
  9. "gorm.io/gorm"
  10. "gorm.io/gorm/clause"
  11. "cold-logistics/app/admin/model"
  12. "cold-logistics/app/admin/service/dto"
  13. "gogs.baozhida.cn/zoie/OAuth-core/pkg"
  14. )
  15. type Company struct {
  16. service.Service
  17. }
  18. // GetPage 获取Address列表
  19. func (e *Company) GetPage(c *dto.CompanyGetPageReq, list *[]model.SysDept, count *int64) error {
  20. var err error
  21. var data model.SysDept
  22. err = e.Orm.Model(&data).
  23. Scopes(
  24. cDto.MakeCondition(c.GetNeedSearch()),
  25. cDto.Paginate(c.GetPageSize(), c.GetPageIndex()),
  26. ).
  27. Find(list).Limit(-1).Offset(-1).
  28. Count(count).Error
  29. if err != nil {
  30. e.Log.Errorf("db error: %s", err)
  31. return global.GetFailedErr
  32. }
  33. return nil
  34. }
  35. func CompanyIdScopes(id int) func(db *gorm.DB) *gorm.DB {
  36. return func(db *gorm.DB) *gorm.DB {
  37. if id == 0 {
  38. return db
  39. }
  40. return db.Where("id = ? ", id)
  41. }
  42. }
  43. func (e *Company) GetAll(c *dto.CompanyGetAllReq, list *[]model.SysDept, count *int64, p *actions.DataPermission) error {
  44. var err error
  45. var data model.SysDept
  46. err = e.Orm.Model(&data).
  47. Scopes(
  48. CompanyIdScopes(p.DeptId),
  49. cDto.MakeCondition(c.GetNeedSearch()),
  50. ).
  51. Find(list).Limit(-1).Offset(-1).
  52. Count(count).Error
  53. if err != nil {
  54. e.Log.Errorf("db error: %s", err)
  55. return global.GetFailedErr
  56. }
  57. return nil
  58. }
  59. // Get 获取SysDept对象
  60. func (e *Company) Get(d *dto.CompanyGetReq, deptModel *model.SysDept) error {
  61. err := e.Orm.
  62. First(deptModel, d.GetId()).Error
  63. if err != nil {
  64. e.Log.Errorf("db error: %s", err)
  65. if errors.Is(err, gorm.ErrRecordNotFound) {
  66. return global.GetNotFoundOrNoPermissionErr
  67. }
  68. return global.GetFailedErr
  69. }
  70. return nil
  71. }
  72. // Insert 创建SysDept对象
  73. func (e *Company) Insert(c *dto.CompanyInsertReq) error {
  74. var err error
  75. var data model.SysDept
  76. tx := e.Orm.Debug().Begin()
  77. defer func() {
  78. if err != nil {
  79. tx.Rollback()
  80. } else {
  81. tx.Commit()
  82. }
  83. }()
  84. var i int64
  85. err = e.Orm.Model(&data).Where("name = ?", c.Name).Count(&i).Error
  86. if err != nil {
  87. e.Log.Errorf("db error: %s", err)
  88. return global.CreateFailedErr
  89. }
  90. if i > 0 {
  91. err = errors.New("公司名称已存在!")
  92. e.Log.Errorf("db error: %s", err)
  93. return err
  94. }
  95. coldCompany, err := nats_server.Cold_ReadCompanyByT_key(c.ColdKey)
  96. if err != nil {
  97. err = errors.New("查询公司秘钥信息失败,请检查秘钥是否正确!")
  98. e.Log.Errorf("db error: %s", err)
  99. return err
  100. }
  101. data.ColdPid = coldCompany.Id
  102. c.Generate(&data)
  103. err = tx.Create(&data).Error
  104. if err != nil {
  105. e.Log.Errorf("db error: %s", err)
  106. return global.CreateFailedErr
  107. }
  108. c.Id = data.Id
  109. deptPath := pkg.IntToString(data.Id) + "/"
  110. if data.ParentId != 0 {
  111. var deptP model.SysDept
  112. tx.First(&deptP, data.ParentId)
  113. deptPath = deptP.Path + deptPath
  114. } else {
  115. deptPath = "/0/" + deptPath
  116. }
  117. var mp = map[string]string{}
  118. mp["path"] = deptPath
  119. err = tx.Model(&data).Update("path", deptPath).Error
  120. if err != nil {
  121. e.Log.Errorf("db error: %s", err)
  122. return global.CreateFailedErr
  123. }
  124. return nil
  125. }
  126. // Update 修改SysDept对象
  127. func (e *Company) Update(c *dto.CompanyUpdateReq) error {
  128. var err error
  129. var deptModel = model.SysDept{}
  130. tx := e.Orm.Debug().Begin()
  131. defer func() {
  132. if err != nil {
  133. tx.Rollback()
  134. } else {
  135. tx.Commit()
  136. }
  137. }()
  138. err = tx.First(&deptModel, c.GetId()).Error
  139. if err != nil {
  140. e.Log.Errorf("db error: %s", err)
  141. if errors.Is(err, gorm.ErrRecordNotFound) {
  142. return global.UpdateNotFoundOrNoPermissionErr
  143. }
  144. return global.UpdateFailedErr
  145. }
  146. if c.ColdKey != deptModel.ColdKey {
  147. var coldCompany nats_server.Company
  148. coldCompany, err = nats_server.Cold_ReadCompanyByT_key(c.ColdKey)
  149. if err != nil {
  150. err = errors.New("查询公司秘钥信息失败,请检查秘钥是否正确!")
  151. e.Log.Errorf("db error: %s", err)
  152. return err
  153. }
  154. deptModel.ColdPid = coldCompany.Id
  155. }
  156. c.Generate(&deptModel)
  157. DeptPath := pkg.IntToString(deptModel.Id) + "/"
  158. if deptModel.ParentId != 0 {
  159. var DeptP model.SysDept
  160. tx.First(&DeptP, deptModel.ParentId)
  161. DeptPath = DeptP.Path + DeptPath
  162. } else {
  163. DeptPath = "/0/" + DeptPath
  164. }
  165. deptModel.Path = DeptPath
  166. db := tx.Save(&deptModel)
  167. if err = db.Error; err != nil {
  168. e.Log.Errorf("UpdateSysDept error: %s", err)
  169. return err
  170. }
  171. return nil
  172. }
  173. // Update 修改SysDept对象
  174. func (e *Company) UpdateMyCompany(c *dto.MyCompanyUpdateReq) error {
  175. var err error
  176. var deptModel = model.SysDept{}
  177. tx := e.Orm.Debug().Begin()
  178. defer func() {
  179. if err != nil {
  180. tx.Rollback()
  181. } else {
  182. tx.Commit()
  183. }
  184. }()
  185. err = tx.First(&deptModel, c.GetId()).Error
  186. if err != nil {
  187. e.Log.Errorf("db error: %s", err)
  188. if errors.Is(err, gorm.ErrRecordNotFound) {
  189. return global.UpdateNotFoundOrNoPermissionErr
  190. }
  191. return global.UpdateFailedErr
  192. }
  193. c.Generate(&deptModel)
  194. db := tx.Save(&deptModel)
  195. if err = db.Error; err != nil {
  196. e.Log.Errorf("UpdateSysDept error: %s", err)
  197. return err
  198. }
  199. return nil
  200. }
  201. // Remove 删除SysDept
  202. // 删除部门 同时删除其子部门、部门下的用户
  203. func (e *Company) Remove(d *dto.CompanyDeleteReq, p *actions.DataPermission) error {
  204. var err error
  205. tx := e.Orm.Begin()
  206. defer func() {
  207. if err != nil {
  208. tx.Rollback()
  209. } else {
  210. tx.Commit()
  211. }
  212. }()
  213. var dept model.SysDept
  214. err = e.Orm.
  215. First(&dept, d.GetId()).Error
  216. if err != nil {
  217. e.Log.Errorf("db error: %s", err)
  218. if errors.Is(err, gorm.ErrRecordNotFound) {
  219. return global.GetNotFoundOrNoPermissionErr
  220. }
  221. return global.DeleteFailedErr
  222. }
  223. var deptList = make([]model.SysDept, 0)
  224. var deptIds = make([]int, 0)
  225. tx.Where("path like ?", dept.Path+"%").Find(&deptList)
  226. // 获取部门及其子部门下ID
  227. for _, v := range deptList {
  228. deptIds = append(deptIds, v.Id)
  229. }
  230. db := tx.Select(clause.Associations).Delete(&deptList)
  231. if err = db.Error; err != nil {
  232. e.Log.Errorf("Delete error: %s", err)
  233. return global.DeleteFailedErr
  234. }
  235. if db.RowsAffected == 0 {
  236. return global.DeleteNotFoundOrNoPermissionErr
  237. }
  238. // 删除部门下的用户
  239. dbUser := tx.Where("dept_id in (?)", deptIds).Delete(&model.SysUser{})
  240. if err = dbUser.Error; err != nil {
  241. e.Log.Errorf("Delete User error: %s", err)
  242. return global.DeleteFailedErr
  243. }
  244. return nil
  245. }