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

1540 lines
65 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. "archive/zip"
  10. "encoding/json"
  11. "fmt"
  12. "github.com/360EntSecGroup-Skylar/excelize"
  13. "github.com/syyongx/php2go"
  14. "io"
  15. "os"
  16. "strconv"
  17. "time"
  18. )
  19. func NursingHomeDataStatisticsExport(req md.NursingHomeDataStatisticsExportReq) {
  20. var titleList []string
  21. var fileName string
  22. var now = time.Now()
  23. xlsx := excelize.NewFile()
  24. xlsx.SetSheetRow("Sheet1", "A1", &titleList)
  25. switch req.Kind {
  26. case 1:
  27. //订单列表
  28. fileName = "/export/nursing_home/订单列表_" + utils.Int64ToStr(now.UnixMilli()) + ".xlsx"
  29. //查找订单数据
  30. sess := db.Db.Desc("nursing_home_package_ord.id")
  31. if req.EnterpriseId != 0 {
  32. sess.And("nursing_home_package_ord.enterprise_id =?", req.EnterpriseId)
  33. }
  34. if req.StartDate != "" {
  35. sess.And("nursing_home_package_ord.create_at >= ?", req.StartDate)
  36. }
  37. if req.EndDate != "" {
  38. sess.And("nursing_home_package_ord.create_at <= ?", req.EndDate)
  39. }
  40. var m []*db.NursingHomePackageOrdWithUserIdentity
  41. _, err := sess.
  42. Join("LEFT", "user_identity", "nursing_home_package_ord.user_identity_id = user_identity.id").
  43. Join("LEFT", "enterprise", "enterprise.id = nursing_home_package_ord.enterprise_id").
  44. Join("LEFT", "user", "user.id = nursing_home_package_ord.uid").
  45. FindAndCount(&m)
  46. if err != nil {
  47. logx.Error(err)
  48. println("<<<<NursingHomeDataStatisticsExport1>>>>>Error:::", err.Error())
  49. return
  50. }
  51. titleList = []string{"单位名称", "用户身份名称", "用户手机号", "购买类型", "总价", "商户订单号", "支付状态", "购买时间"}
  52. xlsx.SetSheetRow("Sheet1", "A1", &titleList)
  53. j := 2 //表头被第一行用了,只能从第二行开始
  54. for _, v := range m {
  55. var kind string //1:按年购买 2:按月购买 3:按天购买 4:补餐
  56. switch v.NursingHomePackageOrd.Kind {
  57. case 1:
  58. kind = "按年购买"
  59. break
  60. case 2:
  61. kind = "按月购买"
  62. break
  63. case 3:
  64. kind = "按天购买"
  65. break
  66. case 4:
  67. kind = "补餐"
  68. break
  69. }
  70. var state string //0:待支付 1:支付成功 2:支付失败
  71. switch v.NursingHomePackageOrd.State {
  72. case enum.NursingHomePackageOrdStateForWait:
  73. state = enum.NursingHomePackageOrdState.String(enum.NursingHomePackageOrdStateForWait)
  74. break
  75. case enum.NursingHomePackageOrdStateForSuccess:
  76. state = enum.NursingHomePackageOrdState.String(enum.NursingHomePackageOrdStateForWait)
  77. break
  78. case enum.NursingHomePackageOrdStateForFail:
  79. state = enum.NursingHomePackageOrdState.String(enum.NursingHomePackageOrdStateForFail)
  80. break
  81. }
  82. xlsx.SetSheetRow("Sheet1", "A"+strconv.Itoa(j), &[]interface{}{v.Enterprise.Name, v.NursingHomePackageOrd.UserIdentityName, v.User.Phone, kind, v.NursingHomePackageOrd.TotalPrice,
  83. v.NursingHomePackageOrd.OutTradeNo, state, v.NursingHomePackageOrd.CreateAt})
  84. j++
  85. }
  86. break
  87. case 2:
  88. //退款申请表
  89. fileName = "/export/nursing_home/退款申请表_" + utils.Int64ToStr(now.UnixMilli()) + ".xlsx"
  90. var m []*db.NursingHomeUserRefundDayWithData
  91. sess := db.Db.Desc("nursing_home_user_refund_day.id")
  92. if req.EnterpriseId != 0 {
  93. sess.And("enterprise.enterprise_id =?", req.EnterpriseId)
  94. }
  95. if req.StartDate != "" {
  96. sess.And("nursing_home_user_refund_day.create_at >= ?", req.StartDate)
  97. }
  98. if req.EndDate != "" {
  99. sess.And("nursing_home_user_refund_day.create_at <= ?", req.EndDate)
  100. }
  101. _, err := sess.
  102. Join("LEFT", "nursing_home_user_with_day", "nursing_home_user_refund_day.records_id = nursing_home_user_with_day.id").
  103. Join("LEFT", "nursing_home_package_ord", "nursing_home_user_refund_day.out_trade_no = nursing_home_package_ord.out_trade_no").
  104. Join("LEFT", "user_identity", "nursing_home_user_with_day.identity_id = user_identity.id").
  105. Join("LEFT", "user", "user_identity.uid = user.id").
  106. Join("LEFT", "enterprise", "user_identity.enterprise_id = enterprise.id").
  107. Desc("nursing_home_user_refund_day.id").
  108. FindAndCount(&m)
  109. if err != nil {
  110. logx.Error(err)
  111. println("<<<<NursingHomeDataStatisticsExport2>>>>>Error:::", err.Error())
  112. return
  113. }
  114. titleList = []string{"退款单号", "所属订单号", "单位名称", "用户身份名称", "用户手机号", "就餐类型", "就餐时间", "金额", "状态", "退款时间"}
  115. xlsx.SetSheetRow("Sheet1", "A1", &titleList)
  116. j := 2 //表头被第一行用了,只能从第二行开始
  117. for _, v := range m {
  118. var kind string //就餐类型(1:早餐 2:午餐 3:晚餐)
  119. switch v.NursingHomeUserWithDay.Kind {
  120. case enum.NursingHomeUserWithDayKindForBreakfast:
  121. kind = enum.NursingHomeUserWithDayKind.String(enum.NursingHomeUserWithDayKindForBreakfast)
  122. break
  123. case enum.NursingHomeUserWithDayKindForLunch:
  124. kind = enum.NursingHomeUserWithDayKind.String(enum.NursingHomeUserWithDayKindForLunch)
  125. break
  126. case enum.NursingHomeUserWithDayKindForDinner:
  127. kind = enum.NursingHomeUserWithDayKind.String(enum.NursingHomeUserWithDayKindForDinner)
  128. break
  129. }
  130. var state string //状态(1:审核中 2:审核通过 3:审核拒绝 4:退款已完成)
  131. switch v.NursingHomeUserRefundDay.State {
  132. case enum.NursingHomeUserRefundDayStateForAuditing:
  133. state = enum.NursingHomeUserRefundDayState.String(enum.NursingHomeUserRefundDayStateForAuditing)
  134. break
  135. case enum.NursingHomeUserRefundDayStateForAuditPass:
  136. state = enum.NursingHomeUserRefundDayState.String(enum.NursingHomeUserRefundDayStateForAuditPass)
  137. break
  138. case enum.NursingHomeUserRefundDayStateForAuditReject:
  139. state = enum.NursingHomeUserRefundDayState.String(enum.NursingHomeUserRefundDayStateForAuditReject)
  140. break
  141. case enum.NursingHomeUserRefundDayStateForAuditComplete:
  142. state = enum.NursingHomeUserRefundDayState.String(enum.NursingHomeUserRefundDayStateForAuditComplete)
  143. break
  144. }
  145. xlsx.SetSheetRow("Sheet1", "A"+strconv.Itoa(j), &[]interface{}{v.NursingHomeUserRefundDay.OutRequestNo, v.NursingHomeUserRefundDay.OutTradeNo, v.Enterprise.Name,
  146. v.NursingHomePackageOrd.UserIdentityName, v.User.Phone, kind, v.NursingHomeUserWithDay.Date, v.NursingHomeUserRefundDay.Amount, state, v.NursingHomeUserRefundDay.RefundDate})
  147. j++
  148. }
  149. xlsx.SaveAs("./static" + fileName + ".xlsx")
  150. break
  151. case 3:
  152. //预定统计表
  153. fileName = "/export/nursing_home/预定统计表_" + utils.Int64ToStr(now.UnixMilli()) + ".xlsx"
  154. var m []*db.NursingHomePackageOrdForReserveWithUserIdentity
  155. sess := db.Db.Desc("nursing_home_package_ord_for_reserve.id")
  156. if req.EnterpriseId != 0 {
  157. sess.And("enterprise.enterprise_id =?", req.EnterpriseId)
  158. }
  159. if req.StartDate != "" {
  160. sess.And("nursing_home_package_ord_for_reserve.meal_time_start >= ?", req.StartDate)
  161. }
  162. if req.EndDate != "" {
  163. sess.And("nursing_home_package_ord_for_reserve.meal_time_start <= ?", req.EndDate)
  164. }
  165. _, err := sess.
  166. Join("LEFT", "nursing_home_package_ord", "nursing_home_package_ord_for_reserve.out_trade_no = nursing_home_package_ord.out_trade_no").
  167. Join("LEFT", "user_identity", "nursing_home_package_ord.user_identity_id = user_identity.id").
  168. Join("LEFT", "enterprise", "enterprise.id = user_identity.enterprise_id").
  169. Join("LEFT", "user", "user.id = user_identity.uid").FindAndCount(&m)
  170. if err != nil {
  171. logx.Error(err)
  172. println("<<<<NursingHomeDataStatisticsExport2>>>>>Error:::", err.Error())
  173. return
  174. }
  175. titleList = []string{"所属订单号", "单位名称", "用户身份名称", "用户手机号", "预定类型", "预定月份", "就餐时间-开始", "就餐时间-截止"}
  176. xlsx.SetSheetRow("Sheet1", "A1", &titleList)
  177. j := 2 //表头被第一行用了,只能从第二行开始
  178. for _, v := range m {
  179. var kind string //预定类型(1:早餐 2:午餐 3:晚餐)
  180. switch v.NursingHomePackageOrdForReserve.Kind {
  181. case enum.NursingHomeUserWithDayKindForBreakfast:
  182. kind = enum.NursingHomeUserWithDayKind.String(enum.NursingHomeUserWithDayKindForBreakfast)
  183. break
  184. case enum.NursingHomeUserWithDayKindForLunch:
  185. kind = enum.NursingHomeUserWithDayKind.String(enum.NursingHomeUserWithDayKindForLunch)
  186. break
  187. case enum.NursingHomeUserWithDayKindForDinner:
  188. kind = enum.NursingHomeUserWithDayKind.String(enum.NursingHomeUserWithDayKindForDinner)
  189. break
  190. }
  191. xlsx.SetSheetRow("Sheet1", "A"+strconv.Itoa(j), &[]interface{}{v.NursingHomePackageOrdForReserve.OutTradeNo, v.Enterprise.Name, v.NursingHomePackageOrd.UserIdentityName,
  192. v.User.Phone, kind, v.NursingHomePackageOrdForReserve.ReserveMonth, v.NursingHomePackageOrdForReserve.MealTimeStart, v.NursingHomePackageOrdForReserve.MealTimeEnd})
  193. j++
  194. }
  195. xlsx.SaveAs("./static" + fileName + ".xlsx")
  196. break
  197. }
  198. //新增数据
  199. nursingHomeExportRecordsDb := db.NursingHomeExportRecordsDb{}
  200. nursingHomeExportRecordsDb.Set()
  201. marshal, _ := json.Marshal(req)
  202. nursingHomeExportRecordsDb.NursingHomeExportRecordsInsert(&model.NursingHomeExportRecords{
  203. Name: fileName,
  204. DownloadPath: fileName,
  205. Kind: req.Kind,
  206. ReqContent: string(marshal),
  207. CreateAt: now.Format("2006-01-02 15:04:05"),
  208. UpdateAt: now.Format("2006-01-02 15:04:05"),
  209. })
  210. }
  211. func SelfSupportForSchoolDataStatisticsExport(req md.SelfSupportForSchoolDataStatisticsExportReq) {
  212. var titleList []string
  213. var fileName, downloadPath string
  214. var now = time.Now()
  215. switch req.Kind {
  216. case 1:
  217. //教师消费统计表
  218. xlsx, _ := excelize.OpenFile("./static/template/自营学校(教师消费统计表).xlsx")
  219. fileName = "教师消费统计表_" + utils.Int64ToStr(now.UnixMilli()) + ".xlsx"
  220. downloadPath = "/export/self_support_for_school/" + fileName
  221. //查询数据
  222. var m []*db.SelfSupportForSchoolOrdWithUserIdentity
  223. sess := db.Db.Desc("self_support_for_school_ord.id")
  224. if req.EnterpriseId != 0 {
  225. sess.And("self_support_for_school_ord.enterprise_id =?", req.EnterpriseId)
  226. }
  227. if req.StartDate != "" {
  228. sess.And("self_support_for_school_ord.face_time >= ?", req.StartDate)
  229. }
  230. if req.EndDate != "" {
  231. sess.And("self_support_for_school_ord.face_time <= ?", req.EndDate)
  232. }
  233. sess.And("user_identity.identity = ?", enum.UserIdentityForSelfSupportForTeacher) //只查询老师
  234. _, err := sess.
  235. Join("LEFT", "user_identity", "self_support_for_school_ord.user_identity_id = user_identity.id").
  236. Join("LEFT", "enterprise", "enterprise.id = user_identity.enterprise_id").
  237. Join("LEFT", "user", "user.id = user_identity.uid").
  238. Join("LEFT", "class_with_user", "class_with_user.user_identity_id = user_identity.id").
  239. Join("LEFT", "class", "class_with_user.class_id = class.id").
  240. Join("LEFT", "grade", "class.grade_id = grade.id").
  241. FindAndCount(&m)
  242. if err != nil {
  243. logx.Error(err)
  244. println("<<<<SelfSupportForSchoolDataStatisticsExport2>>>>>Error:::", err.Error())
  245. return
  246. }
  247. //整合数据
  248. var data = map[string]map[string]map[string]map[string]interface{}{}
  249. for _, vv := range m {
  250. if data[vv.Enterprise.Name] == nil {
  251. data[vv.Enterprise.Name] = make(map[string]map[string]map[string]interface{})
  252. }
  253. if data[vv.Enterprise.Name][vv.UserIdentity.Name] == nil {
  254. data[vv.Enterprise.Name][vv.UserIdentity.Name] = make(map[string]map[string]interface{})
  255. }
  256. mealZh := JudgeSelfSupportForMealTime(vv.SelfSupportForSchoolOrd.FaceTime)
  257. if data[vv.Enterprise.Name][vv.UserIdentity.Name][mealZh] == nil {
  258. data[vv.Enterprise.Name][vv.UserIdentity.Name][mealZh] = make(map[string]interface{})
  259. }
  260. data[vv.Enterprise.Name][vv.UserIdentity.Name]["编号"] = map[string]interface{}{
  261. "id": vv.UserIdentity.IdNo,
  262. }
  263. if data[vv.Enterprise.Name][vv.UserIdentity.Name]["合计"] == nil {
  264. data[vv.Enterprise.Name][vv.UserIdentity.Name]["合计"] = make(map[string]interface{})
  265. }
  266. data[vv.Enterprise.Name][vv.UserIdentity.Name][mealZh]["num"] = utils.AnyToInt64(data[vv.Enterprise.Name][vv.UserIdentity.Name][mealZh]["num"]) + 1
  267. data[vv.Enterprise.Name][vv.UserIdentity.Name][mealZh]["amount"] = utils.AnyToFloat64(data[vv.Enterprise.Name][vv.UserIdentity.Name][mealZh]["amount"]) + utils.AnyToFloat64(vv.SelfSupportForSchoolOrd.TradeAmount)
  268. data[vv.Enterprise.Name][vv.UserIdentity.Name]["合计"]["num"] = utils.AnyToInt64(data[vv.Enterprise.Name][vv.UserIdentity.Name]["合计"]["num"]) + 1
  269. data[vv.Enterprise.Name][vv.UserIdentity.Name]["合计"]["amount"] = utils.AnyToFloat64(data[vv.Enterprise.Name][vv.UserIdentity.Name]["合计"]["amount"]) + utils.AnyToFloat64(vv.SelfSupportForSchoolOrd.TradeAmount)
  270. }
  271. xlsx.SetSheetRow("", "A1", &titleList)
  272. sheetIndex := 2 //第一个表是模板表
  273. j := 3 //表头前两行被占用了,只能从第三行开始
  274. for k, v := range data {
  275. xlsx.NewSheet(k) //1、新建表格
  276. xlsx.CopySheet(1, sheetIndex)
  277. for kk, vv := range v {
  278. xlsx.SetSheetRow(k, "A"+strconv.Itoa(j), &[]interface{}{vv["编号"]["id"], kk,
  279. vv["早餐"]["num"], vv["早餐"]["amount"],
  280. vv["午餐"]["num"], vv["午餐"]["amount"],
  281. vv["晚餐"]["num"], vv["晚餐"]["amount"],
  282. vv["夜宵"]["num"], vv["夜宵"]["amount"],
  283. vv["未知"]["num"], vv["未知"]["amount"],
  284. vv["合计"]["num"], vv["合计"]["amount"],
  285. })
  286. j++
  287. }
  288. sheetIndex++
  289. }
  290. xlsx.DeleteSheet("template") //删除模板表
  291. //将文件保存至服务器
  292. xlsx.SaveAs("./static" + downloadPath)
  293. break
  294. case 2:
  295. //商家分餐数据统计表
  296. xlsx, _ := excelize.OpenFile("./static/template/自营学校(商家分餐数据统计表).xlsx")
  297. fileName = "商家分餐数据统计表_" + utils.Int64ToStr(now.UnixMilli()) + ".xlsx"
  298. downloadPath = "/export/self_support_for_school/" + fileName
  299. //查询出所有的设备
  300. var deviceList []*model.Device
  301. err := db.Db.Where("enterprise_id != 0").Find(&deviceList)
  302. if err != nil {
  303. logx.Error(err)
  304. println("<<<<SelfSupportForSchoolDataStatisticsExport2>>>>>1111Error:::", err.Error())
  305. return
  306. }
  307. var deviceListMapping = map[string]model.Device{}
  308. for _, v := range deviceList {
  309. deviceListMapping[v.DeviceSn] = *v
  310. }
  311. var m []*db.SelfSupportForSchoolOrdWithUserIdentity
  312. sess := db.Db.Desc("self_support_for_school_ord.id")
  313. if req.EnterpriseId != 0 {
  314. sess.And("self_support_for_school_ord.enterprise_id =?", req.EnterpriseId)
  315. }
  316. if req.StartDate != "" {
  317. sess.And("self_support_for_school_ord.face_time >= ?", req.StartDate)
  318. }
  319. if req.EndDate != "" {
  320. sess.And("self_support_for_school_ord.face_time <= ?", req.EndDate)
  321. }
  322. _, err = sess.
  323. Join("LEFT", "user_identity", "self_support_for_school_ord.user_identity_id = user_identity.id").
  324. Join("LEFT", "enterprise", "enterprise.id = user_identity.enterprise_id").
  325. Join("LEFT", "user", "user.id = user_identity.uid").
  326. Join("LEFT", "class_with_user", "class_with_user.user_identity_id = user_identity.id").
  327. Join("LEFT", "class", "class_with_user.class_id = class.id").
  328. Join("LEFT", "grade", "class.grade_id = grade.id").
  329. FindAndCount(&m)
  330. if err != nil {
  331. logx.Error(err)
  332. println("<<<<SelfSupportForSchoolDataStatisticsExport2>>>>>Error:::", err.Error())
  333. return
  334. }
  335. //整合数据
  336. var data = map[string]map[string]map[string]map[string]float64{}
  337. for _, vv := range m {
  338. shopName := deviceListMapping[vv.SelfSupportForSchoolOrd.DeviceSn].Name
  339. if data[vv.Enterprise.Name] == nil {
  340. data[vv.Enterprise.Name] = make(map[string]map[string]map[string]float64)
  341. }
  342. if data[vv.Enterprise.Name][shopName] == nil {
  343. data[vv.Enterprise.Name][shopName] = make(map[string]map[string]float64)
  344. }
  345. mealZh := JudgeSelfSupportForMealTime(vv.SelfSupportForSchoolOrd.FaceTime)
  346. if data[vv.Enterprise.Name][shopName][mealZh] == nil {
  347. data[vv.Enterprise.Name][shopName][mealZh] = make(map[string]float64)
  348. }
  349. if data[vv.Enterprise.Name][shopName]["合计"] == nil {
  350. data[vv.Enterprise.Name][shopName]["合计"] = make(map[string]float64)
  351. }
  352. data[vv.Enterprise.Name][shopName][mealZh]["num"]++
  353. data[vv.Enterprise.Name][shopName][mealZh]["amount"] += utils.StrToFloat64(vv.SelfSupportForSchoolOrd.TradeAmount)
  354. data[vv.Enterprise.Name][shopName]["合计"]["num"]++
  355. data[vv.Enterprise.Name][shopName]["合计"]["amount"] += utils.StrToFloat64(vv.SelfSupportForSchoolOrd.TradeAmount)
  356. }
  357. xlsx.SetSheetRow("", "A1", &titleList)
  358. sheetIndex := 2 //第一个表是模板表
  359. for k, v := range data {
  360. if k == "" {
  361. k = "未知"
  362. }
  363. xlsx.NewSheet(k) //1、新建表格
  364. xlsx.CopySheet(1, sheetIndex)
  365. j := 4 //表头前三行被占用了,只能从第四行开始
  366. for kk, vv := range v {
  367. xlsx.SetSheetRow(k, "A"+strconv.Itoa(j), &[]interface{}{kk,
  368. vv["早餐"]["num"], vv["早餐"]["amount"],
  369. vv["午餐"]["num"], vv["午餐"]["amount"],
  370. vv["晚餐"]["num"], vv["晚餐"]["amount"],
  371. vv["夜宵"]["num"], vv["夜宵"]["amount"],
  372. vv["合计"]["num"], vv["合计"]["amount"],
  373. })
  374. j++
  375. }
  376. sheetIndex++
  377. }
  378. xlsx.DeleteSheet("template") //删除模板表
  379. //将文件保存至服务器
  380. xlsx.SaveAs("./static" + downloadPath)
  381. break
  382. }
  383. //新增数据
  384. selfSupportForSchoolExportRecordsDb := db.SelfSupportForSchoolExportRecordsDb{}
  385. selfSupportForSchoolExportRecordsDb.Set()
  386. marshal, _ := json.Marshal(req)
  387. selfSupportForSchoolExportRecordsDb.SelfSupportForSchoolExportRecordsInsert(&model.SelfSupportForSchoolExportRecords{
  388. Name: fileName,
  389. DownloadPath: downloadPath,
  390. Kind: req.Kind,
  391. ReqContent: string(marshal),
  392. CreateAt: now.Format("2006-01-02 15:04:05"),
  393. UpdateAt: now.Format("2006-01-02 15:04:05"),
  394. })
  395. }
  396. func CentralKitchenForSchoolDataStatisticsExport(req md.CentralKitchenForSchoolDataStatisticsExportReq) {
  397. var titleList []string
  398. var fileName, downloadPath string
  399. var now = time.Now()
  400. for _, kind := range req.Kinds {
  401. xlsx := excelize.NewFile()
  402. switch kind {
  403. case 1:
  404. //订单列表
  405. fileName = "订单列表_" + utils.Int64ToStr(now.UnixMilli()) + ".xlsx"
  406. downloadPath = "/export/central_kitchen_for_school/" + fileName
  407. //查找订单数据
  408. sess := db.Db.Desc("central_kitchen_for_school_package_ord.id")
  409. sess.And("central_kitchen_for_school_package_ord.state =?", enum.CentralKitchenForSchoolPackageOrdStateForSuccess)
  410. if req.EnterpriseId != 0 {
  411. sess.And("central_kitchen_for_school_package_ord.enterprise_id =?", req.EnterpriseId)
  412. }
  413. if req.StartDate != "" {
  414. sess.And("central_kitchen_for_school_package_ord.create_at >= ?", req.StartDate)
  415. }
  416. if req.EndDate != "" {
  417. sess.And("central_kitchen_for_school_package_ord.create_at <= ?", req.EndDate)
  418. }
  419. var m []*db.CentralKitchenForSchoolPackageOrdWithUserIdentity
  420. _, err := sess.
  421. Join("LEFT", "user_identity", "central_kitchen_for_school_package_ord.user_identity_id = user_identity.id").
  422. Join("LEFT", "enterprise", "enterprise.id = user_identity.enterprise_id").
  423. Join("LEFT", "user", "user.id = user_identity.uid").
  424. Join("LEFT", "class_with_user", "class_with_user.user_identity_id = user_identity.id").
  425. Join("LEFT", "class", "class_with_user.class_id = class.id").
  426. Join("LEFT", "grade", "class.grade_id = grade.id").
  427. FindAndCount(&m)
  428. if err != nil {
  429. logx.Error(err)
  430. println("<<<<CentralKitchenForSchoolDataStatisticsExport1>>>>>Error:::", err.Error())
  431. return
  432. }
  433. //查找预定数据
  434. var outTradeNoList []string
  435. for _, v := range m {
  436. outTradeNoList = append(outTradeNoList, v.OutTradeNo)
  437. }
  438. var mm []model.CentralKitchenForSchoolPackageOrdForReserve
  439. sess1 := db.Db.Desc("id")
  440. if req.EnterpriseId != 0 {
  441. sess1.And("enterprise_id = ?", req.EnterpriseId)
  442. }
  443. if len(req.MealKindList) > 0 {
  444. sess1.In("kind", req.MealKindList)
  445. }
  446. err = sess1.In("out_trade_no", outTradeNoList).Find(&mm)
  447. if err != nil {
  448. logx.Error(err)
  449. println("<<<<CentralKitchenForSchoolDataStatisticsExport1>>>>>Error:::", err.Error())
  450. return
  451. }
  452. var exportOutTradeNoList []string
  453. for _, v := range mm {
  454. exportOutTradeNoList = append(exportOutTradeNoList, v.OutTradeNo)
  455. }
  456. titleList = []string{"订单号", "学生姓名", "学校", "年级", "班级", "家长电话", "预定日期", "预定天数", "订单价格", "支付时间", "订单状态"}
  457. xlsx.SetSheetRow("Sheet1", "A1", &titleList)
  458. j := 2 //表头被第一行用了,只能从第二行开始
  459. for _, v := range m {
  460. if !utils.InArr(v.OutTradeNo, exportOutTradeNoList) {
  461. continue
  462. }
  463. var ordState string //订单状态(0:待支付 1:预约成功 2:退款中 3:部分退款 4:已退款 5:已完成)
  464. switch v.CentralKitchenForSchoolPackageOrd.OrdState {
  465. case enum.CentralKitchenForSchoolPackageOrdOrdStateForWait:
  466. ordState = enum.CentralKitchenForSchoolPackageOrdOrdState.String(enum.CentralKitchenForSchoolPackageOrdOrdStateForWait)
  467. break
  468. case enum.CentralKitchenForSchoolPackageOrdOrdStateForSuccess:
  469. ordState = enum.CentralKitchenForSchoolPackageOrdOrdState.String(enum.CentralKitchenForSchoolPackageOrdOrdStateForSuccess)
  470. break
  471. case enum.CentralKitchenForSchoolPackageOrdOrdStateForRefunding:
  472. ordState = enum.CentralKitchenForSchoolPackageOrdOrdState.String(enum.CentralKitchenForSchoolPackageOrdOrdStateForRefunding)
  473. break
  474. case enum.CentralKitchenForSchoolPackageOrdOrdStateForPartRefunded:
  475. ordState = enum.CentralKitchenForSchoolPackageOrdOrdState.String(enum.CentralKitchenForSchoolPackageOrdOrdStateForPartRefunded)
  476. break
  477. case enum.CentralKitchenForSchoolPackageOrdOrdStateForRefunded:
  478. ordState = enum.CentralKitchenForSchoolPackageOrdOrdState.String(enum.CentralKitchenForSchoolPackageOrdOrdStateForRefunded)
  479. break
  480. case enum.CentralKitchenForSchoolPackageOrdOrdStateForComplete:
  481. ordState = enum.CentralKitchenForSchoolPackageOrdOrdState.String(enum.CentralKitchenForSchoolPackageOrdOrdStateForComplete)
  482. break
  483. }
  484. //预定天数、预定日期
  485. var list []*model.CentralKitchenForSchoolUserWithDay
  486. err1 := json.Unmarshal([]byte(v.CentralKitchenForSchoolPackageOrd.WithDayData), &list)
  487. if err1 != nil {
  488. logx.Error(err)
  489. println("<<<<CentralKitchenForSchoolDataStatisticsExport1>>>>>Error:::", err.Error())
  490. return
  491. }
  492. var reserveDays string
  493. for _, v1 := range list {
  494. reserveDays += v1.Date
  495. }
  496. if v.Grade.Name == "" {
  497. v.Grade.Name = "教师"
  498. }
  499. if v.Class.Name == "" {
  500. v.Class.Name = "教师"
  501. }
  502. xlsx.SetSheetRow("Sheet1", "A"+strconv.Itoa(j), &[]interface{}{v.CentralKitchenForSchoolPackageOrd.OutTradeNo, v.CentralKitchenForSchoolPackageOrd.UserIdentityName, v.Enterprise.Name,
  503. v.Grade.Name, v.Class.Name, v.User.Phone, reserveDays, len(list), v.CentralKitchenForSchoolPackageOrd.TotalPrice, v.CentralKitchenForSchoolPackageOrd.CreateAt, ordState})
  504. j++
  505. }
  506. //将文件保存至服务器
  507. xlsx.SaveAs("./static" + downloadPath)
  508. break
  509. case 2:
  510. //退款申请表
  511. fileName = "退款申请表_" + utils.Int64ToStr(now.UnixMilli()) + ".xlsx"
  512. downloadPath = "/export/central_kitchen_for_school/" + fileName
  513. //查找退款申请数据
  514. sess := db.Db.Desc("central_kitchen_for_school_user_refund_day.id")
  515. if req.EnterpriseId != 0 {
  516. sess.And("enterprise.id =?", req.EnterpriseId)
  517. }
  518. if req.StartDate != "" {
  519. sess.And("central_kitchen_for_school_user_refund_day.create_at >= ?", req.StartDate)
  520. }
  521. if req.EndDate != "" {
  522. sess.And("central_kitchen_for_school_user_refund_day.create_at <= ?", req.EndDate)
  523. }
  524. if len(req.MealKindList) > 0 {
  525. sess.In("central_kitchen_for_school_user_with_day.kind", req.MealKindList)
  526. }
  527. var m []*db.CentralKitchenForSchoolUserRefundDayWithData
  528. _, err := sess.
  529. Join("LEFT", "central_kitchen_for_school_user_with_day", "central_kitchen_for_school_user_refund_day.records_id = central_kitchen_for_school_user_with_day.id").
  530. Join("LEFT", "central_kitchen_for_school_package_ord", "central_kitchen_for_school_user_refund_day.out_trade_no = central_kitchen_for_school_package_ord.out_trade_no").
  531. Join("LEFT", "user_identity", "central_kitchen_for_school_user_with_day.identity_id = user_identity.id").
  532. Join("LEFT", "user", "user_identity.uid = user.id").
  533. Join("LEFT", "enterprise", "user_identity.enterprise_id = enterprise.id").
  534. Join("LEFT", "class_with_user", "user_identity.id = class_with_user.user_identity_id").
  535. Join("LEFT", "class", "class_with_user.class_id = class.id").
  536. Join("LEFT", "grade", "class.grade_id = grade.id").
  537. Desc("central_kitchen_for_school_user_refund_day.id").
  538. FindAndCount(&m)
  539. if err != nil {
  540. logx.Error(err)
  541. println("<<<<CentralKitchenForSchoolDataStatisticsExport2>>>>>Error:::", err.Error())
  542. return
  543. }
  544. titleList = []string{"学生姓名", "学校", "年级", "班级", "家长电话", "退款日期", "餐型", "退款金额", "退款状态", "支付时间", "退款操作时间"}
  545. xlsx.SetSheetRow("Sheet1", "A1", &titleList)
  546. j := 2 //表头被第一行用了,只能从第二行开始
  547. for _, v := range m {
  548. var kind string //就餐类型(1:早餐 2:午餐 3:晚餐)
  549. switch v.CentralKitchenForSchoolUserWithDay.Kind {
  550. case enum.CentralKitchenForSchoolUserWithDayKindForBreakfast:
  551. kind = enum.CentralKitchenForSchoolUserWithDayKind.String(enum.CentralKitchenForSchoolUserWithDayKindForBreakfast)
  552. break
  553. case enum.CentralKitchenForSchoolUserWithDayKindForLunch:
  554. kind = enum.CentralKitchenForSchoolUserWithDayKind.String(enum.CentralKitchenForSchoolUserWithDayKindForLunch)
  555. break
  556. case enum.CentralKitchenForSchoolUserWithDayKindForDinner:
  557. kind = enum.CentralKitchenForSchoolUserWithDayKind.String(enum.CentralKitchenForSchoolUserWithDayKindForDinner)
  558. break
  559. }
  560. var state string //状态(1:审核中 2:审核通过 3:审核拒绝 4:退款已完成)
  561. switch v.CentralKitchenForSchoolUserRefundDay.State {
  562. case enum.CentralKitchenForSchoolUserRefundDayStateForAuditing:
  563. state = enum.CentralKitchenForSchoolUserRefundDayState.String(enum.CentralKitchenForSchoolUserRefundDayStateForAuditing)
  564. break
  565. case enum.CentralKitchenForSchoolUserRefundDayStateForAuditPass:
  566. state = enum.CentralKitchenForSchoolUserRefundDayState.String(enum.CentralKitchenForSchoolUserRefundDayStateForAuditPass)
  567. break
  568. case enum.CentralKitchenForSchoolUserRefundDayStateForAuditReject:
  569. state = enum.CentralKitchenForSchoolUserRefundDayState.String(enum.CentralKitchenForSchoolUserRefundDayStateForAuditReject)
  570. break
  571. case enum.CentralKitchenForSchoolUserRefundDayStateForAuditComplete:
  572. state = enum.CentralKitchenForSchoolUserRefundDayState.String(enum.CentralKitchenForSchoolUserRefundDayStateForAuditComplete)
  573. break
  574. }
  575. xlsx.SetSheetRow("Sheet1", "A"+strconv.Itoa(j), &[]interface{}{v.CentralKitchenForSchoolPackageOrd.UserIdentityName, v.Enterprise.Name, v.Grade.Name, v.Class.Name, v.User.Phone,
  576. v.CentralKitchenForSchoolUserWithDay.Date, kind, v.CentralKitchenForSchoolUserRefundDay.Amount, state, v.CentralKitchenForSchoolPackageOrd.CreateAt, v.CentralKitchenForSchoolUserRefundDay.RefundDate})
  577. j++
  578. }
  579. //将文件保存至服务器
  580. xlsx.SaveAs("./static" + downloadPath)
  581. break
  582. case 3:
  583. //学校预定统计表
  584. fileName = "学校预定统计表_" + utils.Int64ToStr(now.UnixMilli()) + ".zip"
  585. downloadPath = "/export/central_kitchen_for_school/" + fileName
  586. var m []*db.CentralKitchenForSchoolPackageOrdForReserveWithUserIdentity
  587. sess := db.Db.Desc("central_kitchen_for_school_package_ord_for_reserve.id")
  588. if req.EnterpriseId != 0 {
  589. sess.And("central_kitchen_for_school_package_ord_for_reserve.enterprise_id =?", req.EnterpriseId)
  590. }
  591. if req.StartDate != "" {
  592. sess.And("central_kitchen_for_school_package_ord_for_reserve.meal_time_start >= ?", req.StartDate)
  593. }
  594. if req.EndDate != "" {
  595. sess.And("central_kitchen_for_school_package_ord_for_reserve.meal_time_start <= ?", req.EndDate)
  596. }
  597. if len(req.MealKindList) > 0 {
  598. sess.In("central_kitchen_for_school_package_ord_for_reserve.kind", req.MealKindList)
  599. }
  600. _, err := sess.
  601. Join("LEFT", "central_kitchen_for_school_package_ord", "central_kitchen_for_school_package_ord_for_reserve.out_trade_no = central_kitchen_for_school_package_ord.out_trade_no").
  602. Join("LEFT", "user_identity", "central_kitchen_for_school_package_ord.user_identity_id = user_identity.id").
  603. Join("LEFT", "enterprise", "enterprise.id = user_identity.enterprise_id").
  604. Join("LEFT", "user", "user.id = user_identity.uid").
  605. Join("LEFT", "class_with_user", "class_with_user.user_identity_id = user_identity.id").
  606. Join("LEFT", "class", "class_with_user.class_id = class.id").
  607. Join("LEFT", "grade", "class.grade_id = grade.id").
  608. FindAndCount(&m)
  609. if err != nil {
  610. logx.Error(err)
  611. println("<<<<SelfSupportForSchoolDataStatisticsExport3>>>>>Error:::", err.Error())
  612. return
  613. }
  614. centralKitchenForSchoolUserWithDayDb := db.CentralKitchenForSchoolUserWithDayDb{}
  615. centralKitchenForSchoolUserWithDayDb.Set(0)
  616. var list = map[string]map[string]map[string]map[string]int64{}
  617. var xlsFileName, xlsDownloadPath []string
  618. for _, v := range m {
  619. if list[v.Enterprise.Name] == nil {
  620. list[v.Enterprise.Name] = make(map[string]map[string]map[string]int64)
  621. }
  622. if list[v.Enterprise.Name][v.Grade.Name] == nil {
  623. list[v.Enterprise.Name][v.Grade.Name] = make(map[string]map[string]int64)
  624. }
  625. if list[v.Enterprise.Name][v.Grade.Name][v.Class.Name] == nil {
  626. list[v.Enterprise.Name][v.Grade.Name][v.Class.Name] = make(map[string]int64)
  627. }
  628. count, err2 := centralKitchenForSchoolUserWithDayDb.CountCentralKitchenForSchoolUserWithDayByOrdNo(v.CentralKitchenForSchoolPackageOrdForReserve.OutTradeNo, enum.CentralKitchenForSchoolUserWithDayStateForCancel)
  629. if err2 != nil {
  630. logx.Error(err)
  631. println("<<<<SelfSupportForSchoolDataStatisticsExport3>>>>>Error:::", err.Error())
  632. return
  633. }
  634. time.Sleep(50 * time.Millisecond) // 50毫秒
  635. if v.CentralKitchenForSchoolPackageOrdForReserve.Kind == enum.CentralKitchenForSchoolUserWithDayKindForBreakfast {
  636. list[v.Enterprise.Name][v.Grade.Name][v.Class.Name]["breakfast"] += count
  637. }
  638. if v.CentralKitchenForSchoolPackageOrdForReserve.Kind == enum.CentralKitchenForSchoolUserWithDayKindForLunch {
  639. list[v.Enterprise.Name][v.Grade.Name][v.Class.Name]["lunch"] += count
  640. }
  641. if v.CentralKitchenForSchoolPackageOrdForReserve.Kind == enum.CentralKitchenForSchoolUserWithDayKindForDinner {
  642. list[v.Enterprise.Name][v.Grade.Name][v.Class.Name]["dinner"] += count
  643. }
  644. }
  645. for k1, v1 := range list {
  646. tmpFileName := k1 + "学校预定统计表_" + utils.Int64ToStr(now.UnixMilli()) + ".xlsx"
  647. tmpDownloadPath := "./static/export/central_kitchen_for_school/" + tmpFileName
  648. titleList = []string{req.StartDate + "~~" + req.EndDate + " " + k1}
  649. xlsx.SetSheetRow("Sheet1", "A1", &titleList)
  650. titleList = []string{"年级", "班级", "早餐", "午餐", "晚餐"}
  651. xlsx.SetSheetRow("Sheet1", "A2", &titleList)
  652. xlsx.MergeCell("Sheet1", "A1", "E1") //合并单元格
  653. style, err := xlsx.NewStyle(`{"fill":{"type":"pattern","color":["FF0000"],"pattern":1}}`)
  654. if err != nil {
  655. logx.Error(err)
  656. println("<<<<CentralKitchenForSchoolDataStatisticsExport3>>>>>Error:::", err.Error())
  657. panic(err)
  658. }
  659. xlsx.SetCellStyle("Sheet1", "A1", "E1", style)
  660. j := 3 //表头前两行用了,只能从第三行开始
  661. for k2, v2 := range v1 {
  662. if k2 == "" {
  663. //若年级为空,则为教师餐
  664. k2 = "教师餐"
  665. }
  666. for k3, v3 := range v2 {
  667. if k3 == "" {
  668. //若班级为空,则为教师餐
  669. k3 = "教师餐"
  670. }
  671. xlsx.SetSheetRow("Sheet1", "A"+strconv.Itoa(j), &[]interface{}{
  672. k2, k3, v3["breakfast"], v3["lunch"], v3["dinner"]})
  673. j++
  674. }
  675. }
  676. //将文件保存至服务器
  677. err1 := xlsx.SaveAs(tmpDownloadPath)
  678. if err1 != nil {
  679. logx.Error(err1)
  680. println("<<<<CentralKitchenForSchoolDataStatisticsExport3>>>>>Error:::", err1.Error())
  681. panic(err1)
  682. }
  683. xlsDownloadPath = append(xlsDownloadPath, tmpDownloadPath)
  684. xlsFileName = append(xlsFileName, tmpFileName)
  685. }
  686. archive, err := os.Create("./static" + downloadPath) //第一步,创建 zip 文件
  687. if err != nil {
  688. logx.Error(err)
  689. println("<<<<CentralKitchenForSchoolDataStatisticsExport3>>>>>Error:::", err.Error())
  690. panic(err)
  691. }
  692. defer archive.Close()
  693. zipWriter := zip.NewWriter(archive) //第二步,创建一个新的 *Writer 对象
  694. for k, v := range xlsDownloadPath {
  695. w, err1 := zipWriter.Create("学校预定统计/" + xlsFileName[k]) //向 zip 文件中添加一个文件,返回一个待压缩的文件内容应写入的 Writer
  696. if err1 != nil {
  697. logx.Error(err1)
  698. println("<<<<CentralKitchenForSchoolDataStatisticsExport3>>>>>Error:::", err1.Error())
  699. panic(err1)
  700. }
  701. f, err1 := os.Open(v) //打开待压缩的文件
  702. if err1 != nil {
  703. logx.Error(err1)
  704. println("<<<<CentralKitchenForSchoolDataStatisticsExport3>>>>>Error:::", err1.Error())
  705. panic(err1)
  706. }
  707. if _, err1 = io.Copy(w, f); err != nil {
  708. logx.Error(err1)
  709. println("<<<<CentralKitchenForSchoolDataStatisticsExport3>>>>>Error:::", err1.Error())
  710. panic(err1)
  711. }
  712. if err1 != nil {
  713. logx.Error(err1)
  714. println("<<<<CentralKitchenForSchoolDataStatisticsExport3>>>>>Error:::", err1.Error())
  715. panic(err1)
  716. }
  717. f.Close()
  718. os.RemoveAll(v) //TODO::移除源文件
  719. }
  720. zipWriter.Close() // 第四步,关闭 zip writer,将所有数据写入指向基础 zip 文件的数据流
  721. if err != nil {
  722. panic(err)
  723. }
  724. break
  725. case 4:
  726. //班级明细表
  727. fileName = "班级明细表_" + utils.Int64ToStr(now.UnixMilli()) + ".zip"
  728. downloadPath = "/export/central_kitchen_for_school/" + fileName
  729. var m []*db.CentralKitchenForSchoolPackageOrdForReserveWithUserIdentity
  730. sess := db.Db.Desc("central_kitchen_for_school_package_ord_for_reserve.id")
  731. if req.EnterpriseId != 0 {
  732. sess.And("central_kitchen_for_school_package_ord_for_reserve.enterprise_id =?", req.EnterpriseId)
  733. }
  734. if req.StartDate != "" {
  735. sess.And("central_kitchen_for_school_package_ord_for_reserve.meal_time_start >= ?", req.StartDate)
  736. }
  737. if req.EndDate != "" {
  738. sess.And("central_kitchen_for_school_package_ord_for_reserve.meal_time_start <= ?", req.EndDate)
  739. }
  740. if len(req.MealKindList) > 0 {
  741. sess.In("central_kitchen_for_school_package_ord_for_reserve.kind", req.MealKindList)
  742. }
  743. _, err := sess.
  744. Join("LEFT", "central_kitchen_for_school_package_ord", "central_kitchen_for_school_package_ord_for_reserve.out_trade_no = central_kitchen_for_school_package_ord.out_trade_no").
  745. Join("LEFT", "user_identity", "central_kitchen_for_school_package_ord.user_identity_id = user_identity.id").
  746. Join("LEFT", "enterprise", "enterprise.id = user_identity.enterprise_id").
  747. Join("LEFT", "user", "user.id = user_identity.uid").
  748. Join("LEFT", "class_with_user", "class_with_user.user_identity_id = user_identity.id").
  749. Join("LEFT", "class", "class_with_user.class_id = class.id").
  750. Join("LEFT", "grade", "class.grade_id = grade.id").
  751. FindAndCount(&m)
  752. if err != nil {
  753. logx.Error(err)
  754. println("<<<<CentralKitchenForSchoolDataStatisticsExport4>>>>>Error:::", err.Error())
  755. return
  756. }
  757. var list = map[string]map[string]map[string]map[string]map[string]int64{}
  758. var xlsFileName, xlsDownloadPath []string
  759. centralKitchenForSchoolUserWithDayDb := db.CentralKitchenForSchoolUserWithDayDb{}
  760. centralKitchenForSchoolUserWithDayDb.Set(0)
  761. for _, v := range m {
  762. if list[v.Enterprise.Name] == nil {
  763. list[v.Enterprise.Name] = make(map[string]map[string]map[string]map[string]int64)
  764. }
  765. if list[v.Enterprise.Name][v.Grade.Name] == nil {
  766. list[v.Enterprise.Name][v.Grade.Name] = make(map[string]map[string]map[string]int64)
  767. }
  768. if list[v.Enterprise.Name][v.Grade.Name][v.Class.Name] == nil {
  769. list[v.Enterprise.Name][v.Grade.Name][v.Class.Name] = make(map[string]map[string]int64)
  770. }
  771. if list[v.Enterprise.Name][v.Grade.Name][v.Class.Name][v.UserIdentity.Name] == nil {
  772. list[v.Enterprise.Name][v.Grade.Name][v.Class.Name][v.UserIdentity.Name] = make(map[string]int64)
  773. }
  774. count, err2 := centralKitchenForSchoolUserWithDayDb.CountCentralKitchenForSchoolUserWithDayByOrdNo(v.CentralKitchenForSchoolPackageOrdForReserve.OutTradeNo, enum.CentralKitchenForSchoolUserWithDayStateForCancel)
  775. if err2 != nil {
  776. logx.Error(err)
  777. println("<<<<SelfSupportForSchoolDataStatisticsExport3>>>>>Error:::", err.Error())
  778. return
  779. }
  780. time.Sleep(50 * time.Millisecond) // 50毫秒
  781. if v.CentralKitchenForSchoolPackageOrdForReserve.Kind == enum.CentralKitchenForSchoolUserWithDayKindForBreakfast {
  782. list[v.Enterprise.Name][v.Grade.Name][v.Class.Name][v.UserIdentity.Name]["breakfast"] += count
  783. }
  784. if v.CentralKitchenForSchoolPackageOrdForReserve.Kind == enum.CentralKitchenForSchoolUserWithDayKindForLunch {
  785. list[v.Enterprise.Name][v.Grade.Name][v.Class.Name][v.UserIdentity.Name]["lunch"] += count
  786. }
  787. if v.CentralKitchenForSchoolPackageOrdForReserve.Kind == enum.CentralKitchenForSchoolUserWithDayKindForDinner {
  788. list[v.Enterprise.Name][v.Grade.Name][v.Class.Name][v.UserIdentity.Name]["dinner"] += count
  789. }
  790. }
  791. for k1, v1 := range list {
  792. tmpFileName := k1 + "班级明细_" + utils.Int64ToStr(now.UnixMilli()) + ".xlsx"
  793. tmpDownloadPath := "./static/export/central_kitchen_for_school/" + tmpFileName
  794. for k2, v2 := range v1 {
  795. if k2 == "" {
  796. //若年级为空,则为教师餐
  797. continue
  798. }
  799. for k3, v3 := range v2 {
  800. titleList = []string{"姓名", "早餐", "午餐", "晚餐"}
  801. xlsx.NewSheet(k3)
  802. xlsx.SetSheetRow(k3, "A1", &titleList)
  803. j := 2 //表头被第一行用了,只能从第二行开始
  804. for k4, v4 := range v3 {
  805. xlsx.SetSheetRow(k3, "A"+strconv.Itoa(j), &[]interface{}{
  806. k4, v4["breakfast"], v4["lunch"], v4["dinner"]})
  807. j++
  808. }
  809. }
  810. }
  811. //将文件保存至服务器
  812. xlsx.DeleteSheet("Sheet1") //TODO::只能放这里删除
  813. err1 := xlsx.SaveAs(tmpDownloadPath)
  814. if err1 != nil {
  815. logx.Error(err1)
  816. println("<<<<CentralKitchenForSchoolDataStatisticsExport4>>>>>Error:::", err1.Error())
  817. panic(err1)
  818. }
  819. xlsDownloadPath = append(xlsDownloadPath, tmpDownloadPath)
  820. xlsFileName = append(xlsFileName, tmpFileName)
  821. }
  822. archive, err := os.Create("./static" + downloadPath) //第一步,创建 zip 文件
  823. if err != nil {
  824. logx.Error(err)
  825. println("<<<<CentralKitchenForSchoolDataStatisticsExport4>>>>>Error:::", err.Error())
  826. panic(err)
  827. }
  828. defer archive.Close()
  829. zipWriter := zip.NewWriter(archive) //第二步,创建一个新的 *Writer 对象
  830. for k, v := range xlsDownloadPath {
  831. w, err1 := zipWriter.Create("班级明细/" + xlsFileName[k]) //向 zip 文件中添加一个文件,返回一个待压缩的文件内容应写入的 Writer
  832. if err1 != nil {
  833. logx.Error(err1)
  834. println("<<<<CentralKitchenForSchoolDataStatisticsExport4>>>>>Error:::", err1.Error())
  835. panic(err1)
  836. }
  837. f, err1 := os.Open(v) //打开待压缩的文件
  838. if err1 != nil {
  839. logx.Error(err1)
  840. println("<<<<CentralKitchenForSchoolDataStatisticsExport4>>>>>Error:::", err1.Error())
  841. panic(err1)
  842. }
  843. if _, err1 = io.Copy(w, f); err != nil {
  844. logx.Error(err1)
  845. println("<<<<CentralKitchenForSchoolDataStatisticsExport4>>>>>Error:::", err1.Error())
  846. panic(err1)
  847. }
  848. if err1 != nil {
  849. logx.Error(err1)
  850. println("<<<<CentralKitchenForSchoolDataStatisticsExport4>>>>>Error:::", err1.Error())
  851. panic(err1)
  852. }
  853. f.Close()
  854. os.RemoveAll(v) //TODO::移除源文件
  855. }
  856. zipWriter.Close() // 第四步,关闭 zip writer,将所有数据写入指向基础 zip 文件的数据流
  857. if err != nil {
  858. panic(err)
  859. }
  860. break
  861. case 5:
  862. //退款明细表
  863. fileName = "退款明细表_" + utils.Int64ToStr(now.UnixMilli()) + ".zip"
  864. downloadPath = "/export/central_kitchen_for_school/" + fileName
  865. sess := db.Db.Where("central_kitchen_for_school_user_refund_day.state =?", enum.CentralKitchenForSchoolUserRefundDayStateForAuditComplete).Desc("central_kitchen_for_school_user_refund_day.id")
  866. if req.EnterpriseId != 0 {
  867. sess.And("enterprise.id =?", req.EnterpriseId)
  868. }
  869. if req.StartDate != "" {
  870. sess.And("central_kitchen_for_school_user_refund_day.create_at >= ?", req.StartDate)
  871. }
  872. if req.EndDate != "" {
  873. sess.And("central_kitchen_for_school_user_refund_day.create_at <= ?", req.EndDate)
  874. }
  875. if len(req.MealKindList) > 0 {
  876. sess.In("central_kitchen_for_school_user_with_day.kind", req.MealKindList)
  877. }
  878. var m []*db.CentralKitchenForSchoolUserRefundDayWithData
  879. _, err := sess.
  880. Join("LEFT", "central_kitchen_for_school_user_with_day", "central_kitchen_for_school_user_refund_day.records_id = central_kitchen_for_school_user_with_day.id").
  881. Join("LEFT", "central_kitchen_for_school_package_ord", "central_kitchen_for_school_user_refund_day.out_trade_no = central_kitchen_for_school_package_ord.out_trade_no").
  882. Join("LEFT", "user_identity", "central_kitchen_for_school_user_with_day.identity_id = user_identity.id").
  883. Join("LEFT", "user", "user_identity.uid = user.id").
  884. Join("LEFT", "enterprise", "user_identity.enterprise_id = enterprise.id").
  885. Join("LEFT", "class_with_user", "user_identity.id = class_with_user.user_identity_id").
  886. Join("LEFT", "class", "class_with_user.class_id = class.id").
  887. Join("LEFT", "grade", "class.grade_id = grade.id").
  888. Desc("central_kitchen_for_school_user_refund_day.id").
  889. FindAndCount(&m)
  890. if err != nil {
  891. logx.Error(err)
  892. println("<<<<CentralKitchenForSchoolDataStatisticsExport5>>>>>Error:::", err.Error())
  893. return
  894. }
  895. var list = map[string]map[string]map[string]map[string]map[string]int{}
  896. var xlsFileName, xlsDownloadPath []string
  897. for _, v := range m {
  898. if list[v.Enterprise.Name] == nil {
  899. list[v.Enterprise.Name] = make(map[string]map[string]map[string]map[string]int)
  900. }
  901. if list[v.Enterprise.Name][v.Grade.Name] == nil {
  902. list[v.Enterprise.Name][v.Grade.Name] = make(map[string]map[string]map[string]int)
  903. }
  904. if list[v.Enterprise.Name][v.Grade.Name][v.Class.Name] == nil {
  905. list[v.Enterprise.Name][v.Grade.Name][v.Class.Name] = make(map[string]map[string]int)
  906. }
  907. if list[v.Enterprise.Name][v.Grade.Name][v.Class.Name][v.UserIdentity.Name] == nil {
  908. list[v.Enterprise.Name][v.Grade.Name][v.Class.Name][v.UserIdentity.Name] = make(map[string]int)
  909. }
  910. //就餐类型(1:早餐 2:午餐 3:晚餐)
  911. if v.CentralKitchenForSchoolUserWithDay.Kind == enum.CentralKitchenForSchoolUserWithDayKindForBreakfast {
  912. list[v.Enterprise.Name][v.Grade.Name][v.Class.Name][v.UserIdentity.Name]["breakfast"]++
  913. }
  914. if v.CentralKitchenForSchoolUserWithDay.Kind == enum.CentralKitchenForSchoolUserWithDayKindForLunch {
  915. list[v.Enterprise.Name][v.Grade.Name][v.Class.Name][v.UserIdentity.Name]["lunch"]++
  916. }
  917. if v.CentralKitchenForSchoolUserWithDay.Kind == enum.CentralKitchenForSchoolUserWithDayKindForDinner {
  918. list[v.Enterprise.Name][v.Grade.Name][v.Class.Name][v.UserIdentity.Name]["dinner"]++
  919. }
  920. }
  921. for k1, v1 := range list {
  922. tmpFileName := k1 + "退款明细_" + utils.Int64ToStr(now.UnixMilli()) + ".xlsx"
  923. tmpDownloadPath := "./static/export/central_kitchen_for_school/" + tmpFileName
  924. for k2, v2 := range v1 {
  925. if k2 == "" {
  926. //若年级为空,则为教师餐
  927. continue
  928. }
  929. for k3, v3 := range v2 {
  930. titleList = []string{"姓名", "早餐", "午餐", "晚餐"}
  931. xlsx.NewSheet(k3)
  932. xlsx.SetSheetRow(k3, "A1", &titleList)
  933. j := 2 //表头被第一行用了,只能从第二行开始
  934. for k4, v4 := range v3 {
  935. xlsx.SetSheetRow(k3, "A"+strconv.Itoa(j), &[]interface{}{
  936. k4, v4["breakfast"], v4["lunch"], v4["dinner"]})
  937. j++
  938. }
  939. }
  940. }
  941. //将文件保存至服务器
  942. xlsx.DeleteSheet("Sheet1") //TODO::只能放这里删除
  943. err1 := xlsx.SaveAs(tmpDownloadPath)
  944. if err1 != nil {
  945. logx.Error(err1)
  946. println("<<<<CentralKitchenForSchoolDataStatisticsExport5>>>>>Error:::", err1.Error())
  947. panic(err1)
  948. }
  949. xlsDownloadPath = append(xlsDownloadPath, tmpDownloadPath)
  950. xlsFileName = append(xlsFileName, tmpFileName)
  951. }
  952. archive, err := os.Create("./static" + downloadPath) //第一步,创建 zip 文件
  953. if err != nil {
  954. logx.Error(err)
  955. println("<<<<CentralKitchenForSchoolDataStatisticsExport5>>>>>Error:::", err.Error())
  956. panic(err)
  957. }
  958. defer archive.Close()
  959. zipWriter := zip.NewWriter(archive) //第二步,创建一个新的 *Writer 对象
  960. for k, v := range xlsDownloadPath {
  961. w, err1 := zipWriter.Create("退款明细/" + xlsFileName[k]) //向 zip 文件中添加一个文件,返回一个待压缩的文件内容应写入的 Writer
  962. if err1 != nil {
  963. logx.Error(err1)
  964. println("<<<<CentralKitchenForSchoolDataStatisticsExport5>>>>>Error:::", err1.Error())
  965. panic(err1)
  966. }
  967. f, err1 := os.Open(v) //打开待压缩的文件
  968. if err1 != nil {
  969. logx.Error(err1)
  970. println("<<<<CentralKitchenForSchoolDataStatisticsExport5>>>>>Error:::", err1.Error())
  971. panic(err1)
  972. }
  973. if _, err1 = io.Copy(w, f); err != nil {
  974. logx.Error(err1)
  975. println("<<<<CentralKitchenForSchoolDataStatisticsExport5>>>>>Error:::", err1.Error())
  976. panic(err1)
  977. }
  978. if err1 != nil {
  979. logx.Error(err1)
  980. println("<<<<CentralKitchenForSchoolDataStatisticsExport5>>>>>Error:::", err1.Error())
  981. panic(err1)
  982. }
  983. f.Close()
  984. os.RemoveAll(v) //TODO::移除源文件
  985. }
  986. zipWriter.Close() // 第四步,关闭 zip writer,将所有数据写入指向基础 zip 文件的数据流
  987. if err != nil {
  988. panic(err)
  989. }
  990. break
  991. case 6:
  992. //收款统计表
  993. fileName = "收款统计表_" + utils.Int64ToStr(now.UnixMilli()) + ".xlsx"
  994. downloadPath = "/export/central_kitchen_for_school/" + fileName
  995. //查找数据
  996. var m []model.Enterprise
  997. sess := db.Db.Desc("id")
  998. if req.EnterpriseId != 0 {
  999. sess.And("id = ?", req.EnterpriseId)
  1000. }
  1001. err := sess.Find(&m)
  1002. if err != nil {
  1003. logx.Error(err)
  1004. println("<<<<CentralKitchenForSchoolDataStatisticsExport6>>>>>Error:::", err.Error())
  1005. return
  1006. }
  1007. titleList = []string{"学校名称", "预定总费用", "退款总计", "实收费用", "预估费率"}
  1008. xlsx.SetSheetRow("Sheet1", "A1", &titleList)
  1009. j := 2 //表头被第一行用了,只能从第二行开始
  1010. for _, v := range m {
  1011. var income, refund string
  1012. //统计收入
  1013. sql1 := fmt.Sprintf("SELECT sum(total_price) as total FROM central_kitchen_for_school_package_ord where state = 1 and enterprise_id = %d and create_at >= '%s' and create_at < '%s'", v.Id, req.StartDate, req.EndDate)
  1014. mapArr1, err := db.QueryNativeString(db.Db, sql1)
  1015. if err != nil {
  1016. logx.Error(err)
  1017. println("<<<<CentralKitchenForSchoolDataStatisticsExport6>>>>>Error:::", err.Error())
  1018. return
  1019. }
  1020. if mapArr1 != nil {
  1021. income = mapArr1[0]["total"]
  1022. }
  1023. //统计退款
  1024. //查询出当前学校下所有的学生
  1025. userIdentityDb := db.UserIdentityDb{}
  1026. userIdentityDb.Set(0)
  1027. userIdentities, _ := userIdentityDb.FindUserIdentityForEnterprise(v.Id)
  1028. var userIdentityIds []string
  1029. for _, v := range *userIdentities {
  1030. userIdentityIds = append(userIdentityIds, utils.IntToStr(v.Id))
  1031. }
  1032. if len(userIdentityIds) != 0 {
  1033. userIdentityStr := php2go.Implode(",", userIdentityIds)
  1034. sql2 := fmt.Sprintf("SELECT sum(amount) as total FROM central_kitchen_for_school_user_refund_day where state = %d and identity_id IN (%s) and refund_date >= '%s' and refund_date < '%s'",
  1035. enum.CentralKitchenForSchoolUserRefundDayStateForAuditComplete, userIdentityStr, req.StartDate, req.EndDate)
  1036. mapArr2, err := db.QueryNativeString(db.Db, sql2)
  1037. if err != nil {
  1038. logx.Error(err)
  1039. println("<<<<CentralKitchenForSchoolDataStatisticsExport6>>>>>Error:::", err.Error())
  1040. return
  1041. }
  1042. if mapArr2 != nil {
  1043. refund = mapArr2[0]["total"]
  1044. }
  1045. }
  1046. realityAmount := utils.StrToFloat64(income) - utils.StrToFloat64(refund)
  1047. rateFee := realityAmount * 0.0038
  1048. xlsx.SetSheetRow("Sheet1", "A"+strconv.Itoa(j), &[]interface{}{v.Name, income, refund, realityAmount, rateFee})
  1049. j++
  1050. }
  1051. //将文件保存至服务器
  1052. xlsx.SaveAs("./static" + downloadPath)
  1053. break
  1054. case 7:
  1055. //班级收款明细表
  1056. fileName = "班级收款明细表_" + utils.Int64ToStr(now.UnixMilli()) + ".zip"
  1057. downloadPath = "/export/central_kitchen_for_school/" + fileName
  1058. //查找订单数据
  1059. sess := db.Db.Desc("central_kitchen_for_school_package_ord.id")
  1060. sess.And("central_kitchen_for_school_package_ord.state =?", enum.CentralKitchenForSchoolPackageOrdStateForSuccess)
  1061. if req.EnterpriseId != 0 {
  1062. sess.And("central_kitchen_for_school_package_ord.enterprise_id =?", req.EnterpriseId)
  1063. }
  1064. if req.StartDate != "" {
  1065. sess.And("central_kitchen_for_school_package_ord.create_at >= ?", req.StartDate)
  1066. }
  1067. if req.EndDate != "" {
  1068. sess.And("central_kitchen_for_school_package_ord.create_at <= ?", req.EndDate)
  1069. }
  1070. var m []*db.CentralKitchenForSchoolPackageOrdWithUserIdentity
  1071. _, err := sess.
  1072. Join("LEFT", "user_identity", "central_kitchen_for_school_package_ord.user_identity_id = user_identity.id").
  1073. Join("LEFT", "enterprise", "enterprise.id = user_identity.enterprise_id").
  1074. Join("LEFT", "user", "user.id = user_identity.uid").
  1075. Join("LEFT", "class_with_user", "class_with_user.user_identity_id = user_identity.id").
  1076. Join("LEFT", "class", "class_with_user.class_id = class.id").
  1077. Join("LEFT", "grade", "class.grade_id = grade.id").
  1078. FindAndCount(&m)
  1079. if err != nil {
  1080. logx.Error(err)
  1081. println("<<<<CentralKitchenForSchoolDataStatisticsExport7>>>>>Error:::", err.Error())
  1082. return
  1083. }
  1084. sess1 := db.Db.Where("central_kitchen_for_school_user_refund_day.state =?", enum.CentralKitchenForSchoolUserRefundDayStateForAuditComplete).Desc("central_kitchen_for_school_user_refund_day.id")
  1085. if req.EnterpriseId != 0 {
  1086. sess1.And("enterprise.id =?", req.EnterpriseId)
  1087. }
  1088. if req.StartDate != "" {
  1089. sess1.And("central_kitchen_for_school_user_refund_day.create_at >= ?", req.StartDate)
  1090. }
  1091. if req.EndDate != "" {
  1092. sess1.And("central_kitchen_for_school_user_refund_day.create_at <= ?", req.EndDate)
  1093. }
  1094. var mm []*db.CentralKitchenForSchoolUserRefundDayWithData
  1095. _, err = sess1.
  1096. Join("LEFT", "central_kitchen_for_school_user_with_day", "central_kitchen_for_school_user_refund_day.records_id = central_kitchen_for_school_user_with_day.id").
  1097. Join("LEFT", "central_kitchen_for_school_package_ord", "central_kitchen_for_school_user_refund_day.out_trade_no = central_kitchen_for_school_package_ord.out_trade_no").
  1098. Join("LEFT", "user_identity", "central_kitchen_for_school_user_with_day.identity_id = user_identity.id").
  1099. Join("LEFT", "user", "user_identity.uid = user.id").
  1100. Join("LEFT", "enterprise", "user_identity.enterprise_id = enterprise.id").
  1101. Join("LEFT", "class_with_user", "user_identity.id = class_with_user.user_identity_id").
  1102. Join("LEFT", "class", "class_with_user.class_id = class.id").
  1103. Join("LEFT", "grade", "class.grade_id = grade.id").
  1104. Desc("central_kitchen_for_school_user_refund_day.id").
  1105. FindAndCount(&mm)
  1106. if err != nil {
  1107. logx.Error(err)
  1108. println("<<<<CentralKitchenForSchoolDataStatisticsExport7>>>>>Error:::", err.Error())
  1109. return
  1110. }
  1111. var list = map[string]map[string]map[string]map[string]float64{}
  1112. for _, v := range m {
  1113. if list[v.Enterprise.Name] == nil {
  1114. list[v.Enterprise.Name] = make(map[string]map[string]map[string]float64)
  1115. }
  1116. if list[v.Enterprise.Name][v.Grade.Name] == nil {
  1117. list[v.Enterprise.Name][v.Grade.Name] = make(map[string]map[string]float64)
  1118. }
  1119. if list[v.Enterprise.Name][v.Grade.Name][v.Class.Name] == nil {
  1120. list[v.Enterprise.Name][v.Grade.Name][v.Class.Name] = make(map[string]float64)
  1121. }
  1122. list[v.Enterprise.Name][v.Grade.Name][v.Class.Name]["income"] += utils.StrToFloat64(v.CentralKitchenForSchoolPackageOrd.TotalPrice)
  1123. }
  1124. for _, v := range mm {
  1125. if list[v.Enterprise.Name] == nil {
  1126. list[v.Enterprise.Name] = make(map[string]map[string]map[string]float64)
  1127. }
  1128. if list[v.Enterprise.Name][v.Grade.Name] == nil {
  1129. list[v.Enterprise.Name][v.Grade.Name] = make(map[string]map[string]float64)
  1130. }
  1131. if list[v.Enterprise.Name][v.Grade.Name][v.Class.Name] == nil {
  1132. list[v.Enterprise.Name][v.Grade.Name][v.Class.Name] = make(map[string]float64)
  1133. }
  1134. list[v.Enterprise.Name][v.Grade.Name][v.Class.Name]["refund"] += utils.StrToFloat64(v.CentralKitchenForSchoolUserRefundDay.Amount)
  1135. }
  1136. var xlsFileName, xlsDownloadPath []string
  1137. for k1, v1 := range list {
  1138. tmpFileName := k1 + "班级收款明细_" + utils.Int64ToStr(now.UnixMilli()) + ".xlsx"
  1139. tmpDownloadPath := "./static/export/central_kitchen_for_school/" + tmpFileName
  1140. titleList = []string{"年级", "班级", "预定总费用", "退款总计"}
  1141. xlsx.SetSheetRow("Sheet1", "A1", &titleList)
  1142. j := 2 //表头被第一行用了,只能从第二行开始
  1143. for k2, v2 := range v1 {
  1144. if k2 == "" {
  1145. //若年级为空,则为教师餐
  1146. continue
  1147. }
  1148. for k3, v3 := range v2 {
  1149. xlsx.SetSheetRow("Sheet1", "A"+strconv.Itoa(j), &[]interface{}{k2, k3, utils.Float64ToStr(v3["income"]), utils.Float64ToStr(v3["refund"])})
  1150. j++
  1151. }
  1152. }
  1153. //将文件保存至服务器
  1154. err1 := xlsx.SaveAs(tmpDownloadPath)
  1155. if err1 != nil {
  1156. logx.Error(err1)
  1157. println("<<<<CentralKitchenForSchoolDataStatisticsExport7>>>>>Error:::", err1.Error())
  1158. panic(err1)
  1159. }
  1160. xlsDownloadPath = append(xlsDownloadPath, tmpDownloadPath)
  1161. xlsFileName = append(xlsFileName, tmpFileName)
  1162. }
  1163. archive, err := os.Create("./static" + downloadPath) //第一步,创建 zip 文件
  1164. if err != nil {
  1165. logx.Error(err)
  1166. println("<<<<CentralKitchenForSchoolDataStatisticsExport7>>>>>Error:::", err.Error())
  1167. panic(err)
  1168. }
  1169. defer archive.Close()
  1170. zipWriter := zip.NewWriter(archive) //第二步,创建一个新的 *Writer 对象
  1171. for k, v := range xlsDownloadPath {
  1172. w, err1 := zipWriter.Create("班级收款明细/" + xlsFileName[k]) //向 zip 文件中添加一个文件,返回一个待压缩的文件内容应写入的 Writer
  1173. if err1 != nil {
  1174. logx.Error(err1)
  1175. println("<<<<CentralKitchenForSchoolDataStatisticsExport7>>>>>Error:::", err1.Error())
  1176. panic(err1)
  1177. }
  1178. f, err1 := os.Open(v) //打开待压缩的文件
  1179. if err1 != nil {
  1180. logx.Error(err1)
  1181. println("<<<<CentralKitchenForSchoolDataStatisticsExport7>>>>>Error:::", err1.Error())
  1182. panic(err1)
  1183. }
  1184. if _, err1 = io.Copy(w, f); err != nil {
  1185. logx.Error(err1)
  1186. println("<<<<CentralKitchenForSchoolDataStatisticsExport7>>>>>Error:::", err1.Error())
  1187. panic(err1)
  1188. }
  1189. if err1 != nil {
  1190. logx.Error(err1)
  1191. println("<<<<CentralKitchenForSchoolDataStatisticsExport7>>>>>Error:::", err1.Error())
  1192. panic(err1)
  1193. }
  1194. f.Close()
  1195. os.RemoveAll(v) //TODO::移除源文件
  1196. }
  1197. zipWriter.Close() // 第四步,关闭 zip writer,将所有数据写入指向基础 zip 文件的数据流
  1198. if err != nil {
  1199. panic(err)
  1200. }
  1201. break
  1202. case 8:
  1203. //学校预定数量统计表
  1204. fileName = "学校预定数量统计表_" + utils.Int64ToStr(now.UnixMilli()) + ".xlsx"
  1205. downloadPath = "/export/central_kitchen_for_school/" + fileName
  1206. var m []*db.CentralKitchenForSchoolPackageOrdForReserveWithUserIdentity
  1207. sess := db.Db.Desc("central_kitchen_for_school_package_ord_for_reserve.id")
  1208. if req.EnterpriseId != 0 {
  1209. sess.And("central_kitchen_for_school_package_ord_for_reserve.enterprise_id =?", req.EnterpriseId)
  1210. }
  1211. if req.StartDate != "" {
  1212. sess.And("central_kitchen_for_school_package_ord_for_reserve.meal_time_start >= ?", req.StartDate)
  1213. }
  1214. if req.EndDate != "" {
  1215. sess.And("central_kitchen_for_school_package_ord_for_reserve.meal_time_start <= ?", req.EndDate)
  1216. }
  1217. if len(req.MealKindList) > 0 {
  1218. sess.In("central_kitchen_for_school_package_ord_for_reserve.kind", req.MealKindList)
  1219. }
  1220. _, err := sess.
  1221. Join("LEFT", "central_kitchen_for_school_package_ord", "central_kitchen_for_school_package_ord_for_reserve.out_trade_no = central_kitchen_for_school_package_ord.out_trade_no").
  1222. Join("LEFT", "user_identity", "central_kitchen_for_school_package_ord.user_identity_id = user_identity.id").
  1223. Join("LEFT", "enterprise", "enterprise.id = user_identity.enterprise_id").
  1224. FindAndCount(&m)
  1225. if err != nil {
  1226. logx.Error(err)
  1227. println("<<<<SelfSupportForSchoolDataStatisticsExport3>>>>>Error:::", err.Error())
  1228. return
  1229. }
  1230. var list = map[string]int64{}
  1231. centralKitchenForSchoolUserWithDayDb := db.CentralKitchenForSchoolUserWithDayDb{}
  1232. centralKitchenForSchoolUserWithDayDb.Set(0)
  1233. for _, v := range m {
  1234. count, err2 := centralKitchenForSchoolUserWithDayDb.CountCentralKitchenForSchoolUserWithDayByOrdNo(v.CentralKitchenForSchoolPackageOrdForReserve.OutTradeNo, enum.CentralKitchenForSchoolUserWithDayStateForCancel)
  1235. if err2 != nil {
  1236. logx.Error(err)
  1237. println("<<<<SelfSupportForSchoolDataStatisticsExport3>>>>>Error:::", err.Error())
  1238. return
  1239. }
  1240. time.Sleep(50 * time.Millisecond) // 50毫秒
  1241. list[v.Enterprise.Name] += count
  1242. }
  1243. titleList = []string{"学校名称", "预定总数"}
  1244. xlsx.SetSheetRow("Sheet1", "A1", &titleList)
  1245. j := 2 //表头被第一行用了,只能从第二行开始
  1246. for k, v := range list {
  1247. xlsx.SetSheetRow("Sheet1", "A"+strconv.Itoa(j), &[]interface{}{k, v})
  1248. j++
  1249. }
  1250. //将文件保存至服务器
  1251. xlsx.SaveAs("./static" + downloadPath)
  1252. break
  1253. }
  1254. //新增数据
  1255. centralKitchenForSchoolExportRecordsDb := db.CentralKitchenForSchoolExportRecordsDb{}
  1256. centralKitchenForSchoolExportRecordsDb.Set()
  1257. marshal, _ := json.Marshal(req)
  1258. centralKitchenForSchoolExportRecordsDb.CentralKitchenForSchoolExportRecordsInsert(&model.CentralKitchenForSchoolExportRecords{
  1259. Name: fileName,
  1260. DownloadPath: downloadPath,
  1261. Kind: kind,
  1262. ReqContent: string(marshal),
  1263. CreateAt: now.Format("2006-01-02 15:04:05"),
  1264. UpdateAt: now.Format("2006-01-02 15:04:05"),
  1265. })
  1266. }
  1267. }
  1268. func CentralKitchenForSchoolDataStatisticsContrast(req md.CentralKitchenForSchoolDataStatisticsContrastReq) {
  1269. var titleList []string
  1270. var fileName, downloadPath string
  1271. var now = time.Now()
  1272. xlsx := excelize.NewFile()
  1273. //数据对比
  1274. fileName = "数据对比_" + utils.Int64ToStr(now.UnixMilli()) + ".zip"
  1275. downloadPath = "/export/central_kitchen_for_school/" + fileName
  1276. var m []*db.CentralKitchenForSchoolPackageOrdForReserveWithUserIdentity
  1277. sess := db.Db.Desc("central_kitchen_for_school_package_ord_for_reserve.id")
  1278. if req.EnterpriseId != 0 {
  1279. sess.And("central_kitchen_for_school_package_ord_for_reserve.enterprise_id =?", req.EnterpriseId)
  1280. }
  1281. sess.And("central_kitchen_for_school_package_ord_for_reserve.meal_time_start >= ?", req.Date1[0])
  1282. sess.And("central_kitchen_for_school_package_ord_for_reserve.meal_time_start <= ?", req.Date1[1])
  1283. if len(req.MealKindList) > 0 {
  1284. sess.In("central_kitchen_for_school_package_ord_for_reserve.kind", req.MealKindList)
  1285. }
  1286. _, err := sess.
  1287. Join("LEFT", "central_kitchen_for_school_package_ord", "central_kitchen_for_school_package_ord_for_reserve.out_trade_no = central_kitchen_for_school_package_ord.out_trade_no").
  1288. Join("LEFT", "user_identity", "central_kitchen_for_school_package_ord.user_identity_id = user_identity.id").
  1289. Join("LEFT", "enterprise", "enterprise.id = user_identity.enterprise_id").
  1290. Join("LEFT", "user", "user.id = user_identity.uid").
  1291. Join("LEFT", "class_with_user", "class_with_user.user_identity_id = user_identity.id").
  1292. Join("LEFT", "class", "class_with_user.class_id = class.id").
  1293. Join("LEFT", "grade", "class.grade_id = grade.id").
  1294. FindAndCount(&m)
  1295. if err != nil {
  1296. logx.Error(err)
  1297. println("<<<<CentralKitchenForSchoolDataStatisticsContrast>>>>>Error:::", err.Error())
  1298. return
  1299. }
  1300. var m1 []*db.CentralKitchenForSchoolPackageOrdForReserveWithUserIdentity
  1301. sess1 := db.Db.Desc("central_kitchen_for_school_package_ord_for_reserve.id")
  1302. if req.EnterpriseId != 0 {
  1303. sess1.And("central_kitchen_for_school_package_ord_for_reserve.enterprise_id =?", req.EnterpriseId)
  1304. }
  1305. sess1.And("central_kitchen_for_school_package_ord_for_reserve.meal_time_start >= ?", req.Date2[0])
  1306. sess1.And("central_kitchen_for_school_package_ord_for_reserve.meal_time_start <= ?", req.Date2[1])
  1307. if len(req.MealKindList) > 0 {
  1308. sess1.In("central_kitchen_for_school_package_ord_for_reserve.kind", req.MealKindList)
  1309. }
  1310. _, err1 := sess1.
  1311. Join("LEFT", "central_kitchen_for_school_package_ord", "central_kitchen_for_school_package_ord_for_reserve.out_trade_no = central_kitchen_for_school_package_ord.out_trade_no").
  1312. Join("LEFT", "user_identity", "central_kitchen_for_school_package_ord.user_identity_id = user_identity.id").
  1313. Join("LEFT", "enterprise", "enterprise.id = user_identity.enterprise_id").
  1314. Join("LEFT", "user", "user.id = user_identity.uid").
  1315. Join("LEFT", "class_with_user", "class_with_user.user_identity_id = user_identity.id").
  1316. Join("LEFT", "class", "class_with_user.class_id = class.id").
  1317. Join("LEFT", "grade", "class.grade_id = grade.id").
  1318. FindAndCount(&m)
  1319. if err1 != nil {
  1320. logx.Error(err1)
  1321. println("<<<<CentralKitchenForSchoolDataStatisticsContrast>>>>>Error:::", err.Error())
  1322. return
  1323. }
  1324. var list = map[string]map[string]map[string]map[string]string{}
  1325. for _, v := range m {
  1326. if list[v.Enterprise.Name] == nil {
  1327. list[v.Enterprise.Name] = make(map[string]map[string]map[string]string)
  1328. }
  1329. if list[v.Enterprise.Name][v.Grade.Name] == nil {
  1330. list[v.Enterprise.Name][v.Grade.Name] = make(map[string]map[string]string)
  1331. }
  1332. if list[v.Enterprise.Name][v.Grade.Name][v.Class.Name] == nil {
  1333. list[v.Enterprise.Name][v.Grade.Name][v.Class.Name] = make(map[string]string)
  1334. }
  1335. list[v.Enterprise.Name][v.Grade.Name][v.Class.Name][v.UserIdentity.Name] = v.User.Phone
  1336. }
  1337. var list1 = map[string]map[string]map[string]map[string]string{}
  1338. for _, v := range m1 {
  1339. if list1[v.Enterprise.Name] == nil {
  1340. list1[v.Enterprise.Name] = make(map[string]map[string]map[string]string)
  1341. }
  1342. if list1[v.Enterprise.Name][v.Grade.Name] == nil {
  1343. list1[v.Enterprise.Name][v.Grade.Name] = make(map[string]map[string]string)
  1344. }
  1345. if list1[v.Enterprise.Name][v.Grade.Name][v.Class.Name] == nil {
  1346. list1[v.Enterprise.Name][v.Grade.Name][v.Class.Name] = make(map[string]string)
  1347. }
  1348. list1[v.Enterprise.Name][v.Grade.Name][v.Class.Name][v.UserIdentity.Name] = v.User.Phone
  1349. }
  1350. var xlsFileName, xlsDownloadPath []string
  1351. for k1, v1 := range list {
  1352. tmpFileName := k1 + "数据对比_" + utils.Int64ToStr(now.UnixMilli()) + ".xlsx"
  1353. tmpDownloadPath := "./static/export/central_kitchen_for_school/" + tmpFileName
  1354. for k2, v2 := range v1 {
  1355. if k2 == "" {
  1356. //若年级为空,则为教师
  1357. continue
  1358. }
  1359. for k3, v3 := range v2 {
  1360. titleList = []string{"姓名", "家长联系电话"}
  1361. xlsx.NewSheet(k3)
  1362. xlsx.SetSheetRow(k3, "A1", &titleList)
  1363. j := 2 //表头被第一行用了,只能从第二行开始
  1364. for k4, v4 := range v3 {
  1365. //TODO::判断当前用户是否在新的月份里面
  1366. if list1[k1] == nil || list1[k1][k2] == nil || list1[k1][k2][k3] == nil || list1[k1][k2][k3][k4] == "" {
  1367. xlsx.SetSheetRow(k3, "A"+strconv.Itoa(j), &[]interface{}{
  1368. k4, v4})
  1369. j++
  1370. }
  1371. }
  1372. }
  1373. }
  1374. //将文件保存至服务器
  1375. xlsx.DeleteSheet("Sheet1") //TODO::只能放这里删除
  1376. err1 := xlsx.SaveAs(tmpDownloadPath)
  1377. if err1 != nil {
  1378. logx.Error(err1)
  1379. println("<<<<CentralKitchenForSchoolDataStatisticsContrast>>>>>Error:::", err1.Error())
  1380. panic(err1)
  1381. }
  1382. xlsDownloadPath = append(xlsDownloadPath, tmpDownloadPath)
  1383. xlsFileName = append(xlsFileName, tmpFileName)
  1384. }
  1385. archive, err := os.Create("./static" + downloadPath) //第一步,创建 zip 文件
  1386. if err != nil {
  1387. logx.Error(err)
  1388. println("<<<<CentralKitchenForSchoolDataStatisticsContrast>>>>>Error:::", err.Error())
  1389. panic(err)
  1390. }
  1391. defer archive.Close()
  1392. zipWriter := zip.NewWriter(archive) //第二步,创建一个新的 *Writer 对象
  1393. for k, v := range xlsDownloadPath {
  1394. w, err1 := zipWriter.Create("数据对比/" + xlsFileName[k]) //向 zip 文件中添加一个文件,返回一个待压缩的文件内容应写入的 Writer
  1395. if err1 != nil {
  1396. logx.Error(err1)
  1397. println("<<<<CentralKitchenForSchoolDataStatisticsContrast>>>>>Error:::", err1.Error())
  1398. panic(err1)
  1399. }
  1400. f, err1 := os.Open(v) //打开待压缩的文件
  1401. if err1 != nil {
  1402. logx.Error(err1)
  1403. println("<<<<CentralKitchenForSchoolDataStatisticsContrast>>>>>Error:::", err1.Error())
  1404. panic(err1)
  1405. }
  1406. if _, err1 = io.Copy(w, f); err != nil {
  1407. logx.Error(err1)
  1408. println("<<<<CentralKitchenForSchoolDataStatisticsContrast>>>>>Error:::", err1.Error())
  1409. panic(err1)
  1410. }
  1411. if err1 != nil {
  1412. logx.Error(err1)
  1413. println("<<<<CentralKitchenForSchoolDataStatisticsContrast>>>>>Error:::", err1.Error())
  1414. panic(err1)
  1415. }
  1416. f.Close()
  1417. os.RemoveAll(v) //TODO::移除源文件
  1418. }
  1419. zipWriter.Close() // 第四步,关闭 zip writer,将所有数据写入指向基础 zip 文件的数据流
  1420. if err != nil {
  1421. panic(err)
  1422. }
  1423. //新增数据
  1424. centralKitchenForSchoolExportRecordsDb := db.CentralKitchenForSchoolExportRecordsDb{}
  1425. centralKitchenForSchoolExportRecordsDb.Set()
  1426. marshal, _ := json.Marshal(req)
  1427. centralKitchenForSchoolExportRecordsDb.CentralKitchenForSchoolExportRecordsInsert(&model.CentralKitchenForSchoolExportRecords{
  1428. Name: fileName,
  1429. DownloadPath: downloadPath,
  1430. Kind: 9, //数据对比
  1431. ReqContent: string(marshal),
  1432. CreateAt: now.Format("2006-01-02 15:04:05"),
  1433. UpdateAt: now.Format("2006-01-02 15:04:05"),
  1434. })
  1435. }
  1436. func JudgeSelfSupportForMealTime(mealDate string) (mealZh string) {
  1437. mealZh = "未知"
  1438. mealTime, _ := time.ParseInLocation("2006-01-02 15:04:05", mealDate, time.Local)
  1439. for _, v := range md.SelfSupportForMealTimeMapping {
  1440. //判断时间大小
  1441. tmpFromTime, _ := time.ParseInLocation("2006-01-02 15:04:05", mealTime.Format("2006-01-02")+" "+v["from"]+":00", time.Local)
  1442. tmpToTime, _ := time.ParseInLocation("2006-01-02 15:04:05", mealTime.Format("2006-01-02")+" "+v["to"]+":00", time.Local)
  1443. fmt.Println(">>>>>>>>>>>>>", mealTime.After(tmpFromTime))
  1444. fmt.Println("<<<<<<<<<<<<<", mealTime.Before(tmpToTime))
  1445. if mealTime.After(tmpFromTime) && mealTime.Before(tmpToTime) {
  1446. mealZh = v["name"]
  1447. }
  1448. }
  1449. return
  1450. }