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

1899 lines
81 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. if req.EnterPriseKind != 0 {
  234. sess.And("enterprise.kind = ?", req.EnterPriseKind)
  235. }
  236. sess.And("user_identity.identity = ?", enum.UserIdentityForSelfSupportForTeacher) //只查询老师
  237. _, err := sess.
  238. Join("LEFT", "user_identity", "self_support_for_school_ord.user_identity_id = user_identity.id").
  239. Join("LEFT", "enterprise", "enterprise.id = user_identity.enterprise_id").
  240. Join("LEFT", "user", "user.id = user_identity.uid").
  241. Join("LEFT", "class_with_user", "class_with_user.user_identity_id = user_identity.id").
  242. Join("LEFT", "class", "class_with_user.class_id = class.id").
  243. Join("LEFT", "grade", "class.grade_id = grade.id").
  244. FindAndCount(&m)
  245. if err != nil {
  246. logx.Error(err)
  247. println("<<<<SelfSupportForSchoolDataStatisticsExport2>>>>>Error:::", err.Error())
  248. return
  249. }
  250. //整合数据
  251. var data = map[string]map[string]map[string]map[string]interface{}{}
  252. for _, vv := range m {
  253. if data[vv.Enterprise.Name] == nil {
  254. data[vv.Enterprise.Name] = make(map[string]map[string]map[string]interface{})
  255. }
  256. if data[vv.Enterprise.Name][vv.UserIdentity.Name] == nil {
  257. data[vv.Enterprise.Name][vv.UserIdentity.Name] = make(map[string]map[string]interface{})
  258. }
  259. mealZh := JudgeSelfSupportForMealTime(vv.SelfSupportForSchoolOrd.FaceTime)
  260. if data[vv.Enterprise.Name][vv.UserIdentity.Name][mealZh] == nil {
  261. data[vv.Enterprise.Name][vv.UserIdentity.Name][mealZh] = make(map[string]interface{})
  262. }
  263. data[vv.Enterprise.Name][vv.UserIdentity.Name]["编号"] = map[string]interface{}{
  264. "id": vv.UserIdentity.IdNo,
  265. }
  266. if data[vv.Enterprise.Name][vv.UserIdentity.Name]["合计"] == nil {
  267. data[vv.Enterprise.Name][vv.UserIdentity.Name]["合计"] = make(map[string]interface{})
  268. }
  269. data[vv.Enterprise.Name][vv.UserIdentity.Name][mealZh]["num"] = utils.AnyToInt64(data[vv.Enterprise.Name][vv.UserIdentity.Name][mealZh]["num"]) + 1
  270. 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)
  271. data[vv.Enterprise.Name][vv.UserIdentity.Name]["合计"]["num"] = utils.AnyToInt64(data[vv.Enterprise.Name][vv.UserIdentity.Name]["合计"]["num"]) + 1
  272. data[vv.Enterprise.Name][vv.UserIdentity.Name]["合计"]["amount"] = utils.AnyToFloat64(data[vv.Enterprise.Name][vv.UserIdentity.Name]["合计"]["amount"]) + utils.AnyToFloat64(vv.SelfSupportForSchoolOrd.TradeAmount)
  273. }
  274. xlsx.SetSheetRow("", "A1", &titleList)
  275. sheetIndex := 2 //第一个表是模板表
  276. j := 3 //表头前两行被占用了,只能从第三行开始
  277. for k, v := range data {
  278. xlsx.NewSheet(k) //1、新建表格
  279. xlsx.CopySheet(1, sheetIndex)
  280. for kk, vv := range v {
  281. xlsx.SetSheetRow(k, "A"+strconv.Itoa(j), &[]interface{}{vv["编号"]["id"], kk,
  282. vv["早餐"]["num"], vv["早餐"]["amount"],
  283. vv["午餐"]["num"], vv["午餐"]["amount"],
  284. vv["晚餐"]["num"], vv["晚餐"]["amount"],
  285. vv["夜宵"]["num"], vv["夜宵"]["amount"],
  286. vv["未知"]["num"], vv["未知"]["amount"],
  287. vv["合计"]["num"], vv["合计"]["amount"],
  288. })
  289. j++
  290. }
  291. sheetIndex++
  292. }
  293. xlsx.DeleteSheet("template") //删除模板表
  294. //将文件保存至服务器
  295. xlsx.SaveAs("./static" + downloadPath)
  296. break
  297. case 2:
  298. //商家分餐数据统计表
  299. xlsx, _ := excelize.OpenFile("./static/template/自营学校(商家分餐数据统计表).xlsx")
  300. fileName = "商家分餐数据统计表_" + utils.Int64ToStr(now.UnixMilli()) + ".xlsx"
  301. downloadPath = "/export/self_support_for_school/" + fileName
  302. //查询出所有的设备
  303. var deviceList []*model.Device
  304. err := db.Db.Where("enterprise_id != 0").Find(&deviceList)
  305. if err != nil {
  306. logx.Error(err)
  307. println("<<<<SelfSupportForSchoolDataStatisticsExport2>>>>>1111Error:::", err.Error())
  308. return
  309. }
  310. var deviceListMapping = map[string]model.Device{}
  311. for _, v := range deviceList {
  312. deviceListMapping[v.DeviceSn] = *v
  313. }
  314. var m []*db.SelfSupportForSchoolOrdWithUserIdentity
  315. sess := db.Db.Desc("self_support_for_school_ord.id")
  316. if req.EnterpriseId != 0 {
  317. sess.And("self_support_for_school_ord.enterprise_id =?", req.EnterpriseId)
  318. }
  319. if req.StartDate != "" {
  320. sess.And("self_support_for_school_ord.face_time >= ?", req.StartDate)
  321. }
  322. if req.EndDate != "" {
  323. sess.And("self_support_for_school_ord.face_time <= ?", req.EndDate)
  324. }
  325. if req.EnterPriseKind != 0 {
  326. sess.And("enterprise.kind = ?", req.EnterPriseKind)
  327. }
  328. _, err = sess.
  329. Join("LEFT", "user_identity", "self_support_for_school_ord.user_identity_id = user_identity.id").
  330. Join("LEFT", "enterprise", "enterprise.id = user_identity.enterprise_id").
  331. Join("LEFT", "user", "user.id = user_identity.uid").
  332. Join("LEFT", "class_with_user", "class_with_user.user_identity_id = user_identity.id").
  333. Join("LEFT", "class", "class_with_user.class_id = class.id").
  334. Join("LEFT", "grade", "class.grade_id = grade.id").
  335. FindAndCount(&m)
  336. if err != nil {
  337. logx.Error(err)
  338. println("<<<<SelfSupportForSchoolDataStatisticsExport2>>>>>Error:::", err.Error())
  339. return
  340. }
  341. //整合数据
  342. var data = map[string]map[string]map[string]map[string]float64{}
  343. for _, vv := range m {
  344. shopName := deviceListMapping[vv.SelfSupportForSchoolOrd.DeviceSn].Name
  345. if data[vv.Enterprise.Name] == nil {
  346. data[vv.Enterprise.Name] = make(map[string]map[string]map[string]float64)
  347. }
  348. if data[vv.Enterprise.Name][shopName] == nil {
  349. data[vv.Enterprise.Name][shopName] = make(map[string]map[string]float64)
  350. }
  351. mealZh := JudgeSelfSupportForMealTime(vv.SelfSupportForSchoolOrd.FaceTime)
  352. if data[vv.Enterprise.Name][shopName][mealZh] == nil {
  353. data[vv.Enterprise.Name][shopName][mealZh] = make(map[string]float64)
  354. }
  355. if data[vv.Enterprise.Name][shopName]["合计"] == nil {
  356. data[vv.Enterprise.Name][shopName]["合计"] = make(map[string]float64)
  357. }
  358. data[vv.Enterprise.Name][shopName][mealZh]["num"]++
  359. data[vv.Enterprise.Name][shopName][mealZh]["amount"] += utils.StrToFloat64(vv.SelfSupportForSchoolOrd.TradeAmount)
  360. data[vv.Enterprise.Name][shopName]["合计"]["num"]++
  361. data[vv.Enterprise.Name][shopName]["合计"]["amount"] += utils.StrToFloat64(vv.SelfSupportForSchoolOrd.TradeAmount)
  362. }
  363. xlsx.SetSheetRow("", "A1", &titleList)
  364. sheetIndex := 2 //第一个表是模板表
  365. for k, v := range data {
  366. if k == "" {
  367. k = "未知"
  368. }
  369. xlsx.NewSheet(k) //1、新建表格
  370. xlsx.CopySheet(1, sheetIndex)
  371. j := 4 //表头前三行被占用了,只能从第四行开始
  372. for kk, vv := range v {
  373. xlsx.SetSheetRow(k, "A"+strconv.Itoa(j), &[]interface{}{kk,
  374. vv["早餐"]["num"], vv["早餐"]["amount"],
  375. vv["午餐"]["num"], vv["午餐"]["amount"],
  376. vv["晚餐"]["num"], vv["晚餐"]["amount"],
  377. vv["夜宵"]["num"], vv["夜宵"]["amount"],
  378. vv["合计"]["num"], vv["合计"]["amount"],
  379. })
  380. j++
  381. }
  382. sheetIndex++
  383. }
  384. xlsx.DeleteSheet("template") //删除模板表
  385. //将文件保存至服务器
  386. xlsx.SaveAs("./static" + downloadPath)
  387. break
  388. }
  389. //新增数据
  390. selfSupportForSchoolExportRecordsDb := db.SelfSupportForSchoolExportRecordsDb{}
  391. selfSupportForSchoolExportRecordsDb.Set()
  392. marshal, _ := json.Marshal(req)
  393. selfSupportForSchoolExportRecordsDb.SelfSupportForSchoolExportRecordsInsert(&model.SelfSupportForSchoolExportRecords{
  394. Name: fileName,
  395. DownloadPath: downloadPath,
  396. Kind: req.Kind,
  397. ReqContent: string(marshal),
  398. CreateAt: now.Format("2006-01-02 15:04:05"),
  399. UpdateAt: now.Format("2006-01-02 15:04:05"),
  400. })
  401. }
  402. func CentralKitchenForSchoolDataStatisticsExport(req md.CentralKitchenForSchoolDataStatisticsExportReq) {
  403. var titleList []string
  404. var fileName, downloadPath string
  405. var now = time.Now()
  406. for _, kind := range req.Kinds {
  407. xlsx := excelize.NewFile()
  408. switch kind {
  409. case 1:
  410. //订单列表
  411. fileName = "订单列表_" + utils.Int64ToStr(now.UnixMilli()) + ".xlsx"
  412. downloadPath = "/export/central_kitchen_for_school/" + fileName
  413. startDate, _ := time.ParseInLocation("2006-01-02 15:04:05", req.StartDate, time.Local)
  414. endDate, _ := time.ParseInLocation("2006-01-02 15:04:05", req.EndDate, time.Local)
  415. //查找订单数据
  416. sess := db.Db.Desc("central_kitchen_for_school_package_ord.id")
  417. sess.And("central_kitchen_for_school_package_ord.state =?", enum.CentralKitchenForSchoolPackageOrdStateForSuccess)
  418. if req.EnterpriseId != 0 {
  419. sess.And("central_kitchen_for_school_package_ord.enterprise_id =?", req.EnterpriseId)
  420. }
  421. if req.StartDate != "" {
  422. sess.And("central_kitchen_for_school_package_ord.create_at >= ?", req.StartDate)
  423. }
  424. if req.EndDate != "" {
  425. sess.And("central_kitchen_for_school_package_ord.create_at <= ?", req.EndDate)
  426. }
  427. if req.EnterPriseKind != 0 {
  428. sess.And("enterprise.kind = ?", req.EnterPriseKind)
  429. }
  430. var m []*db.CentralKitchenForSchoolPackageOrdWithUserIdentity
  431. _, err := sess.
  432. Join("LEFT", "user_identity", "central_kitchen_for_school_package_ord.user_identity_id = user_identity.id").
  433. Join("LEFT", "enterprise", "enterprise.id = user_identity.enterprise_id").
  434. Join("LEFT", "user", "user.id = user_identity.uid").
  435. Join("LEFT", "class_with_user", "class_with_user.user_identity_id = user_identity.id").
  436. Join("LEFT", "class", "class_with_user.class_id = class.id").
  437. Join("LEFT", "grade", "class.grade_id = grade.id").
  438. FindAndCount(&m)
  439. if err != nil {
  440. logx.Error(err)
  441. println("<<<<CentralKitchenForSchoolDataStatisticsExport1>>>>>Error:::", err.Error())
  442. return
  443. }
  444. //查找预定数据
  445. var outTradeNoList []string
  446. for _, v := range m {
  447. outTradeNoList = append(outTradeNoList, v.OutTradeNo)
  448. }
  449. var mm []model.CentralKitchenForSchoolPackageOrdForReserve
  450. sess1 := db.Db.Desc("id")
  451. if req.EnterpriseId != 0 {
  452. sess1.And("enterprise_id = ?", req.EnterpriseId)
  453. }
  454. if len(req.MealKindList) > 0 {
  455. sess1.In("kind", req.MealKindList)
  456. }
  457. err = sess1.In("out_trade_no", outTradeNoList).Find(&mm)
  458. if err != nil {
  459. logx.Error(err)
  460. println("<<<<CentralKitchenForSchoolDataStatisticsExport1>>>>>Error:::", err.Error())
  461. return
  462. }
  463. var exportOutTradeNoList []string
  464. for _, v := range mm {
  465. exportOutTradeNoList = append(exportOutTradeNoList, v.OutTradeNo)
  466. }
  467. titleList = []string{startDate.Format("2006-01-02") + "~~" + endDate.Format("2006-01-02") + " 订单表"}
  468. xlsx.SetSheetRow("Sheet1", "A1", &titleList)
  469. titleList = []string{"订单号", "学生姓名", "学校", "年级", "班级", "家长电话", "预定日期", "预定餐数", "订单价格", "支付时间", "订单状态"}
  470. xlsx.SetSheetRow("Sheet1", "A2", &titleList)
  471. xlsx.MergeCell("Sheet1", "A1", "K1") //合并单元格
  472. style, err := xlsx.NewStyle(`{"fill":{"type":"pattern","color":["FFFF00"],"pattern":1}, "alignment":{"horizontal":"center"}}`)
  473. if err != nil {
  474. logx.Error(err)
  475. println("<<<<CentralKitchenForSchoolDataStatisticsExport3>>>>>Error:::", err.Error())
  476. panic(err)
  477. }
  478. xlsx.SetCellStyle("Sheet1", "A1", "K1", style)
  479. xlsx.SetColWidth("Sheet1", "A", "k", 20)
  480. j := 3 //表头前两行用了,只能从第三行开始
  481. for _, v := range m {
  482. if !utils.InArr(v.OutTradeNo, exportOutTradeNoList) {
  483. continue
  484. }
  485. var ordState string //订单状态(0:待支付 1:预约成功 2:退款中 3:部分退款 4:已退款 5:已完成)
  486. switch v.CentralKitchenForSchoolPackageOrd.OrdState {
  487. case enum.CentralKitchenForSchoolPackageOrdOrdStateForWait:
  488. ordState = enum.CentralKitchenForSchoolPackageOrdOrdState.String(enum.CentralKitchenForSchoolPackageOrdOrdStateForWait)
  489. break
  490. case enum.CentralKitchenForSchoolPackageOrdOrdStateForSuccess:
  491. ordState = enum.CentralKitchenForSchoolPackageOrdOrdState.String(enum.CentralKitchenForSchoolPackageOrdOrdStateForSuccess)
  492. break
  493. case enum.CentralKitchenForSchoolPackageOrdOrdStateForRefunding:
  494. ordState = enum.CentralKitchenForSchoolPackageOrdOrdState.String(enum.CentralKitchenForSchoolPackageOrdOrdStateForRefunding)
  495. break
  496. case enum.CentralKitchenForSchoolPackageOrdOrdStateForPartRefunded:
  497. ordState = enum.CentralKitchenForSchoolPackageOrdOrdState.String(enum.CentralKitchenForSchoolPackageOrdOrdStateForPartRefunded)
  498. break
  499. case enum.CentralKitchenForSchoolPackageOrdOrdStateForRefunded:
  500. ordState = enum.CentralKitchenForSchoolPackageOrdOrdState.String(enum.CentralKitchenForSchoolPackageOrdOrdStateForRefunded)
  501. break
  502. case enum.CentralKitchenForSchoolPackageOrdOrdStateForComplete:
  503. ordState = enum.CentralKitchenForSchoolPackageOrdOrdState.String(enum.CentralKitchenForSchoolPackageOrdOrdStateForComplete)
  504. break
  505. }
  506. //预定餐数、预定日期
  507. var list []*model.CentralKitchenForSchoolUserWithDay
  508. err1 := json.Unmarshal([]byte(v.CentralKitchenForSchoolPackageOrd.WithDayData), &list)
  509. if err1 != nil {
  510. logx.Error(err)
  511. println("<<<<CentralKitchenForSchoolDataStatisticsExport1>>>>>Error:::", err.Error())
  512. return
  513. }
  514. var reserveDays string
  515. for _, v1 := range list {
  516. reserveDays += v1.Date + ", "
  517. }
  518. if v.Grade.Name == "" {
  519. v.Grade.Name = "教师"
  520. }
  521. if v.Class.Name == "" {
  522. v.Class.Name = "教师"
  523. }
  524. xlsx.SetSheetRow("Sheet1", "A"+strconv.Itoa(j), &[]interface{}{v.CentralKitchenForSchoolPackageOrd.OutTradeNo, v.CentralKitchenForSchoolPackageOrd.UserIdentityName, v.Enterprise.Name,
  525. v.Grade.Name, v.Class.Name, v.User.Phone, reserveDays, len(list), v.CentralKitchenForSchoolPackageOrd.TotalPrice, v.CentralKitchenForSchoolPackageOrd.CreateAt, ordState})
  526. j++
  527. }
  528. //将文件保存至服务器
  529. xlsx.SaveAs("./static" + downloadPath)
  530. break
  531. case 2:
  532. //退款申请表
  533. fileName = "退款申请表_" + utils.Int64ToStr(now.UnixMilli()) + ".xlsx"
  534. downloadPath = "/export/central_kitchen_for_school/" + fileName
  535. startDate, _ := time.ParseInLocation("2006-01-02 15:04:05", req.StartDate, time.Local)
  536. endDate, _ := time.ParseInLocation("2006-01-02 15:04:05", req.EndDate, time.Local)
  537. //查找退款申请数据
  538. sess := db.Db.Desc("central_kitchen_for_school_user_refund_day.id")
  539. if req.EnterpriseId != 0 {
  540. sess.And("enterprise.id =?", req.EnterpriseId)
  541. }
  542. if req.StartDate != "" {
  543. sess.And("central_kitchen_for_school_user_refund_day.create_at >= ?", req.StartDate)
  544. }
  545. if req.EndDate != "" {
  546. sess.And("central_kitchen_for_school_user_refund_day.create_at <= ?", req.EndDate)
  547. }
  548. if len(req.MealKindList) > 0 {
  549. sess.In("central_kitchen_for_school_user_with_day.kind", req.MealKindList)
  550. }
  551. if req.EnterPriseKind != 0 {
  552. sess.And("enterprise.kind = ?", req.EnterPriseKind)
  553. }
  554. var m []*db.CentralKitchenForSchoolUserRefundDayWithData
  555. _, err := sess.
  556. 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").
  557. 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").
  558. Join("LEFT", "user_identity", "central_kitchen_for_school_user_with_day.identity_id = user_identity.id").
  559. Join("LEFT", "user", "user_identity.uid = user.id").
  560. Join("LEFT", "enterprise", "user_identity.enterprise_id = enterprise.id").
  561. Join("LEFT", "class_with_user", "user_identity.id = class_with_user.user_identity_id").
  562. Join("LEFT", "class", "class_with_user.class_id = class.id").
  563. Join("LEFT", "grade", "class.grade_id = grade.id").
  564. Desc("central_kitchen_for_school_user_refund_day.id").
  565. FindAndCount(&m)
  566. if err != nil {
  567. logx.Error(err)
  568. println("<<<<CentralKitchenForSchoolDataStatisticsExport2>>>>>Error:::", err.Error())
  569. return
  570. }
  571. titleList = []string{startDate.Format("2006-01-02") + "~~" + endDate.Format("2006-01-02") + " 退款申请表"}
  572. xlsx.SetSheetRow("Sheet1", "A1", &titleList)
  573. titleList = []string{"学生姓名", "学校", "年级", "班级", "家长电话", "退款日期", "餐型", "退款金额", "退款状态", "支付时间", "退款操作时间"}
  574. xlsx.SetSheetRow("Sheet1", "A2", &titleList)
  575. xlsx.MergeCell("Sheet1", "A1", "K1") //合并单元格
  576. style, err := xlsx.NewStyle(`{"fill":{"type":"pattern","color":["FFFF00"],"pattern":1}, "alignment":{"horizontal":"center"}}`)
  577. if err != nil {
  578. logx.Error(err)
  579. println("<<<<CentralKitchenForSchoolDataStatisticsExport3>>>>>Error:::", err.Error())
  580. panic(err)
  581. }
  582. xlsx.SetCellStyle("Sheet1", "A1", "K1", style)
  583. xlsx.SetColWidth("Sheet1", "A", "k", 20)
  584. j := 3 //表头前两行用了,只能从第三行开始
  585. for _, v := range m {
  586. var kind string //就餐类型(1:早餐 2:午餐 3:晚餐)
  587. switch v.CentralKitchenForSchoolUserWithDay.Kind {
  588. case enum.CentralKitchenForSchoolUserWithDayKindForBreakfast:
  589. kind = enum.CentralKitchenForSchoolUserWithDayKind.String(enum.CentralKitchenForSchoolUserWithDayKindForBreakfast)
  590. break
  591. case enum.CentralKitchenForSchoolUserWithDayKindForLunch:
  592. kind = enum.CentralKitchenForSchoolUserWithDayKind.String(enum.CentralKitchenForSchoolUserWithDayKindForLunch)
  593. break
  594. case enum.CentralKitchenForSchoolUserWithDayKindForDinner:
  595. kind = enum.CentralKitchenForSchoolUserWithDayKind.String(enum.CentralKitchenForSchoolUserWithDayKindForDinner)
  596. break
  597. }
  598. var state string //状态(1:审核中 2:审核通过 3:审核拒绝 4:退款已完成)
  599. switch v.CentralKitchenForSchoolUserRefundDay.State {
  600. case enum.CentralKitchenForSchoolUserRefundDayStateForAuditing:
  601. state = enum.CentralKitchenForSchoolUserRefundDayState.String(enum.CentralKitchenForSchoolUserRefundDayStateForAuditing)
  602. break
  603. case enum.CentralKitchenForSchoolUserRefundDayStateForAuditPass:
  604. state = enum.CentralKitchenForSchoolUserRefundDayState.String(enum.CentralKitchenForSchoolUserRefundDayStateForAuditPass)
  605. break
  606. case enum.CentralKitchenForSchoolUserRefundDayStateForAuditReject:
  607. state = enum.CentralKitchenForSchoolUserRefundDayState.String(enum.CentralKitchenForSchoolUserRefundDayStateForAuditReject)
  608. break
  609. case enum.CentralKitchenForSchoolUserRefundDayStateForAuditComplete:
  610. state = enum.CentralKitchenForSchoolUserRefundDayState.String(enum.CentralKitchenForSchoolUserRefundDayStateForAuditComplete)
  611. break
  612. }
  613. xlsx.SetSheetRow("Sheet1", "A"+strconv.Itoa(j), &[]interface{}{v.CentralKitchenForSchoolPackageOrd.UserIdentityName, v.Enterprise.Name, v.Grade.Name, v.Class.Name, v.User.Phone,
  614. v.CentralKitchenForSchoolUserWithDay.Date, kind, v.CentralKitchenForSchoolUserRefundDay.Amount, state, v.CentralKitchenForSchoolPackageOrd.CreateAt, v.CentralKitchenForSchoolUserRefundDay.RefundDate})
  615. j++
  616. }
  617. //将文件保存至服务器
  618. xlsx.SaveAs("./static" + downloadPath)
  619. break
  620. case 3:
  621. //学校预定统计表
  622. fileName = "学校预定统计表_" + utils.Int64ToStr(now.UnixMilli()) + ".zip"
  623. downloadPath = "/export/central_kitchen_for_school/" + fileName
  624. var m []*db.CentralKitchenForSchoolPackageOrdForReserveWithUserIdentity
  625. sess := db.Db.Desc("central_kitchen_for_school_package_ord_for_reserve.id")
  626. if req.EnterpriseId != 0 {
  627. sess.And("central_kitchen_for_school_package_ord_for_reserve.enterprise_id =?", req.EnterpriseId)
  628. }
  629. var months []time.Month
  630. if req.StartDate != "" {
  631. sDate, _ := time.ParseInLocation("2006-01-02 15:04:05", req.StartDate, time.Local)
  632. eDate, _ := time.ParseInLocation("2006-01-02 15:04:05", req.EndDate, time.Local)
  633. _, sMonth, _ := sDate.Date()
  634. _, eMonth, _ := eDate.Date()
  635. diffMonth := sMonth - eMonth
  636. if diffMonth == 0 {
  637. //TODO::查询一个月
  638. months = append(months, sMonth)
  639. } else {
  640. for i := sMonth; i <= eMonth; i++ {
  641. months = append(months, i)
  642. }
  643. }
  644. }
  645. if len(months) > 0 {
  646. sess.In("central_kitchen_for_school_package_ord_for_reserve.reserve_month", months)
  647. }
  648. //if req.EndDate != "" {
  649. // sess.And("central_kitchen_for_school_package_ord_for_reserve.meal_time_start <= ?", req.EndDate)
  650. //}
  651. if len(req.MealKindList) > 0 {
  652. sess.In("central_kitchen_for_school_package_ord_for_reserve.kind", req.MealKindList)
  653. }
  654. if req.EnterPriseKind != 0 {
  655. sess.And("enterprise.kind = ?", req.EnterPriseKind)
  656. }
  657. _, err := sess.
  658. 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").
  659. Join("LEFT", "user_identity", "central_kitchen_for_school_package_ord.user_identity_id = user_identity.id").
  660. Join("LEFT", "enterprise", "enterprise.id = user_identity.enterprise_id").
  661. Join("LEFT", "user", "user.id = user_identity.uid").
  662. Join("LEFT", "class_with_user", "class_with_user.user_identity_id = user_identity.id").
  663. Join("LEFT", "class", "class_with_user.class_id = class.id").
  664. Join("LEFT", "grade", "class.grade_id = grade.id").
  665. FindAndCount(&m)
  666. if err != nil {
  667. logx.Error(err)
  668. println("<<<<SelfSupportForSchoolDataStatisticsExport3>>>>>Error:::", err.Error())
  669. return
  670. }
  671. centralKitchenForSchoolUserWithDayDb := db.CentralKitchenForSchoolUserWithDayDb{}
  672. centralKitchenForSchoolUserWithDayDb.Set(0)
  673. var list = map[string]map[string]map[string]map[string]int64{}
  674. var xlsFileName, xlsDownloadPath []string
  675. for _, v := range m {
  676. if list[v.Enterprise.Name] == nil {
  677. list[v.Enterprise.Name] = make(map[string]map[string]map[string]int64)
  678. }
  679. if list[v.Enterprise.Name][v.Grade.Name] == nil {
  680. list[v.Enterprise.Name][v.Grade.Name] = make(map[string]map[string]int64)
  681. }
  682. if list[v.Enterprise.Name][v.Grade.Name][v.Class.Name] == nil {
  683. list[v.Enterprise.Name][v.Grade.Name][v.Class.Name] = make(map[string]int64)
  684. }
  685. count, err2 := centralKitchenForSchoolUserWithDayDb.CountCentralKitchenForSchoolUserWithDayByOrdNo(
  686. v.CentralKitchenForSchoolPackageOrdForReserve.OutTradeNo, enum.CentralKitchenForSchoolUserWithDayStateForCancel,
  687. req.StartDate, req.EndDate, req.MealKindList)
  688. if err2 != nil {
  689. logx.Error(err)
  690. println("<<<<SelfSupportForSchoolDataStatisticsExport3>>>>>Error:::", err.Error())
  691. return
  692. }
  693. time.Sleep(20 * time.Millisecond) // 20毫秒
  694. if v.CentralKitchenForSchoolPackageOrdForReserve.Kind == enum.CentralKitchenForSchoolUserWithDayKindForBreakfast {
  695. list[v.Enterprise.Name][v.Grade.Name][v.Class.Name]["breakfast"] += count
  696. }
  697. if v.CentralKitchenForSchoolPackageOrdForReserve.Kind == enum.CentralKitchenForSchoolUserWithDayKindForLunch {
  698. list[v.Enterprise.Name][v.Grade.Name][v.Class.Name]["lunch"] += count
  699. }
  700. if v.CentralKitchenForSchoolPackageOrdForReserve.Kind == enum.CentralKitchenForSchoolUserWithDayKindForDinner {
  701. list[v.Enterprise.Name][v.Grade.Name][v.Class.Name]["dinner"] += count
  702. }
  703. }
  704. for k1, v1 := range list {
  705. tmpFileName := k1 + "学校预定统计表_" + utils.Int64ToStr(now.UnixMilli()) + ".xlsx"
  706. tmpDownloadPath := "./static/export/central_kitchen_for_school/" + tmpFileName
  707. startDate, _ := time.ParseInLocation("2006-01-02 15:04:05", req.StartDate, time.Local)
  708. endDate, _ := time.ParseInLocation("2006-01-02 15:04:05", req.EndDate, time.Local)
  709. titleList = []string{startDate.Format("2006-01-02") + "~~" + endDate.Format("2006-01-02") + " (" + k1 + ") "}
  710. xlsx.SetSheetRow("Sheet1", "A1", &titleList)
  711. titleList = []string{"年级", "班级", "早餐", "午餐", "晚餐"}
  712. xlsx.SetSheetRow("Sheet1", "A2", &titleList)
  713. xlsx.MergeCell("Sheet1", "A1", "E1") //合并单元格
  714. style, err := xlsx.NewStyle(`{"fill":{"type":"pattern","color":["FFFF00"],"pattern":1}, "alignment":{"horizontal":"center"}}`)
  715. if err != nil {
  716. logx.Error(err)
  717. println("<<<<CentralKitchenForSchoolDataStatisticsExport3>>>>>Error:::", err.Error())
  718. panic(err)
  719. }
  720. xlsx.SetCellStyle("Sheet1", "A1", "E1", style)
  721. xlsx.SetColWidth("Sheet1", "A", "E", 20)
  722. j := 3 //表头前两行用了,只能从第三行开始
  723. var sumClassNum, sumBreakfast, sumLunch, sumDinner = int64(0), int64(0), int64(0), int64(0)
  724. tmpEnterpriseDb := db.EnterpriseDb{}
  725. tmpEnterpriseDb.Set()
  726. tmpEnterprise, err11 := tmpEnterpriseDb.GetEnterpriseByName(k1)
  727. if err11 != nil {
  728. logx.Error(err11)
  729. println("<<<<CentralKitchenForSchoolDataStatisticsExport3>>>>>Error:::", err11.Error())
  730. panic(err11)
  731. }
  732. tmpClassDb := db.ClassDb{}
  733. tmpClassDb.Set(0)
  734. classList, err := tmpClassDb.FindClassAscByEnterprise(tmpEnterprise.Id)
  735. if err != nil {
  736. logx.Error(err)
  737. println("<<<<CentralKitchenForSchoolDataStatisticsExport4>>>>>Error:::", err.Error())
  738. panic(err)
  739. }
  740. for _, class := range *classList {
  741. for k2, v2 := range v1 {
  742. if k2 == "" {
  743. //若年级为空,则为教师餐
  744. k2 = "教师餐"
  745. }
  746. for k3, v3 := range v2 {
  747. if class.Name != k3 {
  748. continue
  749. }
  750. if k3 == "" {
  751. //若班级为空,则为教师餐
  752. k3 = "教师餐"
  753. }
  754. xlsx.SetSheetRow("Sheet1", "A"+strconv.Itoa(j), &[]interface{}{
  755. k2, k3, v3["breakfast"], v3["lunch"], v3["dinner"]})
  756. sumClassNum++
  757. sumBreakfast += v3["breakfast"]
  758. sumLunch += v3["lunch"]
  759. sumDinner += v3["dinner"]
  760. j++
  761. }
  762. }
  763. }
  764. xlsx.SetSheetRow("Sheet1", "A"+strconv.Itoa(j), &[]interface{}{
  765. "合计", sumClassNum, sumBreakfast, sumLunch, sumDinner})
  766. //将文件保存至服务器
  767. err1 := xlsx.SaveAs(tmpDownloadPath)
  768. if err1 != nil {
  769. logx.Error(err1)
  770. println("<<<<CentralKitchenForSchoolDataStatisticsExport3>>>>>Error:::", err1.Error())
  771. panic(err1)
  772. }
  773. xlsDownloadPath = append(xlsDownloadPath, tmpDownloadPath)
  774. xlsFileName = append(xlsFileName, tmpFileName)
  775. }
  776. archive, err := os.Create("./static" + downloadPath) //第一步,创建 zip 文件
  777. if err != nil {
  778. logx.Error(err)
  779. println("<<<<CentralKitchenForSchoolDataStatisticsExport3>>>>>Error:::", err.Error())
  780. panic(err)
  781. }
  782. defer archive.Close()
  783. zipWriter := zip.NewWriter(archive) //第二步,创建一个新的 *Writer 对象
  784. for k, v := range xlsDownloadPath {
  785. w, err1 := zipWriter.Create("学校预定统计/" + xlsFileName[k]) //向 zip 文件中添加一个文件,返回一个待压缩的文件内容应写入的 Writer
  786. if err1 != nil {
  787. logx.Error(err1)
  788. println("<<<<CentralKitchenForSchoolDataStatisticsExport3>>>>>Error:::", err1.Error())
  789. panic(err1)
  790. }
  791. f, err1 := os.Open(v) //打开待压缩的文件
  792. if err1 != nil {
  793. logx.Error(err1)
  794. println("<<<<CentralKitchenForSchoolDataStatisticsExport3>>>>>Error:::", err1.Error())
  795. panic(err1)
  796. }
  797. if _, err1 = io.Copy(w, f); err != nil {
  798. logx.Error(err1)
  799. println("<<<<CentralKitchenForSchoolDataStatisticsExport3>>>>>Error:::", err1.Error())
  800. panic(err1)
  801. }
  802. if err1 != nil {
  803. logx.Error(err1)
  804. println("<<<<CentralKitchenForSchoolDataStatisticsExport3>>>>>Error:::", err1.Error())
  805. panic(err1)
  806. }
  807. f.Close()
  808. os.RemoveAll(v) //TODO::移除源文件
  809. }
  810. zipWriter.Close() // 第四步,关闭 zip writer,将所有数据写入指向基础 zip 文件的数据流
  811. if err != nil {
  812. panic(err)
  813. }
  814. break
  815. case 4:
  816. //班级明细表
  817. fileName = "班级明细表_" + utils.Int64ToStr(now.UnixMilli()) + ".zip"
  818. downloadPath = "/export/central_kitchen_for_school/" + fileName
  819. var m []*db.CentralKitchenForSchoolPackageOrdForReserveWithUserIdentity
  820. sess := db.Db.Desc("central_kitchen_for_school_package_ord_for_reserve.id")
  821. if req.EnterpriseId != 0 {
  822. sess.And("central_kitchen_for_school_package_ord_for_reserve.enterprise_id =?", req.EnterpriseId)
  823. }
  824. var months []time.Month
  825. if req.StartDate != "" {
  826. sDate, _ := time.ParseInLocation("2006-01-02 15:04:05", req.StartDate, time.Local)
  827. eDate, _ := time.ParseInLocation("2006-01-02 15:04:05", req.EndDate, time.Local)
  828. _, sMonth, _ := sDate.Date()
  829. _, eMonth, _ := eDate.Date()
  830. diffMonth := sMonth - eMonth
  831. if diffMonth == 0 {
  832. //TODO::查询一个月
  833. months = append(months, sMonth)
  834. } else {
  835. for i := sMonth; i <= eMonth; i++ {
  836. months = append(months, i)
  837. }
  838. }
  839. }
  840. if len(months) > 0 {
  841. sess.In("central_kitchen_for_school_package_ord_for_reserve.reserve_month", months)
  842. }
  843. //if req.EndDate != "" {
  844. // sess.And("central_kitchen_for_school_package_ord_for_reserve.meal_time_start <= ?", req.EndDate)
  845. //}
  846. if len(req.MealKindList) > 0 {
  847. sess.In("central_kitchen_for_school_package_ord_for_reserve.kind", req.MealKindList)
  848. }
  849. if req.EnterPriseKind != 0 {
  850. sess.And("enterprise.kind = ?", req.EnterPriseKind)
  851. }
  852. _, err := sess.
  853. 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").
  854. Join("LEFT", "user_identity", "central_kitchen_for_school_package_ord.user_identity_id = user_identity.id").
  855. Join("LEFT", "enterprise", "enterprise.id = user_identity.enterprise_id").
  856. Join("LEFT", "user", "user.id = user_identity.uid").
  857. Join("LEFT", "class_with_user", "class_with_user.user_identity_id = user_identity.id").
  858. Join("LEFT", "class", "class_with_user.class_id = class.id").
  859. Join("LEFT", "grade", "class.grade_id = grade.id").
  860. Asc("class.id").
  861. FindAndCount(&m)
  862. if err != nil {
  863. logx.Error(err)
  864. println("<<<<CentralKitchenForSchoolDataStatisticsExport4>>>>>Error:::", err.Error())
  865. return
  866. }
  867. var list = map[string]map[string]map[string]map[string]map[string]int64{}
  868. var xlsFileName, xlsDownloadPath []string
  869. centralKitchenForSchoolUserWithDayDb := db.CentralKitchenForSchoolUserWithDayDb{}
  870. centralKitchenForSchoolUserWithDayDb.Set(0)
  871. for _, v := range m {
  872. if list[v.Enterprise.Name] == nil {
  873. list[v.Enterprise.Name] = make(map[string]map[string]map[string]map[string]int64)
  874. }
  875. if list[v.Enterprise.Name][v.Grade.Name] == nil {
  876. list[v.Enterprise.Name][v.Grade.Name] = make(map[string]map[string]map[string]int64)
  877. }
  878. if list[v.Enterprise.Name][v.Grade.Name][v.Class.Name] == nil {
  879. list[v.Enterprise.Name][v.Grade.Name][v.Class.Name] = make(map[string]map[string]int64)
  880. }
  881. if list[v.Enterprise.Name][v.Grade.Name][v.Class.Name][v.UserIdentity.Name] == nil {
  882. list[v.Enterprise.Name][v.Grade.Name][v.Class.Name][v.UserIdentity.Name] = make(map[string]int64)
  883. }
  884. count, err2 := centralKitchenForSchoolUserWithDayDb.CountCentralKitchenForSchoolUserWithDayByOrdNo(
  885. v.CentralKitchenForSchoolPackageOrdForReserve.OutTradeNo, enum.CentralKitchenForSchoolUserWithDayStateForCancel,
  886. req.StartDate, req.EndDate, req.MealKindList)
  887. if err2 != nil {
  888. logx.Error(err)
  889. println("<<<<SelfSupportForSchoolDataStatisticsExport3>>>>>Error:::", err.Error())
  890. return
  891. }
  892. if count == 0 {
  893. continue
  894. }
  895. time.Sleep(20 * time.Millisecond) // 20毫秒
  896. if v.CentralKitchenForSchoolPackageOrdForReserve.Kind == enum.CentralKitchenForSchoolUserWithDayKindForBreakfast {
  897. list[v.Enterprise.Name][v.Grade.Name][v.Class.Name][v.UserIdentity.Name]["breakfast"] += count
  898. }
  899. if v.CentralKitchenForSchoolPackageOrdForReserve.Kind == enum.CentralKitchenForSchoolUserWithDayKindForLunch {
  900. list[v.Enterprise.Name][v.Grade.Name][v.Class.Name][v.UserIdentity.Name]["lunch"] += count
  901. }
  902. if v.CentralKitchenForSchoolPackageOrdForReserve.Kind == enum.CentralKitchenForSchoolUserWithDayKindForDinner {
  903. list[v.Enterprise.Name][v.Grade.Name][v.Class.Name][v.UserIdentity.Name]["dinner"] += count
  904. }
  905. }
  906. for k1, v1 := range list {
  907. tmpFileName := k1 + "班级明细_" + utils.Int64ToStr(now.UnixMilli()) + ".xlsx"
  908. tmpDownloadPath := "./static/export/central_kitchen_for_school/" + tmpFileName
  909. startDate, _ := time.ParseInLocation("2006-01-02 15:04:05", req.StartDate, time.Local)
  910. endDate, _ := time.ParseInLocation("2006-01-02 15:04:05", req.EndDate, time.Local)
  911. //查询当前学校下的班级升序
  912. tmpEnterpriseDb := db.EnterpriseDb{}
  913. tmpEnterpriseDb.Set()
  914. tmpEnterprise, err11 := tmpEnterpriseDb.GetEnterpriseByName(k1)
  915. if err11 != nil {
  916. logx.Error(err11)
  917. println("<<<<CentralKitchenForSchoolDataStatisticsExport4>>>>>Error:::", err11.Error())
  918. panic(err11)
  919. }
  920. tmpClassDb := db.ClassDb{}
  921. tmpClassDb.Set(0)
  922. classList, err := tmpClassDb.FindClassAscByEnterprise(tmpEnterprise.Id)
  923. if err != nil {
  924. logx.Error(err)
  925. println("<<<<CentralKitchenForSchoolDataStatisticsExport4>>>>>Error:::", err.Error())
  926. panic(err)
  927. }
  928. for _, class := range *classList {
  929. for k2, v2 := range v1 {
  930. if k2 == "" {
  931. //若年级为空,则为教师餐
  932. continue
  933. }
  934. for k3, v3 := range v2 {
  935. if class.Name != k3 {
  936. continue
  937. }
  938. xlsx.NewSheet(k3)
  939. titleList = []string{startDate.Format("2006-01-02") + "~~" + endDate.Format("2006-01-02") + " [" + k1 + ", " + k3 + "] 班级明细"}
  940. xlsx.SetSheetRow(k3, "A1", &titleList)
  941. titleList = []string{"姓名", "早餐", "午餐", "晚餐"}
  942. xlsx.SetSheetRow(k3, "A2", &titleList)
  943. xlsx.MergeCell(k3, "A1", "D1") //合并单元格
  944. style, err := xlsx.NewStyle(`{"fill":{"type":"pattern","color":["FFFF00"],"pattern":1}, "alignment":{"horizontal":"center"}}`)
  945. if err != nil {
  946. logx.Error(err)
  947. println("<<<<CentralKitchenForSchoolDataStatisticsExport3>>>>>Error:::", err.Error())
  948. panic(err)
  949. }
  950. xlsx.SetCellStyle(k3, "A1", "D1", style)
  951. xlsx.SetColWidth(k3, "A", "D", 20)
  952. var sumBreakfast, sumLunch, sumDinner = int64(0), int64(0), int64(0)
  953. j := 3 //表头前两行用了,只能从第三行开始
  954. for k4, v4 := range v3 {
  955. if v4["breakfast"] == 0 && v4["lunch"] == 0 && v4["dinner"] == 0 {
  956. continue
  957. }
  958. xlsx.SetSheetRow(k3, "A"+strconv.Itoa(j), &[]interface{}{
  959. k4, v4["breakfast"], v4["lunch"], v4["dinner"]})
  960. sumBreakfast += v4["breakfast"]
  961. sumLunch += v4["lunch"]
  962. sumDinner += v4["dinner"]
  963. j++
  964. }
  965. xlsx.SetSheetRow(k3, "A"+strconv.Itoa(j), &[]interface{}{
  966. "合计", sumBreakfast, sumLunch, sumDinner})
  967. }
  968. }
  969. }
  970. //将文件保存至服务器
  971. xlsx.DeleteSheet("Sheet1") //TODO::只能放这里删除
  972. err1 := xlsx.SaveAs(tmpDownloadPath)
  973. if err1 != nil {
  974. logx.Error(err1)
  975. println("<<<<CentralKitchenForSchoolDataStatisticsExport4>>>>>Error:::", err1.Error())
  976. panic(err1)
  977. }
  978. xlsDownloadPath = append(xlsDownloadPath, tmpDownloadPath)
  979. xlsFileName = append(xlsFileName, tmpFileName)
  980. }
  981. archive, err := os.Create("./static" + downloadPath) //第一步,创建 zip 文件
  982. if err != nil {
  983. logx.Error(err)
  984. println("<<<<CentralKitchenForSchoolDataStatisticsExport4>>>>>Error:::", err.Error())
  985. panic(err)
  986. }
  987. defer archive.Close()
  988. zipWriter := zip.NewWriter(archive) //第二步,创建一个新的 *Writer 对象
  989. for k, v := range xlsDownloadPath {
  990. w, err1 := zipWriter.Create("班级明细/" + xlsFileName[k]) //向 zip 文件中添加一个文件,返回一个待压缩的文件内容应写入的 Writer
  991. if err1 != nil {
  992. logx.Error(err1)
  993. println("<<<<CentralKitchenForSchoolDataStatisticsExport4>>>>>Error:::", err1.Error())
  994. panic(err1)
  995. }
  996. f, err1 := os.Open(v) //打开待压缩的文件
  997. if err1 != nil {
  998. logx.Error(err1)
  999. println("<<<<CentralKitchenForSchoolDataStatisticsExport4>>>>>Error:::", err1.Error())
  1000. panic(err1)
  1001. }
  1002. if _, err1 = io.Copy(w, f); err != nil {
  1003. logx.Error(err1)
  1004. println("<<<<CentralKitchenForSchoolDataStatisticsExport4>>>>>Error:::", err1.Error())
  1005. panic(err1)
  1006. }
  1007. if err1 != nil {
  1008. logx.Error(err1)
  1009. println("<<<<CentralKitchenForSchoolDataStatisticsExport4>>>>>Error:::", err1.Error())
  1010. panic(err1)
  1011. }
  1012. f.Close()
  1013. os.RemoveAll(v) //TODO::移除源文件
  1014. }
  1015. zipWriter.Close() // 第四步,关闭 zip writer,将所有数据写入指向基础 zip 文件的数据流
  1016. if err != nil {
  1017. panic(err)
  1018. }
  1019. break
  1020. case 5:
  1021. //退款明细表
  1022. fileName = "退款明细表_" + utils.Int64ToStr(now.UnixMilli()) + ".zip"
  1023. downloadPath = "/export/central_kitchen_for_school/" + fileName
  1024. sess := db.Db.Where("central_kitchen_for_school_user_refund_day.state =?", enum.CentralKitchenForSchoolUserRefundDayStateForAuditComplete).Desc("central_kitchen_for_school_user_refund_day.id")
  1025. if req.EnterpriseId != 0 {
  1026. sess.And("enterprise.id =?", req.EnterpriseId)
  1027. }
  1028. if req.StartDate != "" {
  1029. sess.And("central_kitchen_for_school_user_refund_day.create_at >= ?", req.StartDate)
  1030. }
  1031. if req.EndDate != "" {
  1032. sess.And("central_kitchen_for_school_user_refund_day.create_at <= ?", req.EndDate)
  1033. }
  1034. if len(req.MealKindList) > 0 {
  1035. sess.In("central_kitchen_for_school_user_with_day.kind", req.MealKindList)
  1036. }
  1037. if req.EnterPriseKind != 0 {
  1038. sess.And("enterprise.kind = ?", req.EnterPriseKind)
  1039. }
  1040. var m []*db.CentralKitchenForSchoolUserRefundDayWithData
  1041. _, err := sess.
  1042. 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").
  1043. 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").
  1044. Join("LEFT", "user_identity", "central_kitchen_for_school_user_with_day.identity_id = user_identity.id").
  1045. Join("LEFT", "user", "user_identity.uid = user.id").
  1046. Join("LEFT", "enterprise", "user_identity.enterprise_id = enterprise.id").
  1047. Join("LEFT", "class_with_user", "user_identity.id = class_with_user.user_identity_id").
  1048. Join("LEFT", "class", "class_with_user.class_id = class.id").
  1049. Join("LEFT", "grade", "class.grade_id = grade.id").
  1050. Desc("central_kitchen_for_school_user_refund_day.id").
  1051. FindAndCount(&m)
  1052. if err != nil {
  1053. logx.Error(err)
  1054. println("<<<<CentralKitchenForSchoolDataStatisticsExport5>>>>>Error:::", err.Error())
  1055. return
  1056. }
  1057. var list = map[string]map[string]map[string]map[string]map[string]int{}
  1058. var xlsFileName, xlsDownloadPath []string
  1059. for _, v := range m {
  1060. if list[v.Enterprise.Name] == nil {
  1061. list[v.Enterprise.Name] = make(map[string]map[string]map[string]map[string]int)
  1062. }
  1063. if list[v.Enterprise.Name][v.Grade.Name] == nil {
  1064. list[v.Enterprise.Name][v.Grade.Name] = make(map[string]map[string]map[string]int)
  1065. }
  1066. if list[v.Enterprise.Name][v.Grade.Name][v.Class.Name] == nil {
  1067. list[v.Enterprise.Name][v.Grade.Name][v.Class.Name] = make(map[string]map[string]int)
  1068. }
  1069. if list[v.Enterprise.Name][v.Grade.Name][v.Class.Name][v.UserIdentity.Name] == nil {
  1070. list[v.Enterprise.Name][v.Grade.Name][v.Class.Name][v.UserIdentity.Name] = make(map[string]int)
  1071. }
  1072. //就餐类型(1:早餐 2:午餐 3:晚餐)
  1073. if v.CentralKitchenForSchoolUserWithDay.Kind == enum.CentralKitchenForSchoolUserWithDayKindForBreakfast {
  1074. list[v.Enterprise.Name][v.Grade.Name][v.Class.Name][v.UserIdentity.Name]["breakfast"]++
  1075. }
  1076. if v.CentralKitchenForSchoolUserWithDay.Kind == enum.CentralKitchenForSchoolUserWithDayKindForLunch {
  1077. list[v.Enterprise.Name][v.Grade.Name][v.Class.Name][v.UserIdentity.Name]["lunch"]++
  1078. }
  1079. if v.CentralKitchenForSchoolUserWithDay.Kind == enum.CentralKitchenForSchoolUserWithDayKindForDinner {
  1080. list[v.Enterprise.Name][v.Grade.Name][v.Class.Name][v.UserIdentity.Name]["dinner"]++
  1081. }
  1082. }
  1083. for k1, v1 := range list {
  1084. tmpFileName := k1 + "退款明细_" + utils.Int64ToStr(now.UnixMilli()) + ".xlsx"
  1085. tmpDownloadPath := "./static/export/central_kitchen_for_school/" + tmpFileName
  1086. startDate, _ := time.ParseInLocation("2006-01-02 15:04:05", req.StartDate, time.Local)
  1087. endDate, _ := time.ParseInLocation("2006-01-02 15:04:05", req.EndDate, time.Local)
  1088. for k2, v2 := range v1 {
  1089. if k2 == "" {
  1090. //若年级为空,则为教师餐
  1091. continue
  1092. }
  1093. for k3, v3 := range v2 {
  1094. xlsx.NewSheet(k3)
  1095. titleList = []string{startDate.Format("2006-01-02") + "~~" + endDate.Format("2006-01-02") + " [" + k1 + ", " + k3 + "] 退款明细"}
  1096. xlsx.SetSheetRow(k3, "A1", &titleList)
  1097. titleList = []string{"姓名", "早餐", "午餐", "晚餐"}
  1098. xlsx.SetSheetRow(k3, "A2", &titleList)
  1099. xlsx.MergeCell(k3, "A1", "D1") //合并单元格
  1100. style, err := xlsx.NewStyle(`{"fill":{"type":"pattern","color":["FFFF00"],"pattern":1}, "alignment":{"horizontal":"center"}}`)
  1101. if err != nil {
  1102. logx.Error(err)
  1103. println("<<<<CentralKitchenForSchoolDataStatisticsExport3>>>>>Error:::", err.Error())
  1104. panic(err)
  1105. }
  1106. xlsx.SetCellStyle(k3, "A1", "D1", style)
  1107. xlsx.SetColWidth(k3, "A", "D", 20)
  1108. var sumBreakfast, sumLunch, sumDinner = 0, 0, 0
  1109. j := 3 //表头前两行用了,只能从第三行开始
  1110. for k4, v4 := range v3 {
  1111. xlsx.SetSheetRow(k3, "A"+strconv.Itoa(j), &[]interface{}{
  1112. k4, v4["breakfast"], v4["lunch"], v4["dinner"]})
  1113. sumBreakfast += v4["breakfast"]
  1114. sumLunch += v4["lunch"]
  1115. sumDinner += v4["dinner"]
  1116. j++
  1117. }
  1118. xlsx.SetSheetRow(k3, "A"+strconv.Itoa(j), &[]interface{}{
  1119. "合计", sumBreakfast, sumLunch, sumDinner})
  1120. }
  1121. }
  1122. //将文件保存至服务器
  1123. xlsx.DeleteSheet("Sheet1") //TODO::只能放这里删除
  1124. err1 := xlsx.SaveAs(tmpDownloadPath)
  1125. if err1 != nil {
  1126. logx.Error(err1)
  1127. println("<<<<CentralKitchenForSchoolDataStatisticsExport5>>>>>Error:::", err1.Error())
  1128. panic(err1)
  1129. }
  1130. xlsDownloadPath = append(xlsDownloadPath, tmpDownloadPath)
  1131. xlsFileName = append(xlsFileName, tmpFileName)
  1132. }
  1133. archive, err := os.Create("./static" + downloadPath) //第一步,创建 zip 文件
  1134. if err != nil {
  1135. logx.Error(err)
  1136. println("<<<<CentralKitchenForSchoolDataStatisticsExport5>>>>>Error:::", err.Error())
  1137. panic(err)
  1138. }
  1139. defer archive.Close()
  1140. zipWriter := zip.NewWriter(archive) //第二步,创建一个新的 *Writer 对象
  1141. for k, v := range xlsDownloadPath {
  1142. w, err1 := zipWriter.Create("退款明细/" + xlsFileName[k]) //向 zip 文件中添加一个文件,返回一个待压缩的文件内容应写入的 Writer
  1143. if err1 != nil {
  1144. logx.Error(err1)
  1145. println("<<<<CentralKitchenForSchoolDataStatisticsExport5>>>>>Error:::", err1.Error())
  1146. panic(err1)
  1147. }
  1148. f, err1 := os.Open(v) //打开待压缩的文件
  1149. if err1 != nil {
  1150. logx.Error(err1)
  1151. println("<<<<CentralKitchenForSchoolDataStatisticsExport5>>>>>Error:::", err1.Error())
  1152. panic(err1)
  1153. }
  1154. if _, err1 = io.Copy(w, f); err != nil {
  1155. logx.Error(err1)
  1156. println("<<<<CentralKitchenForSchoolDataStatisticsExport5>>>>>Error:::", err1.Error())
  1157. panic(err1)
  1158. }
  1159. if err1 != nil {
  1160. logx.Error(err1)
  1161. println("<<<<CentralKitchenForSchoolDataStatisticsExport5>>>>>Error:::", err1.Error())
  1162. panic(err1)
  1163. }
  1164. f.Close()
  1165. os.RemoveAll(v) //TODO::移除源文件
  1166. }
  1167. zipWriter.Close() // 第四步,关闭 zip writer,将所有数据写入指向基础 zip 文件的数据流
  1168. if err != nil {
  1169. panic(err)
  1170. }
  1171. break
  1172. case 6:
  1173. //收款统计表
  1174. fileName = "收款统计表_" + utils.Int64ToStr(now.UnixMilli()) + ".xlsx"
  1175. downloadPath = "/export/central_kitchen_for_school/" + fileName
  1176. startDate, _ := time.ParseInLocation("2006-01-02 15:04:05", req.StartDate, time.Local)
  1177. endDate, _ := time.ParseInLocation("2006-01-02 15:04:05", req.EndDate, time.Local)
  1178. //查找数据
  1179. var m []model.Enterprise
  1180. sess := db.Db.Desc("id")
  1181. if req.EnterPriseKind != 0 {
  1182. sess.And("kind = ?", req.EnterPriseKind)
  1183. }
  1184. if req.EnterpriseId != 0 {
  1185. sess.And("id = ?", req.EnterpriseId)
  1186. }
  1187. err := sess.Find(&m)
  1188. if err != nil {
  1189. logx.Error(err)
  1190. println("<<<<CentralKitchenForSchoolDataStatisticsExport6>>>>>Error:::", err.Error())
  1191. return
  1192. }
  1193. titleList = []string{startDate.Format("2006-01-02") + "~~" + endDate.Format("2006-01-02") + " 收款统计表"}
  1194. xlsx.SetSheetRow("Sheet1", "A1", &titleList)
  1195. titleList = []string{"学校名称", "预定总费用", "退款总计", "实收费用", "预估费率"}
  1196. xlsx.SetSheetRow("Sheet1", "A2", &titleList)
  1197. xlsx.MergeCell("Sheet1", "A1", "E1") //合并单元格
  1198. style, err := xlsx.NewStyle(`{"fill":{"type":"pattern","color":["FFFF00"],"pattern":1}, "alignment":{"horizontal":"center"}}`)
  1199. if err != nil {
  1200. logx.Error(err)
  1201. println("<<<<CentralKitchenForSchoolDataStatisticsExport3>>>>>Error:::", err.Error())
  1202. panic(err)
  1203. }
  1204. xlsx.SetCellStyle("Sheet1", "A1", "E1", style)
  1205. xlsx.SetColWidth("Sheet1", "A", "E", 20)
  1206. j := 3 //表头前两行用了,只能从第三行开始
  1207. for _, v := range m {
  1208. var income, refund string
  1209. //统计收入
  1210. 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)
  1211. mapArr1, err := db.QueryNativeString(db.Db, sql1)
  1212. if err != nil {
  1213. logx.Error(err)
  1214. println("<<<<CentralKitchenForSchoolDataStatisticsExport6>>>>>Error:::", err.Error())
  1215. return
  1216. }
  1217. if mapArr1 != nil {
  1218. income = mapArr1[0]["total"]
  1219. }
  1220. //统计退款
  1221. //查询出当前学校下所有的学生
  1222. userIdentityDb := db.UserIdentityDb{}
  1223. userIdentityDb.Set(0)
  1224. userIdentities, _ := userIdentityDb.FindUserIdentityForEnterprise(v.Id)
  1225. var userIdentityIds []string
  1226. for _, v := range *userIdentities {
  1227. userIdentityIds = append(userIdentityIds, utils.IntToStr(v.Id))
  1228. }
  1229. if len(userIdentityIds) != 0 {
  1230. userIdentityStr := php2go.Implode(",", userIdentityIds)
  1231. 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'",
  1232. enum.CentralKitchenForSchoolUserRefundDayStateForAuditComplete, userIdentityStr, req.StartDate, req.EndDate)
  1233. mapArr2, err := db.QueryNativeString(db.Db, sql2)
  1234. if err != nil {
  1235. logx.Error(err)
  1236. println("<<<<CentralKitchenForSchoolDataStatisticsExport6>>>>>Error:::", err.Error())
  1237. return
  1238. }
  1239. if mapArr2 != nil {
  1240. refund = mapArr2[0]["total"]
  1241. }
  1242. }
  1243. realityAmount := utils.StrToFloat64(income) - utils.StrToFloat64(refund)
  1244. rateFee := realityAmount * 0.0038
  1245. xlsx.SetSheetRow("Sheet1", "A"+strconv.Itoa(j), &[]interface{}{v.Name, income, refund, realityAmount, rateFee})
  1246. j++
  1247. }
  1248. //将文件保存至服务器
  1249. xlsx.SaveAs("./static" + downloadPath)
  1250. break
  1251. case 7:
  1252. //班级收款明细表
  1253. fileName = "班级收款明细表_" + utils.Int64ToStr(now.UnixMilli()) + ".zip"
  1254. downloadPath = "/export/central_kitchen_for_school/" + fileName
  1255. //查找订单数据
  1256. sess := db.Db.Desc("central_kitchen_for_school_package_ord.id")
  1257. sess.And("central_kitchen_for_school_package_ord.state =?", enum.CentralKitchenForSchoolPackageOrdStateForSuccess).And("user_identity.identity = ?", enum.UserIdentityForCentralKitchenForStudent)
  1258. if req.EnterpriseId != 0 {
  1259. sess.And("central_kitchen_for_school_package_ord.enterprise_id =?", req.EnterpriseId)
  1260. }
  1261. if req.StartDate != "" {
  1262. sess.And("central_kitchen_for_school_package_ord.create_at >= ?", req.StartDate)
  1263. }
  1264. if req.EndDate != "" {
  1265. sess.And("central_kitchen_for_school_package_ord.create_at <= ?", req.EndDate)
  1266. }
  1267. if req.EnterPriseKind != 0 {
  1268. sess.And("enterprise.kind = ?", req.EnterPriseKind)
  1269. }
  1270. var m []*db.CentralKitchenForSchoolPackageOrdWithUserIdentity
  1271. _, err := sess.
  1272. Join("LEFT", "user_identity", "central_kitchen_for_school_package_ord.user_identity_id = user_identity.id").
  1273. Join("LEFT", "enterprise", "enterprise.id = user_identity.enterprise_id").
  1274. Join("LEFT", "user", "user.id = user_identity.uid").
  1275. Join("LEFT", "class_with_user", "class_with_user.user_identity_id = user_identity.id").
  1276. Join("LEFT", "class", "class_with_user.class_id = class.id").
  1277. Join("LEFT", "grade", "class.grade_id = grade.id").
  1278. FindAndCount(&m)
  1279. if err != nil {
  1280. logx.Error(err)
  1281. println("<<<<CentralKitchenForSchoolDataStatisticsExport7>>>>>Error:::", err.Error())
  1282. return
  1283. }
  1284. sess1 := db.Db.Where("central_kitchen_for_school_user_refund_day.state =?", enum.CentralKitchenForSchoolUserRefundDayStateForAuditComplete).And("user_identity.identity = ?", enum.UserIdentityForCentralKitchenForStudent).Desc("central_kitchen_for_school_user_refund_day.id")
  1285. if req.EnterpriseId != 0 {
  1286. sess1.And("enterprise.id =?", req.EnterpriseId)
  1287. }
  1288. if req.StartDate != "" {
  1289. sess1.And("central_kitchen_for_school_user_refund_day.create_at >= ?", req.StartDate)
  1290. }
  1291. if req.EndDate != "" {
  1292. sess1.And("central_kitchen_for_school_user_refund_day.create_at <= ?", req.EndDate)
  1293. }
  1294. var mm []*db.CentralKitchenForSchoolUserRefundDayWithData
  1295. _, err = sess1.
  1296. 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").
  1297. 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").
  1298. Join("LEFT", "user_identity", "central_kitchen_for_school_user_with_day.identity_id = user_identity.id").
  1299. Join("LEFT", "user", "user_identity.uid = user.id").
  1300. Join("LEFT", "enterprise", "user_identity.enterprise_id = enterprise.id").
  1301. Join("LEFT", "class_with_user", "user_identity.id = class_with_user.user_identity_id").
  1302. Join("LEFT", "class", "class_with_user.class_id = class.id").
  1303. Join("LEFT", "grade", "class.grade_id = grade.id").
  1304. Desc("central_kitchen_for_school_user_refund_day.id").
  1305. FindAndCount(&mm)
  1306. if err != nil {
  1307. logx.Error(err)
  1308. println("<<<<CentralKitchenForSchoolDataStatisticsExport7>>>>>Error:::", err.Error())
  1309. return
  1310. }
  1311. var list = map[string]map[string]map[string]map[string]float64{}
  1312. for _, v := range m {
  1313. if list[v.Enterprise.Name] == nil {
  1314. list[v.Enterprise.Name] = make(map[string]map[string]map[string]float64)
  1315. }
  1316. if list[v.Enterprise.Name][v.Grade.Name] == nil {
  1317. list[v.Enterprise.Name][v.Grade.Name] = make(map[string]map[string]float64)
  1318. }
  1319. if list[v.Enterprise.Name][v.Grade.Name][v.Class.Name] == nil {
  1320. list[v.Enterprise.Name][v.Grade.Name][v.Class.Name] = make(map[string]float64)
  1321. }
  1322. list[v.Enterprise.Name][v.Grade.Name][v.Class.Name]["income"] += utils.StrToFloat64(v.CentralKitchenForSchoolPackageOrd.TotalPrice)
  1323. }
  1324. for _, v := range mm {
  1325. if list[v.Enterprise.Name] == nil {
  1326. list[v.Enterprise.Name] = make(map[string]map[string]map[string]float64)
  1327. }
  1328. if list[v.Enterprise.Name][v.Grade.Name] == nil {
  1329. list[v.Enterprise.Name][v.Grade.Name] = make(map[string]map[string]float64)
  1330. }
  1331. if list[v.Enterprise.Name][v.Grade.Name][v.Class.Name] == nil {
  1332. list[v.Enterprise.Name][v.Grade.Name][v.Class.Name] = make(map[string]float64)
  1333. }
  1334. list[v.Enterprise.Name][v.Grade.Name][v.Class.Name]["refund"] += utils.StrToFloat64(v.CentralKitchenForSchoolUserRefundDay.Amount)
  1335. }
  1336. var xlsFileName, xlsDownloadPath []string
  1337. for k1, v1 := range list {
  1338. tmpFileName := k1 + "班级收款明细_" + utils.Int64ToStr(now.UnixMilli()) + ".xlsx"
  1339. tmpDownloadPath := "./static/export/central_kitchen_for_school/" + tmpFileName
  1340. startDate, _ := time.ParseInLocation("2006-01-02 15:04:05", req.StartDate, time.Local)
  1341. endDate, _ := time.ParseInLocation("2006-01-02 15:04:05", req.EndDate, time.Local)
  1342. titleList = []string{startDate.Format("2006-01-02") + "~~" + endDate.Format("2006-01-02") + " (" + k1 + ") 班级收款明细"}
  1343. xlsx.SetSheetRow("Sheet1", "A1", &titleList)
  1344. titleList = []string{"年级", "班级", "预定总费用", "退款总计"}
  1345. xlsx.SetSheetRow("Sheet1", "A2", &titleList)
  1346. xlsx.MergeCell("Sheet1", "A1", "D1") //合并单元格
  1347. style, err := xlsx.NewStyle(`{"fill":{"type":"pattern","color":["FFFF00"],"pattern":1}, "alignment":{"horizontal":"center"}}`)
  1348. if err != nil {
  1349. logx.Error(err)
  1350. println("<<<<CentralKitchenForSchoolDataStatisticsExport3>>>>>Error:::", err.Error())
  1351. panic(err)
  1352. }
  1353. xlsx.SetCellStyle("Sheet1", "A1", "D1", style)
  1354. xlsx.SetColWidth("Sheet1", "A", "D", 20)
  1355. j := 3 //表头前两行用了,只能从第三行开始
  1356. var sumClassNum = int64(0)
  1357. var sumIncome, sumRefund = float64(0), float64(0)
  1358. for k2, v2 := range v1 {
  1359. if k2 == "" {
  1360. //若年级为空,则为教师餐
  1361. continue
  1362. }
  1363. for k3, v3 := range v2 {
  1364. xlsx.SetSheetRow("Sheet1", "A"+strconv.Itoa(j), &[]interface{}{k2, k3, utils.Float64ToStr(v3["income"]), utils.Float64ToStr(v3["refund"])})
  1365. sumClassNum++
  1366. sumIncome += v3["income"]
  1367. sumRefund += v3["refund"]
  1368. j++
  1369. }
  1370. }
  1371. xlsx.SetSheetRow("Sheet1", "A"+strconv.Itoa(j), &[]interface{}{
  1372. "合计", sumClassNum, sumIncome, sumRefund})
  1373. //将文件保存至服务器
  1374. err1 := xlsx.SaveAs(tmpDownloadPath)
  1375. if err1 != nil {
  1376. logx.Error(err1)
  1377. println("<<<<CentralKitchenForSchoolDataStatisticsExport7>>>>>Error:::", err1.Error())
  1378. panic(err1)
  1379. }
  1380. xlsDownloadPath = append(xlsDownloadPath, tmpDownloadPath)
  1381. xlsFileName = append(xlsFileName, tmpFileName)
  1382. }
  1383. archive, err := os.Create("./static" + downloadPath) //第一步,创建 zip 文件
  1384. if err != nil {
  1385. logx.Error(err)
  1386. println("<<<<CentralKitchenForSchoolDataStatisticsExport7>>>>>Error:::", err.Error())
  1387. panic(err)
  1388. }
  1389. defer archive.Close()
  1390. zipWriter := zip.NewWriter(archive) //第二步,创建一个新的 *Writer 对象
  1391. for k, v := range xlsDownloadPath {
  1392. w, err1 := zipWriter.Create("班级收款明细/" + xlsFileName[k]) //向 zip 文件中添加一个文件,返回一个待压缩的文件内容应写入的 Writer
  1393. if err1 != nil {
  1394. logx.Error(err1)
  1395. println("<<<<CentralKitchenForSchoolDataStatisticsExport7>>>>>Error:::", err1.Error())
  1396. panic(err1)
  1397. }
  1398. f, err1 := os.Open(v) //打开待压缩的文件
  1399. if err1 != nil {
  1400. logx.Error(err1)
  1401. println("<<<<CentralKitchenForSchoolDataStatisticsExport7>>>>>Error:::", err1.Error())
  1402. panic(err1)
  1403. }
  1404. if _, err1 = io.Copy(w, f); err != nil {
  1405. logx.Error(err1)
  1406. println("<<<<CentralKitchenForSchoolDataStatisticsExport7>>>>>Error:::", err1.Error())
  1407. panic(err1)
  1408. }
  1409. if err1 != nil {
  1410. logx.Error(err1)
  1411. println("<<<<CentralKitchenForSchoolDataStatisticsExport7>>>>>Error:::", err1.Error())
  1412. panic(err1)
  1413. }
  1414. f.Close()
  1415. os.RemoveAll(v) //TODO::移除源文件
  1416. }
  1417. zipWriter.Close() // 第四步,关闭 zip writer,将所有数据写入指向基础 zip 文件的数据流
  1418. if err != nil {
  1419. panic(err)
  1420. }
  1421. break
  1422. case 8:
  1423. //学校预定数量统计表
  1424. fileName = "学校预定数量统计表_" + utils.Int64ToStr(now.UnixMilli()) + ".xlsx"
  1425. downloadPath = "/export/central_kitchen_for_school/" + fileName
  1426. startDate, _ := time.ParseInLocation("2006-01-02 15:04:05", req.StartDate, time.Local)
  1427. endDate, _ := time.ParseInLocation("2006-01-02 15:04:05", req.EndDate, time.Local)
  1428. var m []*db.CentralKitchenForSchoolPackageOrdForReserveWithUserIdentity
  1429. sess := db.Db.Desc("central_kitchen_for_school_package_ord_for_reserve.id")
  1430. if req.EnterpriseId != 0 {
  1431. sess.And("central_kitchen_for_school_package_ord_for_reserve.enterprise_id =?", req.EnterpriseId)
  1432. }
  1433. var months []time.Month
  1434. if req.StartDate != "" {
  1435. sDate, _ := time.ParseInLocation("2006-01-02 15:04:05", req.StartDate, time.Local)
  1436. eDate, _ := time.ParseInLocation("2006-01-02 15:04:05", req.EndDate, time.Local)
  1437. _, sMonth, _ := sDate.Date()
  1438. _, eMonth, _ := eDate.Date()
  1439. diffMonth := sMonth - eMonth
  1440. if diffMonth == 0 {
  1441. //TODO::查询一个月
  1442. months = append(months, sMonth)
  1443. } else {
  1444. for i := sMonth; i <= eMonth; i++ {
  1445. months = append(months, i)
  1446. }
  1447. }
  1448. }
  1449. if len(months) > 0 {
  1450. sess.In("central_kitchen_for_school_package_ord_for_reserve.reserve_month", months)
  1451. }
  1452. //if req.EndDate != "" {
  1453. // sess.And("central_kitchen_for_school_package_ord_for_reserve.meal_time_start <= ?", req.EndDate)
  1454. //}
  1455. if len(req.MealKindList) > 0 {
  1456. sess.In("central_kitchen_for_school_package_ord_for_reserve.kind", req.MealKindList)
  1457. }
  1458. if req.EnterPriseKind != 0 {
  1459. sess.And("enterprise.kind = ?", req.EnterPriseKind)
  1460. }
  1461. _, err := sess.
  1462. 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").
  1463. Join("LEFT", "user_identity", "central_kitchen_for_school_package_ord.user_identity_id = user_identity.id").
  1464. Join("LEFT", "enterprise", "enterprise.id = user_identity.enterprise_id").
  1465. FindAndCount(&m)
  1466. if err != nil {
  1467. logx.Error(err)
  1468. println("<<<<SelfSupportForSchoolDataStatisticsExport3>>>>>Error:::", err.Error())
  1469. return
  1470. }
  1471. var list = map[string]map[string]int64{}
  1472. centralKitchenForSchoolUserWithDayDb := db.CentralKitchenForSchoolUserWithDayDb{}
  1473. centralKitchenForSchoolUserWithDayDb.Set(0)
  1474. for _, v := range m {
  1475. if list[v.Enterprise.Name] == nil {
  1476. list[v.Enterprise.Name] = map[string]int64{}
  1477. }
  1478. count, err2 := centralKitchenForSchoolUserWithDayDb.CountCentralKitchenForSchoolUserWithDayByOrdNo(
  1479. v.CentralKitchenForSchoolPackageOrdForReserve.OutTradeNo, enum.CentralKitchenForSchoolUserWithDayStateForCancel,
  1480. req.StartDate, req.EndDate, req.MealKindList)
  1481. if err2 != nil {
  1482. logx.Error(err)
  1483. println("<<<<SelfSupportForSchoolDataStatisticsExport3>>>>>Error:::", err.Error())
  1484. return
  1485. }
  1486. count1, err2 := centralKitchenForSchoolUserWithDayDb.CountCentralKitchenForSchoolUserWithDayByOrdNoAndKind(v.CentralKitchenForSchoolPackageOrdForReserve.OutTradeNo, enum.CentralKitchenForSchoolUserWithDayKindForBreakfast, enum.CentralKitchenForSchoolUserWithDayStateForCancel)
  1487. if err2 != nil {
  1488. logx.Error(err)
  1489. println("<<<<SelfSupportForSchoolDataStatisticsExport3>>>>>Error:::", err.Error())
  1490. return
  1491. }
  1492. count2, err2 := centralKitchenForSchoolUserWithDayDb.CountCentralKitchenForSchoolUserWithDayByOrdNoAndKind(v.CentralKitchenForSchoolPackageOrdForReserve.OutTradeNo, enum.CentralKitchenForSchoolUserWithDayKindForLunch, enum.CentralKitchenForSchoolUserWithDayStateForCancel)
  1493. if err2 != nil {
  1494. logx.Error(err)
  1495. println("<<<<SelfSupportForSchoolDataStatisticsExport3>>>>>Error:::", err.Error())
  1496. return
  1497. }
  1498. count3, err2 := centralKitchenForSchoolUserWithDayDb.CountCentralKitchenForSchoolUserWithDayByOrdNoAndKind(v.CentralKitchenForSchoolPackageOrdForReserve.OutTradeNo, enum.CentralKitchenForSchoolUserWithDayKindForDinner, enum.CentralKitchenForSchoolUserWithDayStateForCancel)
  1499. if err2 != nil {
  1500. logx.Error(err)
  1501. println("<<<<SelfSupportForSchoolDataStatisticsExport3>>>>>Error:::", err.Error())
  1502. return
  1503. }
  1504. list[v.Enterprise.Name]["count"] += count
  1505. list[v.Enterprise.Name][enum.CentralKitchenForSchoolUserWithDayKind(enum.CentralKitchenForSchoolUserWithDayKindForBreakfast).String()] += count1
  1506. list[v.Enterprise.Name][enum.CentralKitchenForSchoolUserWithDayKind(enum.CentralKitchenForSchoolUserWithDayKindForLunch).String()] += count2
  1507. list[v.Enterprise.Name][enum.CentralKitchenForSchoolUserWithDayKind(enum.CentralKitchenForSchoolUserWithDayKindForDinner).String()] += count3
  1508. time.Sleep(20 * time.Millisecond) // 20毫秒
  1509. }
  1510. titleList = []string{startDate.Format("2006-01-02") + "~~" + endDate.Format("2006-01-02") + " 预定数量统计表"}
  1511. xlsx.SetSheetRow("Sheet1", "A1", &titleList)
  1512. titleList = []string{"学校名称", "预定总数",
  1513. enum.CentralKitchenForSchoolUserWithDayKind(enum.CentralKitchenForSchoolUserWithDayKindForBreakfast).String(),
  1514. enum.CentralKitchenForSchoolUserWithDayKind(enum.CentralKitchenForSchoolUserWithDayKindForLunch).String(),
  1515. enum.CentralKitchenForSchoolUserWithDayKind(enum.CentralKitchenForSchoolUserWithDayKindForDinner).String(),
  1516. }
  1517. xlsx.SetSheetRow("Sheet1", "A2", &titleList)
  1518. xlsx.MergeCell("Sheet1", "A1", "E1") //合并单元格
  1519. style, err := xlsx.NewStyle(`{"fill":{"type":"pattern","color":["FFFF00"],"pattern":1}, "alignment":{"horizontal":"center"}}`)
  1520. if err != nil {
  1521. logx.Error(err)
  1522. println("<<<<CentralKitchenForSchoolDataStatisticsExport3>>>>>Error:::", err.Error())
  1523. panic(err)
  1524. }
  1525. xlsx.SetCellStyle("Sheet1", "A1", "E1", style)
  1526. xlsx.SetColWidth("Sheet1", "A", "A", 30)
  1527. xlsx.SetColWidth("Sheet1", "B", "E", 15)
  1528. j := 3 //表头前两行用了,只能从第三行开始
  1529. total := int64(0)
  1530. total1 := int64(0)
  1531. total2 := int64(0)
  1532. total3 := int64(0)
  1533. for k, v := range list {
  1534. xlsx.SetSheetRow("Sheet1", "A"+strconv.Itoa(j), &[]interface{}{k, v["count"],
  1535. v[enum.CentralKitchenForSchoolUserWithDayKind(enum.CentralKitchenForSchoolUserWithDayKindForBreakfast).String()],
  1536. v[enum.CentralKitchenForSchoolUserWithDayKind(enum.CentralKitchenForSchoolUserWithDayKindForLunch).String()],
  1537. v[enum.CentralKitchenForSchoolUserWithDayKind(enum.CentralKitchenForSchoolUserWithDayKindForDinner).String()]})
  1538. total += v["count"]
  1539. total1 += v[enum.CentralKitchenForSchoolUserWithDayKind(enum.CentralKitchenForSchoolUserWithDayKindForBreakfast).String()]
  1540. total2 += v[enum.CentralKitchenForSchoolUserWithDayKind(enum.CentralKitchenForSchoolUserWithDayKindForLunch).String()]
  1541. total3 += v[enum.CentralKitchenForSchoolUserWithDayKind(enum.CentralKitchenForSchoolUserWithDayKindForDinner).String()]
  1542. j++
  1543. }
  1544. xlsx.SetSheetRow("Sheet1", "A"+strconv.Itoa(j), &[]interface{}{
  1545. "合计", total, total1, total2, total3})
  1546. //将文件保存至服务器
  1547. xlsx.SaveAs("./static" + downloadPath)
  1548. break
  1549. }
  1550. //新增数据
  1551. centralKitchenForSchoolExportRecordsDb := db.CentralKitchenForSchoolExportRecordsDb{}
  1552. centralKitchenForSchoolExportRecordsDb.Set()
  1553. marshal, _ := json.Marshal(req)
  1554. centralKitchenForSchoolExportRecordsDb.CentralKitchenForSchoolExportRecordsInsert(&model.CentralKitchenForSchoolExportRecords{
  1555. Name: fileName,
  1556. DownloadPath: downloadPath,
  1557. Kind: kind,
  1558. ReqContent: string(marshal),
  1559. CreateAt: now.Format("2006-01-02 15:04:05"),
  1560. UpdateAt: now.Format("2006-01-02 15:04:05"),
  1561. })
  1562. }
  1563. }
  1564. func CentralKitchenForSchoolDataStatisticsContrast(req md.CentralKitchenForSchoolDataStatisticsContrastReq) {
  1565. var titleList []string
  1566. var fileName, downloadPath string
  1567. var now = time.Now()
  1568. xlsx := excelize.NewFile()
  1569. //数据对比
  1570. fileName = "数据对比_" + utils.Int64ToStr(now.UnixMilli()) + ".zip"
  1571. downloadPath = "/export/central_kitchen_for_school/" + fileName
  1572. var m []*db.CentralKitchenForSchoolPackageOrdForReserveWithUserIdentity
  1573. sess := db.Db.Desc("central_kitchen_for_school_package_ord_for_reserve.id")
  1574. if req.EnterpriseId != 0 {
  1575. sess.And("central_kitchen_for_school_package_ord_for_reserve.enterprise_id =?", req.EnterpriseId)
  1576. }
  1577. sess.And("central_kitchen_for_school_package_ord_for_reserve.meal_time_start >= ?", req.Date1[0])
  1578. sess.And("central_kitchen_for_school_package_ord_for_reserve.meal_time_start <= ?", req.Date1[1])
  1579. if len(req.MealKindList) > 0 {
  1580. sess.In("central_kitchen_for_school_package_ord_for_reserve.kind", req.MealKindList)
  1581. }
  1582. if req.EnterPriseKind != 0 {
  1583. sess.And("enterprise.kind = ?", req.EnterPriseKind)
  1584. }
  1585. _, err := sess.
  1586. 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").
  1587. Join("LEFT", "user_identity", "central_kitchen_for_school_package_ord.user_identity_id = user_identity.id").
  1588. Join("LEFT", "enterprise", "enterprise.id = user_identity.enterprise_id").
  1589. Join("LEFT", "user", "user.id = user_identity.uid").
  1590. Join("LEFT", "class_with_user", "class_with_user.user_identity_id = user_identity.id").
  1591. Join("LEFT", "class", "class_with_user.class_id = class.id").
  1592. Join("LEFT", "grade", "class.grade_id = grade.id").
  1593. FindAndCount(&m)
  1594. if err != nil {
  1595. logx.Error(err)
  1596. println("<<<<CentralKitchenForSchoolDataStatisticsContrast>>>>>Error:::", err.Error())
  1597. return
  1598. }
  1599. var m1 []*db.CentralKitchenForSchoolPackageOrdForReserveWithUserIdentity
  1600. sess1 := db.Db.Desc("central_kitchen_for_school_package_ord_for_reserve.id")
  1601. if req.EnterpriseId != 0 {
  1602. sess1.And("central_kitchen_for_school_package_ord_for_reserve.enterprise_id =?", req.EnterpriseId)
  1603. }
  1604. sess1.And("central_kitchen_for_school_package_ord_for_reserve.meal_time_start >= ?", req.Date2[0])
  1605. sess1.And("central_kitchen_for_school_package_ord_for_reserve.meal_time_start <= ?", req.Date2[1])
  1606. if len(req.MealKindList) > 0 {
  1607. sess1.In("central_kitchen_for_school_package_ord_for_reserve.kind", req.MealKindList)
  1608. }
  1609. _, err1 := sess1.
  1610. 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").
  1611. Join("LEFT", "user_identity", "central_kitchen_for_school_package_ord.user_identity_id = user_identity.id").
  1612. Join("LEFT", "enterprise", "enterprise.id = user_identity.enterprise_id").
  1613. Join("LEFT", "user", "user.id = user_identity.uid").
  1614. Join("LEFT", "class_with_user", "class_with_user.user_identity_id = user_identity.id").
  1615. Join("LEFT", "class", "class_with_user.class_id = class.id").
  1616. Join("LEFT", "grade", "class.grade_id = grade.id").
  1617. FindAndCount(&m1)
  1618. if err1 != nil {
  1619. logx.Error(err1)
  1620. println("<<<<CentralKitchenForSchoolDataStatisticsContrast>>>>>Error:::", err.Error())
  1621. return
  1622. }
  1623. var list = map[string]map[string]map[string]map[string]string{}
  1624. for _, v := range m {
  1625. if list[v.Enterprise.Name] == nil {
  1626. list[v.Enterprise.Name] = make(map[string]map[string]map[string]string)
  1627. }
  1628. if list[v.Enterprise.Name][v.Grade.Name] == nil {
  1629. list[v.Enterprise.Name][v.Grade.Name] = make(map[string]map[string]string)
  1630. }
  1631. if list[v.Enterprise.Name][v.Grade.Name][v.Class.Name] == nil {
  1632. list[v.Enterprise.Name][v.Grade.Name][v.Class.Name] = make(map[string]string)
  1633. }
  1634. list[v.Enterprise.Name][v.Grade.Name][v.Class.Name][v.UserIdentity.Name] = v.User.Phone
  1635. }
  1636. var list1 = map[string]map[string]map[string]map[string]string{}
  1637. for _, v := range m1 {
  1638. if list1[v.Enterprise.Name] == nil {
  1639. list1[v.Enterprise.Name] = make(map[string]map[string]map[string]string)
  1640. }
  1641. if list1[v.Enterprise.Name][v.Grade.Name] == nil {
  1642. list1[v.Enterprise.Name][v.Grade.Name] = make(map[string]map[string]string)
  1643. }
  1644. if list1[v.Enterprise.Name][v.Grade.Name][v.Class.Name] == nil {
  1645. list1[v.Enterprise.Name][v.Grade.Name][v.Class.Name] = make(map[string]string)
  1646. }
  1647. list1[v.Enterprise.Name][v.Grade.Name][v.Class.Name][v.UserIdentity.Name] = v.User.Phone
  1648. }
  1649. var xlsFileName, xlsDownloadPath []string
  1650. for k1, v1 := range list {
  1651. tmpFileName := k1 + "数据对比_" + utils.Int64ToStr(now.UnixMilli()) + ".xlsx"
  1652. tmpDownloadPath := "./static/export/central_kitchen_for_school/" + tmpFileName
  1653. startDate, _ := time.ParseInLocation("2006-01-02 15:04:05", req.Date1[0], time.Local)
  1654. endDate, _ := time.ParseInLocation("2006-01-02 15:04:05", req.Date2[0], time.Local)
  1655. //查询当前学校下的班级升序
  1656. tmpEnterpriseDb := db.EnterpriseDb{}
  1657. tmpEnterpriseDb.Set()
  1658. tmpEnterprise, err11 := tmpEnterpriseDb.GetEnterpriseByName(k1)
  1659. if err11 != nil {
  1660. logx.Error(err11)
  1661. println("<<<<CentralKitchenForSchoolDataStatisticsExport4>>>>>Error:::", err11.Error())
  1662. panic(err11)
  1663. }
  1664. tmpClassDb := db.ClassDb{}
  1665. tmpClassDb.Set(0)
  1666. classList, err := tmpClassDb.FindClassAscByEnterprise(tmpEnterprise.Id)
  1667. if err != nil {
  1668. logx.Error(err)
  1669. println("<<<<CentralKitchenForSchoolDataStatisticsExport4>>>>>Error:::", err.Error())
  1670. panic(err)
  1671. }
  1672. for _, class := range *classList {
  1673. for k2, v2 := range v1 {
  1674. if k2 == "" {
  1675. //若年级为空,则为教师
  1676. continue
  1677. }
  1678. for k3, v3 := range v2 {
  1679. if class.Name != k3 {
  1680. continue
  1681. }
  1682. xlsx.NewSheet(k3)
  1683. titleList = []string{startDate.Format("2006-01") + "~~" + endDate.Format("2006-01") + " [" + k1 + ", " + k3 + "] 订餐数据对比"}
  1684. xlsx.SetSheetRow(k3, "A1", &titleList)
  1685. titleList = []string{"姓名", "家长联系电话"}
  1686. xlsx.SetSheetRow(k3, "A2", &titleList)
  1687. xlsx.MergeCell(k3, "A1", "B1") //合并单元格
  1688. style, err := xlsx.NewStyle(`{"fill":{"type":"pattern","color":["FFFF00"],"pattern":1}, "alignment":{"horizontal":"center"}}`)
  1689. if err != nil {
  1690. logx.Error(err)
  1691. println("<<<<CentralKitchenForSchoolDataStatisticsExport3>>>>>Error:::", err.Error())
  1692. panic(err)
  1693. }
  1694. xlsx.SetCellStyle(k3, "A1", "B1", style)
  1695. xlsx.SetColWidth(k3, "A", "B", 40)
  1696. j := 2 //表头被第一行用了,只能从第二行开始
  1697. for k4, v4 := range v3 {
  1698. //TODO::判断当前用户是否在新的月份里面
  1699. if list1[k1] == nil || list1[k1][k2] == nil || list1[k1][k2][k3] == nil || list1[k1][k2][k3][k4] == "" {
  1700. xlsx.SetSheetRow(k3, "A"+strconv.Itoa(j), &[]interface{}{
  1701. k4, v4})
  1702. j++
  1703. }
  1704. }
  1705. }
  1706. }
  1707. }
  1708. //将文件保存至服务器
  1709. xlsx.DeleteSheet("Sheet1") //TODO::只能放这里删除
  1710. err1 := xlsx.SaveAs(tmpDownloadPath)
  1711. if err1 != nil {
  1712. logx.Error(err1)
  1713. println("<<<<CentralKitchenForSchoolDataStatisticsContrast>>>>>Error:::", err1.Error())
  1714. panic(err1)
  1715. }
  1716. xlsDownloadPath = append(xlsDownloadPath, tmpDownloadPath)
  1717. xlsFileName = append(xlsFileName, tmpFileName)
  1718. }
  1719. archive, err := os.Create("./static" + downloadPath) //第一步,创建 zip 文件
  1720. if err != nil {
  1721. logx.Error(err)
  1722. println("<<<<CentralKitchenForSchoolDataStatisticsContrast>>>>>Error:::", err.Error())
  1723. panic(err)
  1724. }
  1725. defer archive.Close()
  1726. zipWriter := zip.NewWriter(archive) //第二步,创建一个新的 *Writer 对象
  1727. for k, v := range xlsDownloadPath {
  1728. w, err1 := zipWriter.Create("数据对比/" + xlsFileName[k]) //向 zip 文件中添加一个文件,返回一个待压缩的文件内容应写入的 Writer
  1729. if err1 != nil {
  1730. logx.Error(err1)
  1731. println("<<<<CentralKitchenForSchoolDataStatisticsContrast>>>>>Error:::", err1.Error())
  1732. panic(err1)
  1733. }
  1734. f, err1 := os.Open(v) //打开待压缩的文件
  1735. if err1 != nil {
  1736. logx.Error(err1)
  1737. println("<<<<CentralKitchenForSchoolDataStatisticsContrast>>>>>Error:::", err1.Error())
  1738. panic(err1)
  1739. }
  1740. if _, err1 = io.Copy(w, f); err != nil {
  1741. logx.Error(err1)
  1742. println("<<<<CentralKitchenForSchoolDataStatisticsContrast>>>>>Error:::", err1.Error())
  1743. panic(err1)
  1744. }
  1745. if err1 != nil {
  1746. logx.Error(err1)
  1747. println("<<<<CentralKitchenForSchoolDataStatisticsContrast>>>>>Error:::", err1.Error())
  1748. panic(err1)
  1749. }
  1750. f.Close()
  1751. os.RemoveAll(v) //TODO::移除源文件
  1752. }
  1753. zipWriter.Close() // 第四步,关闭 zip writer,将所有数据写入指向基础 zip 文件的数据流
  1754. if err != nil {
  1755. panic(err)
  1756. }
  1757. //新增数据
  1758. centralKitchenForSchoolExportRecordsDb := db.CentralKitchenForSchoolExportRecordsDb{}
  1759. centralKitchenForSchoolExportRecordsDb.Set()
  1760. marshal, _ := json.Marshal(req)
  1761. centralKitchenForSchoolExportRecordsDb.CentralKitchenForSchoolExportRecordsInsert(&model.CentralKitchenForSchoolExportRecords{
  1762. Name: fileName,
  1763. DownloadPath: downloadPath,
  1764. Kind: 9, //数据对比
  1765. ReqContent: string(marshal),
  1766. CreateAt: now.Format("2006-01-02 15:04:05"),
  1767. UpdateAt: now.Format("2006-01-02 15:04:05"),
  1768. })
  1769. }
  1770. func JudgeSelfSupportForMealTime(mealDate string) (mealZh string) {
  1771. mealZh = "未知"
  1772. mealTime, _ := time.ParseInLocation("2006-01-02 15:04:05", mealDate, time.Local)
  1773. for _, v := range md.SelfSupportForMealTimeMapping {
  1774. //判断时间大小
  1775. tmpFromTime, _ := time.ParseInLocation("2006-01-02 15:04:05", mealTime.Format("2006-01-02")+" "+v["from"]+":00", time.Local)
  1776. tmpToTime, _ := time.ParseInLocation("2006-01-02 15:04:05", mealTime.Format("2006-01-02")+" "+v["to"]+":00", time.Local)
  1777. fmt.Println(">>>>>>>>>>>>>", mealTime.After(tmpFromTime))
  1778. fmt.Println("<<<<<<<<<<<<<", mealTime.Before(tmpToTime))
  1779. if mealTime.After(tmpFromTime) && mealTime.Before(tmpToTime) {
  1780. mealZh = v["name"]
  1781. }
  1782. }
  1783. return
  1784. }