智慧食堂
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.

380 righe
8.8 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. classDb := db.ClassDb{}
  96. classDb.Set(utils.StrToInt(gradeId))
  97. classList, err := classDb.FindClass()
  98. if err != nil {
  99. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  100. return
  101. }
  102. e.OutSuc(c, map[string]interface{}{
  103. "list": classList,
  104. }, nil)
  105. return
  106. }
  107. func EnterpriseAdd(c *gin.Context) {
  108. var req md.EnterpriseAddReq
  109. err := c.ShouldBindJSON(&req)
  110. if err != nil {
  111. err = validate.HandleValidateErr(err)
  112. err1 := err.(e.E)
  113. e.OutErr(c, err1.Code, err1.Error())
  114. return
  115. }
  116. enterpriseDb := db.EnterpriseDb{}
  117. enterpriseDb.Set()
  118. now := time.Now()
  119. var pvd = 2
  120. if req.Kind == enum.EnterprisePvdByCentralKitchenForSchool || req.Kind == enum.EnterprisePvdByCentralKitchenForFactory {
  121. pvd = 1
  122. }
  123. enterprise := model.Enterprise{
  124. Name: req.Name,
  125. Pvd: int32(pvd),
  126. Kind: req.Kind,
  127. CompanyId: req.CompanyId,
  128. State: enum2.CompanyStateForNormal,
  129. Memo: req.Memo,
  130. CreateAt: now,
  131. UpdateAt: now,
  132. }
  133. _, err = enterpriseDb.EnterpriseInsert(&enterprise)
  134. if err != nil {
  135. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  136. return
  137. }
  138. e.OutSuc(c, "success", nil)
  139. return
  140. }
  141. func EnterpriseDelete(c *gin.Context) {
  142. var req md.EnterpriseDeleteReq
  143. err := c.ShouldBindJSON(&req)
  144. if err != nil {
  145. err = validate.HandleValidateErr(err)
  146. err1 := err.(e.E)
  147. e.OutErr(c, err1.Code, err1.Error())
  148. return
  149. }
  150. err = svc.EnterpriseDelete(req.EnterpriseIds)
  151. if err != nil {
  152. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  153. return
  154. }
  155. e.OutSuc(c, "success", nil)
  156. return
  157. }
  158. func EnterpriseUpdate(c *gin.Context) {
  159. var req md.EnterpriseUpdateReq
  160. err := c.ShouldBindJSON(&req)
  161. if err != nil {
  162. err = validate.HandleValidateErr(err)
  163. err1 := err.(e.E)
  164. e.OutErr(c, err1.Code, err1.Error())
  165. return
  166. }
  167. //1、更新 enterprise
  168. enterpriseDb := db.EnterpriseDb{}
  169. enterpriseDb.Set()
  170. enterprise, err := enterpriseDb.GetEnterprise(req.Id)
  171. if err != nil {
  172. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  173. return
  174. }
  175. if enterprise == nil {
  176. e.OutErr(c, e.ERR_NO_DATA, "未查询到对应记录")
  177. return
  178. }
  179. var pvd = 2
  180. if req.Kind == enum.EnterprisePvdByCentralKitchenForSchool || req.Kind == enum.EnterprisePvdByCentralKitchenForFactory {
  181. pvd = 1
  182. }
  183. now := time.Now()
  184. enterprise.Name = req.Name
  185. enterprise.Memo = req.Memo
  186. enterprise.Pvd = int32(pvd)
  187. enterprise.Kind = req.Kind
  188. enterprise.CompanyId = req.CompanyId
  189. enterprise.UpdateAt = now
  190. _, err = enterpriseDb.EnterpriseUpdate(enterprise, "name", "memo", "pvd", "kind", "company_id", "update_at")
  191. if err != nil {
  192. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  193. return
  194. }
  195. //2、更新 grade && class 数据
  196. //2、删除 grade && class 数据
  197. gradeDb := db.GradeDb{}
  198. gradeDb.Set(req.Id)
  199. //_, err = gradeDb.ClassDeleteBySessionForEnterprise(db.Db.NewSession(), req.Id)
  200. //if err != nil {
  201. // e.OutErr(c, e.ERR_DB_ORM, err.Error())
  202. // return
  203. //}
  204. classDb := db.ClassDb{}
  205. classDb.Set(0)
  206. //_, err = classDb.ClassDeleteBySessionForEnterprise(db.Db.NewSession(), req.Id)
  207. //if err != nil {
  208. // e.OutErr(c, e.ERR_DB_ORM, err.Error())
  209. // return
  210. //}
  211. //新增 grade 数据 && class 数据
  212. for _, v := range req.GradeList {
  213. var classes []*model.Class
  214. if v.Id == 0 {
  215. //TODO::新增
  216. insertId, err1 := gradeDb.GradeInsert(&model.Grade{
  217. EnterpriseId: req.Id,
  218. Name: v.Name,
  219. Memo: "",
  220. CreateAt: now,
  221. UpdateAt: now,
  222. })
  223. if err1 != nil {
  224. e.OutErr(c, e.ERR_DB_ORM, err1.Error())
  225. return
  226. }
  227. for _, v1 := range v.ClassList {
  228. classes = append(classes, &model.Class{
  229. Name: v1.Name,
  230. Memo: "",
  231. GradeId: insertId,
  232. EnterpriseId: req.Id,
  233. CreateAt: now,
  234. UpdateAt: now,
  235. })
  236. }
  237. } else {
  238. //TODO::编辑
  239. grade, err3 := gradeDb.GetGrade(v.Id)
  240. if err3 != nil {
  241. e.OutErr(c, e.ERR_DB_ORM, err3.Error())
  242. return
  243. }
  244. if grade == nil {
  245. e.OutErr(c, e.ERR_NO_DATA, "未查询到相关年级信息")
  246. return
  247. }
  248. grade.Name = v.Name
  249. _, err4 := gradeDb.GradeUpdate(grade, "name")
  250. if err4 != nil {
  251. e.OutErr(c, e.ERR_DB_ORM, err4.Error())
  252. return
  253. }
  254. for _, v1 := range v.ClassList {
  255. if v1.Id == 0 {
  256. //新增
  257. classes = append(classes, &model.Class{
  258. Name: v1.Name,
  259. Memo: "",
  260. GradeId: grade.Id,
  261. EnterpriseId: req.Id,
  262. CreateAt: now,
  263. UpdateAt: now,
  264. })
  265. } else {
  266. //编辑
  267. class, err5 := classDb.GetClass(v1.Id)
  268. if err5 != nil {
  269. e.OutErr(c, e.ERR_DB_ORM, err5.Error())
  270. return
  271. }
  272. if class == nil {
  273. e.OutErr(c, e.ERR_NO_DATA, "未查询到相关班级信息")
  274. return
  275. }
  276. class.Name = v1.Name
  277. _, err6 := classDb.ClassUpdate(class, "name")
  278. if err6 != nil {
  279. e.OutErr(c, e.ERR_DB_ORM, err6.Error())
  280. return
  281. }
  282. }
  283. }
  284. }
  285. if len(classes) > 0 {
  286. _, err2 := classDb.BatchAddClass(classes)
  287. if err2 != nil {
  288. e.OutErr(c, e.ERR_DB_ORM, err2.Error())
  289. return
  290. }
  291. }
  292. }
  293. e.OutSuc(c, "success", nil)
  294. return
  295. }
  296. func EnterpriseAddGrade(c *gin.Context) {
  297. var req md.EnterpriseUpdateStateReq
  298. err := c.ShouldBindJSON(&req)
  299. if err != nil {
  300. err = validate.HandleValidateErr(err)
  301. err1 := err.(e.E)
  302. e.OutErr(c, err1.Code, err1.Error())
  303. return
  304. }
  305. enterpriseDb := db.EnterpriseDb{}
  306. enterpriseDb.Set()
  307. enterprise, err := enterpriseDb.GetEnterprise(req.Id)
  308. if err != nil {
  309. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  310. return
  311. }
  312. if enterprise == nil {
  313. e.OutErr(c, e.ERR_NO_DATA, "未查询到对应记录")
  314. return
  315. }
  316. now := time.Now()
  317. enterprise.State = req.State
  318. enterprise.UpdateAt = now
  319. _, err = enterpriseDb.EnterpriseUpdate(enterprise, "state", "update_at")
  320. if err != nil {
  321. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  322. return
  323. }
  324. e.OutSuc(c, "success", nil)
  325. return
  326. }
  327. func EnterpriseUpdateState(c *gin.Context) {
  328. var req md.EnterpriseUpdateStateReq
  329. err := c.ShouldBindJSON(&req)
  330. if err != nil {
  331. err = validate.HandleValidateErr(err)
  332. err1 := err.(e.E)
  333. e.OutErr(c, err1.Code, err1.Error())
  334. return
  335. }
  336. enterpriseDb := db.EnterpriseDb{}
  337. enterpriseDb.Set()
  338. enterprise, err := enterpriseDb.GetEnterprise(req.Id)
  339. if err != nil {
  340. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  341. return
  342. }
  343. if enterprise == nil {
  344. e.OutErr(c, e.ERR_NO_DATA, "未查询到对应记录")
  345. return
  346. }
  347. now := time.Now()
  348. enterprise.State = req.State
  349. enterprise.UpdateAt = now
  350. _, err = enterpriseDb.EnterpriseUpdate(enterprise, "state", "update_at")
  351. if err != nil {
  352. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  353. return
  354. }
  355. e.OutSuc(c, "success", nil)
  356. return
  357. }