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

hdl_enterprise.go 7.6 KiB

пре 1 година
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  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. gradeDb := db.GradeDb{}
  197. gradeDb.Set(req.Id)
  198. _, err = gradeDb.ClassDeleteBySessionForEnterprise(db.Db.NewSession(), req.Id)
  199. if err != nil {
  200. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  201. return
  202. }
  203. classDb := db.ClassDb{}
  204. classDb.Set(0)
  205. _, err = classDb.ClassDeleteBySessionForEnterprise(db.Db.NewSession(), req.Id)
  206. if err != nil {
  207. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  208. return
  209. }
  210. //新增 grade 数据 && class 数据
  211. for _, v := range req.GradeList {
  212. insertId, err1 := gradeDb.GradeInsert(&model.Grade{
  213. EnterpriseId: req.Id,
  214. Name: v.Name,
  215. Memo: "",
  216. CreateAt: now,
  217. UpdateAt: now,
  218. })
  219. if err1 != nil {
  220. e.OutErr(c, e.ERR_DB_ORM, err1.Error())
  221. return
  222. }
  223. var classes []*model.Class
  224. for _, v1 := range v.ClassList {
  225. classes = append(classes, &model.Class{
  226. Name: v1.Name,
  227. Memo: "",
  228. GradeId: insertId,
  229. EnterpriseId: req.Id,
  230. CreateAt: now,
  231. UpdateAt: now,
  232. })
  233. }
  234. if len(classes) > 0 {
  235. _, err2 := classDb.BatchAddClass(classes)
  236. if err2 != nil {
  237. e.OutErr(c, e.ERR_DB_ORM, err2.Error())
  238. return
  239. }
  240. }
  241. }
  242. e.OutSuc(c, "success", nil)
  243. return
  244. }
  245. func EnterpriseAddGrade(c *gin.Context) {
  246. var req md.EnterpriseUpdateStateReq
  247. err := c.ShouldBindJSON(&req)
  248. if err != nil {
  249. err = validate.HandleValidateErr(err)
  250. err1 := err.(e.E)
  251. e.OutErr(c, err1.Code, err1.Error())
  252. return
  253. }
  254. enterpriseDb := db.EnterpriseDb{}
  255. enterpriseDb.Set()
  256. enterprise, err := enterpriseDb.GetEnterprise(req.Id)
  257. if err != nil {
  258. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  259. return
  260. }
  261. if enterprise == nil {
  262. e.OutErr(c, e.ERR_NO_DATA, "未查询到对应记录")
  263. return
  264. }
  265. now := time.Now()
  266. enterprise.State = req.State
  267. enterprise.UpdateAt = now
  268. _, err = enterpriseDb.EnterpriseUpdate(enterprise, "state", "update_at")
  269. if err != nil {
  270. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  271. return
  272. }
  273. e.OutSuc(c, "success", nil)
  274. return
  275. }
  276. func EnterpriseUpdateState(c *gin.Context) {
  277. var req md.EnterpriseUpdateStateReq
  278. err := c.ShouldBindJSON(&req)
  279. if err != nil {
  280. err = validate.HandleValidateErr(err)
  281. err1 := err.(e.E)
  282. e.OutErr(c, err1.Code, err1.Error())
  283. return
  284. }
  285. enterpriseDb := db.EnterpriseDb{}
  286. enterpriseDb.Set()
  287. enterprise, err := enterpriseDb.GetEnterprise(req.Id)
  288. if err != nil {
  289. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  290. return
  291. }
  292. if enterprise == nil {
  293. e.OutErr(c, e.ERR_NO_DATA, "未查询到对应记录")
  294. return
  295. }
  296. now := time.Now()
  297. enterprise.State = req.State
  298. enterprise.UpdateAt = now
  299. _, err = enterpriseDb.EnterpriseUpdate(enterprise, "state", "update_at")
  300. if err != nil {
  301. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  302. return
  303. }
  304. e.OutSuc(c, "success", nil)
  305. return
  306. }