智慧食堂
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

592 line
16 KiB

  1. package hdl
  2. import (
  3. enum2 "applet/app/admin/enum"
  4. "applet/app/admin/lib/validate"
  5. "applet/app/admin/md"
  6. "applet/app/admin/svc"
  7. "applet/app/db"
  8. "applet/app/db/model"
  9. "applet/app/e"
  10. "applet/app/enum"
  11. "applet/app/utils"
  12. "github.com/gin-gonic/gin"
  13. "time"
  14. )
  15. func EnterpriseList(c *gin.Context) {
  16. var req md.EnterpriseListReq
  17. err := c.ShouldBindJSON(&req)
  18. if err != nil {
  19. err = validate.HandleValidateErr(err)
  20. err1 := err.(e.E)
  21. e.OutErr(c, err1.Code, err1.Error())
  22. return
  23. }
  24. if req.Limit == 0 {
  25. req.Limit = 10
  26. }
  27. if req.Page == 0 {
  28. req.Page = 10
  29. }
  30. admin := svc.GetUser(c)
  31. enterprises, total, err := svc.EnterpriseList(*admin, req)
  32. if err != nil {
  33. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  34. return
  35. }
  36. e.OutSuc(c, map[string]interface{}{
  37. "list": enterprises,
  38. "total": total,
  39. "state": []map[string]interface{}{
  40. {
  41. "name": enum.EnterpriseState(enum.EnterpriseStateForNormal).String(),
  42. "value": enum.EnterpriseStateForNormal,
  43. },
  44. {
  45. "name": enum.EnterpriseState(enum.EnterpriseStateForFreeze).String(),
  46. "value": enum.EnterpriseStateForFreeze,
  47. },
  48. },
  49. "kind": []map[string]interface{}{
  50. {
  51. "name": enum.EnterprisePvd(enum.EnterprisePvdByCentralKitchenForSchool).String(),
  52. "value": enum.EnterprisePvdByCentralKitchenForSchool,
  53. },
  54. {
  55. "name": enum.EnterprisePvd(enum.EnterprisePvdByCentralKitchenForFactory).String(),
  56. "value": enum.EnterprisePvdByCentralKitchenForFactory,
  57. },
  58. {
  59. "name": enum.EnterprisePvd(enum.EnterprisePvdBySelfSupportForSchool).String(),
  60. "value": enum.EnterprisePvdBySelfSupportForSchool,
  61. },
  62. {
  63. "name": enum.EnterprisePvd(enum.EnterprisePvdBySelfSupportForFactory).String(),
  64. "value": enum.EnterprisePvdBySelfSupportForFactory,
  65. },
  66. {
  67. "name": enum.EnterprisePvd(enum.EnterprisePvdByNursingHome).String(),
  68. "value": enum.EnterprisePvdByNursingHome,
  69. },
  70. },
  71. }, nil)
  72. return
  73. }
  74. func Detail(c *gin.Context) {
  75. enterpriseId := c.DefaultQuery("id", "")
  76. detail, err := svc.EnterpriseDetail(utils.StrToInt(enterpriseId))
  77. if err != nil {
  78. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  79. return
  80. }
  81. e.OutSuc(c, detail, nil)
  82. return
  83. }
  84. func SchoolBelowGrade(c *gin.Context) {
  85. enterpriseId := c.DefaultQuery("enterprise_id", "")
  86. gradeDb := db.GradeDb{}
  87. gradeDb.Set(utils.StrToInt(enterpriseId))
  88. gradeList, err := gradeDb.FindGrade()
  89. if err != nil {
  90. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  91. return
  92. }
  93. e.OutSuc(c, map[string]interface{}{
  94. "list": gradeList,
  95. }, nil)
  96. return
  97. }
  98. func SchoolGradeBelowClass(c *gin.Context) {
  99. gradeId := c.DefaultQuery("grade_id", "")
  100. enterpriseId := c.DefaultQuery("enterprise_id", "")
  101. classDb := db.ClassDb{}
  102. if gradeId != "" {
  103. classDb.Set(utils.StrToInt(gradeId))
  104. classList, err := classDb.FindClass()
  105. if err != nil {
  106. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  107. return
  108. }
  109. e.OutSuc(c, map[string]interface{}{
  110. "list": classList,
  111. }, nil)
  112. } else {
  113. classDb.Set(0)
  114. classList, err := classDb.FindClassByEnterprise(enterpriseId)
  115. if err != nil {
  116. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  117. return
  118. }
  119. e.OutSuc(c, map[string]interface{}{
  120. "list": classList,
  121. }, nil)
  122. }
  123. return
  124. }
  125. func EnterpriseAdd(c *gin.Context) {
  126. var req md.EnterpriseAddReq
  127. err := c.ShouldBindJSON(&req)
  128. if err != nil {
  129. err = validate.HandleValidateErr(err)
  130. err1 := err.(e.E)
  131. e.OutErr(c, err1.Code, err1.Error())
  132. return
  133. }
  134. enterpriseDb := db.EnterpriseDb{}
  135. enterpriseDb.Set()
  136. now := time.Now()
  137. var pvd = 2
  138. if req.Kind == enum.EnterprisePvdByCentralKitchenForSchool || req.Kind == enum.EnterprisePvdByCentralKitchenForFactory {
  139. pvd = 1
  140. }
  141. enterprise := model.Enterprise{
  142. Name: req.Name,
  143. Pvd: int32(pvd),
  144. Kind: req.Kind,
  145. CompanyId: req.CompanyId,
  146. State: enum2.CompanyStateForNormal,
  147. Memo: req.Memo,
  148. CreateAt: now.Format("2006-01-02 15:04:05"),
  149. UpdateAt: now.Format("2006-01-02 15:04:05"),
  150. }
  151. enterpriseId, err := enterpriseDb.EnterpriseInsert(&enterprise)
  152. if err != nil {
  153. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  154. return
  155. }
  156. if req.Kind == enum.EnterprisePvdByCentralKitchenForSchool {
  157. //新增 `central_kitchen_for_school_set` && `central_kitchen_for_school_with_spec`
  158. centralKitchenForSchoolSetDb := db.CentralKitchenForSchoolSetDb{}
  159. centralKitchenForSchoolSetDb.Set(enterpriseId)
  160. centralKitchenForSchoolSetDb.CentralKitchenForSchoolSetInsert(&model.CentralKitchenForSchoolSet{
  161. EnterpriseId: enterpriseId,
  162. IsOpenTeacherReportMeal: 1,
  163. IsOpenReportMealForDay: 1,
  164. IsOpenReportMealForMonth: 1,
  165. IsOpenReportMealForSemester: 1,
  166. IsOpenTeacherReportMealForDay: 1,
  167. IsOpenTeacherReportMealForMonth: 1,
  168. IsOpenTeacherReportMealForSemester: 1,
  169. CreateAt: now.Format("2006-01-02 15:04:05"),
  170. UpdateAt: now.Format("2006-01-02 15:04:05"),
  171. })
  172. centralKitchenForSchoolWithSpec := db.CentralKitchenForSchoolWithSpec{}
  173. centralKitchenForSchoolWithSpec.Set(enterpriseId)
  174. centralKitchenForSchoolWithSpec.CentralKitchenForSchoolWithSpecInsert(&model.CentralKitchenForSchoolWithSpec{
  175. EnterpriseId: enterpriseId,
  176. IsOpenBreakfast: 1,
  177. IsOpenLunch: 1,
  178. IsOpenDinner: 1,
  179. BreakfastUnitPrice: "0.00",
  180. LunchUnitPrice: "0.00",
  181. DinnerUnitPrice: "0.00",
  182. BreakfastUnitPriceForTeacher: "0.00",
  183. LunchUnitPriceForTeacher: "0.00",
  184. DinnerUnitPriceForTeacher: "0.00",
  185. CreateAt: now.Format("2006-01-02 15:04:05"),
  186. UpdateAt: now.Format("2006-01-02 15:04:05"),
  187. })
  188. }
  189. if req.Kind == enum.EnterprisePvdByNursingHome {
  190. //新增 `nursing_home_set` && `nursing_home_with_spec`
  191. nursingHomeSetDb := db.NursingHomeSetDb{}
  192. nursingHomeSetDb.Set(enterpriseId)
  193. nursingHomeSetDb.NursingHomeSetInsert(&model.NursingHomeSet{
  194. EnterpriseId: enterpriseId,
  195. IsOpenReportMealForDay: 1,
  196. IsOpenReportMealForMonth: 1,
  197. IsOpenReportMealForYear: 1,
  198. CreateAt: now.Format("2006-01-02 15:04:05"),
  199. UpdateAt: now.Format("2006-01-02 15:04:05"),
  200. })
  201. nursingHomeWithSpec := db.NursingHomeWithSpec{}
  202. nursingHomeWithSpec.Set(enterpriseId)
  203. nursingHomeWithSpec.NursingHomeWithSpecInsert(&model.NursingHomeWithSpec{
  204. EnterpriseId: enterpriseId,
  205. IsOpenBreakfast: 1,
  206. IsOpenLunch: 1,
  207. IsOpenDinner: 1,
  208. BreakfastUnitPrice: "0.00",
  209. LunchUnitPrice: "0.00",
  210. DinnerUnitPrice: "0.00",
  211. CreateAt: now.Format("2006-01-02 15:04:05"),
  212. UpdateAt: now.Format("2006-01-02 15:04:05"),
  213. })
  214. }
  215. e.OutSuc(c, "success", nil)
  216. return
  217. }
  218. func EnterpriseDelete(c *gin.Context) {
  219. var req md.EnterpriseDeleteReq
  220. err := c.ShouldBindJSON(&req)
  221. if err != nil {
  222. err = validate.HandleValidateErr(err)
  223. err1 := err.(e.E)
  224. e.OutErr(c, err1.Code, err1.Error())
  225. return
  226. }
  227. err = svc.EnterpriseDelete(req.EnterpriseIds)
  228. if err != nil {
  229. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  230. return
  231. }
  232. e.OutSuc(c, "success", nil)
  233. return
  234. }
  235. func EnterpriseUpdate(c *gin.Context) {
  236. var req md.EnterpriseUpdateReq
  237. err := c.ShouldBindJSON(&req)
  238. if err != nil {
  239. err = validate.HandleValidateErr(err)
  240. err1 := err.(e.E)
  241. e.OutErr(c, err1.Code, err1.Error())
  242. return
  243. }
  244. //1、更新 enterprise
  245. enterpriseDb := db.EnterpriseDb{}
  246. enterpriseDb.Set()
  247. enterprise, err := enterpriseDb.GetEnterprise(req.Id)
  248. if err != nil {
  249. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  250. return
  251. }
  252. if enterprise == nil {
  253. e.OutErr(c, e.ERR_NO_DATA, "未查询到对应记录")
  254. return
  255. }
  256. var pvd = 2
  257. if req.Kind == enum.EnterprisePvdByCentralKitchenForSchool || req.Kind == enum.EnterprisePvdByCentralKitchenForFactory || req.Kind == enum.EnterprisePvdByNursingHome {
  258. pvd = 1
  259. }
  260. now := time.Now()
  261. enterprise.Name = req.Name
  262. enterprise.Memo = req.Memo
  263. enterprise.Pvd = int32(pvd)
  264. enterprise.Kind = req.Kind
  265. enterprise.CompanyId = req.CompanyId
  266. enterprise.UpdateAt = now.Format("2006-01-02 15:04:05")
  267. _, err = enterpriseDb.EnterpriseUpdate(enterprise, "name", "memo", "pvd", "kind", "company_id", "update_at")
  268. if err != nil {
  269. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  270. return
  271. }
  272. //2、更新 grade && class 数据
  273. //2、删除 grade && class 数据
  274. gradeDb := db.GradeDb{}
  275. gradeDb.Set(req.Id)
  276. //_, err = gradeDb.ClassDeleteBySessionForEnterprise(db.Db.NewSession(), req.Id)
  277. //if err != nil {
  278. // e.OutErr(c, e.ERR_DB_ORM, err.Error())
  279. // return
  280. //}
  281. classDb := db.ClassDb{}
  282. classDb.Set(0)
  283. //_, err = classDb.ClassDeleteBySessionForEnterprise(db.Db.NewSession(), req.Id)
  284. //if err != nil {
  285. // e.OutErr(c, e.ERR_DB_ORM, err.Error())
  286. // return
  287. //}
  288. //新增 grade 数据 && class 数据
  289. for _, v := range req.GradeList {
  290. var classes []*model.Class
  291. if v.Id == 0 {
  292. //TODO::新增
  293. insertId, err1 := gradeDb.GradeInsert(&model.Grade{
  294. EnterpriseId: req.Id,
  295. Name: v.Name,
  296. Memo: "",
  297. CreateAt: now.Format("2006-01-02 15:04:05"),
  298. UpdateAt: now.Format("2006-01-02 15:04:05"),
  299. })
  300. if err1 != nil {
  301. e.OutErr(c, e.ERR_DB_ORM, err1.Error())
  302. return
  303. }
  304. for _, v1 := range v.ClassList {
  305. classes = append(classes, &model.Class{
  306. Name: v1.Name,
  307. Memo: "",
  308. GradeId: insertId,
  309. EnterpriseId: req.Id,
  310. CreateAt: now.Format("2006-01-02 15:04:05"),
  311. UpdateAt: now.Format("2006-01-02 15:04:05"),
  312. })
  313. }
  314. } else {
  315. //TODO::编辑
  316. grade, err3 := gradeDb.GetGrade(v.Id)
  317. if err3 != nil {
  318. e.OutErr(c, e.ERR_DB_ORM, err3.Error())
  319. return
  320. }
  321. if grade == nil {
  322. e.OutErr(c, e.ERR_NO_DATA, "未查询到相关年级信息")
  323. return
  324. }
  325. grade.Name = v.Name
  326. _, err4 := gradeDb.GradeUpdate(grade, "name")
  327. if err4 != nil {
  328. e.OutErr(c, e.ERR_DB_ORM, err4.Error())
  329. return
  330. }
  331. for _, v1 := range v.ClassList {
  332. if v1.Id == 0 {
  333. //新增
  334. classes = append(classes, &model.Class{
  335. Name: v1.Name,
  336. Memo: "",
  337. GradeId: grade.Id,
  338. EnterpriseId: req.Id,
  339. CreateAt: now.Format("2006-01-02 15:04:05"),
  340. UpdateAt: now.Format("2006-01-02 15:04:05"),
  341. })
  342. } else {
  343. //编辑
  344. class, err5 := classDb.GetClass(v1.Id)
  345. if err5 != nil {
  346. e.OutErr(c, e.ERR_DB_ORM, err5.Error())
  347. return
  348. }
  349. if class == nil {
  350. e.OutErr(c, e.ERR_NO_DATA, "未查询到相关班级信息")
  351. return
  352. }
  353. class.Name = v1.Name
  354. _, err6 := classDb.ClassUpdate(class, "name")
  355. if err6 != nil {
  356. e.OutErr(c, e.ERR_DB_ORM, err6.Error())
  357. return
  358. }
  359. }
  360. }
  361. }
  362. if len(classes) > 0 {
  363. _, err2 := classDb.BatchAddClass(classes)
  364. if err2 != nil {
  365. e.OutErr(c, e.ERR_DB_ORM, err2.Error())
  366. return
  367. }
  368. }
  369. }
  370. e.OutSuc(c, "success", nil)
  371. return
  372. }
  373. func EnterpriseAddGrade(c *gin.Context) {
  374. var req md.EnterpriseUpdateStateReq
  375. err := c.ShouldBindJSON(&req)
  376. if err != nil {
  377. err = validate.HandleValidateErr(err)
  378. err1 := err.(e.E)
  379. e.OutErr(c, err1.Code, err1.Error())
  380. return
  381. }
  382. enterpriseDb := db.EnterpriseDb{}
  383. enterpriseDb.Set()
  384. enterprise, err := enterpriseDb.GetEnterprise(req.Id)
  385. if err != nil {
  386. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  387. return
  388. }
  389. if enterprise == nil {
  390. e.OutErr(c, e.ERR_NO_DATA, "未查询到对应记录")
  391. return
  392. }
  393. now := time.Now()
  394. enterprise.State = req.State
  395. enterprise.UpdateAt = now.Format("2006-01-02 15:04:05")
  396. _, err = enterpriseDb.EnterpriseUpdate(enterprise, "state", "update_at")
  397. if err != nil {
  398. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  399. return
  400. }
  401. e.OutSuc(c, "success", nil)
  402. return
  403. }
  404. func EnterpriseUpdateState(c *gin.Context) {
  405. var req md.EnterpriseUpdateStateReq
  406. err := c.ShouldBindJSON(&req)
  407. if err != nil {
  408. err = validate.HandleValidateErr(err)
  409. err1 := err.(e.E)
  410. e.OutErr(c, err1.Code, err1.Error())
  411. return
  412. }
  413. enterpriseDb := db.EnterpriseDb{}
  414. enterpriseDb.Set()
  415. enterprise, err := enterpriseDb.GetEnterprise(req.Id)
  416. if err != nil {
  417. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  418. return
  419. }
  420. if enterprise == nil {
  421. e.OutErr(c, e.ERR_NO_DATA, "未查询到对应记录")
  422. return
  423. }
  424. now := time.Now()
  425. enterprise.State = req.State
  426. enterprise.UpdateAt = now.Format("2006-01-02 15:04:05")
  427. _, err = enterpriseDb.EnterpriseUpdate(enterprise, "state", "update_at")
  428. if err != nil {
  429. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  430. return
  431. }
  432. e.OutSuc(c, "success", nil)
  433. return
  434. }
  435. func ListCentralKitchenForSchoolPackageForSystem(c *gin.Context) {
  436. var req md.ListCentralKitchenForSchoolPackageForSystemReq
  437. err := c.ShouldBindJSON(&req)
  438. if err != nil {
  439. err = validate.HandleValidateErr(err)
  440. err1 := err.(e.E)
  441. e.OutErr(c, err1.Code, err1.Error())
  442. return
  443. }
  444. centralKitchenForSchoolPackageForSystemDb := db.CentralKitchenForSchoolPackageForSystemDb{}
  445. centralKitchenForSchoolPackageForSystemDb.Set()
  446. list, total, err := centralKitchenForSchoolPackageForSystemDb.CentralKitchenForSchoolPackageForSystemList(req)
  447. if err != nil {
  448. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  449. return
  450. }
  451. e.OutSuc(c, map[string]interface{}{
  452. "list": list,
  453. "total": total,
  454. }, nil)
  455. return
  456. }
  457. func SetCentralKitchenForSchoolWithSpecForSystem(c *gin.Context) {
  458. var req md.SetCentralKitchenForSchoolWithSpecForSystemReq
  459. err := c.ShouldBindJSON(&req)
  460. if err != nil {
  461. err = validate.HandleValidateErr(err)
  462. err1 := err.(e.E)
  463. e.OutErr(c, err1.Code, err1.Error())
  464. return
  465. }
  466. centralKitchenForSchoolWithSpecForSystemDb := db.CentralKitchenForSchoolWithSpecForSystem{}
  467. centralKitchenForSchoolWithSpecForSystemDb.Set()
  468. system, err := centralKitchenForSchoolWithSpecForSystemDb.GetCentralKitchenForSchoolWithSpecForSystem()
  469. if err != nil {
  470. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  471. return
  472. }
  473. system.BreakfastUnitPrice = req.BreakfastUnitPrice
  474. system.LunchUnitPrice = req.LunchUnitPrice
  475. system.DinnerUnitPrice = req.DinnerUnitPrice
  476. system.BreakfastUnitPriceForTeacher = req.BreakfastUnitPriceForTeacher
  477. system.LunchUnitPriceForTeacher = req.LunchUnitPriceForTeacher
  478. system.DinnerUnitPriceForTeacher = req.DinnerUnitPriceForTeacher
  479. system.UpdateAt = time.Now().Format("2006-01-02 15:04:05")
  480. updateAffected, err := centralKitchenForSchoolWithSpecForSystemDb.CentralKitchenForSchoolWithSpecForSystemUpdate(system.Id, system)
  481. if err != nil {
  482. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  483. return
  484. }
  485. if updateAffected <= 0 {
  486. e.OutErr(c, e.ERR, "更新数据失败")
  487. return
  488. }
  489. e.OutSuc(c, "success", nil)
  490. return
  491. }
  492. func GetCentralKitchenForSchoolWithSpecForSystem(c *gin.Context) {
  493. centralKitchenForSchoolWithSpecForSystemDb := db.CentralKitchenForSchoolWithSpecForSystem{}
  494. centralKitchenForSchoolWithSpecForSystemDb.Set()
  495. system, err := centralKitchenForSchoolWithSpecForSystemDb.GetCentralKitchenForSchoolWithSpecForSystem()
  496. if err != nil {
  497. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  498. return
  499. }
  500. e.OutSuc(c, map[string]interface{}{
  501. "data": system,
  502. }, nil)
  503. return
  504. }
  505. func SaveCentralKitchenForSchoolPackageForSystem(c *gin.Context) {
  506. var req md.SaveCentralKitchenForSchoolPackageForSystemReq
  507. err := c.ShouldBindJSON(&req)
  508. if err != nil {
  509. err = validate.HandleValidateErr(err)
  510. err1 := err.(e.E)
  511. e.OutErr(c, err1.Code, err1.Error())
  512. return
  513. }
  514. //判断是新增 / 编辑
  515. if req.Id > 0 {
  516. err = svc.UpdateCentralKitchenForSchoolPackageForSystem(req)
  517. if err != nil {
  518. e.OutErr(c, e.ERR, err.Error())
  519. return
  520. }
  521. } else {
  522. err = svc.AddCentralKitchenForSchoolPackageForSystem(req)
  523. if err != nil {
  524. e.OutErr(c, e.ERR, err.Error())
  525. return
  526. }
  527. }
  528. e.OutSuc(c, "success", nil)
  529. return
  530. }
  531. func SyncCentralKitchenForSchoolPackageForSystem(c *gin.Context) {
  532. var req md.SyncCentralKitchenForSchoolPackageForSystemReq
  533. err := c.ShouldBindJSON(&req)
  534. if err != nil {
  535. err = validate.HandleValidateErr(err)
  536. err1 := err.(e.E)
  537. e.OutErr(c, err1.Code, err1.Error())
  538. return
  539. }
  540. err = svc.SyncCentralKitchenForSchoolPackageForSystem(req)
  541. if err != nil {
  542. e.OutErr(c, e.ERR, err.Error())
  543. return
  544. }
  545. e.OutSuc(c, "success", nil)
  546. return
  547. }
  548. func DeleteCentralKitchenForSchoolPackageForSystem(c *gin.Context) {
  549. id := c.Param("id")
  550. err := svc.DeleteCentralKitchenForSchoolPackageForSystem(utils.StrToInt(id))
  551. if err != nil {
  552. e.OutErr(c, e.ERR, err.Error())
  553. return
  554. }
  555. e.OutSuc(c, "success", nil)
  556. return
  557. }