智慧食堂
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

394 linhas
9.4 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. _, err = enterpriseDb.EnterpriseInsert(&enterprise)
  147. if err != nil {
  148. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  149. return
  150. }
  151. e.OutSuc(c, "success", nil)
  152. return
  153. }
  154. func EnterpriseDelete(c *gin.Context) {
  155. var req md.EnterpriseDeleteReq
  156. err := c.ShouldBindJSON(&req)
  157. if err != nil {
  158. err = validate.HandleValidateErr(err)
  159. err1 := err.(e.E)
  160. e.OutErr(c, err1.Code, err1.Error())
  161. return
  162. }
  163. err = svc.EnterpriseDelete(req.EnterpriseIds)
  164. if err != nil {
  165. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  166. return
  167. }
  168. e.OutSuc(c, "success", nil)
  169. return
  170. }
  171. func EnterpriseUpdate(c *gin.Context) {
  172. var req md.EnterpriseUpdateReq
  173. err := c.ShouldBindJSON(&req)
  174. if err != nil {
  175. err = validate.HandleValidateErr(err)
  176. err1 := err.(e.E)
  177. e.OutErr(c, err1.Code, err1.Error())
  178. return
  179. }
  180. //1、更新 enterprise
  181. enterpriseDb := db.EnterpriseDb{}
  182. enterpriseDb.Set()
  183. enterprise, err := enterpriseDb.GetEnterprise(req.Id)
  184. if err != nil {
  185. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  186. return
  187. }
  188. if enterprise == nil {
  189. e.OutErr(c, e.ERR_NO_DATA, "未查询到对应记录")
  190. return
  191. }
  192. var pvd = 2
  193. if req.Kind == enum.EnterprisePvdByCentralKitchenForSchool || req.Kind == enum.EnterprisePvdByCentralKitchenForFactory {
  194. pvd = 1
  195. }
  196. now := time.Now()
  197. enterprise.Name = req.Name
  198. enterprise.Memo = req.Memo
  199. enterprise.Pvd = int32(pvd)
  200. enterprise.Kind = req.Kind
  201. enterprise.CompanyId = req.CompanyId
  202. enterprise.UpdateAt = now.Format("2006-01-02 15:04:05")
  203. _, err = enterpriseDb.EnterpriseUpdate(enterprise, "name", "memo", "pvd", "kind", "company_id", "update_at")
  204. if err != nil {
  205. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  206. return
  207. }
  208. //2、更新 grade && class 数据
  209. //2、删除 grade && class 数据
  210. gradeDb := db.GradeDb{}
  211. gradeDb.Set(req.Id)
  212. //_, err = gradeDb.ClassDeleteBySessionForEnterprise(db.Db.NewSession(), req.Id)
  213. //if err != nil {
  214. // e.OutErr(c, e.ERR_DB_ORM, err.Error())
  215. // return
  216. //}
  217. classDb := db.ClassDb{}
  218. classDb.Set(0)
  219. //_, err = classDb.ClassDeleteBySessionForEnterprise(db.Db.NewSession(), req.Id)
  220. //if err != nil {
  221. // e.OutErr(c, e.ERR_DB_ORM, err.Error())
  222. // return
  223. //}
  224. //新增 grade 数据 && class 数据
  225. for _, v := range req.GradeList {
  226. var classes []*model.Class
  227. if v.Id == 0 {
  228. //TODO::新增
  229. insertId, err1 := gradeDb.GradeInsert(&model.Grade{
  230. EnterpriseId: req.Id,
  231. Name: v.Name,
  232. Memo: "",
  233. CreateAt: now.Format("2006-01-02 15:04:05"),
  234. UpdateAt: now.Format("2006-01-02 15:04:05"),
  235. })
  236. if err1 != nil {
  237. e.OutErr(c, e.ERR_DB_ORM, err1.Error())
  238. return
  239. }
  240. for _, v1 := range v.ClassList {
  241. classes = append(classes, &model.Class{
  242. Name: v1.Name,
  243. Memo: "",
  244. GradeId: insertId,
  245. EnterpriseId: req.Id,
  246. CreateAt: now.Format("2006-01-02 15:04:05"),
  247. UpdateAt: now.Format("2006-01-02 15:04:05"),
  248. })
  249. }
  250. } else {
  251. //TODO::编辑
  252. grade, err3 := gradeDb.GetGrade(v.Id)
  253. if err3 != nil {
  254. e.OutErr(c, e.ERR_DB_ORM, err3.Error())
  255. return
  256. }
  257. if grade == nil {
  258. e.OutErr(c, e.ERR_NO_DATA, "未查询到相关年级信息")
  259. return
  260. }
  261. grade.Name = v.Name
  262. _, err4 := gradeDb.GradeUpdate(grade, "name")
  263. if err4 != nil {
  264. e.OutErr(c, e.ERR_DB_ORM, err4.Error())
  265. return
  266. }
  267. for _, v1 := range v.ClassList {
  268. if v1.Id == 0 {
  269. //新增
  270. classes = append(classes, &model.Class{
  271. Name: v1.Name,
  272. Memo: "",
  273. GradeId: grade.Id,
  274. EnterpriseId: req.Id,
  275. CreateAt: now.Format("2006-01-02 15:04:05"),
  276. UpdateAt: now.Format("2006-01-02 15:04:05"),
  277. })
  278. } else {
  279. //编辑
  280. class, err5 := classDb.GetClass(v1.Id)
  281. if err5 != nil {
  282. e.OutErr(c, e.ERR_DB_ORM, err5.Error())
  283. return
  284. }
  285. if class == nil {
  286. e.OutErr(c, e.ERR_NO_DATA, "未查询到相关班级信息")
  287. return
  288. }
  289. class.Name = v1.Name
  290. _, err6 := classDb.ClassUpdate(class, "name")
  291. if err6 != nil {
  292. e.OutErr(c, e.ERR_DB_ORM, err6.Error())
  293. return
  294. }
  295. }
  296. }
  297. }
  298. if len(classes) > 0 {
  299. _, err2 := classDb.BatchAddClass(classes)
  300. if err2 != nil {
  301. e.OutErr(c, e.ERR_DB_ORM, err2.Error())
  302. return
  303. }
  304. }
  305. }
  306. e.OutSuc(c, "success", nil)
  307. return
  308. }
  309. func EnterpriseAddGrade(c *gin.Context) {
  310. var req md.EnterpriseUpdateStateReq
  311. err := c.ShouldBindJSON(&req)
  312. if err != nil {
  313. err = validate.HandleValidateErr(err)
  314. err1 := err.(e.E)
  315. e.OutErr(c, err1.Code, err1.Error())
  316. return
  317. }
  318. enterpriseDb := db.EnterpriseDb{}
  319. enterpriseDb.Set()
  320. enterprise, err := enterpriseDb.GetEnterprise(req.Id)
  321. if err != nil {
  322. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  323. return
  324. }
  325. if enterprise == nil {
  326. e.OutErr(c, e.ERR_NO_DATA, "未查询到对应记录")
  327. return
  328. }
  329. now := time.Now()
  330. enterprise.State = req.State
  331. enterprise.UpdateAt = now.Format("2006-01-02 15:04:05")
  332. _, err = enterpriseDb.EnterpriseUpdate(enterprise, "state", "update_at")
  333. if err != nil {
  334. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  335. return
  336. }
  337. e.OutSuc(c, "success", nil)
  338. return
  339. }
  340. func EnterpriseUpdateState(c *gin.Context) {
  341. var req md.EnterpriseUpdateStateReq
  342. err := c.ShouldBindJSON(&req)
  343. if err != nil {
  344. err = validate.HandleValidateErr(err)
  345. err1 := err.(e.E)
  346. e.OutErr(c, err1.Code, err1.Error())
  347. return
  348. }
  349. enterpriseDb := db.EnterpriseDb{}
  350. enterpriseDb.Set()
  351. enterprise, err := enterpriseDb.GetEnterprise(req.Id)
  352. if err != nil {
  353. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  354. return
  355. }
  356. if enterprise == nil {
  357. e.OutErr(c, e.ERR_NO_DATA, "未查询到对应记录")
  358. return
  359. }
  360. now := time.Now()
  361. enterprise.State = req.State
  362. enterprise.UpdateAt = now.Format("2006-01-02 15:04:05")
  363. _, err = enterpriseDb.EnterpriseUpdate(enterprise, "state", "update_at")
  364. if err != nil {
  365. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  366. return
  367. }
  368. e.OutSuc(c, "success", nil)
  369. return
  370. }