operation_log.go 28 KB

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