template.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495
  1. package maintainance
  2. import (
  3. "adm-vehicle-style/consts"
  4. "adm-vehicle-style/errors"
  5. "adm-vehicle-style/model"
  6. v1 "adm-vehicle-style/pb/v1"
  7. "context"
  8. "encoding/json"
  9. "fmt"
  10. "time"
  11. "git.getensh.com/common/gopkgsv2/database"
  12. "git.getensh.com/common/gopkgsv2/logger"
  13. "go.uber.org/zap"
  14. "google.golang.org/grpc/status"
  15. "gorm.io/gorm"
  16. )
  17. func TemplateList(ctx context.Context, req *v1.TemplateListRequest) (reply *v1.TemplateListReply, err error) {
  18. reply = &v1.TemplateListReply{}
  19. defer func() {
  20. if r := recover(); r != nil {
  21. err = fmt.Errorf("%+v", r)
  22. e := &status.Status{}
  23. if er := json.Unmarshal([]byte(err.Error()), e); er != nil {
  24. logger.Error("err",
  25. zap.String("system_err", err.Error()),
  26. zap.Stack("stacktrace"))
  27. }
  28. }
  29. }()
  30. db := database.DB()
  31. if req.Name != "" {
  32. db = db.Where("name like", "%"+req.Name+"%")
  33. }
  34. list, err := model.NewMaintenanceTemplateModel().List(db)
  35. if err != nil {
  36. if err == gorm.ErrRecordNotFound {
  37. return reply, nil
  38. }
  39. }
  40. reply.List = make([]*v1.Template, 0, len(list))
  41. for _, v := range list {
  42. reply.List = append(reply.List, &v1.Template{
  43. Id: v.ID,
  44. Name: v.Name,
  45. })
  46. }
  47. return reply, nil
  48. }
  49. func TemplateDetailList(ctx context.Context, req *v1.TemplateDetailListRequest) (reply *v1.TemplateDetailListReply, err error) {
  50. reply = &v1.TemplateDetailListReply{}
  51. defer func() {
  52. if r := recover(); r != nil {
  53. err = fmt.Errorf("%+v", r)
  54. e := &status.Status{}
  55. if er := json.Unmarshal([]byte(err.Error()), e); er != nil {
  56. logger.Error("err",
  57. zap.String("system_err", err.Error()),
  58. zap.Stack("stacktrace"))
  59. }
  60. }
  61. }()
  62. db := database.DB()
  63. if req.Tid > 0 {
  64. db = db.Where("tid = ?", req.Tid)
  65. }
  66. count, err := model.NewMaintenanceTitleTemplateModel().Count(db)
  67. if err != nil && err != gorm.ErrRecordNotFound {
  68. return reply, errors.SystemError
  69. }
  70. if err == gorm.ErrRecordNotFound || count == 0 {
  71. return reply, nil
  72. }
  73. pageSize := consts.PageSize
  74. if req.PageSize != 0 {
  75. pageSize = int(req.PageSize)
  76. }
  77. // 构造分页类
  78. pagination := model.NewPagination(int(req.Page), pageSize, 0).GetLimitOffset()
  79. list, err := model.NewMaintenanceTitleTemplateModel().List(db, pagination)
  80. if err != nil && err != gorm.ErrRecordNotFound {
  81. return reply, errors.SystemError
  82. }
  83. if err == gorm.ErrRecordNotFound {
  84. return reply, nil
  85. }
  86. reply.List = make([]*v1.TemplateDetail, 0, len(list))
  87. for _, v := range list {
  88. template, err := model.NewMaintenanceTemplateModel().Get(database.DB().Where("id = ?", v.Tid))
  89. if err != nil && err != gorm.ErrRecordNotFound {
  90. return reply, errors.SystemError
  91. }
  92. if err == gorm.ErrRecordNotFound {
  93. return reply, nil
  94. }
  95. reply.List = append(reply.List, &v1.TemplateDetail{
  96. Id: v.ID,
  97. Name: template.Name,
  98. StartMile: v.StartMile,
  99. StartDate: v.StartDate,
  100. MaintainMileMinCycle: v.MaintainMileMinCycle,
  101. MaintainDateMinCycle: v.MaintainDateMinCycle,
  102. Tid: v.Tid,
  103. })
  104. }
  105. reply.Total = count
  106. return reply, nil
  107. }
  108. func TemplateDetail(ctx context.Context, req *v1.TemplateDetailRequest) (reply *v1.TemplateDetailReply, err error) {
  109. reply = &v1.TemplateDetailReply{}
  110. defer func() {
  111. if r := recover(); r != nil {
  112. err = fmt.Errorf("%+v", r)
  113. e := &status.Status{}
  114. if er := json.Unmarshal([]byte(err.Error()), e); er != nil {
  115. logger.Error("err",
  116. zap.String("system_err", err.Error()),
  117. zap.Stack("stacktrace"))
  118. }
  119. }
  120. }()
  121. list, err := model.NewMaintainDetailTemplateModel().ListByItem(database.DB().Where("tid = ?", req.Tid))
  122. if err != nil {
  123. if err == gorm.ErrRecordNotFound {
  124. return reply, nil
  125. }
  126. return reply, errors.SystemError
  127. }
  128. template, err := model.NewMaintenanceTemplateModel().Get(database.DB().Where("id = ?", req.Tid))
  129. if err != nil && err != gorm.ErrRecordNotFound {
  130. return reply, errors.SystemError
  131. }
  132. if err == gorm.ErrRecordNotFound {
  133. return reply, nil
  134. }
  135. for _, v := range list {
  136. i := v1.MaintainItem{
  137. Id: v.ID,
  138. Item: v.ItemName,
  139. StartMile: v.StartMile,
  140. StartDate: v.StartDate,
  141. MileCycle: v.MileCycle,
  142. DateCycle: v.DateCycle,
  143. ItemId: v.ItemId,
  144. }
  145. switch v.ItemType {
  146. case consts.Maintainance:
  147. reply.Maintainance = append(reply.Maintainance, &i)
  148. case consts.Repair:
  149. reply.Repair = append(reply.Repair, &i)
  150. case consts.Wash:
  151. reply.Wash = append(reply.Wash, &i)
  152. }
  153. }
  154. reply.Name = template.Name
  155. return reply, nil
  156. }
  157. func addTemplateTitle(db *gorm.DB, titleId, tid, startMile, mileCycle, startDate, dateCycle int64) error {
  158. if titleId > 0 {
  159. values := map[string]interface{}{
  160. "start_mile": startMile,
  161. "start_date": startDate,
  162. "maintain_mile_min_cycle": mileCycle,
  163. "maintain_date_min_cycle": dateCycle,
  164. "repair_cycle": mileCycle,
  165. "wash_cycle": mileCycle,
  166. "updated_at": time.Now().Format("2006-01-02 15:04:05"),
  167. }
  168. if err := model.NewMaintenanceTitleTemplateModel().Update(db.Where("id = ?", titleId), values); err != nil {
  169. return errors.SystemError
  170. }
  171. } else {
  172. // 没有title
  173. title := &model.MaintenanceTitleTemplate{
  174. Tid: tid,
  175. StartMile: startMile,
  176. StartDate: startDate,
  177. MaintainMileMinCycle: mileCycle,
  178. MaintainDateMinCycle: dateCycle,
  179. RepairCycle: mileCycle,
  180. WashCycle: mileCycle,
  181. CreatedAt: time.Now(),
  182. UpdatedAt: time.Now(),
  183. }
  184. if err := model.NewMaintenanceTitleTemplateModel().Insert(db, title); err != nil {
  185. return errors.SystemError
  186. }
  187. }
  188. return nil
  189. }
  190. func AddTemplateDetail(ctx context.Context, req *v1.AddTemplateDetailRequest) (reply *v1.AddTemplateDetailReply, err error) {
  191. reply = &v1.AddTemplateDetailReply{}
  192. defer func() {
  193. if r := recover(); r != nil {
  194. err = fmt.Errorf("%+v", r)
  195. e := &status.Status{}
  196. if er := json.Unmarshal([]byte(err.Error()), e); er != nil {
  197. logger.Error("err",
  198. zap.String("system_err", err.Error()),
  199. zap.Stack("stacktrace"))
  200. }
  201. }
  202. }()
  203. if req.Tid == 0 && req.Name == "" {
  204. return reply, errors.ParamsError
  205. }
  206. var (
  207. hasOil bool
  208. tid = req.Tid
  209. titleId int64
  210. tx = database.DB().Begin()
  211. )
  212. defer func() {
  213. if err != nil {
  214. tx.Rollback()
  215. return
  216. }
  217. tx.Commit()
  218. }()
  219. if req.Tid > 0 {
  220. // 判断是否插入机油
  221. info, err := model.NewMaintainDetailTemplateModel().Get(database.DB().Where("tid = ? AND item_id = ?", req.Tid, consts.Oil))
  222. if err != nil && err != gorm.ErrRecordNotFound {
  223. return nil, errors.SystemError
  224. }
  225. if err == nil && info != nil && info.ID != 0 {
  226. hasOil = true
  227. }
  228. // 获取maintain_title
  229. title, err := model.NewMaintenanceTitleTemplateModel().Get(database.DB().Where("tid = ?", req.Tid))
  230. if err != nil && err != gorm.ErrRecordNotFound {
  231. return nil, errors.SystemError
  232. }
  233. if err == nil && title != nil && title.ID > 0 {
  234. titleId = title.ID
  235. }
  236. } else if req.Tid == 0 && req.Name != "" {
  237. // 写入新模板
  238. template := &model.MaintenanceTemplate{
  239. Name: req.Name,
  240. }
  241. err = model.NewMaintenanceTemplateModel().Insert(tx, template)
  242. if err != nil {
  243. return reply, errors.SystemError
  244. }
  245. tid = template.ID
  246. }
  247. // 写入详情
  248. detail := &model.MaintenanceDetailTemplate{
  249. Tid: tid,
  250. ItemId: req.ItemId,
  251. StartMile: req.StartMile,
  252. MileCycle: req.MileCycle,
  253. StartDate: req.StartDate,
  254. DateCycle: req.DateCycle,
  255. CreatedAt: time.Now(),
  256. UpdatedAt: time.Now(),
  257. }
  258. if err = model.NewMaintainDetailTemplateModel().Insert(tx, detail); err != nil {
  259. return reply, errors.SystemError
  260. }
  261. reply.Tid = tid
  262. // 以前插入机油,直接结束, 且当次插入不是机油
  263. if hasOil && req.ItemId != consts.Oil {
  264. return reply, nil
  265. }
  266. // 如果此条为机油
  267. if req.ItemId == consts.Oil {
  268. if err := addTemplateTitle(tx, titleId, tid, req.StartMile, req.MileCycle, req.StartDate, req.DateCycle); err != nil {
  269. return reply, err
  270. }
  271. return reply, nil
  272. }
  273. // 如果都不是
  274. // 获取已有的维保数据
  275. list, err := model.NewMaintainDetailTemplateModel().
  276. List(database.DB().Where("tid = ?", req.Tid))
  277. if err != nil && err != gorm.ErrRecordNotFound {
  278. return nil, errors.SystemError
  279. }
  280. var (
  281. idx int
  282. miniMile int64
  283. )
  284. list = append(list, model.MaintenanceDetailTemplate{
  285. StartMile: req.StartMile,
  286. MileCycle: req.MileCycle,
  287. StartDate: req.StartDate,
  288. DateCycle: req.DateCycle,
  289. })
  290. for k, v := range list {
  291. if miniMile == 0 {
  292. miniMile = v.MileCycle
  293. idx = k
  294. }
  295. if v.MileCycle != 0 && v.MileCycle < miniMile {
  296. miniMile = v.MileCycle
  297. idx = k
  298. }
  299. }
  300. if err := addTemplateTitle(tx, titleId, tid, list[idx].StartMile, list[idx].MileCycle, list[idx].StartDate, list[idx].DateCycle); err != nil {
  301. return reply, err
  302. }
  303. return reply, nil
  304. }
  305. func UpdateTemplateDetail(ctx context.Context, req *v1.UpdateMaintainManualRequest) (reply *v1.EmptyReply, err error) {
  306. reply = &v1.EmptyReply{}
  307. defer func() {
  308. if r := recover(); r != nil {
  309. err = fmt.Errorf("%+v", r)
  310. e := &status.Status{}
  311. if er := json.Unmarshal([]byte(err.Error()), e); er != nil {
  312. logger.Error("err",
  313. zap.String("system_err", err.Error()),
  314. zap.Stack("stacktrace"))
  315. }
  316. }
  317. }()
  318. if req.Name != "" && req.Tid > 0 {
  319. if err := model.NewMaintenanceTemplateModel().Update(database.DB().Where("id = ?", req.Tid), map[string]interface{}{
  320. "name": req.Name,
  321. }); err != nil {
  322. return reply, errors.SystemError
  323. }
  324. }
  325. if req.Id == 0 {
  326. return reply, nil
  327. }
  328. info, err := model.NewMaintainDetailTemplateModel().Get(database.DB().Where("id = ?", req.Id))
  329. if err != nil && err != gorm.ErrRecordNotFound {
  330. return nil, errors.SystemError
  331. }
  332. // 获取maintain_title
  333. title, err := model.NewMaintenanceTitleTemplateModel().Get(database.DB().Where("tid = ?", info.Tid))
  334. if err != nil && err != gorm.ErrRecordNotFound {
  335. return nil, errors.SystemError
  336. }
  337. // 判断是否插入机油
  338. oil, err := model.NewMaintainDetailTemplateModel().Get(database.DB().Where("tid = ? AND item_id = ?", info.Tid, consts.Oil))
  339. if err != nil && err != gorm.ErrRecordNotFound {
  340. return nil, errors.SystemError
  341. }
  342. var (
  343. hasOil bool
  344. )
  345. if err == nil && oil != nil && oil.ID != 0 {
  346. hasOil = true
  347. }
  348. var (
  349. tx = database.DB().Begin()
  350. )
  351. defer func() {
  352. if err != nil {
  353. tx.Rollback()
  354. return
  355. }
  356. tx.Commit()
  357. }()
  358. values := map[string]interface{}{
  359. "start_mile": req.StartMile,
  360. "mile_cycle": req.MileCycle,
  361. "start_date": req.StartDate,
  362. "date_cycle": req.DateCycle,
  363. "item_id": req.ItemId,
  364. "updated_at": time.Now().Format("2006-01-02 15:04:05"),
  365. }
  366. if err := model.NewMaintainDetailTemplateModel().Update(tx.Where("id = ?", req.Id), values); err != nil {
  367. return reply, errors.SystemError
  368. }
  369. // 以前插入机油,直接结束, 且当次插入不是机油
  370. if hasOil && req.ItemId != consts.Oil {
  371. return reply, nil
  372. }
  373. // 如果此条为机油
  374. if req.ItemId == consts.Oil {
  375. if err := addTemplateTitle(tx, title.ID, info.Tid, req.StartMile, req.MileCycle, req.StartDate, req.DateCycle); err != nil {
  376. return reply, err
  377. }
  378. return reply, nil
  379. }
  380. // 如果都不是
  381. // 获取已有的维保数据
  382. list, err := model.NewMaintainDetailTemplateModel().List(database.DB().Where("tid = ?", info.Tid))
  383. if err != nil && err != gorm.ErrRecordNotFound {
  384. return nil, errors.SystemError
  385. }
  386. var (
  387. idx int
  388. miniMile int64
  389. )
  390. if len(list) > 0 {
  391. for k := range list {
  392. if list[k].ID == req.Id {
  393. list[k].StartMile = req.StartMile
  394. list[k].StartDate = req.StartDate
  395. list[k].MileCycle = req.MileCycle
  396. list[k].DateCycle = req.DateCycle
  397. }
  398. }
  399. } else {
  400. list = append(list, model.MaintenanceDetailTemplate{
  401. StartMile: req.StartMile,
  402. MileCycle: req.MileCycle,
  403. StartDate: req.StartDate,
  404. DateCycle: req.DateCycle,
  405. })
  406. }
  407. for k, v := range list {
  408. if miniMile == 0 {
  409. miniMile = v.MileCycle
  410. idx = k
  411. }
  412. if v.MileCycle != 0 && v.MileCycle < miniMile {
  413. miniMile = v.MileCycle
  414. idx = k
  415. }
  416. }
  417. if err := addTemplateTitle(tx, title.ID, info.Tid, list[idx].StartMile, list[idx].MileCycle, list[idx].StartDate, list[idx].DateCycle); err != nil {
  418. return reply, err
  419. }
  420. return reply, nil
  421. }