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

388 lines
16 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. "applet/app/utils/logx"
  9. "encoding/json"
  10. "github.com/360EntSecGroup-Skylar/excelize"
  11. "strconv"
  12. "time"
  13. )
  14. func NursingHomeDataStatisticsExport(req md.NursingHomeDataStatisticsExportReq) {
  15. var titleList []string
  16. var fileName string
  17. var now = time.Now()
  18. xlsx := excelize.NewFile()
  19. xlsx.SetSheetRow("Sheet1", "A1", &titleList)
  20. switch req.Kind {
  21. case 1:
  22. //订单列表
  23. fileName = "/export/nursing_home/订单列表_" + utils.Int64ToStr(now.UnixMilli()) + ".xlsx"
  24. //查找订单数据
  25. sess := db.Db.Desc("nursing_home_package_ord.id")
  26. if req.EnterpriseId != 0 {
  27. sess.And("nursing_home_package_ord.enterprise_id =?", req.EnterpriseId)
  28. }
  29. if req.StartDate != "" {
  30. sess.And("nursing_home_package_ord.create_at >= ?", req.StartDate)
  31. }
  32. if req.EndDate != "" {
  33. sess.And("nursing_home_package_ord.create_at <= ?", req.EndDate)
  34. }
  35. var m []*db.NursingHomePackageOrdWithUserIdentity
  36. _, err := sess.
  37. Join("LEFT", "user_identity", "nursing_home_package_ord.user_identity_id = user_identity.id").
  38. Join("LEFT", "enterprise", "enterprise.id = nursing_home_package_ord.enterprise_id").
  39. Join("LEFT", "user", "user.id = nursing_home_package_ord.uid").
  40. FindAndCount(&m)
  41. if err != nil {
  42. logx.Error(err)
  43. println("<<<<NursingHomeDataStatisticsExport1>>>>>Error:::", err.Error())
  44. return
  45. }
  46. titleList = []string{"单位名称", "用户身份名称", "用户手机号", "购买类型", "总价", "商户订单号", "支付状态", "购买时间"}
  47. j := 2 //表头被第一行用了,只能从第二行开始
  48. for _, v := range m {
  49. var kind string //1:按年购买 2:按月购买 3:按天购买 4:补餐
  50. switch v.NursingHomePackageOrd.Kind {
  51. case 1:
  52. kind = "按年购买"
  53. break
  54. case 2:
  55. kind = "按月购买"
  56. break
  57. case 3:
  58. kind = "按天购买"
  59. break
  60. case 4:
  61. kind = "补餐"
  62. break
  63. }
  64. var state string //0:待支付 1:支付成功 2:支付失败
  65. switch v.NursingHomePackageOrd.State {
  66. case enum.NursingHomePackageOrdStateForWait:
  67. state = enum.NursingHomePackageOrdState.String(enum.NursingHomePackageOrdStateForWait)
  68. break
  69. case enum.NursingHomePackageOrdStateForSuccess:
  70. state = enum.NursingHomePackageOrdState.String(enum.NursingHomePackageOrdStateForWait)
  71. break
  72. case enum.NursingHomePackageOrdStateForFail:
  73. state = enum.NursingHomePackageOrdState.String(enum.NursingHomePackageOrdStateForFail)
  74. break
  75. }
  76. xlsx.SetSheetRow("Sheet1", "A"+strconv.Itoa(j), &[]interface{}{v.Enterprise.Name, v.NursingHomePackageOrd.UserIdentityName, v.User.Phone, kind, v.NursingHomePackageOrd.TotalPrice,
  77. v.NursingHomePackageOrd.OutTradeNo, state, v.NursingHomePackageOrd.CreateAt})
  78. j++
  79. }
  80. xlsx.SaveAs("./static" + fileName)
  81. break
  82. case 2:
  83. //退款申请表
  84. fileName = "/export/nursing_home/退款申请表_" + utils.Int64ToStr(now.UnixMilli()) + ".xlsx"
  85. var m []*db.NursingHomeUserRefundDayWithData
  86. sess := db.Db.Desc("nursing_home_user_refund_day.id")
  87. if req.EnterpriseId != 0 {
  88. sess.And("enterprise.enterprise_id =?", req.EnterpriseId)
  89. }
  90. if req.StartDate != "" {
  91. sess.And("nursing_home_user_refund_day.create_at >= ?", req.StartDate)
  92. }
  93. if req.EndDate != "" {
  94. sess.And("nursing_home_user_refund_day.create_at <= ?", req.EndDate)
  95. }
  96. _, err := sess.
  97. Join("LEFT", "nursing_home_user_with_day", "nursing_home_user_refund_day.records_id = nursing_home_user_with_day.id").
  98. Join("LEFT", "nursing_home_package_ord", "nursing_home_user_refund_day.out_trade_no = nursing_home_package_ord.out_trade_no").
  99. Join("LEFT", "user_identity", "nursing_home_user_with_day.identity_id = user_identity.id").
  100. Join("LEFT", "user", "user_identity.uid = user.id").
  101. Join("LEFT", "enterprise", "user_identity.enterprise_id = enterprise.id").
  102. Desc("nursing_home_user_refund_day.id").
  103. FindAndCount(&m)
  104. if err != nil {
  105. logx.Error(err)
  106. println("<<<<NursingHomeDataStatisticsExport2>>>>>Error:::", err.Error())
  107. return
  108. }
  109. titleList = []string{"退款单号", "所属订单号", "单位名称", "用户身份名称", "用户手机号", "就餐类型", "就餐时间", "金额", "状态", "退款时间"}
  110. j := 2 //表头被第一行用了,只能从第二行开始
  111. for _, v := range m {
  112. var kind string //就餐类型(1:早餐 2:午餐 3:晚餐)
  113. switch v.NursingHomeUserWithDay.Kind {
  114. case enum.NursingHomeUserWithDayKindForBreakfast:
  115. kind = enum.NursingHomeUserWithDayKind.String(enum.NursingHomeUserWithDayKindForBreakfast)
  116. break
  117. case enum.NursingHomeUserWithDayKindForLunch:
  118. kind = enum.NursingHomeUserWithDayKind.String(enum.NursingHomeUserWithDayKindForLunch)
  119. break
  120. case enum.NursingHomeUserWithDayKindForDinner:
  121. kind = enum.NursingHomeUserWithDayKind.String(enum.NursingHomeUserWithDayKindForDinner)
  122. break
  123. }
  124. var state string //状态(1:审核中 2:审核通过 3:审核拒绝 4:退款已完成)
  125. switch v.NursingHomeUserRefundDay.State {
  126. case enum.NursingHomeUserRefundDayStateForAuditing:
  127. state = enum.NursingHomeUserRefundDayState.String(enum.NursingHomeUserRefundDayStateForAuditing)
  128. break
  129. case enum.NursingHomeUserRefundDayStateForAuditPass:
  130. state = enum.NursingHomeUserRefundDayState.String(enum.NursingHomeUserRefundDayStateForAuditPass)
  131. break
  132. case enum.NursingHomeUserRefundDayStateForAuditReject:
  133. state = enum.NursingHomeUserRefundDayState.String(enum.NursingHomeUserRefundDayStateForAuditReject)
  134. break
  135. case enum.NursingHomeUserRefundDayStateForAuditComplete:
  136. state = enum.NursingHomeUserRefundDayState.String(enum.NursingHomeUserRefundDayStateForAuditComplete)
  137. break
  138. }
  139. xlsx.SetSheetRow("Sheet1", "A"+strconv.Itoa(j), &[]interface{}{v.NursingHomeUserRefundDay.OutRequestNo, v.NursingHomeUserRefundDay.OutTradeNo, v.Enterprise.Name,
  140. v.NursingHomePackageOrd.UserIdentityName, v.User.Phone, kind, v.NursingHomeUserWithDay.Date, v.NursingHomeUserRefundDay.Amount, state, v.NursingHomeUserRefundDay.RefundDate})
  141. j++
  142. }
  143. xlsx.SaveAs("./static" + fileName + ".xlsx")
  144. break
  145. case 3:
  146. //预定统计表
  147. fileName = "/export/nursing_home/预定统计表_" + utils.Int64ToStr(now.UnixMilli()) + ".xlsx"
  148. var m []*db.NursingHomePackageOrdForReserveWithUserIdentity
  149. sess := db.Db.Desc("nursing_home_package_ord_for_reserve.id")
  150. if req.EnterpriseId != 0 {
  151. sess.And("enterprise.enterprise_id =?", req.EnterpriseId)
  152. }
  153. if req.StartDate != "" {
  154. sess.And("nursing_home_package_ord_for_reserve.meal_time_start >= ?", req.StartDate)
  155. }
  156. if req.EndDate != "" {
  157. sess.And("nursing_home_package_ord_for_reserve.meal_time_start <= ?", req.EndDate)
  158. }
  159. _, err := sess.
  160. Join("LEFT", "nursing_home_package_ord", "nursing_home_package_ord_for_reserve.out_trade_no = nursing_home_package_ord.out_trade_no").
  161. Join("LEFT", "user_identity", "nursing_home_package_ord.user_identity_id = user_identity.id").
  162. Join("LEFT", "enterprise", "enterprise.id = user_identity.enterprise_id").
  163. Join("LEFT", "user", "user.id = user_identity.uid").FindAndCount(&m)
  164. if err != nil {
  165. logx.Error(err)
  166. println("<<<<NursingHomeDataStatisticsExport2>>>>>Error:::", err.Error())
  167. return
  168. }
  169. titleList = []string{"所属订单号", "单位名称", "用户身份名称", "用户手机号", "预定类型", "预定月份", "就餐时间-开始", "就餐时间-截止"}
  170. j := 2 //表头被第一行用了,只能从第二行开始
  171. for _, v := range m {
  172. var kind string //预定类型(1:早餐 2:午餐 3:晚餐)
  173. switch v.NursingHomePackageOrdForReserve.Kind {
  174. case enum.NursingHomeUserWithDayKindForBreakfast:
  175. kind = enum.NursingHomeUserWithDayKind.String(enum.NursingHomeUserWithDayKindForBreakfast)
  176. break
  177. case enum.NursingHomeUserWithDayKindForLunch:
  178. kind = enum.NursingHomeUserWithDayKind.String(enum.NursingHomeUserWithDayKindForLunch)
  179. break
  180. case enum.NursingHomeUserWithDayKindForDinner:
  181. kind = enum.NursingHomeUserWithDayKind.String(enum.NursingHomeUserWithDayKindForDinner)
  182. break
  183. }
  184. xlsx.SetSheetRow("Sheet1", "A"+strconv.Itoa(j), &[]interface{}{v.NursingHomePackageOrdForReserve.OutTradeNo, v.Enterprise.Name, v.NursingHomePackageOrd.UserIdentityName,
  185. v.User.Phone, kind, v.NursingHomePackageOrdForReserve.ReserveMonth, v.NursingHomePackageOrdForReserve.MealTimeStart, v.NursingHomePackageOrdForReserve.MealTimeEnd})
  186. j++
  187. }
  188. xlsx.SaveAs("./static" + fileName + ".xlsx")
  189. break
  190. }
  191. //新增数据
  192. nursingHomeExportRecordsDb := db.NursingHomeExportRecordsDb{}
  193. nursingHomeExportRecordsDb.Set()
  194. marshal, _ := json.Marshal(req)
  195. nursingHomeExportRecordsDb.NursingHomeExportRecordsInsert(&model.NursingHomeExportRecords{
  196. Name: fileName,
  197. DownloadPath: fileName,
  198. Kind: req.Kind,
  199. ReqContent: string(marshal),
  200. CreateAt: now.Format("2006-01-02 15:04:05"),
  201. UpdateAt: now.Format("2006-01-02 15:04:05"),
  202. })
  203. }
  204. func SelfSupportForSchoolDataStatisticsExport(req md.SelfSupportForSchoolDataStatisticsExportReq) {
  205. var titleList []string
  206. var fileName string
  207. var now = time.Now()
  208. xlsx := excelize.NewFile()
  209. xlsx.SetSheetRow("Sheet1", "A1", &titleList)
  210. switch req.Kind {
  211. case 1:
  212. //学生列表
  213. fileName = "/export/self_support_for_school/学生列表_" + utils.Int64ToStr(now.UnixMilli()) + ".xlsx"
  214. //查找订单数据
  215. sess := db.Db.Desc("user_identity.id")
  216. sess.And("identity =?", enum.UserIdentityForSelfSupportForStudent)
  217. if req.EnterpriseId != 0 {
  218. sess.And("user_identity.enterprise_id =?", req.EnterpriseId)
  219. }
  220. if req.StartDate != "" {
  221. sess.And("user_identity.create_at >= ?", req.StartDate)
  222. }
  223. if req.EndDate != "" {
  224. sess.And("user_identity.create_at <= ?", req.EndDate)
  225. }
  226. var m []*db.UserIdentityWithUser
  227. _, err := sess.
  228. Join("LEFT", "user", "user_identity.uid = user.id").
  229. Join("LEFT", "enterprise", "user_identity.enterprise_id = enterprise.id").
  230. Join("LEFT", "class_with_user", "class_with_user.user_identity_id = user_identity.id").
  231. Join("LEFT", "class", "class_with_user.class_id = class.id").
  232. Join("LEFT", "grade", "class.grade_id = grade.id").
  233. Join("LEFT", "self_support_for_user_face_info", "user_identity.id = self_support_for_user_face_info.user_identity_id").
  234. FindAndCount(&m)
  235. if err != nil {
  236. logx.Error(err)
  237. println("<<<<SelfSupportForSchoolDataStatisticsExport1>>>>>Error:::", err.Error())
  238. return
  239. }
  240. titleList = []string{"单位名称", "姓名", "身份证号", "家长电话", "采脸方式", "支付状态", "刷脸状态", "年级", "班级", "注册时间"}
  241. j := 2 //表头被第一行用了,只能从第二行开始
  242. for _, v := range m {
  243. var kind string //采集人脸方式(1:个采 2:集采)
  244. switch v.SelfSupportForUserFaceInfo.CollectFaceType {
  245. case 1:
  246. kind = "个采"
  247. break
  248. case 2:
  249. kind = "集采"
  250. break
  251. }
  252. xlsx.SetSheetRow("Sheet1", "A"+strconv.Itoa(j), &[]interface{}{v.Enterprise.Name, v.UserIdentity.Name, v.UserIdentity.IdNo, v.User.Phone, kind,
  253. v.SelfSupportForUserFaceInfo.SchoolFacePaymentStatus, v.SelfSupportForUserFaceInfo.SchoolFacePassStatus, v.Grade.Name, v.Class.Name, v.UserIdentity.CreateAt})
  254. j++
  255. }
  256. xlsx.SaveAs("./static" + fileName)
  257. break
  258. case 2:
  259. //教师列表
  260. fileName = "/export/self_support_for_school/教师列表_" + utils.Int64ToStr(now.UnixMilli()) + ".xlsx"
  261. //查找订单数据
  262. sess := db.Db.Desc("user_identity.id")
  263. sess.And("identity =?", enum.UserIdentityForSelfSupportForTeacher)
  264. if req.EnterpriseId != 0 {
  265. sess.And("user_identity.enterprise_id =?", req.EnterpriseId)
  266. }
  267. if req.StartDate != "" {
  268. sess.And("user_identity.create_at >= ?", req.StartDate)
  269. }
  270. if req.EndDate != "" {
  271. sess.And("user_identity.create_at <= ?", req.EndDate)
  272. }
  273. var m []*db.UserIdentityWithUser
  274. _, err := sess.
  275. Join("LEFT", "user", "user_identity.uid = user.id").
  276. Join("LEFT", "enterprise", "user_identity.enterprise_id = enterprise.id").
  277. Join("LEFT", "self_support_for_user_face_info", "user_identity.id = self_support_for_user_face_info.user_identity_id").
  278. FindAndCount(&m)
  279. if err != nil {
  280. logx.Error(err)
  281. println("<<<<SelfSupportForSchoolDataStatisticsExport2>>>>>Error:::", err.Error())
  282. return
  283. }
  284. titleList = []string{"单位名称", "姓名", "身份证号", "电话", "采脸方式", "支付状态", "刷脸状态", "注册时间"}
  285. j := 2 //表头被第一行用了,只能从第二行开始
  286. for _, v := range m {
  287. var kind string //采集人脸方式(1:个采 2:集采)
  288. switch v.SelfSupportForUserFaceInfo.CollectFaceType {
  289. case 1:
  290. kind = "个采"
  291. break
  292. case 2:
  293. kind = "集采"
  294. break
  295. }
  296. xlsx.SetSheetRow("Sheet1", "A"+strconv.Itoa(j), &[]interface{}{v.Enterprise.Name, v.UserIdentity.Name, v.UserIdentity.IdNo, v.User.Phone, kind,
  297. v.SelfSupportForUserFaceInfo.SchoolFacePaymentStatus, v.SelfSupportForUserFaceInfo.SchoolFacePassStatus, v.UserIdentity.CreateAt})
  298. j++
  299. }
  300. xlsx.SaveAs("./static" + fileName)
  301. break
  302. case 3:
  303. //消费明细表
  304. fileName = "/export/self_support_for_school/消费明细表_" + utils.Int64ToStr(now.UnixMilli()) + ".xlsx"
  305. var m []*db.SelfSupportForSchoolOrdWithUserIdentity
  306. sess := db.Db.Desc("self_support_for_school_ord.id")
  307. if req.EnterpriseId != 0 {
  308. sess.And("self_support_for_school_ord.enterprise_id =?", req.EnterpriseId)
  309. }
  310. if req.StartDate != "" {
  311. sess.And("self_support_for_school_ord.face_time >= ?", req.StartDate)
  312. }
  313. if req.EndDate != "" {
  314. sess.And("self_support_for_school_ord.face_time <= ?", req.EndDate)
  315. }
  316. _, err := sess.
  317. Join("LEFT", "user_identity", "self_support_for_school_ord.user_identity_id = user_identity.id").
  318. Join("LEFT", "enterprise", "enterprise.id = user_identity.enterprise_id").
  319. Join("LEFT", "user", "user.id = user_identity.uid").
  320. Join("LEFT", "class_with_user", "class_with_user.user_identity_id = user_identity.id").
  321. Join("LEFT", "class", "class_with_user.class_id = class.id").
  322. Join("LEFT", "grade", "class.grade_id = grade.id").
  323. FindAndCount(&m)
  324. if err != nil {
  325. logx.Error(err)
  326. println("<<<<SelfSupportForSchoolDataStatisticsExport3>>>>>Error:::", err.Error())
  327. return
  328. }
  329. titleList = []string{"订单编号", "支付交易号", "用户手机号", "就餐人姓名", "学校名称", "年级", "班级", "金额", "支付状态", "就餐时间", "设备编号", "身份"}
  330. j := 2 //表头被第一行用了,只能从第二行开始
  331. for _, v := range m {
  332. var state string //支付状态(0:待支付 1:支付成功)
  333. switch v.SelfSupportForSchoolOrd.OrderStatus {
  334. case 0:
  335. state = "待支付"
  336. break
  337. case 1:
  338. state = "支付成功"
  339. break
  340. }
  341. var identity string
  342. switch enum.UserIdentityForSelfSupportForTeacher {
  343. case enum.UserIdentityForSelfSupportForTeacher:
  344. identity = enum.UserIdentity.String(enum.UserIdentityForSelfSupportForTeacher)
  345. break
  346. case enum.UserIdentityForSelfSupportForStudent:
  347. identity = enum.UserIdentity.String(enum.UserIdentityForSelfSupportForStudent)
  348. break
  349. default:
  350. identity = "未知"
  351. }
  352. xlsx.SetSheetRow("Sheet1", "A"+strconv.Itoa(j), &[]interface{}{v.SelfSupportForSchoolOrd.OutOrderNo, v.SelfSupportForSchoolOrd.TradeNo, v.UserIdentity.Name, v.Enterprise.Name,
  353. v.Grade.Name, v.Class.Name, v.SelfSupportForSchoolOrd.TradeAmount, state, v.SelfSupportForSchoolOrd.FaceTime, v.SelfSupportForSchoolOrd.DeviceSn, identity})
  354. j++
  355. }
  356. xlsx.SaveAs("./static" + fileName + ".xlsx")
  357. break
  358. }
  359. //新增数据
  360. selfSupportForSchoolExportRecordsDb := db.SelfSupportForSchoolExportRecordsDb{}
  361. selfSupportForSchoolExportRecordsDb.Set()
  362. marshal, _ := json.Marshal(req)
  363. selfSupportForSchoolExportRecordsDb.SelfSupportForSchoolExportRecordsInsert(&model.SelfSupportForSchoolExportRecords{
  364. Name: fileName,
  365. DownloadPath: fileName,
  366. Kind: req.Kind,
  367. ReqContent: string(marshal),
  368. CreateAt: now.Format("2006-01-02 15:04:05"),
  369. UpdateAt: now.Format("2006-01-02 15:04:05"),
  370. })
  371. }