智慧食堂
Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.

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