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

204 lines
5.9 KiB

  1. package svc
  2. import (
  3. "applet/app/admin/md"
  4. "applet/app/db"
  5. "applet/app/db/model"
  6. "applet/app/enum"
  7. "applet/app/utils"
  8. "errors"
  9. "fmt"
  10. "time"
  11. )
  12. func DeleteFactoryIdentity(id int) (err error) {
  13. session := db.Db.NewSession()
  14. defer session.Close()
  15. session.Begin()
  16. //1、删除 `role`
  17. factoryIdentityDb := db.FactoryIdentityDb{}
  18. factoryIdentityDb.Set(id)
  19. _, err = factoryIdentityDb.FactoryIdentityDeleteBySession(session, id)
  20. if err != nil {
  21. _ = session.Rollback()
  22. return
  23. }
  24. //2、删除 `factory_identity_with_user_identity`
  25. factoryIdentityWithUserIdentityDb := db.FactoryIdentityWithUserIdentityDb{}
  26. factoryIdentityWithUserIdentityDb.Set()
  27. _, err = factoryIdentityWithUserIdentityDb.FactoryIdentityWithUserIdentityDeleteForRoleBySession(session, id)
  28. if err != nil {
  29. _ = session.Rollback()
  30. return
  31. }
  32. return session.Commit()
  33. }
  34. //func RoleBindPermissionGroup(req md.RoleBindPermissionGroupReq) (err error) {
  35. // session := db.Db.NewSession()
  36. // defer session.Close()
  37. // session.Begin()
  38. // //1、查询 `role`
  39. // roleDb := db.RoleDb{}
  40. // roleDb.Set(req.RoleId)
  41. // role, err := roleDb.GetRole()
  42. // if err != nil {
  43. // return
  44. // }
  45. // if role == nil {
  46. // return errors.New("未查询到相应记录")
  47. // }
  48. //
  49. // //1、删除 `role_permission_group`
  50. // rolePermissionGroupDb := db.RolePermissionGroupDb{}
  51. // rolePermissionGroupDb.Set()
  52. // _, err = rolePermissionGroupDb.RolePermissionGroupDeleteForRoleBySession(session, req.RoleId)
  53. // if err != nil {
  54. // _ = session.Rollback()
  55. // return
  56. // }
  57. //
  58. // //2、新增 `role_permission_group``
  59. // var mm []*model.RolePermissionGroup
  60. // now := time.Now()
  61. // for _, v := range req.PermissionIds {
  62. // mm = append(mm, &model.RolePermissionGroup{
  63. // RoleId: role.Id,
  64. // GroupId: v,
  65. // CreateAt: now.Format("2006-01-02 15:04:05"),
  66. // UpdateAt: now.Format("2006-01-02 15:04:05"),
  67. // })
  68. // }
  69. // _, err = rolePermissionGroupDb.BatchAddRolePermissionGroupBySession(session, mm)
  70. // if err != nil {
  71. // _ = session.Rollback()
  72. // return
  73. // }
  74. //
  75. // return session.Commit()
  76. //}
  77. func BindUserFactoryIdentity(req md.BindFactoryIdentityReq) (err error) {
  78. session := db.Db.NewSession()
  79. defer session.Close()
  80. session.Begin()
  81. //1、查询 `role`
  82. userIdentityDb := db.UserIdentityDb{}
  83. userIdentityDb.Set(0)
  84. role, err := userIdentityDb.GetUserIdentity(req.UserIdentitiesId)
  85. if err != nil {
  86. return
  87. }
  88. if role == nil {
  89. return errors.New("未查询到相应记录")
  90. }
  91. //2、删除 `factory_identity_with_user_identity`
  92. factoryIdentityWithUserIdentityDb := db.FactoryIdentityWithUserIdentityDb{}
  93. factoryIdentityWithUserIdentityDb.Set()
  94. _, err = factoryIdentityWithUserIdentityDb.FactoryIdentityWithUserIdentityDeleteForRoleBySession(session, req.FactoryIdentityId)
  95. if err != nil {
  96. _ = session.Rollback()
  97. return
  98. }
  99. //3、新增 `删除 `factory_identity_with_user_identity``
  100. now := time.Now()
  101. _, err = factoryIdentityWithUserIdentityDb.AddFactoryIdentityWithUserIdentityBySession(session, &model.FactoryIdentityWithUserIdentity{
  102. UserIdentityId: req.UserIdentitiesId,
  103. FactoryIdentityId: req.FactoryIdentityId,
  104. CreateAt: now.Format("2006-01-02 15:04:05"),
  105. UpdateAt: now.Format("2006-01-02 15:04:05"),
  106. })
  107. if err != nil {
  108. _ = session.Rollback()
  109. return
  110. }
  111. return session.Commit()
  112. }
  113. func FactoryInfo(enterpriseId int) (err error, resp md.FactoryInfoResp) {
  114. //1、查询`enterprise`
  115. enterpriseDb := db.EnterpriseDb{}
  116. enterpriseDb.Set()
  117. enterprise, err := enterpriseDb.GetEnterprise(enterpriseId)
  118. if err != nil {
  119. return
  120. }
  121. resp.Name = enterprise.Name
  122. resp.Memo = enterprise.Memo
  123. resp.Kind = enum.EnterpriseKind(enterprise.Kind).String()
  124. resp.Mode = enum.EnterpriseMode(enterprise.Mode).String()
  125. resp.Pvd = enum.EnterprisePvd(enterprise.Pvd).String()
  126. resp.State = enum.EnterpriseState(enterprise.State).String()
  127. //2、统计 "用户"、 "工人" 数量
  128. userIdentityDb := db.UserIdentityDb{}
  129. userIdentityDb.Set(0)
  130. resp.WorkerNum, err = userIdentityDb.CountUserIdentityForEnterprise(enterpriseId, enum.UserIdentityForFactory)
  131. if err != nil {
  132. return
  133. }
  134. //userDb := db.UserDb{}
  135. //userDb.Set()
  136. //resp.UserNum, err = userDb.CountUserForEnterprise(enterpriseId)
  137. //if err != nil {
  138. // return
  139. //}
  140. //3、统计 订单数据
  141. now := time.Now()
  142. today := now.AddDate(0, 0, +1).Format("2006-01-02") //今日
  143. thisMonth := time.Date(now.Year(), now.Month(), 1, 0, 0, 0, 0, now.Location()).Format("2006-01-02") //本月
  144. sql1 := fmt.Sprintf("SELECT SUM(total_amount) AS this_month_consume_amount FROM factory_ord where gmt_payment >= '%s' and gmt_payment <= '%s'", thisMonth, today)
  145. sql2 := fmt.Sprintf("SELECT count(*) AS this_month_order_num FROM factory_ord where gmt_payment >= '%s' and gmt_payment <= '%s'", thisMonth, today)
  146. sql3 := fmt.Sprintf("SELECT SUM(service_fee) AS this_month_service_amount FROM factory_ord where gmt_payment >= '%s' and gmt_payment <= '%s'", thisMonth, today)
  147. sql4 := fmt.Sprintf("SELECT SUM(receipt_amount) AS this_month_receipt_amount FROM factory_ord where gmt_payment >= '%s' and gmt_payment <= '%s'", thisMonth, today)
  148. mapArr1, err := db.QueryNativeString(db.Db, sql1)
  149. if err != nil {
  150. return
  151. }
  152. if mapArr1 == nil {
  153. resp.ThisMonthConsumeAmount = 0
  154. } else {
  155. resp.ThisMonthConsumeAmount = utils.StrToFloat64(mapArr1[0]["this_month_consume_amount"])
  156. }
  157. mapArr2, err := db.QueryNativeString(db.Db, sql2)
  158. if err != nil {
  159. return
  160. }
  161. if mapArr2 == nil {
  162. resp.ThisMonthOrderNum = 0
  163. } else {
  164. resp.ThisMonthOrderNum = utils.StrToFloat64(mapArr2[0]["this_month_order_num"])
  165. }
  166. mapArr3, err := db.QueryNativeString(db.Db, sql3)
  167. if err != nil {
  168. return
  169. }
  170. if mapArr3 == nil {
  171. resp.ThisMonthServiceAmount = 0
  172. } else {
  173. resp.ThisMonthServiceAmount = utils.StrToFloat64(mapArr3[0]["this_month_service_amount"])
  174. }
  175. mapArr4, err := db.QueryNativeString(db.Db, sql4)
  176. if err != nil {
  177. return
  178. }
  179. if mapArr4 == nil {
  180. resp.ThisMonthReceiptAmount = 0
  181. } else {
  182. resp.ThisMonthReceiptAmount = utils.StrToFloat64(mapArr4[0]["receipt_amount"])
  183. }
  184. return
  185. }