operation_log.go 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768
  1. package service
  2. import (
  3. "errors"
  4. "gas-cylinder-api/app/admin/model"
  5. "gas-cylinder-api/app/admin/service/dto"
  6. "gas-cylinder-api/common/actions"
  7. cDto "gas-cylinder-api/common/dto"
  8. "gas-cylinder-api/common/global"
  9. cModel "gas-cylinder-api/common/model"
  10. log "gogs.baozhida.cn/zoie/OAuth-core/logger"
  11. "gogs.baozhida.cn/zoie/OAuth-core/service"
  12. "gorm.io/gorm/utils"
  13. "gorm.io/gorm"
  14. "time"
  15. )
  16. type OperationLog struct {
  17. service.Service
  18. }
  19. // GetPage 获取OperationLog列表
  20. func (e *OperationLog) GetPage(c *dto.OperationLogGetPageReq, list *[]model.OperationLog, count *int64, p *actions.DataPermission) error {
  21. var err error
  22. var data model.OperationLog
  23. err = e.Orm.Model(&data).
  24. Scopes(
  25. cDto.MakeCondition(c.GetNeedSearch()),
  26. cDto.Paginate(c.GetPageSize(), c.GetPageIndex()),
  27. actions.Permission(data.TableName(), p),
  28. ).
  29. Preload("OptUserObj").
  30. Preload("ObjectUserObj").
  31. Preload("ObjectCustomerObj").
  32. Preload("OptCustomerObj").
  33. Preload("CompanyObj").
  34. Preload("CurrentEnterpriseObj").
  35. Preload("CurrentStationObj").
  36. Preload("CurrentStoreObj").
  37. Preload("CurrentTruckObj").
  38. Preload("CurrentAddressObj").
  39. Find(list).Limit(-1).Offset(-1).
  40. Count(count).Error
  41. if err != nil {
  42. e.Log.Errorf("db error: %s", err)
  43. return global.GetFailedErr
  44. }
  45. return nil
  46. }
  47. // Get 获取OperationLog对象
  48. func (e *OperationLog) Get(d *dto.OperationLogGetReq, carInfoModel *model.OperationLog, p *actions.DataPermission) error {
  49. err := e.Orm.
  50. Scopes(actions.Permission(carInfoModel.TableName(), p)).
  51. First(carInfoModel, d.GetId()).Error
  52. if err != nil {
  53. e.Log.Errorf("db error: %s", err)
  54. if errors.Is(err, gorm.ErrRecordNotFound) {
  55. return global.GetNotFoundOrNoPermissionErr
  56. }
  57. return global.GetFailedErr
  58. }
  59. return nil
  60. }
  61. // Insert 创建OperationLog对象
  62. func (e *OperationLog) Insert(c *dto.OperationLogInsertReq, p *actions.DataPermission) error {
  63. var err error
  64. data := make([]model.OperationLog, 0)
  65. //company, err := model.GetProvCodeById(p.DeptId)
  66. if err != nil {
  67. e.Log.Errorf("db error: %s", err)
  68. return global.CreateFailedErr
  69. }
  70. user, err := model.GetUserCode(p.UserId)
  71. if err != nil {
  72. e.Log.Errorf("db error: %s", err)
  73. return global.CreateFailedErr
  74. }
  75. tx := e.Orm.Begin()
  76. defer func() {
  77. if err != nil {
  78. tx.Rollback()
  79. } else {
  80. tx.Commit()
  81. }
  82. }()
  83. switch c.OptType {
  84. case "25", "27":
  85. // 送气员领重瓶
  86. for _, chipUid := range c.ChipUidList {
  87. // 1、通过高频ID查询气瓶内编码
  88. var gasCylinder model.GasCylinder
  89. err = tx.Where("uid = ?", chipUid).First(&gasCylinder).Error
  90. if err != nil {
  91. tx.Rollback()
  92. log.Errorf("db error: %s", err)
  93. return global.CreateFailedErr
  94. }
  95. operationLog := model.OperationLog{
  96. ProvOperationLog: model.ProvOperationLog{
  97. InnerCode: gasCylinder.InnerCode,
  98. OptType: c.OptType,
  99. OptUser: user.ProvUserId,
  100. ObjectUser: user.ProvUserId,
  101. CompanyId: user.ProvUser.CmpCode,
  102. CurrentEnterprise: user.ProvUser.CmpCode,
  103. CurrentStore: user.Dept.CmpCode,
  104. Lng: utils.ToString(user.Dept.ProvStore.Lng),
  105. Lat: utils.ToString(user.Dept.ProvStore.Lat),
  106. OptTime: time.Now().Format("2006-01-02 15:04:05"),
  107. },
  108. ControlBy: cModel.ControlBy{
  109. CreateBy: p.UserId,
  110. },
  111. DeptBy: cModel.DeptBy{
  112. DeptId: p.DeptId,
  113. },
  114. }
  115. if c.OptType == "27" {
  116. // 修改用户关联的气瓶信息
  117. var cgc model.CustomerGasCylinder
  118. err = tx.Where("inner_code = ? and state = 1", gasCylinder.InnerCode).First(&cgc).Error
  119. if err != nil {
  120. log.Errorf("db error: %s", err)
  121. return global.CreateFailedErr
  122. }
  123. cgc.ReturnTime = cModel.Time(time.Now())
  124. cgc.State = 2
  125. err = tx.Save(&cgc).Error
  126. if err != nil {
  127. log.Errorf("db error: %s", err)
  128. return global.CreateFailedErr
  129. }
  130. operationLog.ObjectCustomer = cgc.CustomerId
  131. operationLog.OptCustomer = cgc.CustomerId
  132. operationLog.CurrentAddress = cgc.CustomerId
  133. }
  134. data = append(data, operationLog)
  135. }
  136. case "6", "10", "21", "31", "33", "34", "35", "37":
  137. // 门店端
  138. for _, chipUid := range c.ChipUidList {
  139. // 1、通过高频ID查询气瓶内编码
  140. var gasCylinder model.GasCylinder
  141. err = tx.Where("uid = ?", chipUid).First(&gasCylinder).Error
  142. if err != nil {
  143. tx.Rollback()
  144. log.Errorf("db error: %s", err)
  145. return global.CreateFailedErr
  146. }
  147. if c.OptType == "21" {
  148. err = tx.Model(&model.OperationLog{}).
  149. Where("inner_code = ? and state = 1", gasCylinder.InnerCode).
  150. Update("state", 2).Error
  151. if err != nil {
  152. tx.Rollback()
  153. log.Errorf("db error: %s", err)
  154. return global.CreateFailedErr
  155. }
  156. }
  157. operationLog := model.OperationLog{
  158. ProvOperationLog: model.ProvOperationLog{
  159. InnerCode: gasCylinder.InnerCode,
  160. OptType: c.OptType,
  161. OptUser: user.ProvUserId,
  162. ObjectUser: user.ProvUserId,
  163. CompanyId: user.ProvUser.CmpCode,
  164. CurrentEnterprise: user.ProvUser.CmpCode,
  165. CurrentStore: user.ProvUser.CmpCode,
  166. Lng: utils.ToString(user.Dept.ProvStore.Lng),
  167. Lat: utils.ToString(user.Dept.ProvStore.Lat),
  168. OptTime: time.Now().Format("2006-01-02 15:04:05"),
  169. },
  170. ControlBy: cModel.ControlBy{
  171. CreateBy: p.UserId,
  172. },
  173. DeptBy: cModel.DeptBy{
  174. DeptId: p.DeptId,
  175. },
  176. }
  177. data = append(data, operationLog)
  178. }
  179. case "11", "12", "16", "17", "19":
  180. // 司机端
  181. for _, chipUid := range c.ChipUidList {
  182. // 1、通过高频ID查询气瓶内编码
  183. var gasCylinder model.GasCylinder
  184. err = tx.Where("uid = ?", chipUid).First(&gasCylinder).Error
  185. if err != nil {
  186. tx.Rollback()
  187. e.Log.Errorf("获取气瓶信息失败: %s", err)
  188. return errors.New("获取气瓶信息失败")
  189. }
  190. var truckUserCarInfo model.TruckUserCarInfo
  191. err = tx.Where("prov_user_id = ?", user.ProvUserId).First(&truckUserCarInfo).Error
  192. if err != nil {
  193. tx.Rollback()
  194. e.Log.Errorf("获取车辆信息失败: %s", err)
  195. return errors.New("获取车辆信息失败")
  196. }
  197. operationLog := model.OperationLog{
  198. ProvOperationLog: model.ProvOperationLog{
  199. InnerCode: gasCylinder.InnerCode,
  200. OptType: c.OptType,
  201. OptUser: user.ProvUserId,
  202. ObjectUser: user.ProvUserId,
  203. //CompanyId: user.ProvUser.CmpCode,
  204. //CurrentEnterprise: user.ProvUser.CmpCode,
  205. //CurrentStore: user.ProvUser.CmpCode,
  206. CurrentTruck: user.ProvUserId,
  207. CurrentMotor: truckUserCarInfo.CarNo,
  208. OptTime: time.Now().Format("2006-01-02 15:04:05"),
  209. },
  210. ControlBy: cModel.ControlBy{
  211. CreateBy: p.UserId,
  212. },
  213. DeptBy: cModel.DeptBy{
  214. DeptId: p.DeptId,
  215. },
  216. }
  217. if c.OptType == "12" || c.OptType == "16" || c.OptType == "17" {
  218. operationLog.CurrentStation = c.CurrentEnterprise
  219. operationLog.CompanyId = c.CurrentEnterprise
  220. operationLog.CurrentEnterprise = c.CurrentEnterprise
  221. }
  222. if c.OptType == "11" || c.OptType == "19" {
  223. operationLog.CurrentStore = c.CurrentEnterprise
  224. operationLog.CompanyId = c.CurrentEnterprise
  225. operationLog.CurrentEnterprise = c.CurrentEnterprise
  226. }
  227. data = append(data, operationLog)
  228. }
  229. case "13", "14", "15":
  230. // 气站端
  231. for _, chipUid := range c.ChipUidList {
  232. // 1、通过高频ID查询气瓶内编码
  233. var gasCylinder model.GasCylinder
  234. err = tx.Where("uid = ?", chipUid).First(&gasCylinder).Error
  235. if err != nil {
  236. tx.Rollback()
  237. e.Log.Errorf("获取气瓶信息失败: %s", err)
  238. return errors.New("获取气瓶信息失败")
  239. }
  240. operationLog := model.OperationLog{
  241. ProvOperationLog: model.ProvOperationLog{
  242. InnerCode: gasCylinder.InnerCode,
  243. OptType: c.OptType,
  244. ObjectUser: user.ProvUserId,
  245. OptUser: user.ProvUserId,
  246. CompanyId: user.ProvUser.CmpCode,
  247. CurrentEnterprise: user.ProvUser.CmpCode,
  248. //CurrentStore: user.ProvUser.CmpCode,
  249. CurrentStation: user.ProvUser.CmpCode,
  250. Lng: utils.ToString(user.Dept.ProvStore.Lng),
  251. Lat: utils.ToString(user.Dept.ProvStore.Lat),
  252. OptTime: time.Now().Format("2006-01-02 15:04:05"),
  253. },
  254. ControlBy: cModel.ControlBy{
  255. CreateBy: p.UserId,
  256. },
  257. DeptBy: cModel.DeptBy{
  258. DeptId: p.DeptId,
  259. },
  260. }
  261. data = append(data, operationLog)
  262. }
  263. }
  264. provList := make([]model.ProvOperationLog, 0)
  265. for _, v := range data {
  266. provList = append(provList, v.GenProvOperationLog())
  267. }
  268. err = e.UpdateGasCylinderStatus(tx, c, user, p)
  269. if err != nil {
  270. e.Log.Errorf("更新钢瓶状态失败: %s", err)
  271. return global.CreateFailedErr
  272. }
  273. // TODO 同步省平台 1.1.1.22 批量新增操作记录
  274. err = tx.Create(&data).Error
  275. if err != nil {
  276. e.Log.Errorf("db error: %s", err)
  277. return global.CreateFailedErr
  278. }
  279. return nil
  280. }
  281. func (e *OperationLog) UpdateGasCylinderStatus(tx *gorm.DB, c *dto.OperationLogInsertReq, user model.SysUser, p *actions.DataPermission) (err error) {
  282. switch c.OptType {
  283. case "15":
  284. // 删除气站重瓶
  285. err = tx.Where("inner_code in (?) and user_id = 0 and company_id = ? and status = ?",
  286. c.ChipUidList, user.Dept.Id, model.GasCylinderStatusWeighty).
  287. Delete(&model.GasCylinderStatus{}).Error
  288. if err != nil {
  289. e.Log.Errorf("db error: %s", err)
  290. return global.CreateFailedErr
  291. }
  292. case "17":
  293. // 司机确认重瓶从气站出库 添加司机重瓶
  294. for _, chipUid := range c.ChipUidList {
  295. gasCylinderStatus := &model.GasCylinderStatus{
  296. InnerCode: chipUid,
  297. UserId: user.Id,
  298. CompanyId: user.Dept.Id,
  299. Status: model.GasCylinderStatusWeighty,
  300. ControlBy: cModel.ControlBy{
  301. CreateBy: p.UserId,
  302. },
  303. DeptBy: cModel.DeptBy{
  304. DeptId: p.DeptId,
  305. },
  306. }
  307. // 司机添加重瓶
  308. err = tx.Where(model.GasCylinderStatus{InnerCode: chipUid, UserId: user.Id, CompanyId: user.Dept.Id}).
  309. Assign(model.GasCylinderStatus{Status: model.GasCylinderStatusWeighty}).FirstOrCreate(&gasCylinderStatus).Error
  310. if err != nil {
  311. e.Log.Errorf("db error: %s", err)
  312. return global.CreateFailedErr
  313. }
  314. }
  315. case "19":
  316. // 删除司机重瓶
  317. err = tx.Where("inner_code in (?) and user_id = ? and company_id = ? and status = ?",
  318. c.ChipUidList, user.Id, user.Dept.Id, model.GasCylinderStatusWeighty).
  319. Delete(&model.GasCylinderStatus{}).Error
  320. if err != nil {
  321. e.Log.Errorf("db error: %s", err)
  322. return global.CreateFailedErr
  323. }
  324. case "31":
  325. // 送气员领重瓶出库 添加送气员重瓶
  326. for _, chipUid := range c.ChipUidList {
  327. gasCylinderStatus := &model.GasCylinderStatus{
  328. InnerCode: chipUid,
  329. UserId: 0,
  330. CompanyId: user.Dept.Id,
  331. Status: model.GasCylinderStatusWeighty,
  332. ControlBy: cModel.ControlBy{
  333. CreateBy: p.UserId,
  334. },
  335. DeptBy: cModel.DeptBy{
  336. DeptId: p.DeptId,
  337. },
  338. }
  339. // 门店添加重瓶
  340. err = tx.Where(model.GasCylinderStatus{InnerCode: chipUid, UserId: user.Id, CompanyId: user.Dept.Id}).
  341. Assign(model.GasCylinderStatus{Status: model.GasCylinderStatusWeighty}).FirstOrCreate(&gasCylinderStatus).Error
  342. if err != nil {
  343. e.Log.Errorf("db error: %s", err)
  344. return global.CreateFailedErr
  345. }
  346. }
  347. case "37":
  348. // 门店重瓶出库 删除门店重瓶
  349. err = tx.Where("inner_code in (?) and user_id = 0 and company_id = ?", c.ChipUidList, user.Dept.Id).
  350. Delete(&model.GasCylinderStatus{}).Error
  351. if err != nil {
  352. e.Log.Errorf("db error: %s", err)
  353. return global.CreateFailedErr
  354. }
  355. case "25":
  356. // 送气员领重瓶出库 添加送气员重瓶
  357. for _, chipUid := range c.ChipUidList {
  358. // 送气员领重瓶出库
  359. gasCylinderStatus := &model.GasCylinderStatus{
  360. InnerCode: chipUid,
  361. UserId: user.Id,
  362. CompanyId: user.Dept.Id,
  363. Status: model.GasCylinderStatusWeighty,
  364. ControlBy: cModel.ControlBy{
  365. CreateBy: p.UserId,
  366. },
  367. DeptBy: cModel.DeptBy{
  368. DeptId: p.DeptId,
  369. },
  370. }
  371. // 添加送气员重瓶
  372. err = tx.Where(model.GasCylinderStatus{InnerCode: chipUid, UserId: user.Id, CompanyId: user.Dept.Id}).
  373. Assign(model.GasCylinderStatus{Status: model.GasCylinderStatusWeighty}).FirstOrCreate(&gasCylinderStatus).Error
  374. if err != nil {
  375. e.Log.Errorf("db error: %s", err)
  376. return global.CreateFailedErr
  377. }
  378. }
  379. case "27":
  380. // 删除送气员空瓶
  381. err = tx.Where("inner_code in (?) and user_id = ? and company_id = ? and status = ?", c.ChipUidList, user.Id, user.Dept.Id, model.GasCylinderStatusEmpty).
  382. Delete(&model.GasCylinderStatus{}).Error
  383. if err != nil {
  384. e.Log.Errorf("db error: %s", err)
  385. return global.CreateFailedErr
  386. }
  387. case "21":
  388. //21门店回收空瓶
  389. for _, chipUid := range c.ChipUidList {
  390. gasCylinderStatus := &model.GasCylinderStatus{
  391. InnerCode: chipUid,
  392. UserId: 0,
  393. CompanyId: user.Dept.Id,
  394. Status: model.GasCylinderStatusWeighty,
  395. ControlBy: cModel.ControlBy{
  396. CreateBy: p.UserId,
  397. },
  398. DeptBy: cModel.DeptBy{
  399. DeptId: p.DeptId,
  400. },
  401. }
  402. // 添加门店空瓶
  403. err = tx.Where(model.GasCylinderStatus{InnerCode: chipUid, UserId: user.Id, CompanyId: user.Dept.Id}).
  404. Assign(model.GasCylinderStatus{Status: model.GasCylinderStatusEmpty}).FirstOrCreate(&gasCylinderStatus).Error
  405. if err != nil {
  406. e.Log.Errorf("db error: %s", err)
  407. return global.CreateFailedErr
  408. }
  409. }
  410. case "10":
  411. // 删除门店空瓶
  412. err = tx.Where("inner_code in (?) and user_id = 0 and company_id = ? and status = ?", c.ChipUidList, user.Dept.Id, model.GasCylinderStatusEmpty).
  413. Delete(&model.GasCylinderStatus{}).Error
  414. if err != nil {
  415. e.Log.Errorf("db error: %s", err)
  416. return global.CreateFailedErr
  417. }
  418. case "11":
  419. // 添加司机空瓶
  420. for _, chipUid := range c.ChipUidList {
  421. gasCylinderStatus := &model.GasCylinderStatus{
  422. InnerCode: chipUid,
  423. UserId: user.Id,
  424. CompanyId: user.Dept.Id,
  425. Status: model.GasCylinderStatusWeighty,
  426. ControlBy: cModel.ControlBy{
  427. CreateBy: p.UserId,
  428. },
  429. DeptBy: cModel.DeptBy{
  430. DeptId: p.DeptId,
  431. },
  432. }
  433. err = tx.Where(model.GasCylinderStatus{InnerCode: chipUid, UserId: user.Id, CompanyId: user.Dept.Id}).
  434. Assign(model.GasCylinderStatus{Status: model.GasCylinderStatusEmpty}).FirstOrCreate(&gasCylinderStatus).Error
  435. if err != nil {
  436. e.Log.Errorf("db error: %s", err)
  437. return global.CreateFailedErr
  438. }
  439. }
  440. case "13":
  441. // 删除司机空瓶
  442. err = tx.Where("inner_code in (?) and user_id = ? and company_id = ? and status = ?", c.ChipUidList, user.Id, user.Dept.Id, model.GasCylinderStatusEmpty).
  443. Delete(&model.GasCylinderStatus{}).Error
  444. if err != nil {
  445. e.Log.Errorf("db error: %s", err)
  446. return global.CreateFailedErr
  447. }
  448. case "12":
  449. // 添加气站空瓶
  450. for _, chipUid := range c.ChipUidList {
  451. gasCylinderStatus := &model.GasCylinderStatus{
  452. InnerCode: chipUid,
  453. UserId: 0,
  454. CompanyId: user.Dept.Id,
  455. Status: model.GasCylinderStatusEmpty,
  456. ControlBy: cModel.ControlBy{
  457. CreateBy: p.UserId,
  458. },
  459. DeptBy: cModel.DeptBy{
  460. DeptId: p.DeptId,
  461. },
  462. }
  463. err = tx.Where(model.GasCylinderStatus{InnerCode: chipUid, UserId: user.Id, CompanyId: user.Dept.Id}).
  464. Assign(model.GasCylinderStatus{Status: model.GasCylinderStatusEmpty}).FirstOrCreate(&gasCylinderStatus).Error
  465. if err != nil {
  466. e.Log.Errorf("db error: %s", err)
  467. return global.CreateFailedErr
  468. }
  469. }
  470. case "14":
  471. // 气站空瓶转重瓶
  472. err = tx.Model(&model.GasCylinderStatus{}).Where("inner_code in (?) and user_id = 0 and company_id = ? and status = ?", c.ChipUidList, user.Dept.Id, model.GasCylinderStatusEmpty).
  473. Updates(map[string]interface{}{
  474. "status": model.GasCylinderStatusWeighty,
  475. }).Error
  476. if err != nil {
  477. e.Log.Errorf("db error: %s", err)
  478. return global.CreateFailedErr
  479. }
  480. case "16":
  481. // 删除司机重瓶
  482. err = tx.Where("inner_code in (?) and user_id = ? and company_id = ? and status = ?",
  483. c.ChipUidList, user.Id, user.Dept.Id, model.GasCylinderStatusWeighty).
  484. Delete(&model.GasCylinderStatus{}).Error
  485. if err != nil {
  486. e.Log.Errorf("db error: %s", err)
  487. return global.CreateFailedErr
  488. }
  489. case "016":
  490. // 添加气站空瓶
  491. for _, chipUid := range c.ChipUidList {
  492. gasCylinderStatus := &model.GasCylinderStatus{
  493. InnerCode: chipUid,
  494. UserId: 0,
  495. CompanyId: user.Dept.Id,
  496. Status: model.GasCylinderStatusWeighty,
  497. ControlBy: cModel.ControlBy{
  498. CreateBy: p.UserId,
  499. },
  500. DeptBy: cModel.DeptBy{
  501. DeptId: p.DeptId,
  502. },
  503. }
  504. // 气站添加重瓶
  505. err = tx.Where(model.GasCylinderStatus{InnerCode: chipUid, UserId: user.Id, CompanyId: user.Dept.Id}).
  506. Assign(model.GasCylinderStatus{Status: model.GasCylinderStatusWeighty}).FirstOrCreate(&gasCylinderStatus).Error
  507. if err != nil {
  508. e.Log.Errorf("db error: %s", err)
  509. return global.CreateFailedErr
  510. }
  511. }
  512. case "33":
  513. // 删除门店重瓶
  514. err = tx.Where("inner_code in (?) and user_id = 0 and company_id = ? and status = ?",
  515. c.ChipUidList, user.Dept.Id, model.GasCylinderStatusWeighty).
  516. Delete(&model.GasCylinderStatus{}).Error
  517. if err != nil {
  518. e.Log.Errorf("db error: %s", err)
  519. return global.CreateFailedErr
  520. }
  521. case "033":
  522. // 添加气站空瓶
  523. for _, chipUid := range c.ChipUidList {
  524. gasCylinderStatus := &model.GasCylinderStatus{
  525. InnerCode: chipUid,
  526. UserId: user.Id,
  527. CompanyId: user.Dept.Id,
  528. Status: model.GasCylinderStatusWeighty,
  529. ControlBy: cModel.ControlBy{
  530. CreateBy: p.UserId,
  531. },
  532. DeptBy: cModel.DeptBy{
  533. DeptId: p.DeptId,
  534. },
  535. }
  536. // 司机添加重瓶
  537. err = tx.Where(model.GasCylinderStatus{InnerCode: chipUid, UserId: user.Id, CompanyId: user.Dept.Id}).
  538. Assign(model.GasCylinderStatus{Status: model.GasCylinderStatusWeighty}).FirstOrCreate(&gasCylinderStatus).Error
  539. if err != nil {
  540. e.Log.Errorf("db error: %s", err)
  541. return global.CreateFailedErr
  542. }
  543. }
  544. }
  545. return nil
  546. }
  547. func (e *OperationLog) InsertForGasStation(c *dto.OperationLogInsertForGasStationReq) error {
  548. var err error
  549. tx := e.Orm.Begin()
  550. defer func() {
  551. if err != nil {
  552. tx.Rollback()
  553. } else {
  554. tx.Commit()
  555. }
  556. }()
  557. // 通过sn查询设备信息
  558. var device model.Device
  559. err = tx.Where("sn = ?", c.Sn).Preload("User").First(&device).Error
  560. if err != nil {
  561. tx.Rollback()
  562. e.Log.Errorf("db error: %s", err)
  563. return global.CreateFailedErr
  564. }
  565. // 1、通过高频ID查询气瓶内编码
  566. var gasCylinder model.GasCylinder
  567. err = tx.Where("uid = ?", c.ChipUid).First(&gasCylinder).Error
  568. operationLog := model.OperationLog{
  569. ProvOperationLog: model.ProvOperationLog{
  570. OptType: device.OptType,
  571. OptTime: time.Now().Format("2006-01-02 15:04:05"),
  572. OptUser: device.ProvUserId,
  573. InnerCode: gasCylinder.InnerCode,
  574. CompanyId: device.ProvCmpCode,
  575. CurrentEnterprise: device.ProvCmpCode,
  576. CurrentStation: device.ProvCmpCode,
  577. },
  578. DeptBy: cModel.DeptBy{
  579. DeptId: device.DeptId,
  580. },
  581. }
  582. // TODO 同步省平台 1.1.1.20 新增操作记录
  583. //ipr := data.GenProvOperationLog()
  584. err = tx.Create(&operationLog).Error
  585. if err != nil {
  586. tx.Rollback()
  587. log.Errorf("db error: %s", err)
  588. return global.CreateFailedErr
  589. }
  590. return nil
  591. }
  592. // 送气员送达重瓶,送气订单取消
  593. func InsertOperationLogByOptType26Or36(tx *gorm.DB, OptType string, chipUids []string, order model.Order) error {
  594. var err error
  595. for _, chipUid := range chipUids {
  596. // 1、通过高频ID查询气瓶内编码
  597. var gasCylinder model.GasCylinder
  598. err = tx.Where("uid = ?", chipUid).First(&gasCylinder).Error
  599. if err != nil {
  600. return errors.New("获取气瓶信息失败")
  601. }
  602. // 新增操作记录
  603. operationLog := model.OperationLog{
  604. ProvOperationLog: model.ProvOperationLog{
  605. OptType: OptType,
  606. OptTime: time.Now().Format("2006-01-02 15:04:05"),
  607. OptUser: order.User.ProvUserId,
  608. ObjectUser: order.User.ProvUserId,
  609. InnerCode: gasCylinder.InnerCode,
  610. ObjectCustomer: order.CustomerId,
  611. OptCustomer: order.CustomerId,
  612. CompanyId: order.Dept.CmpCode,
  613. CurrentEnterprise: order.Store.CmpCode,
  614. CurrentStore: order.Store.CmpCode,
  615. CurrentAddress: order.CustomerId,
  616. },
  617. ControlBy: cModel.ControlBy{
  618. CreateBy: order.UserId,
  619. },
  620. DeptBy: cModel.DeptBy{
  621. DeptId: order.StoreId,
  622. },
  623. }
  624. if OptType == "26" {
  625. // 新增客户气瓶信息
  626. err = tx.Create(&model.CustomerGasCylinder{
  627. CustomerId: order.CustomerId,
  628. InnerCode: gasCylinder.InnerCode,
  629. State: 1,
  630. BorrowTime: cModel.Time(time.Now()),
  631. }).Error
  632. if err != nil {
  633. tx.Rollback()
  634. log.Errorf("db error: %s", err)
  635. return global.CreateFailedErr
  636. }
  637. // 删除送气员重瓶
  638. err = tx.Where("inner_code = ? and user_id = ? and company_id = ? and status = ?",
  639. gasCylinder.InnerCode, order.UserId, order.StoreId, model.GasCylinderStatusWeighty).
  640. Delete(&model.GasCylinderStatus{}).Error
  641. if err != nil {
  642. log.Errorf("db error: %s", err)
  643. return global.CreateFailedErr
  644. }
  645. }
  646. if OptType == "36" {
  647. // 查询气瓶信息
  648. var cgc model.CustomerGasCylinder
  649. err = tx.Where("inner_code = ? and customer_id=? and state = 1", gasCylinder.InnerCode, order.CustomerId).First(&cgc).Error
  650. if err != nil {
  651. log.Errorf("db error: %s", err)
  652. return global.CreateFailedErr
  653. }
  654. cgc.ReturnTime = cModel.Time(time.Now())
  655. cgc.State = 2
  656. err = tx.Save(&cgc).Error
  657. if err != nil {
  658. log.Errorf("db error: %s", err)
  659. return global.CreateFailedErr
  660. }
  661. // 添加送气员重瓶
  662. gasCylinderStatus := &model.GasCylinderStatus{
  663. InnerCode: chipUid,
  664. UserId: order.UserId,
  665. CompanyId: order.StoreId,
  666. Status: model.GasCylinderStatusWeighty,
  667. ControlBy: cModel.ControlBy{
  668. CreateBy: order.UserId,
  669. },
  670. DeptBy: cModel.DeptBy{
  671. DeptId: order.StoreId,
  672. },
  673. }
  674. err = tx.Where(model.GasCylinderStatus{InnerCode: chipUid, UserId: order.UserId, CompanyId: order.StoreId}).
  675. Assign(model.GasCylinderStatus{Status: model.GasCylinderStatusWeighty}).FirstOrCreate(&gasCylinderStatus).Error
  676. if err != nil {
  677. log.Errorf("db error: %s", err)
  678. return global.CreateFailedErr
  679. }
  680. operationLog.ObjectCustomer = cgc.CustomerId
  681. operationLog.OptCustomer = cgc.CustomerId
  682. operationLog.CurrentAddress = cgc.CustomerId
  683. }
  684. // TODO 同步省平台 1.1.1.20 新增操作记录
  685. //ipr := data.GenProvOperationLog()
  686. err = tx.Create(&operationLog).Error
  687. if err != nil {
  688. tx.Rollback()
  689. log.Errorf("db error: %s", err)
  690. return global.CreateFailedErr
  691. }
  692. }
  693. return nil
  694. }
  695. // ListByUid 通过uid获取OperationLog列表
  696. func (e *OperationLog) ListByUid(InnerCode string, list *[]model.OperationLog) error {
  697. var err error
  698. var data model.OperationLog
  699. err = e.Orm.Model(&data).
  700. Where("inner_code = ? and state = 1", InnerCode).
  701. Preload("OptUserObj").
  702. Preload("ObjectUserObj").
  703. Preload("ObjectCustomerObj").
  704. Preload("OptCustomerObj").
  705. Preload("CompanyObj").
  706. Preload("CurrentEnterpriseObj").
  707. Preload("CurrentStationObj").
  708. Preload("CurrentStoreObj").
  709. Preload("CurrentTruckObj").
  710. Preload("CurrentAddressObj").
  711. Find(list).Order("opt_time").Error
  712. if err != nil {
  713. e.Log.Errorf("db error: %s", err)
  714. return global.GetFailedErr
  715. }
  716. return nil
  717. }