car.go 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  1. package service
  2. import (
  3. "cold-logistics/app/admin/model"
  4. "cold-logistics/app/admin/service/dto"
  5. "cold-logistics/common/actions"
  6. cDto "cold-logistics/common/dto"
  7. "cold-logistics/common/global"
  8. "cold-logistics/common/nats/nats_server"
  9. "errors"
  10. "gogs.baozhida.cn/zoie/OAuth-core/service"
  11. "gorm.io/gorm"
  12. )
  13. type Car struct {
  14. service.Service
  15. }
  16. func CarIsBindUserScopes(isBind bool) func(db *gorm.DB) *gorm.DB {
  17. return func(db *gorm.DB) *gorm.DB {
  18. // 未发货
  19. if isBind {
  20. return db.Where("user_id > 0")
  21. }
  22. return db
  23. }
  24. }
  25. // GetPage 获取Car列表
  26. func (e *Car) GetPage(c *dto.CarGetPageReq, list *[]model.Car, count *int64, p *actions.DataPermission) error {
  27. var err error
  28. var data model.Car
  29. if c.PageSize == 9999 {
  30. err = e.Orm.Model(&data).
  31. Scopes(
  32. actions.Permission(data.TableName(), p),
  33. ).
  34. Find(list).Limit(-1).Offset(-1).
  35. Count(count).Error
  36. } else {
  37. err = e.Orm.Model(&data).
  38. Scopes(
  39. cDto.MakeCondition(c.GetNeedSearch()),
  40. cDto.Paginate(c.GetPageSize(), c.GetPageIndex()),
  41. actions.Permission(data.TableName(), p),
  42. CarIsBindUserScopes(c.IsBind),
  43. ).
  44. Joins("User").
  45. Where("car_no like ? or User.nick_name like ?", "%"+c.Name+"%", "%"+c.Name+"%").
  46. Find(list).Limit(-1).Offset(-1).
  47. Count(count).Error
  48. }
  49. if err != nil {
  50. e.Log.Errorf("db error: %s", err)
  51. return global.GetFailedErr
  52. }
  53. return nil
  54. }
  55. // Get 获取Car对象
  56. func (e *Car) Get(d *dto.CarGetReq, carModel *model.Car, p *actions.DataPermission) error {
  57. err := e.Orm.
  58. Scopes(actions.Permission(carModel.TableName(), p)).
  59. Preload("User").
  60. First(carModel, d.GetId()).Error
  61. if err != nil {
  62. e.Log.Errorf("db error: %s", err)
  63. if errors.Is(err, gorm.ErrRecordNotFound) {
  64. return global.GetNotFoundOrNoPermissionErr
  65. }
  66. return global.GetFailedErr
  67. }
  68. return nil
  69. }
  70. // Insert 创建Car对象
  71. func (e *Car) Insert(c *dto.CarInsertReq, p *actions.DataPermission) error {
  72. var err error
  73. var data model.Car
  74. tx := e.Orm.Begin()
  75. defer func() {
  76. if err != nil {
  77. tx.Rollback()
  78. } else {
  79. tx.Commit()
  80. }
  81. }()
  82. var i int64
  83. err = tx.Model(&data).Where("car_no = ?", c.CarNo).Count(&i).Error
  84. if err != nil {
  85. e.Log.Errorf("db error: %s", err)
  86. return global.CreateFailedErr
  87. }
  88. if i > 0 {
  89. err = errors.New("车牌号已存在!")
  90. e.Log.Errorf("db error: %s", err)
  91. return err
  92. }
  93. var j int64
  94. err = tx.Model(&data).Where("user_id = ?", c.UserId).Count(&j).Error
  95. if err != nil {
  96. e.Log.Errorf("db error: %s", err)
  97. return global.CreateFailedErr
  98. }
  99. if j > 0 {
  100. err = errors.New("该用户已绑定其他车辆!")
  101. e.Log.Errorf("db error: %s", err)
  102. return err
  103. }
  104. var k int64
  105. err = tx.Model(&data).Where("sn = ?", c.Sn).Count(&k).Error
  106. if err != nil {
  107. e.Log.Errorf("db error: %s", err)
  108. return global.CreateFailedErr
  109. }
  110. if k > 0 {
  111. err = errors.New("该SN已绑定其他车辆!")
  112. e.Log.Errorf("db error: %s", err)
  113. return err
  114. }
  115. var device nats_server.Device
  116. var company model.SysDept
  117. device, err = nats_server.Cold_ReadDeviceByT_sn(c.Sn)
  118. if err != nil {
  119. err = errors.New("获取SN信息失败,请检查SN是否正确!")
  120. e.Log.Errorf("db error: %s", err)
  121. return err
  122. }
  123. company, err = model.GetCompanyById(p.DeptId)
  124. if err != nil {
  125. e.Log.Errorf("db error: %s", err)
  126. return err
  127. }
  128. if device.T_pid != company.Id {
  129. err = errors.New("获取SN信息失败,请检查SN是否正确!")
  130. e.Log.Errorf("获取SN信息失败,device.T_pid != company.Id")
  131. return err
  132. }
  133. // 添加车辆
  134. c.Generate(&data)
  135. data.HistorySn = []string{data.Sn}
  136. err = tx.Create(&data).Error
  137. if err != nil {
  138. e.Log.Errorf("db error: %s", err)
  139. return global.CreateFailedErr
  140. }
  141. c.Id = data.Id
  142. return nil
  143. }
  144. // Update 修改Car对象
  145. func (e *Car) Update(c *dto.CarUpdateReq, p *actions.DataPermission) error {
  146. var err error
  147. tx := e.Orm.Begin()
  148. defer func() {
  149. if err != nil {
  150. tx.Rollback()
  151. } else {
  152. tx.Commit()
  153. }
  154. }()
  155. var carModel = model.Car{}
  156. // 查询车辆是否存在
  157. err = e.Orm.Scopes(actions.Permission(carModel.TableName(), p)).
  158. First(&carModel, c.GetId()).Error
  159. if err != nil {
  160. e.Log.Errorf("db error: %s", err)
  161. if errors.Is(err, gorm.ErrRecordNotFound) {
  162. return global.UpdateNotFoundOrNoPermissionErr
  163. }
  164. return global.UpdateFailedErr
  165. }
  166. if carModel.Sn != c.Sn {
  167. var k int64
  168. var data = model.Car{}
  169. err = tx.Model(&data).Where("sn = ?", c.Sn).Count(&k).Error
  170. if err != nil {
  171. e.Log.Errorf("db error: %s", err)
  172. return global.CreateFailedErr
  173. }
  174. if k > 0 {
  175. err = errors.New("该Sn已绑定其他车辆!")
  176. e.Log.Errorf("db error: %s", err)
  177. return err
  178. }
  179. var device nats_server.Device
  180. var company model.SysDept
  181. device, err = nats_server.Cold_ReadDeviceByT_sn(c.Sn)
  182. if err != nil {
  183. err = errors.New("获取SN信息失败,请检查SN是否正确!")
  184. e.Log.Errorf("db error: %s", err)
  185. return err
  186. }
  187. company, err = model.GetCompanyById(p.DeptId)
  188. if err != nil {
  189. e.Log.Errorf("db error: %s", err)
  190. return err
  191. }
  192. if device.T_pid != company.Id {
  193. err = errors.New("获取SN信息失败,请检查SN是否正确!")
  194. e.Log.Errorf("获取SN信息失败,device.T_pid != company.Id")
  195. return err
  196. }
  197. carModel.HistorySn = append(carModel.HistorySn, c.Sn)
  198. }
  199. if carModel.UserId != c.UserId {
  200. var k int64
  201. var data = model.Car{}
  202. err = tx.Model(&data).Where("user_id = ?", c.UserId).Count(&k).Error
  203. if err != nil {
  204. e.Log.Errorf("db error: %s", err)
  205. return global.CreateFailedErr
  206. }
  207. if k > 0 {
  208. err = errors.New("该用户已绑定其他车辆!")
  209. e.Log.Errorf("db error: %s", err)
  210. return err
  211. }
  212. }
  213. c.Generate(&carModel)
  214. err = tx.Save(&carModel).Error
  215. if err != nil {
  216. e.Log.Errorf("db error: %s", err)
  217. return global.UpdateFailedErr
  218. }
  219. c.Id = carModel.Id
  220. return nil
  221. }
  222. // Remove 删除Car
  223. func (e *Car) Remove(c *dto.CarDeleteReq, p *actions.DataPermission) error {
  224. var err error
  225. tx := e.Orm.Begin()
  226. defer func() {
  227. if err != nil {
  228. tx.Rollback()
  229. } else {
  230. tx.Commit()
  231. }
  232. }()
  233. var carModel model.Car
  234. // 查询车辆是否存在
  235. err = e.Orm.Scopes(actions.Permission(carModel.TableName(), p)).
  236. First(&carModel, c.GetId()).Error
  237. if err != nil {
  238. e.Log.Errorf("db error: %s", err)
  239. if errors.Is(err, gorm.ErrRecordNotFound) {
  240. return global.DeleteNotFoundOrNoPermissionErr
  241. }
  242. return global.DeleteFailedErr
  243. }
  244. db := tx.Delete(&carModel)
  245. if err = db.Error; err != nil {
  246. e.Log.Errorf("db error: %s", err)
  247. return global.DeleteFailedErr
  248. }
  249. if db.RowsAffected == 0 {
  250. return global.DeleteNotFoundOrNoPermissionErr
  251. }
  252. return nil
  253. }
  254. // GetUserList 获取绑定车辆的用户
  255. func (e *Car) GetUserList(c *dto.SysUserGetPageReq, p *actions.DataPermission, list *[]model.SysUserForBind, count *int64) error {
  256. var err error
  257. var userList []model.SysUserForBind
  258. var carModel model.Car
  259. var userModel model.SysUserForBind
  260. var userIds []int
  261. // 查询车辆是否存在
  262. err = e.Orm.Model(&carModel).Select("user_id").Scopes(actions.Permission(carModel.TableName(), p)).
  263. Find(&userIds).Error
  264. if err != nil {
  265. e.Log.Errorf("db error: %s", err)
  266. return global.GetFailedErr
  267. }
  268. err = e.Orm.
  269. Scopes(
  270. cDto.MakeCondition(c.GetNeedSearch()),
  271. cDto.Paginate(c.GetPageSize(), c.GetPageIndex()),
  272. actions.UserPermission(userModel.TableName(), p),
  273. ).
  274. Where("user_type = ? ", model.UserTypeSys).
  275. Find(&userList).Limit(-1).Offset(-1).
  276. Count(count).Error
  277. if err != nil {
  278. e.Log.Errorf("db error: %s", err)
  279. return global.GetFailedErr
  280. }
  281. userIdMap := make(map[int]bool)
  282. for _, id := range userIds {
  283. userIdMap[id] = true
  284. }
  285. for i := 0; i < len(userList); i++ {
  286. if _, ok := userIdMap[userList[i].Id]; ok {
  287. userList[i].IsBind = true
  288. }
  289. }
  290. *list = userList
  291. return nil
  292. }
  293. func (e *Car) BatchInsert(c *dto.CarBatchInsertReq) error {
  294. var err error
  295. tx := e.Orm.Begin()
  296. defer func() {
  297. if err != nil {
  298. tx.Rollback()
  299. } else {
  300. tx.Commit()
  301. }
  302. }()
  303. for _, car := range c.List {
  304. var data model.Car
  305. var k int64
  306. err = tx.Model(&data).Where("sn = ?", car.Sn).Count(&k).Error
  307. if err != nil {
  308. e.Log.Errorf("db error: %s", err)
  309. return global.CreateFailedErr
  310. }
  311. if k > 0 {
  312. continue
  313. }
  314. // 添加车辆
  315. car.CreateBy = c.CreateBy
  316. car.DeptId = c.DeptId
  317. car.Status = c.Status
  318. car.UserId = c.UserId
  319. car.Generate(&data)
  320. data.HistorySn = []string{data.Sn}
  321. err = tx.Create(&data).Error
  322. if err != nil {
  323. e.Log.Errorf("db error: %s", err)
  324. return global.CreateFailedErr
  325. }
  326. }
  327. return nil
  328. }