operation_log.go 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  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"
  13. "time"
  14. )
  15. type OperationLog struct {
  16. service.Service
  17. }
  18. // GetPage 获取OperationLog列表
  19. func (e *OperationLog) GetPage(c *dto.OperationLogGetPageReq, list *[]model.OperationLog, count *int64, p *actions.DataPermission) error {
  20. var err error
  21. var data model.OperationLog
  22. err = e.Orm.Model(&data).
  23. Scopes(
  24. cDto.MakeCondition(c.GetNeedSearch()),
  25. cDto.Paginate(c.GetPageSize(), c.GetPageIndex()),
  26. actions.Permission(data.TableName(), p),
  27. ).
  28. Find(list).Limit(-1).Offset(-1).
  29. Count(count).Error
  30. if err != nil {
  31. e.Log.Errorf("db error: %s", err)
  32. return global.GetFailedErr
  33. }
  34. return nil
  35. }
  36. // Get 获取OperationLog对象
  37. func (e *OperationLog) Get(d *dto.OperationLogGetReq, carInfoModel *model.OperationLog, p *actions.DataPermission) error {
  38. err := e.Orm.
  39. Scopes(actions.Permission(carInfoModel.TableName(), p)).
  40. First(carInfoModel, d.GetId()).Error
  41. if err != nil {
  42. e.Log.Errorf("db error: %s", err)
  43. if errors.Is(err, gorm.ErrRecordNotFound) {
  44. return global.GetNotFoundOrNoPermissionErr
  45. }
  46. return global.GetFailedErr
  47. }
  48. return nil
  49. }
  50. // Insert 创建OperationLog对象
  51. func (e *OperationLog) Insert(c *dto.OperationLogInsertReq, p *actions.DataPermission) error {
  52. var err error
  53. data := make([]model.OperationLog, 0)
  54. company, err := model.GetProvCode(p.DeptId)
  55. if err != nil {
  56. e.Log.Errorf("db error: %s", err)
  57. return global.CreateFailedErr
  58. }
  59. user, err := model.GetUserCode(p.UserId)
  60. if err != nil {
  61. e.Log.Errorf("db error: %s", err)
  62. return global.CreateFailedErr
  63. }
  64. tx := e.Orm.Begin()
  65. defer func() {
  66. if err != nil {
  67. tx.Rollback()
  68. } else {
  69. tx.Commit()
  70. }
  71. }()
  72. switch c.OptType {
  73. case "25", "27":
  74. // 送气员领重瓶
  75. for _, chipUid := range c.ChipUidList {
  76. // 1、通过高频ID查询气瓶内编码
  77. var gasCylinder model.GasCylinder
  78. err = tx.Where("uid = ?", chipUid).First(&gasCylinder).Error
  79. operationLog := model.OperationLog{
  80. ProvOperationLog: model.ProvOperationLog{
  81. InnerCode: gasCylinder.InnerCode,
  82. OptType: c.OptType,
  83. OptUser: user.ProvUserId,
  84. CompanyId: user.ProvUser.CmpCode,
  85. CurrentEnterprise: company.CmpCode,
  86. OptTime: time.Now().Format("2006-01-02 15:04:05"),
  87. },
  88. ControlBy: cModel.ControlBy{
  89. CreateBy: p.UserId,
  90. },
  91. DeptBy: cModel.DeptBy{
  92. DeptId: p.DeptId,
  93. },
  94. }
  95. data = append(data, operationLog)
  96. if c.OptType == "27" {
  97. // 修改用户关联的气瓶信息
  98. var cgc model.CustomerGasCylinder
  99. err = tx.Where("inner_code = ? and state = 1", gasCylinder.InnerCode).First(&cgc).Error
  100. if err != nil {
  101. tx.Rollback()
  102. log.Errorf("db error: %s", err)
  103. return global.CreateFailedErr
  104. }
  105. cgc.ReturnTime = cModel.Time(time.Now())
  106. cgc.State = 2
  107. err = tx.Save(&cgc).Error
  108. if err != nil {
  109. tx.Rollback()
  110. log.Errorf("db error: %s", err)
  111. return global.CreateFailedErr
  112. }
  113. }
  114. }
  115. case "6", "10", "21", "31", "33", "34", "35", "37":
  116. // 门店端
  117. for _, chipUid := range c.ChipUidList {
  118. // 1、通过高频ID查询气瓶内编码
  119. var gasCylinder model.GasCylinder
  120. err = tx.Where("uid = ?", chipUid).First(&gasCylinder).Error
  121. if err != nil {
  122. tx.Rollback()
  123. log.Errorf("db error: %s", err)
  124. return global.CreateFailedErr
  125. }
  126. if c.OptType == "21" {
  127. err = tx.Model(&model.OperationLog{}).Where("inner_code = ? and state = 1", gasCylinder.InnerCode).Updates(map[string]int{
  128. "state": 2,
  129. }).Error
  130. if err != nil {
  131. tx.Rollback()
  132. log.Errorf("db error: %s", err)
  133. return global.CreateFailedErr
  134. }
  135. }
  136. operationLog := model.OperationLog{
  137. ProvOperationLog: model.ProvOperationLog{
  138. InnerCode: gasCylinder.InnerCode,
  139. OptType: c.OptType,
  140. OptUser: user.ProvUserId,
  141. CompanyId: user.ProvUser.CmpCode,
  142. CurrentEnterprise: user.ProvUser.CmpCode,
  143. CurrentStore: user.ProvUser.CmpCode,
  144. OptTime: time.Now().Format("2006-01-02 15:04:05"),
  145. },
  146. ControlBy: cModel.ControlBy{
  147. CreateBy: p.UserId,
  148. },
  149. DeptBy: cModel.DeptBy{
  150. DeptId: p.DeptId,
  151. },
  152. }
  153. data = append(data, operationLog)
  154. }
  155. case "11", "12", "16", "17", "19":
  156. // 司机端
  157. for _, chipUid := range c.ChipUidList {
  158. // 1、通过高频ID查询气瓶内编码
  159. var gasCylinder model.GasCylinder
  160. err = tx.Where("uid = ?", chipUid).First(&gasCylinder).Error
  161. operationLog := model.OperationLog{
  162. ProvOperationLog: model.ProvOperationLog{
  163. InnerCode: gasCylinder.InnerCode,
  164. OptType: c.OptType,
  165. OptUser: user.ProvUserId,
  166. CompanyId: user.ProvUser.CmpCode,
  167. CurrentEnterprise: user.ProvUser.CmpCode,
  168. CurrentStore: user.ProvUser.CmpCode,
  169. OptTime: time.Now().Format("2006-01-02 15:04:05"),
  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. }
  181. provList := make([]model.ProvOperationLog, 0)
  182. for _, datum := range data {
  183. provList = append(provList, datum.GenProvOperationLog())
  184. }
  185. // TODO 同步省平台 1.1.1.22 批量新增操作记录
  186. err = tx.Create(&data).Error
  187. if err != nil {
  188. tx.Rollback()
  189. e.Log.Errorf("db error: %s", err)
  190. return global.CreateFailedErr
  191. }
  192. return nil
  193. }
  194. func (e *OperationLog) InsertForGasStation(c *dto.OperationLogInsertForGasStationReq) error {
  195. var err error
  196. tx := e.Orm.Begin()
  197. defer func() {
  198. if err != nil {
  199. tx.Rollback()
  200. } else {
  201. tx.Commit()
  202. }
  203. }()
  204. // 通过sn查询设备信息
  205. var device model.Device
  206. err = tx.Where("sn = ?", c.Sn).Preload("User").First(&device).Error
  207. if err != nil {
  208. tx.Rollback()
  209. e.Log.Errorf("db error: %s", err)
  210. return global.CreateFailedErr
  211. }
  212. // 1、通过高频ID查询气瓶内编码
  213. var gasCylinder model.GasCylinder
  214. err = tx.Where("uid = ?", c.ChipUid).First(&gasCylinder).Error
  215. operationLog := model.OperationLog{
  216. ProvOperationLog: model.ProvOperationLog{
  217. OptType: device.OptType,
  218. OptTime: time.Now().Format("2006-01-02 15:04:05"),
  219. OptUser: device.ProvUserId,
  220. InnerCode: gasCylinder.InnerCode,
  221. CompanyId: device.ProvCmpCode,
  222. CurrentEnterprise: device.ProvCmpCode,
  223. CurrentStation: device.ProvCmpCode,
  224. },
  225. DeptBy: cModel.DeptBy{
  226. DeptId: device.DeptId,
  227. },
  228. }
  229. // TODO 同步省平台 1.1.1.20 新增操作记录
  230. //ipr := data.GenProvOperationLog()
  231. err = tx.Create(&operationLog).Error
  232. if err != nil {
  233. tx.Rollback()
  234. log.Errorf("db error: %s", err)
  235. return global.CreateFailedErr
  236. }
  237. return nil
  238. }
  239. // 送气员送达重瓶,送气订单取消
  240. func InsertOperationLogByOptType26Or36(tx *gorm.DB, OptType string, chipUid string, order model.Order) error {
  241. var err error
  242. // 1、通过高频ID查询气瓶内编码
  243. var gasCylinder model.GasCylinder
  244. err = tx.Where("uid = ?", chipUid).First(&gasCylinder).Error
  245. operationLog := model.OperationLog{
  246. ProvOperationLog: model.ProvOperationLog{
  247. OptType: OptType,
  248. OptTime: time.Now().Format("2006-01-02 15:04:05"),
  249. OptUser: order.User.ProvUserId,
  250. InnerCode: gasCylinder.InnerCode,
  251. ObjectCustomer: order.CustomerId,
  252. OptCustomer: order.CustomerId,
  253. CompanyId: order.Dept.CmpCode,
  254. CurrentEnterprise: order.Store.CmpCode,
  255. CurrentAddress: order.CustomerId,
  256. },
  257. ControlBy: cModel.ControlBy{
  258. CreateBy: order.UserId,
  259. },
  260. DeptBy: cModel.DeptBy{
  261. DeptId: order.DeptId,
  262. },
  263. }
  264. // TODO 同步省平台 1.1.1.20 新增操作记录
  265. //ipr := data.GenProvOperationLog()
  266. err = tx.Create(&operationLog).Error
  267. if err != nil {
  268. tx.Rollback()
  269. log.Errorf("db error: %s", err)
  270. return global.CreateFailedErr
  271. }
  272. if OptType == "26" {
  273. // 新增客户气瓶信息
  274. err = tx.Create(&model.CustomerGasCylinder{
  275. CustomerId: order.CustomerId,
  276. InnerCode: gasCylinder.InnerCode,
  277. State: 1,
  278. BorrowTime: cModel.Time(time.Now()),
  279. }).Error
  280. if err != nil {
  281. tx.Rollback()
  282. log.Errorf("db error: %s", err)
  283. return global.CreateFailedErr
  284. }
  285. }
  286. if OptType == "36" {
  287. // 查询气瓶信息
  288. var cgc model.CustomerGasCylinder
  289. err = tx.Where("inner_code = ? and state = 1", gasCylinder.InnerCode, order.CustomerId).First(&cgc).Error
  290. if err != nil {
  291. tx.Rollback()
  292. log.Errorf("db error: %s", err)
  293. return global.CreateFailedErr
  294. }
  295. cgc.ReturnTime = cModel.Time(time.Now())
  296. cgc.State = 2
  297. err = tx.Save(&cgc).Error
  298. if err != nil {
  299. tx.Rollback()
  300. log.Errorf("db error: %s", err)
  301. return global.CreateFailedErr
  302. }
  303. }
  304. return nil
  305. }
  306. // ListByUid 通过uid获取OperationLog列表
  307. func (e *OperationLog) ListByUid(InnerCode string, list *[]model.OperationLog) error {
  308. var err error
  309. var data model.OperationLog
  310. err = e.Orm.Model(&data).
  311. Where("inner_code = ? and state = 1", InnerCode).
  312. Preload("OptUserObj").
  313. Preload("ObjectUserObj").
  314. Preload("ObjectCustomerObj").
  315. Preload("OptCustomerObj").
  316. Preload("CompanyObj").
  317. Preload("CurrentEnterpriseObj").
  318. Preload("CurrentStationObj").
  319. Preload("CurrentStoreObj").
  320. Preload("CurrentTruckObj").
  321. Preload("CurrentAddressObj").
  322. Find(list).Error
  323. if err != nil {
  324. e.Log.Errorf("db error: %s", err)
  325. return global.GetFailedErr
  326. }
  327. return nil
  328. }