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

2433 lines
104 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. "strings"
  18. "time"
  19. )
  20. func NursingHomeDataStatisticsExport(req md.NursingHomeDataStatisticsExportReq) {
  21. var titleList []string
  22. var fileName string
  23. var now = time.Now()
  24. xlsx := excelize.NewFile()
  25. xlsx.SetSheetRow("Sheet1", "A1", &titleList)
  26. switch req.Kind {
  27. case 1:
  28. //订单列表
  29. fileName = "/export/nursing_home/订单列表_" + utils.Int64ToStr(now.UnixMilli()) + ".xlsx"
  30. //查找订单数据
  31. sess := db.Db.Desc("nursing_home_package_ord.id")
  32. if req.EnterpriseId != 0 {
  33. sess.And("nursing_home_package_ord.enterprise_id =?", req.EnterpriseId)
  34. }
  35. if req.StartDate != "" {
  36. sess.And("nursing_home_package_ord.create_at >= ?", req.StartDate)
  37. }
  38. if req.EndDate != "" {
  39. sess.And("nursing_home_package_ord.create_at <= ?", req.EndDate)
  40. }
  41. var m []*db.NursingHomePackageOrdWithUserIdentity
  42. _, err := sess.
  43. Join("LEFT", "user_identity", "nursing_home_package_ord.user_identity_id = user_identity.id").
  44. Join("LEFT", "enterprise", "enterprise.id = nursing_home_package_ord.enterprise_id").
  45. Join("LEFT", "user", "user.id = nursing_home_package_ord.uid").
  46. FindAndCount(&m)
  47. if err != nil {
  48. logx.Error(err)
  49. println("<<<<NursingHomeDataStatisticsExport1>>>>>Error:::", err.Error())
  50. return
  51. }
  52. titleList = []string{"单位名称", "用户身份名称", "用户手机号", "购买类型", "总价", "商户订单号", "支付状态", "购买时间"}
  53. xlsx.SetSheetRow("Sheet1", "A1", &titleList)
  54. j := 2 //表头被第一行用了,只能从第二行开始
  55. for _, v := range m {
  56. var kind string //1:按年购买 2:按月购买 3:按天购买 4:补餐
  57. switch v.NursingHomePackageOrd.Kind {
  58. case 1:
  59. kind = "按年购买"
  60. break
  61. case 2:
  62. kind = "按月购买"
  63. break
  64. case 3:
  65. kind = "按天购买"
  66. break
  67. case 4:
  68. kind = "补餐"
  69. break
  70. }
  71. var state string //0:待支付 1:支付成功 2:支付失败
  72. switch v.NursingHomePackageOrd.State {
  73. case enum.NursingHomePackageOrdStateForWait:
  74. state = enum.NursingHomePackageOrdState.String(enum.NursingHomePackageOrdStateForWait)
  75. break
  76. case enum.NursingHomePackageOrdStateForSuccess:
  77. state = enum.NursingHomePackageOrdState.String(enum.NursingHomePackageOrdStateForWait)
  78. break
  79. case enum.NursingHomePackageOrdStateForFail:
  80. state = enum.NursingHomePackageOrdState.String(enum.NursingHomePackageOrdStateForFail)
  81. break
  82. }
  83. xlsx.SetSheetRow("Sheet1", "A"+strconv.Itoa(j), &[]interface{}{v.Enterprise.Name, v.NursingHomePackageOrd.UserIdentityName, v.User.Phone, kind, v.NursingHomePackageOrd.TotalPrice,
  84. v.NursingHomePackageOrd.OutTradeNo, state, v.NursingHomePackageOrd.CreateAt})
  85. j++
  86. }
  87. break
  88. case 2:
  89. //退款申请表
  90. fileName = "/export/nursing_home/退款申请表_" + utils.Int64ToStr(now.UnixMilli()) + ".xlsx"
  91. var m []*db.NursingHomeUserRefundDayWithData
  92. sess := db.Db.Desc("nursing_home_user_refund_day.id")
  93. if req.EnterpriseId != 0 {
  94. sess.And("enterprise.enterprise_id =?", req.EnterpriseId)
  95. }
  96. if req.StartDate != "" {
  97. sess.And("nursing_home_user_refund_day.create_at >= ?", req.StartDate)
  98. }
  99. if req.EndDate != "" {
  100. sess.And("nursing_home_user_refund_day.create_at <= ?", req.EndDate)
  101. }
  102. _, err := sess.
  103. Join("LEFT", "nursing_home_user_with_day", "nursing_home_user_refund_day.records_id = nursing_home_user_with_day.id").
  104. Join("LEFT", "nursing_home_package_ord", "nursing_home_user_refund_day.out_trade_no = nursing_home_package_ord.out_trade_no").
  105. Join("LEFT", "user_identity", "nursing_home_user_with_day.identity_id = user_identity.id").
  106. Join("LEFT", "user", "user_identity.uid = user.id").
  107. Join("LEFT", "enterprise", "user_identity.enterprise_id = enterprise.id").
  108. Desc("nursing_home_user_refund_day.id").
  109. FindAndCount(&m)
  110. if err != nil {
  111. logx.Error(err)
  112. println("<<<<NursingHomeDataStatisticsExport2>>>>>Error:::", err.Error())
  113. return
  114. }
  115. titleList = []string{"退款单号", "所属订单号", "单位名称", "用户身份名称", "用户手机号", "就餐类型", "就餐时间", "金额", "状态", "退款时间"}
  116. xlsx.SetSheetRow("Sheet1", "A1", &titleList)
  117. j := 2 //表头被第一行用了,只能从第二行开始
  118. for _, v := range m {
  119. var kind string //就餐类型(1:早餐 2:午餐 3:晚餐)
  120. switch v.NursingHomeUserWithDay.Kind {
  121. case enum.NursingHomeUserWithDayKindForBreakfast:
  122. kind = enum.NursingHomeUserWithDayKind.String(enum.NursingHomeUserWithDayKindForBreakfast)
  123. break
  124. case enum.NursingHomeUserWithDayKindForLunch:
  125. kind = enum.NursingHomeUserWithDayKind.String(enum.NursingHomeUserWithDayKindForLunch)
  126. break
  127. case enum.NursingHomeUserWithDayKindForDinner:
  128. kind = enum.NursingHomeUserWithDayKind.String(enum.NursingHomeUserWithDayKindForDinner)
  129. break
  130. }
  131. var state string //状态(1:审核中 2:审核通过 3:审核拒绝 4:退款已完成)
  132. switch v.NursingHomeUserRefundDay.State {
  133. case enum.NursingHomeUserRefundDayStateForAuditing:
  134. state = enum.NursingHomeUserRefundDayState.String(enum.NursingHomeUserRefundDayStateForAuditing)
  135. break
  136. case enum.NursingHomeUserRefundDayStateForAuditPass:
  137. state = enum.NursingHomeUserRefundDayState.String(enum.NursingHomeUserRefundDayStateForAuditPass)
  138. break
  139. case enum.NursingHomeUserRefundDayStateForAuditReject:
  140. state = enum.NursingHomeUserRefundDayState.String(enum.NursingHomeUserRefundDayStateForAuditReject)
  141. break
  142. case enum.NursingHomeUserRefundDayStateForAuditComplete:
  143. state = enum.NursingHomeUserRefundDayState.String(enum.NursingHomeUserRefundDayStateForAuditComplete)
  144. break
  145. }
  146. xlsx.SetSheetRow("Sheet1", "A"+strconv.Itoa(j), &[]interface{}{v.NursingHomeUserRefundDay.OutRequestNo, v.NursingHomeUserRefundDay.OutTradeNo, v.Enterprise.Name,
  147. v.NursingHomePackageOrd.UserIdentityName, v.User.Phone, kind, v.NursingHomeUserWithDay.Date, v.NursingHomeUserRefundDay.Amount, state, v.NursingHomeUserRefundDay.RefundDate})
  148. j++
  149. }
  150. xlsx.SaveAs("./static" + fileName + ".xlsx")
  151. break
  152. case 3:
  153. //预定统计表
  154. fileName = "/export/nursing_home/预定统计表_" + utils.Int64ToStr(now.UnixMilli()) + ".xlsx"
  155. var m []*db.NursingHomePackageOrdForReserveWithUserIdentity
  156. sess := db.Db.Desc("nursing_home_package_ord_for_reserve.id")
  157. if req.EnterpriseId != 0 {
  158. sess.And("enterprise.enterprise_id =?", req.EnterpriseId)
  159. }
  160. if req.StartDate != "" {
  161. sess.And("nursing_home_package_ord_for_reserve.meal_time_start >= ?", req.StartDate)
  162. }
  163. if req.EndDate != "" {
  164. sess.And("nursing_home_package_ord_for_reserve.meal_time_start <= ?", req.EndDate)
  165. }
  166. _, err := sess.
  167. Join("LEFT", "nursing_home_package_ord", "nursing_home_package_ord_for_reserve.out_trade_no = nursing_home_package_ord.out_trade_no").
  168. Join("LEFT", "user_identity", "nursing_home_package_ord.user_identity_id = user_identity.id").
  169. Join("LEFT", "enterprise", "enterprise.id = user_identity.enterprise_id").
  170. Join("LEFT", "user", "user.id = user_identity.uid").FindAndCount(&m)
  171. if err != nil {
  172. logx.Error(err)
  173. println("<<<<NursingHomeDataStatisticsExport2>>>>>Error:::", err.Error())
  174. return
  175. }
  176. titleList = []string{"所属订单号", "单位名称", "用户身份名称", "用户手机号", "预定类型", "预定月份", "就餐时间-开始", "就餐时间-截止"}
  177. xlsx.SetSheetRow("Sheet1", "A1", &titleList)
  178. j := 2 //表头被第一行用了,只能从第二行开始
  179. for _, v := range m {
  180. var kind string //预定类型(1:早餐 2:午餐 3:晚餐)
  181. switch v.NursingHomePackageOrdForReserve.Kind {
  182. case enum.NursingHomeUserWithDayKindForBreakfast:
  183. kind = enum.NursingHomeUserWithDayKind.String(enum.NursingHomeUserWithDayKindForBreakfast)
  184. break
  185. case enum.NursingHomeUserWithDayKindForLunch:
  186. kind = enum.NursingHomeUserWithDayKind.String(enum.NursingHomeUserWithDayKindForLunch)
  187. break
  188. case enum.NursingHomeUserWithDayKindForDinner:
  189. kind = enum.NursingHomeUserWithDayKind.String(enum.NursingHomeUserWithDayKindForDinner)
  190. break
  191. }
  192. xlsx.SetSheetRow("Sheet1", "A"+strconv.Itoa(j), &[]interface{}{v.NursingHomePackageOrdForReserve.OutTradeNo, v.Enterprise.Name, v.NursingHomePackageOrd.UserIdentityName,
  193. v.User.Phone, kind, v.NursingHomePackageOrdForReserve.ReserveMonth, v.NursingHomePackageOrdForReserve.MealTimeStart, v.NursingHomePackageOrdForReserve.MealTimeEnd})
  194. j++
  195. }
  196. xlsx.SaveAs("./static" + fileName + ".xlsx")
  197. break
  198. }
  199. //新增数据
  200. nursingHomeExportRecordsDb := db.NursingHomeExportRecordsDb{}
  201. nursingHomeExportRecordsDb.Set()
  202. marshal, _ := json.Marshal(req)
  203. nursingHomeExportRecordsDb.NursingHomeExportRecordsInsert(&model.NursingHomeExportRecords{
  204. Name: fileName,
  205. DownloadPath: fileName,
  206. Kind: req.Kind,
  207. ReqContent: string(marshal),
  208. CreateAt: now.Format("2006-01-02 15:04:05"),
  209. UpdateAt: now.Format("2006-01-02 15:04:05"),
  210. })
  211. }
  212. func SelfSupportForSchoolDataStatisticsExport(req md.SelfSupportForSchoolDataStatisticsExportReq) {
  213. var titleList []string
  214. var fileName, downloadPath string
  215. var now = time.Now()
  216. switch req.Kind {
  217. case 1:
  218. //教师消费统计表
  219. xlsx, _ := excelize.OpenFile("./static/template/自营学校(教师消费统计表).xlsx")
  220. fileName = "教师消费统计表_" + utils.Int64ToStr(now.UnixMilli()) + ".xlsx"
  221. downloadPath = "/export/self_support_for_school/" + fileName
  222. //查询数据
  223. var m []*db.SelfSupportForSchoolOrdWithUserIdentity
  224. sess := db.Db.Desc("self_support_for_school_ord.id")
  225. if req.EnterpriseId != 0 {
  226. sess.And("self_support_for_school_ord.enterprise_id =?", req.EnterpriseId)
  227. }
  228. if req.StartDate != "" {
  229. sess.And("self_support_for_school_ord.face_time >= ?", req.StartDate)
  230. }
  231. if req.EndDate != "" {
  232. sess.And("self_support_for_school_ord.face_time <= ?", req.EndDate)
  233. }
  234. if req.EnterPriseKind != 0 {
  235. sess.And("enterprise.kind = ?", req.EnterPriseKind)
  236. }
  237. sess.And("user_identity.identity = ?", enum.UserIdentityForSelfSupportForTeacher) //只查询老师
  238. _, err := sess.
  239. Join("LEFT", "user_identity", "self_support_for_school_ord.user_identity_id = user_identity.id").
  240. Join("LEFT", "enterprise", "enterprise.id = user_identity.enterprise_id").
  241. Join("LEFT", "user", "user.id = user_identity.uid").
  242. Join("LEFT", "class_with_user", "class_with_user.user_identity_id = user_identity.id").
  243. Join("LEFT", "class", "class_with_user.class_id = class.id").
  244. Join("LEFT", "grade", "class.grade_id = grade.id").
  245. FindAndCount(&m)
  246. if err != nil {
  247. logx.Error(err)
  248. println("<<<<SelfSupportForSchoolDataStatisticsExport2>>>>>Error:::", err.Error())
  249. return
  250. }
  251. //整合数据
  252. var data = map[string]map[string]map[string]map[string]interface{}{}
  253. for _, vv := range m {
  254. if data[vv.Enterprise.Name] == nil {
  255. data[vv.Enterprise.Name] = make(map[string]map[string]map[string]interface{})
  256. }
  257. if data[vv.Enterprise.Name][vv.UserIdentity.Name] == nil {
  258. data[vv.Enterprise.Name][vv.UserIdentity.Name] = make(map[string]map[string]interface{})
  259. }
  260. mealZh := JudgeSelfSupportForMealTime(vv.SelfSupportForSchoolOrd.FaceTime)
  261. if data[vv.Enterprise.Name][vv.UserIdentity.Name][mealZh] == nil {
  262. data[vv.Enterprise.Name][vv.UserIdentity.Name][mealZh] = make(map[string]interface{})
  263. }
  264. data[vv.Enterprise.Name][vv.UserIdentity.Name]["编号"] = map[string]interface{}{
  265. "id": vv.UserIdentity.IdNo,
  266. }
  267. if data[vv.Enterprise.Name][vv.UserIdentity.Name]["合计"] == nil {
  268. data[vv.Enterprise.Name][vv.UserIdentity.Name]["合计"] = make(map[string]interface{})
  269. }
  270. data[vv.Enterprise.Name][vv.UserIdentity.Name][mealZh]["num"] = utils.AnyToInt64(data[vv.Enterprise.Name][vv.UserIdentity.Name][mealZh]["num"]) + 1
  271. 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)
  272. data[vv.Enterprise.Name][vv.UserIdentity.Name]["合计"]["num"] = utils.AnyToInt64(data[vv.Enterprise.Name][vv.UserIdentity.Name]["合计"]["num"]) + 1
  273. data[vv.Enterprise.Name][vv.UserIdentity.Name]["合计"]["amount"] = utils.AnyToFloat64(data[vv.Enterprise.Name][vv.UserIdentity.Name]["合计"]["amount"]) + utils.AnyToFloat64(vv.SelfSupportForSchoolOrd.TradeAmount)
  274. }
  275. xlsx.SetSheetRow("", "A1", &titleList)
  276. sheetIndex := 2 //第一个表是模板表
  277. j := 3 //表头前两行被占用了,只能从第三行开始
  278. for k, v := range data {
  279. xlsx.NewSheet(k) //1、新建表格
  280. xlsx.CopySheet(1, sheetIndex)
  281. for kk, vv := range v {
  282. xlsx.SetSheetRow(k, "A"+strconv.Itoa(j), &[]interface{}{vv["编号"]["id"], kk,
  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. vv["合计"]["num"], vv["合计"]["amount"],
  289. })
  290. j++
  291. }
  292. sheetIndex++
  293. }
  294. xlsx.DeleteSheet("template") //删除模板表
  295. //将文件保存至服务器
  296. xlsx.SaveAs("./static" + downloadPath)
  297. break
  298. case 2:
  299. //商家分餐数据统计表
  300. xlsx, _ := excelize.OpenFile("./static/template/自营学校(商家分餐数据统计表).xlsx")
  301. fileName = "商家分餐数据统计表_" + utils.Int64ToStr(now.UnixMilli()) + ".xlsx"
  302. downloadPath = "/export/self_support_for_school/" + fileName
  303. //查询出所有的设备
  304. var deviceList []*model.Device
  305. err := db.Db.Where("enterprise_id != 0").Find(&deviceList)
  306. if err != nil {
  307. logx.Error(err)
  308. println("<<<<SelfSupportForSchoolDataStatisticsExport2>>>>>1111Error:::", err.Error())
  309. return
  310. }
  311. var deviceListMapping = map[string]model.Device{}
  312. for _, v := range deviceList {
  313. deviceListMapping[v.DeviceSn] = *v
  314. }
  315. var m []*db.SelfSupportForSchoolOrdWithUserIdentity
  316. sess := db.Db.Desc("self_support_for_school_ord.id")
  317. if req.EnterpriseId != 0 {
  318. sess.And("self_support_for_school_ord.enterprise_id =?", req.EnterpriseId)
  319. }
  320. if req.StartDate != "" {
  321. sess.And("self_support_for_school_ord.face_time >= ?", req.StartDate)
  322. }
  323. if req.EndDate != "" {
  324. sess.And("self_support_for_school_ord.face_time <= ?", req.EndDate)
  325. }
  326. if req.EnterPriseKind != 0 {
  327. sess.And("enterprise.kind = ?", req.EnterPriseKind)
  328. }
  329. _, err = sess.
  330. Join("LEFT", "user_identity", "self_support_for_school_ord.user_identity_id = user_identity.id").
  331. Join("LEFT", "enterprise", "enterprise.id = user_identity.enterprise_id").
  332. Join("LEFT", "user", "user.id = user_identity.uid").
  333. Join("LEFT", "class_with_user", "class_with_user.user_identity_id = user_identity.id").
  334. Join("LEFT", "class", "class_with_user.class_id = class.id").
  335. Join("LEFT", "grade", "class.grade_id = grade.id").
  336. FindAndCount(&m)
  337. if err != nil {
  338. logx.Error(err)
  339. println("<<<<SelfSupportForSchoolDataStatisticsExport2>>>>>Error:::", err.Error())
  340. return
  341. }
  342. //整合数据
  343. var data = map[string]map[string]map[string]map[string]float64{}
  344. for _, vv := range m {
  345. shopName := deviceListMapping[vv.SelfSupportForSchoolOrd.DeviceSn].Name
  346. if data[vv.Enterprise.Name] == nil {
  347. data[vv.Enterprise.Name] = make(map[string]map[string]map[string]float64)
  348. }
  349. if data[vv.Enterprise.Name][shopName] == nil {
  350. data[vv.Enterprise.Name][shopName] = make(map[string]map[string]float64)
  351. }
  352. mealZh := JudgeSelfSupportForMealTime(vv.SelfSupportForSchoolOrd.FaceTime)
  353. if data[vv.Enterprise.Name][shopName][mealZh] == nil {
  354. data[vv.Enterprise.Name][shopName][mealZh] = make(map[string]float64)
  355. }
  356. if data[vv.Enterprise.Name][shopName]["合计"] == nil {
  357. data[vv.Enterprise.Name][shopName]["合计"] = make(map[string]float64)
  358. }
  359. data[vv.Enterprise.Name][shopName][mealZh]["num"]++
  360. data[vv.Enterprise.Name][shopName][mealZh]["amount"] += utils.StrToFloat64(vv.SelfSupportForSchoolOrd.TradeAmount)
  361. data[vv.Enterprise.Name][shopName]["合计"]["num"]++
  362. data[vv.Enterprise.Name][shopName]["合计"]["amount"] += utils.StrToFloat64(vv.SelfSupportForSchoolOrd.TradeAmount)
  363. }
  364. xlsx.SetSheetRow("", "A1", &titleList)
  365. sheetIndex := 2 //第一个表是模板表
  366. for k, v := range data {
  367. if k == "" {
  368. k = "未知"
  369. }
  370. xlsx.NewSheet(k) //1、新建表格
  371. xlsx.CopySheet(1, sheetIndex)
  372. j := 4 //表头前三行被占用了,只能从第四行开始
  373. for kk, vv := range v {
  374. if kk == "" {
  375. kk = k
  376. }
  377. xlsx.SetSheetRow(k, "A"+strconv.Itoa(j), &[]interface{}{kk,
  378. vv["早餐"]["num"], vv["早餐"]["amount"],
  379. vv["午餐"]["num"], vv["午餐"]["amount"],
  380. vv["晚餐"]["num"], vv["晚餐"]["amount"],
  381. vv["夜宵"]["num"], vv["夜宵"]["amount"],
  382. vv["合计"]["num"], vv["合计"]["amount"],
  383. })
  384. j++
  385. }
  386. sheetIndex++
  387. }
  388. xlsx.DeleteSheet("template") //删除模板表
  389. //将文件保存至服务器
  390. xlsx.SaveAs("./static" + downloadPath)
  391. break
  392. case 3:
  393. //就餐数据统计表
  394. xlsx, _ := excelize.OpenFile("./static/template/自营学校(就餐数据统计表).xlsx")
  395. fileName = "就餐数据统计表_" + utils.Int64ToStr(now.UnixMilli()) + ".xlsx"
  396. downloadPath = "/export/self_support_for_school/" + fileName
  397. //查询出所有的设备
  398. //var deviceList []*model.Device
  399. //err := db.Db.Where("enterprise_id != 0").Find(&deviceList)
  400. //if err != nil {
  401. // logx.Error(err)
  402. // println("<<<<SelfSupportForSchoolDataStatisticsExport2>>>>>1111Error:::", err.Error())
  403. // return
  404. //}
  405. //var deviceListMapping = map[string]model.Device{}
  406. //for _, v := range deviceList {
  407. // deviceListMapping[v.DeviceSn] = *v
  408. //}
  409. var m []*db.SelfSupportForSchoolOrdWithUserIdentity
  410. sess := db.Db.Desc("self_support_for_school_ord.id")
  411. if req.EnterpriseId != 0 {
  412. sess.And("self_support_for_school_ord.enterprise_id =?", req.EnterpriseId)
  413. }
  414. if req.StartDate != "" {
  415. sess.And("self_support_for_school_ord.face_time >= ?", req.StartDate)
  416. }
  417. if req.EndDate != "" {
  418. sess.And("self_support_for_school_ord.face_time <= ?", req.EndDate)
  419. }
  420. if req.EnterPriseKind != 0 {
  421. sess.And("enterprise.kind = ?", req.EnterPriseKind)
  422. }
  423. _, err := sess.
  424. Join("LEFT", "user_identity", "self_support_for_school_ord.user_identity_id = user_identity.id").
  425. Join("LEFT", "enterprise", "enterprise.id = user_identity.enterprise_id").
  426. Join("LEFT", "user", "user.id = user_identity.uid").
  427. Join("LEFT", "class_with_user", "class_with_user.user_identity_id = user_identity.id").
  428. Join("LEFT", "class", "class_with_user.class_id = class.id").
  429. Join("LEFT", "grade", "class.grade_id = grade.id").
  430. FindAndCount(&m)
  431. if err != nil {
  432. logx.Error(err)
  433. println("<<<<SelfSupportForSchoolDataStatisticsExport3>>>>>Error:::", err.Error())
  434. return
  435. }
  436. //整合数据
  437. var data = map[string]map[string]map[string]map[string]float64{}
  438. date1, err := time.ParseInLocation("2006-01-02 15:04:05", req.StartDate, time.Local)
  439. if err != nil {
  440. logx.Error(err)
  441. println("<<<<SelfSupportForSchoolDataStatisticsExport3>>>>>Error:::", err.Error())
  442. return
  443. }
  444. date2, err := time.ParseInLocation("2006-01-02 15:04:05", req.EndDate, time.Local)
  445. if err != nil {
  446. logx.Error(err)
  447. println("<<<<SelfSupportForSchoolDataStatisticsExport3>>>>>Error:::", err.Error())
  448. return
  449. }
  450. diffDays := int(date2.Sub(date1).Hours() / 24)
  451. var dates []string
  452. for i := 0; i <= diffDays; i++ {
  453. date := date1.AddDate(0, 0, i)
  454. shopName := date.Format("2006-01-02")
  455. dates = append(dates, shopName)
  456. for _, vv := range m {
  457. tmpDate, err1 := time.ParseInLocation("2006-01-02 15:04:05", vv.SelfSupportForSchoolOrd.FaceTime, time.Local)
  458. if err1 != nil {
  459. logx.Error(err1)
  460. println("<<<<SelfSupportForSchoolDataStatisticsExport3>>>>>Error:::", err1.Error())
  461. return
  462. }
  463. fmt.Println(tmpDate.Format("2006-01-02"))
  464. if shopName == tmpDate.Format("2006-01-02") {
  465. if data[vv.Enterprise.Name] == nil {
  466. data[vv.Enterprise.Name] = make(map[string]map[string]map[string]float64)
  467. }
  468. if data[vv.Enterprise.Name][shopName] == nil {
  469. data[vv.Enterprise.Name][shopName] = make(map[string]map[string]float64)
  470. }
  471. mealZh := JudgeSelfSupportForMealTime(vv.SelfSupportForSchoolOrd.FaceTime)
  472. if data[vv.Enterprise.Name][shopName][mealZh] == nil {
  473. data[vv.Enterprise.Name][shopName][mealZh] = make(map[string]float64)
  474. }
  475. if data[vv.Enterprise.Name][shopName]["合计"] == nil {
  476. data[vv.Enterprise.Name][shopName]["合计"] = make(map[string]float64)
  477. }
  478. data[vv.Enterprise.Name][shopName][mealZh]["num"]++
  479. data[vv.Enterprise.Name][shopName][mealZh]["amount"] += utils.StrToFloat64(vv.SelfSupportForSchoolOrd.TradeAmount)
  480. data[vv.Enterprise.Name][shopName]["合计"]["num"]++
  481. data[vv.Enterprise.Name][shopName]["合计"]["amount"] += utils.StrToFloat64(vv.SelfSupportForSchoolOrd.TradeAmount)
  482. }
  483. }
  484. }
  485. xlsx.SetSheetRow("", "A1", &titleList)
  486. sheetIndex := 2 //第一个表是模板表
  487. for k, v := range data {
  488. if k == "" {
  489. k = "未知"
  490. }
  491. xlsx.NewSheet(k) //1、新建表格
  492. xlsx.CopySheet(1, sheetIndex)
  493. j := 4 //表头前三行被占用了,只能从第四行开始
  494. for _, date := range dates {
  495. for kk, vv := range v {
  496. if date == kk {
  497. xlsx.SetSheetRow(k, "A"+strconv.Itoa(j), &[]interface{}{kk,
  498. vv["早餐"]["num"], vv["早餐"]["amount"],
  499. vv["午餐"]["num"], vv["午餐"]["amount"],
  500. vv["晚餐"]["num"], vv["晚餐"]["amount"],
  501. vv["夜宵"]["num"], vv["夜宵"]["amount"],
  502. vv["合计"]["num"], vv["合计"]["amount"],
  503. })
  504. j++
  505. }
  506. sheetIndex++
  507. }
  508. }
  509. }
  510. xlsx.DeleteSheet("template") //删除模板表
  511. //将文件保存至服务器
  512. xlsx.SaveAs("./static" + downloadPath)
  513. break
  514. case 4:
  515. //设备就餐数据统计表
  516. fileName = "设备就餐数据统计_" + utils.Int64ToStr(now.UnixMilli()) + ".zip"
  517. downloadPath = "/export/self_support_for_school/" + fileName
  518. //查询出所有的设备
  519. var deviceList []*model.Device
  520. if req.EnterpriseId == 0 {
  521. err := db.Db.Where("enterprise_id != 0").Find(&deviceList)
  522. if err != nil {
  523. logx.Error(err)
  524. println("<<<<SelfSupportForSchoolDataStatisticsExport4>>>>>1111Error:::", err.Error())
  525. return
  526. }
  527. } else {
  528. err := db.Db.Where("enterprise_id = ?", req.EnterpriseId).Find(&deviceList)
  529. if err != nil {
  530. logx.Error(err)
  531. println("<<<<SelfSupportForSchoolDataStatisticsExport4>>>>>1111Error:::", err.Error())
  532. return
  533. }
  534. }
  535. date1, err := time.ParseInLocation("2006-01-02 15:04:05", req.StartDate, time.Local)
  536. if err != nil {
  537. logx.Error(err)
  538. println("<<<<SelfSupportForSchoolDataStatisticsExport4>>>>>Error:::", err.Error())
  539. return
  540. }
  541. date2, err := time.ParseInLocation("2006-01-02 15:04:05", req.EndDate, time.Local)
  542. if err != nil {
  543. logx.Error(err)
  544. println("<<<<SelfSupportForSchoolDataStatisticsExport4>>>>>Error:::", err.Error())
  545. return
  546. }
  547. diffDays := int(date2.Sub(date1).Hours() / 24)
  548. var deviceListMapping = map[string]model.Device{}
  549. //整合数据
  550. var data = map[string]map[string]map[string]map[string]float64{}
  551. var xlsFileName, xlsDownloadPath []string
  552. for _, v := range deviceList {
  553. deviceListMapping[v.DeviceSn] = *v
  554. var m []*db.SelfSupportForSchoolOrdWithUserIdentityV2
  555. sess := db.Db.Where("device_sn = ?", v.DeviceSn).And("enterprise_id =?", req.EnterpriseId).Desc("self_support_for_school_ord.id")
  556. if req.EnterpriseId != 0 {
  557. sess.And("self_support_for_school_ord.enterprise_id =?", req.EnterpriseId)
  558. }
  559. if req.StartDate != "" {
  560. sess.And("self_support_for_school_ord.face_time >= ?", req.StartDate)
  561. }
  562. if req.EndDate != "" {
  563. sess.And("self_support_for_school_ord.face_time <= ?", req.EndDate)
  564. }
  565. _, err = sess.Join("LEFT", "enterprise", "self_support_for_school_ord.enterprise_id = enterprise.id").FindAndCount(&m)
  566. if err != nil {
  567. logx.Error(err)
  568. println("<<<<SelfSupportForSchoolDataStatisticsExport4>>>>>Error:::", err.Error())
  569. return
  570. }
  571. for _, vv := range m {
  572. tmpTime, err1 := time.ParseInLocation("2006-01-02 15:04:05", vv.SelfSupportForSchoolOrd.FaceTime, time.Local)
  573. if err1 != nil {
  574. logx.Error(err1)
  575. println("<<<<SelfSupportForSchoolDataStatisticsExport4>>>>>Error:::", err1.Error())
  576. return
  577. }
  578. tmpDate := tmpTime.Format("2006-01-02")
  579. if data[vv.Enterprise.Name] == nil {
  580. data[vv.Enterprise.Name] = make(map[string]map[string]map[string]float64)
  581. }
  582. if data[vv.Enterprise.Name][v.Name] == nil {
  583. data[vv.Enterprise.Name][v.Name] = make(map[string]map[string]float64)
  584. }
  585. if data[vv.Enterprise.Name][v.Name][tmpDate] == nil {
  586. data[vv.Enterprise.Name][v.Name][tmpDate] = make(map[string]float64)
  587. }
  588. data[vv.Enterprise.Name][v.Name][tmpDate]["num"]++
  589. data[vv.Enterprise.Name][v.Name][tmpDate]["amount"] += utils.StrToFloat64(vv.SelfSupportForSchoolOrd.TradeAmount)
  590. }
  591. }
  592. for k1, v := range data {
  593. tmpFileName := k1 + "设备就餐数据统计表_" + utils.Int64ToStr(now.UnixMilli()) + ".xlsx"
  594. tmpDownloadPath := "./static/export/self_support_for_school/" + tmpFileName
  595. xlsx := excelize.NewFile()
  596. var totalData = map[string]map[string]float64{}
  597. for k2, vv := range v {
  598. var sumNum, sumAmount = float64(0), float64(0)
  599. xlsx.NewSheet(k2)
  600. titleList = []string{" 设 备 分 餐 数 据 统 计 表 "}
  601. xlsx.SetSheetRow(k2, "A1", &titleList)
  602. titleList = []string{"日 期", "消费笔数", "消费金额"}
  603. xlsx.SetSheetRow(k2, "A2", &titleList)
  604. style, err := xlsx.NewStyle(`{"fill":{"type":"pattern","color":["FFFF00"],"pattern":1}, "alignment":{"horizontal":"center"}}`)
  605. if err != nil {
  606. logx.Error(err)
  607. println("<<<<SelfSupportForSchoolDataStatisticsExport4>>>>>Error:::", err.Error())
  608. panic(err)
  609. }
  610. xlsx.MergeCell(k2, "A1", "C1") //合并单元格
  611. xlsx.SetCellStyle(k2, "A1", "C1", style)
  612. xlsx.SetColWidth(k2, "A", "C", 20)
  613. j := 3 //表头前两行用了,只能从第三行开始
  614. for i := 0; i <= diffDays; i++ {
  615. date := date1.AddDate(0, 0, i).Format("2006-01-02")
  616. if vv[date] != nil {
  617. xlsx.SetSheetRow(k2, "A"+strconv.Itoa(j), &[]interface{}{date, vv[date]["num"], vv[date]["amount"]})
  618. sumNum += vv[date]["num"]
  619. sumAmount += vv[date]["amount"]
  620. } else {
  621. xlsx.SetSheetRow(k2, "A"+strconv.Itoa(j), &[]interface{}{date, 0, 0})
  622. }
  623. j++
  624. }
  625. xlsx.SetSheetRow(k2, "A"+strconv.Itoa(j), &[]interface{}{
  626. "合计", sumNum, sumAmount})
  627. if totalData[k2] == nil {
  628. totalData[k2] = map[string]float64{}
  629. }
  630. totalData[k2]["num"] = sumNum
  631. totalData[k2]["amount"] = sumAmount
  632. }
  633. //xlsx.NewSheet("所有档口")
  634. xlsx.SetSheetName("Sheet1", "所有档口")
  635. titleList = []string{" 设 备 分 餐 数 据 统 计 表 "}
  636. xlsx.SetSheetRow("所有档口", "A1", &titleList)
  637. titleList = []string{"名 称", "消费笔数", "消费金额"}
  638. xlsx.SetSheetRow("所有档口", "A2", &titleList)
  639. style, err := xlsx.NewStyle(`{"fill":{"type":"pattern","color":["FFFF00"],"pattern":1}, "alignment":{"horizontal":"center"}}`)
  640. if err != nil {
  641. logx.Error(err)
  642. println("<<<<SelfSupportForSchoolDataStatisticsExport4>>>>>Error:::", err.Error())
  643. panic(err)
  644. }
  645. xlsx.MergeCell("所有档口", "A1", "C1") //合并单元格
  646. xlsx.SetCellStyle("所有档口", "A1", "C1", style)
  647. xlsx.SetColWidth("所有档口", "A", "C", 20)
  648. j := 3 //表头前两行用了,只能从第三行开始
  649. var totalNum, totalAmount float64
  650. for key, val := range totalData {
  651. totalNum += val["num"]
  652. totalAmount += val["amount"]
  653. xlsx.SetSheetRow("所有档口", "A"+strconv.Itoa(j), &[]interface{}{key, val["num"], val["amount"]})
  654. j++
  655. }
  656. xlsx.SetSheetRow("所有档口", "A"+strconv.Itoa(j), &[]interface{}{"合计", totalNum, totalAmount})
  657. //xlsx.DeleteSheet("Sheet1") //TODO::只能放这里删除
  658. //将文件保存至服务器
  659. err1 := xlsx.SaveAs(tmpDownloadPath)
  660. if err1 != nil {
  661. logx.Error(err1)
  662. println("<<<<SelfSupportForSchoolDataStatisticsExport4>>>>>Error:::", err1.Error())
  663. panic(err1)
  664. }
  665. xlsDownloadPath = append(xlsDownloadPath, tmpDownloadPath)
  666. xlsFileName = append(xlsFileName, tmpFileName)
  667. }
  668. archive, err := os.Create("./static" + downloadPath) //第一步,创建 zip 文件
  669. if err != nil {
  670. logx.Error(err)
  671. println("<<<<SelfSupportForSchoolDataStatisticsExport4>>>>>Error:::", err.Error())
  672. panic(err)
  673. }
  674. defer archive.Close()
  675. zipWriter := zip.NewWriter(archive) //第二步,创建一个新的 *Writer 对象
  676. for k, v := range xlsDownloadPath {
  677. w, err1 := zipWriter.Create("设备就餐数据统计/" + xlsFileName[k]) //向 zip 文件中添加一个文件,返回一个待压缩的文件内容应写入的 Writer
  678. if err1 != nil {
  679. logx.Error(err1)
  680. println("<<<<SelfSupportForSchoolDataStatisticsExport4>>>>>Error:::", err1.Error())
  681. panic(err1)
  682. }
  683. f, err1 := os.Open(v) //打开待压缩的文件
  684. if err1 != nil {
  685. logx.Error(err1)
  686. println("<<<<CentralKitchenForSchoolDataStatisticsExport7>>>>>Error:::", err1.Error())
  687. panic(err1)
  688. }
  689. if _, err1 = io.Copy(w, f); err != nil {
  690. logx.Error(err1)
  691. println("<<<<CentralKitchenForSchoolDataStatisticsExport7>>>>>Error:::", err1.Error())
  692. panic(err1)
  693. }
  694. if err1 != nil {
  695. logx.Error(err1)
  696. println("<<<<CentralKitchenForSchoolDataStatisticsExport7>>>>>Error:::", err1.Error())
  697. panic(err1)
  698. }
  699. f.Close()
  700. os.RemoveAll(v) //TODO::移除源文件
  701. }
  702. zipWriter.Close() // 第四步,关闭 zip writer,将所有数据写入指向基础 zip 文件的数据流
  703. if err != nil {
  704. panic(err)
  705. }
  706. break
  707. }
  708. //新增数据
  709. selfSupportForSchoolExportRecordsDb := db.SelfSupportForSchoolExportRecordsDb{}
  710. selfSupportForSchoolExportRecordsDb.Set()
  711. marshal, _ := json.Marshal(req)
  712. selfSupportForSchoolExportRecordsDb.SelfSupportForSchoolExportRecordsInsert(&model.SelfSupportForSchoolExportRecords{
  713. Name: fileName,
  714. DownloadPath: downloadPath,
  715. Kind: req.Kind,
  716. ReqContent: string(marshal),
  717. CreateAt: now.Format("2006-01-02 15:04:05"),
  718. UpdateAt: now.Format("2006-01-02 15:04:05"),
  719. })
  720. }
  721. func CentralKitchenForSchoolDataStatisticsExport(req md.CentralKitchenForSchoolDataStatisticsExportReq) {
  722. var titleList []string
  723. var fileName, downloadPath string
  724. var now = time.Now()
  725. for _, kind := range req.Kinds {
  726. switch kind {
  727. case 1:
  728. xlsx := excelize.NewFile()
  729. //订单列表
  730. fileName = "订单列表_" + utils.Int64ToStr(now.UnixMilli()) + ".xlsx"
  731. downloadPath = "/export/central_kitchen_for_school/" + fileName
  732. startDate, _ := time.ParseInLocation("2006-01-02 15:04:05", req.StartDate, time.Local)
  733. endDate, _ := time.ParseInLocation("2006-01-02 15:04:05", req.EndDate, time.Local)
  734. //查找订单数据
  735. sess := db.Db.Desc("central_kitchen_for_school_package_ord.id")
  736. sess.And("central_kitchen_for_school_package_ord.state =?", enum.CentralKitchenForSchoolPackageOrdStateForSuccess)
  737. if req.EnterpriseId != 0 {
  738. sess.And("central_kitchen_for_school_package_ord.enterprise_id =?", req.EnterpriseId)
  739. }
  740. if req.StartDate != "" {
  741. sess.And("central_kitchen_for_school_package_ord.create_at >= ?", req.StartDate)
  742. }
  743. if req.EndDate != "" {
  744. sess.And("central_kitchen_for_school_package_ord.create_at <= ?", req.EndDate)
  745. }
  746. if req.EnterPriseKind != 0 {
  747. sess.And("enterprise.kind = ?", req.EnterPriseKind)
  748. }
  749. var m []*db.CentralKitchenForSchoolPackageOrdWithUserIdentity
  750. _, err := sess.
  751. Join("LEFT", "user_identity", "central_kitchen_for_school_package_ord_for_reserve.user_identity_id = user_identity.id").
  752. Join("LEFT", "enterprise", "enterprise.id = user_identity.enterprise_id").
  753. Join("LEFT", "user", "user.id = user_identity.uid").
  754. Join("LEFT", "class_with_user", "class_with_user.user_identity_id = user_identity.id").
  755. Join("LEFT", "class", "class_with_user.class_id = class.id").
  756. Join("LEFT", "grade", "class.grade_id = grade.id").
  757. FindAndCount(&m)
  758. if err != nil {
  759. logx.Error(err)
  760. println("<<<<CentralKitchenForSchoolDataStatisticsExport1>>>>>Error:::", err.Error())
  761. return
  762. }
  763. //查找预定数据
  764. var outTradeNoList []string
  765. for _, v := range m {
  766. outTradeNoList = append(outTradeNoList, v.OutTradeNo)
  767. }
  768. var mm []model.CentralKitchenForSchoolPackageOrdForReserve
  769. sess1 := db.Db.Desc("id")
  770. if req.EnterpriseId != 0 {
  771. sess1.And("enterprise_id = ?", req.EnterpriseId)
  772. }
  773. if len(req.MealKindList) > 0 {
  774. sess1.In("kind", req.MealKindList)
  775. }
  776. err = sess1.In("out_trade_no", outTradeNoList).Find(&mm)
  777. if err != nil {
  778. logx.Error(err)
  779. println("<<<<CentralKitchenForSchoolDataStatisticsExport1>>>>>Error:::", err.Error())
  780. return
  781. }
  782. var exportOutTradeNoList []string
  783. for _, v := range mm {
  784. exportOutTradeNoList = append(exportOutTradeNoList, v.OutTradeNo)
  785. }
  786. titleList = []string{startDate.Format("2006-01-02") + "~~" + endDate.Format("2006-01-02") + " 订单表"}
  787. xlsx.SetSheetRow("Sheet1", "A1", &titleList)
  788. titleList = []string{"订单号", "学生姓名", "学校", "年级", "班级", "家长电话", "预定日期", "预定餐数", "订单价格", "支付时间", "订单状态"}
  789. xlsx.SetSheetRow("Sheet1", "A2", &titleList)
  790. xlsx.MergeCell("Sheet1", "A1", "K1") //合并单元格
  791. style, err := xlsx.NewStyle(`{"fill":{"type":"pattern","color":["FFFF00"],"pattern":1}, "alignment":{"horizontal":"center"}}`)
  792. if err != nil {
  793. logx.Error(err)
  794. println("<<<<CentralKitchenForSchoolDataStatisticsExport3>>>>>Error:::", err.Error())
  795. panic(err)
  796. }
  797. xlsx.SetCellStyle("Sheet1", "A1", "K1", style)
  798. xlsx.SetColWidth("Sheet1", "A", "k", 20)
  799. j := 3 //表头前两行用了,只能从第三行开始
  800. for _, v := range m {
  801. if !utils.InArr(v.OutTradeNo, exportOutTradeNoList) {
  802. continue
  803. }
  804. var ordState string //订单状态(0:待支付 1:预约成功 2:退款中 3:部分退款 4:已退款 5:已完成)
  805. switch v.CentralKitchenForSchoolPackageOrd.OrdState {
  806. case enum.CentralKitchenForSchoolPackageOrdOrdStateForWait:
  807. ordState = enum.CentralKitchenForSchoolPackageOrdOrdState.String(enum.CentralKitchenForSchoolPackageOrdOrdStateForWait)
  808. break
  809. case enum.CentralKitchenForSchoolPackageOrdOrdStateForSuccess:
  810. ordState = enum.CentralKitchenForSchoolPackageOrdOrdState.String(enum.CentralKitchenForSchoolPackageOrdOrdStateForSuccess)
  811. break
  812. case enum.CentralKitchenForSchoolPackageOrdOrdStateForRefunding:
  813. ordState = enum.CentralKitchenForSchoolPackageOrdOrdState.String(enum.CentralKitchenForSchoolPackageOrdOrdStateForRefunding)
  814. break
  815. case enum.CentralKitchenForSchoolPackageOrdOrdStateForPartRefunded:
  816. ordState = enum.CentralKitchenForSchoolPackageOrdOrdState.String(enum.CentralKitchenForSchoolPackageOrdOrdStateForPartRefunded)
  817. break
  818. case enum.CentralKitchenForSchoolPackageOrdOrdStateForRefunded:
  819. ordState = enum.CentralKitchenForSchoolPackageOrdOrdState.String(enum.CentralKitchenForSchoolPackageOrdOrdStateForRefunded)
  820. break
  821. case enum.CentralKitchenForSchoolPackageOrdOrdStateForComplete:
  822. ordState = enum.CentralKitchenForSchoolPackageOrdOrdState.String(enum.CentralKitchenForSchoolPackageOrdOrdStateForComplete)
  823. break
  824. }
  825. //预定餐数、预定日期
  826. var list []*model.CentralKitchenForSchoolUserWithDay
  827. err1 := json.Unmarshal([]byte(v.CentralKitchenForSchoolPackageOrd.WithDayData), &list)
  828. if err1 != nil {
  829. logx.Error(err)
  830. println("<<<<CentralKitchenForSchoolDataStatisticsExport1>>>>>Error:::", err.Error())
  831. return
  832. }
  833. var reserveDays string
  834. for _, v1 := range list {
  835. reserveDays += v1.Date + ", "
  836. }
  837. if v.Grade.Name == "" {
  838. v.Grade.Name = "教师"
  839. }
  840. if v.Class.Name == "" {
  841. v.Class.Name = "教师"
  842. }
  843. xlsx.SetSheetRow("Sheet1", "A"+strconv.Itoa(j), &[]interface{}{v.CentralKitchenForSchoolPackageOrd.OutTradeNo, v.CentralKitchenForSchoolPackageOrd.UserIdentityName, v.Enterprise.Name,
  844. v.Grade.Name, v.Class.Name, v.User.Phone, reserveDays, len(list), v.CentralKitchenForSchoolPackageOrd.TotalPrice, v.CentralKitchenForSchoolPackageOrd.CreateAt, ordState})
  845. j++
  846. }
  847. //将文件保存至服务器
  848. xlsx.SaveAs("./static" + downloadPath)
  849. break
  850. case 2:
  851. xlsx := excelize.NewFile()
  852. //退款申请表
  853. fileName = "退款申请表_" + utils.Int64ToStr(now.UnixMilli()) + ".xlsx"
  854. downloadPath = "/export/central_kitchen_for_school/" + fileName
  855. startDate, _ := time.ParseInLocation("2006-01-02 15:04:05", req.StartDate, time.Local)
  856. endDate, _ := time.ParseInLocation("2006-01-02 15:04:05", req.EndDate, time.Local)
  857. //查找退款申请数据
  858. sess := db.Db.Desc("central_kitchen_for_school_user_refund_day.id")
  859. if req.EnterpriseId != 0 {
  860. sess.And("enterprise.id =?", req.EnterpriseId)
  861. }
  862. if req.StartDate != "" {
  863. sess.And("central_kitchen_for_school_user_refund_day.create_at >= ?", req.StartDate)
  864. }
  865. if req.EndDate != "" {
  866. sess.And("central_kitchen_for_school_user_refund_day.create_at <= ?", req.EndDate)
  867. }
  868. if len(req.MealKindList) > 0 {
  869. sess.In("central_kitchen_for_school_user_with_day.kind", req.MealKindList)
  870. }
  871. if req.EnterPriseKind != 0 {
  872. sess.And("enterprise.kind = ?", req.EnterPriseKind)
  873. }
  874. var m []*db.CentralKitchenForSchoolUserRefundDayWithData
  875. _, err := sess.
  876. 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").
  877. 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").
  878. Join("LEFT", "user_identity", "central_kitchen_for_school_user_with_day.identity_id = user_identity.id").
  879. Join("LEFT", "user", "user_identity.uid = user.id").
  880. Join("LEFT", "enterprise", "user_identity.enterprise_id = enterprise.id").
  881. Join("LEFT", "class_with_user", "user_identity.id = class_with_user.user_identity_id").
  882. Join("LEFT", "class", "class_with_user.class_id = class.id").
  883. Join("LEFT", "grade", "class.grade_id = grade.id").
  884. Desc("central_kitchen_for_school_user_refund_day.id").
  885. FindAndCount(&m)
  886. if err != nil {
  887. logx.Error(err)
  888. println("<<<<CentralKitchenForSchoolDataStatisticsExport2>>>>>Error:::", err.Error())
  889. return
  890. }
  891. titleList = []string{startDate.Format("2006-01-02") + "~~" + endDate.Format("2006-01-02") + " 退款申请表"}
  892. xlsx.SetSheetRow("Sheet1", "A1", &titleList)
  893. titleList = []string{"学生姓名", "学校", "年级", "班级", "家长电话", "退款日期", "餐型", "退款金额", "退款状态", "支付时间", "退款操作时间"}
  894. xlsx.SetSheetRow("Sheet1", "A2", &titleList)
  895. xlsx.MergeCell("Sheet1", "A1", "K1") //合并单元格
  896. style, err := xlsx.NewStyle(`{"fill":{"type":"pattern","color":["FFFF00"],"pattern":1}, "alignment":{"horizontal":"center"}}`)
  897. if err != nil {
  898. logx.Error(err)
  899. println("<<<<CentralKitchenForSchoolDataStatisticsExport3>>>>>Error:::", err.Error())
  900. panic(err)
  901. }
  902. xlsx.SetCellStyle("Sheet1", "A1", "K1", style)
  903. xlsx.SetColWidth("Sheet1", "A", "k", 20)
  904. j := 3 //表头前两行用了,只能从第三行开始
  905. for _, v := range m {
  906. var kind string //就餐类型(1:早餐 2:午餐 3:晚餐)
  907. switch v.CentralKitchenForSchoolUserWithDay.Kind {
  908. case enum.CentralKitchenForSchoolUserWithDayKindForBreakfast:
  909. kind = enum.CentralKitchenForSchoolUserWithDayKind.String(enum.CentralKitchenForSchoolUserWithDayKindForBreakfast)
  910. break
  911. case enum.CentralKitchenForSchoolUserWithDayKindForLunch:
  912. kind = enum.CentralKitchenForSchoolUserWithDayKind.String(enum.CentralKitchenForSchoolUserWithDayKindForLunch)
  913. break
  914. case enum.CentralKitchenForSchoolUserWithDayKindForDinner:
  915. kind = enum.CentralKitchenForSchoolUserWithDayKind.String(enum.CentralKitchenForSchoolUserWithDayKindForDinner)
  916. break
  917. }
  918. var state string //状态(1:审核中 2:审核通过 3:审核拒绝 4:退款已完成)
  919. switch v.CentralKitchenForSchoolUserRefundDay.State {
  920. case enum.CentralKitchenForSchoolUserRefundDayStateForAuditing:
  921. state = enum.CentralKitchenForSchoolUserRefundDayState.String(enum.CentralKitchenForSchoolUserRefundDayStateForAuditing)
  922. break
  923. case enum.CentralKitchenForSchoolUserRefundDayStateForAuditPass:
  924. state = enum.CentralKitchenForSchoolUserRefundDayState.String(enum.CentralKitchenForSchoolUserRefundDayStateForAuditPass)
  925. break
  926. case enum.CentralKitchenForSchoolUserRefundDayStateForAuditReject:
  927. state = enum.CentralKitchenForSchoolUserRefundDayState.String(enum.CentralKitchenForSchoolUserRefundDayStateForAuditReject)
  928. break
  929. case enum.CentralKitchenForSchoolUserRefundDayStateForAuditComplete:
  930. state = enum.CentralKitchenForSchoolUserRefundDayState.String(enum.CentralKitchenForSchoolUserRefundDayStateForAuditComplete)
  931. break
  932. }
  933. xlsx.SetSheetRow("Sheet1", "A"+strconv.Itoa(j), &[]interface{}{v.CentralKitchenForSchoolPackageOrd.UserIdentityName, v.Enterprise.Name, v.Grade.Name, v.Class.Name, v.User.Phone,
  934. v.CentralKitchenForSchoolUserWithDay.Date, kind, v.CentralKitchenForSchoolUserRefundDay.Amount, state, v.CentralKitchenForSchoolPackageOrd.CreateAt, v.CentralKitchenForSchoolUserRefundDay.RefundDate})
  935. j++
  936. }
  937. //将文件保存至服务器
  938. xlsx.SaveAs("./static" + downloadPath)
  939. break
  940. case 3:
  941. //学校预定统计表
  942. fileName = "学校预定统计表_" + utils.Int64ToStr(now.UnixMilli()) + ".zip"
  943. downloadPath = "/export/central_kitchen_for_school/" + fileName
  944. var m []*db.CentralKitchenForSchoolPackageOrdForReserveWithUserIdentity
  945. sess := db.Db.Desc("central_kitchen_for_school_package_ord_for_reserve.id")
  946. if req.EnterpriseId != 0 {
  947. sess.And("central_kitchen_for_school_package_ord_for_reserve.enterprise_id =?", req.EnterpriseId)
  948. }
  949. var months []time.Month
  950. if req.StartDate != "" {
  951. sDate, _ := time.ParseInLocation("2006-01-02 15:04:05", req.StartDate, time.Local)
  952. eDate, _ := time.ParseInLocation("2006-01-02 15:04:05", req.EndDate, time.Local)
  953. _, sMonth, _ := sDate.Date()
  954. _, eMonth, _ := eDate.Date()
  955. diffMonth := sMonth - eMonth
  956. if diffMonth == 0 {
  957. //TODO::查询一个月
  958. months = append(months, sMonth)
  959. } else {
  960. for i := sMonth; i <= eMonth; i++ {
  961. months = append(months, i)
  962. }
  963. }
  964. }
  965. if len(months) > 0 {
  966. sess.In("central_kitchen_for_school_package_ord_for_reserve.reserve_month", months)
  967. }
  968. //if req.EndDate != "" {
  969. // sess.And("central_kitchen_for_school_package_ord_for_reserve.meal_time_start <= ?", req.EndDate)
  970. //}
  971. if len(req.MealKindList) > 0 {
  972. sess.In("central_kitchen_for_school_package_ord_for_reserve.kind", req.MealKindList)
  973. }
  974. if req.EnterPriseKind != 0 {
  975. sess.And("enterprise.kind = ?", req.EnterPriseKind)
  976. }
  977. _, err := sess.
  978. 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").
  979. Join("LEFT", "user_identity", "central_kitchen_for_school_package_ord_for_reserve.user_identity_id = user_identity.id").
  980. Join("LEFT", "enterprise", "enterprise.id = user_identity.enterprise_id").
  981. Join("LEFT", "user", "user.id = user_identity.uid").
  982. Join("LEFT", "class_with_user", "class_with_user.user_identity_id = user_identity.id").
  983. Join("LEFT", "class", "class_with_user.class_id = class.id").
  984. Join("LEFT", "grade", "class.grade_id = grade.id").
  985. FindAndCount(&m)
  986. if err != nil {
  987. logx.Error(err)
  988. println("<<<<SelfSupportForSchoolDataStatisticsExport3>>>>>Error:::", err.Error())
  989. return
  990. }
  991. var ordNoArr, mealKindListArr []string
  992. for _, v := range m {
  993. ordNoArr = append(ordNoArr, v.CentralKitchenForSchoolPackageOrdForReserve.OutTradeNo)
  994. }
  995. var nativeSql = "SELECT COUNT(*) AS total, kind , ord_no FROM `central_kitchen_for_school_user_with_day` WHERE `ord_no` IN (%s) AND (state !=%d) AND (state !=%d) AND (DATE >= '%s') AND (DATE <= '%s') AND `kind` IN (%s) GROUP BY `ord_no`, `kind`"
  996. for _, v := range req.MealKindList {
  997. mealKindListArr = append(mealKindListArr, utils.IntToStr(v))
  998. }
  999. sDate, _ := time.ParseInLocation("2006-01-02 15:04:05", req.StartDate, time.Local)
  1000. eDate, _ := time.ParseInLocation("2006-01-02 15:04:05", req.EndDate, time.Local)
  1001. nativeSql = fmt.Sprintf(nativeSql, strings.Join(ordNoArr, ","), enum.CentralKitchenForSchoolUserWithDayStateForCancel, enum.CentralKitchenForSchoolUserWithDayStateForCanceling, sDate.Format("2006-01-02"), eDate.Format("2006-01-02"), strings.Join(mealKindListArr, ","))
  1002. nativeResults, err := db.QueryNativeString(db.Db, nativeSql)
  1003. var nativeResultsMap = map[string]map[int]int64{}
  1004. for _, v := range nativeResults {
  1005. if nativeResultsMap[v["ord_no"]] == nil {
  1006. nativeResultsMap[v["ord_no"]] = make(map[int]int64)
  1007. }
  1008. nativeResultsMap[v["ord_no"]][utils.StrToInt(v["kind"])] = utils.StrToInt64(v["total"])
  1009. }
  1010. centralKitchenForSchoolUserWithDayDb := db.CentralKitchenForSchoolUserWithDayDb{}
  1011. centralKitchenForSchoolUserWithDayDb.Set(0)
  1012. var list = map[string]map[string]map[string]map[string]int64{}
  1013. var xlsFileName, xlsDownloadPath []string
  1014. for _, v := range m {
  1015. if list[v.Enterprise.Name] == nil {
  1016. list[v.Enterprise.Name] = make(map[string]map[string]map[string]int64)
  1017. }
  1018. if list[v.Enterprise.Name][v.Grade.Name] == nil {
  1019. list[v.Enterprise.Name][v.Grade.Name] = make(map[string]map[string]int64)
  1020. }
  1021. if list[v.Enterprise.Name][v.Grade.Name][v.Class.Name] == nil {
  1022. list[v.Enterprise.Name][v.Grade.Name][v.Class.Name] = make(map[string]int64)
  1023. }
  1024. //count, err2 := centralKitchenForSchoolUserWithDayDb.CountCentralKitchenForSchoolUserWithDayByOrdNo(
  1025. // v.CentralKitchenForSchoolPackageOrdForReserve.OutTradeNo,
  1026. // enum.CentralKitchenForSchoolUserWithDayStateForCancel, enum.CentralKitchenForSchoolUserWithDayStateForCanceling,
  1027. // req.StartDate, req.EndDate, []int{v.CentralKitchenForSchoolPackageOrdForReserve.Kind})
  1028. //if err2 != nil {
  1029. // logx.Error(err)
  1030. // println("<<<<SelfSupportForSchoolDataStatisticsExport3>>>>>Error:::", err.Error())
  1031. // return
  1032. //}
  1033. //time.Sleep(2 * time.Millisecond) // 2毫秒
  1034. //if v.CentralKitchenForSchoolPackageOrdForReserve.Kind == enum.CentralKitchenForSchoolUserWithDayKindForBreakfast {
  1035. // list[v.Enterprise.Name][v.Grade.Name][v.Class.Name]["breakfast"] += count
  1036. //}
  1037. //if v.CentralKitchenForSchoolPackageOrdForReserve.Kind == enum.CentralKitchenForSchoolUserWithDayKindForLunch {
  1038. // list[v.Enterprise.Name][v.Grade.Name][v.Class.Name]["lunch"] += count
  1039. //}
  1040. //if v.CentralKitchenForSchoolPackageOrdForReserve.Kind == enum.CentralKitchenForSchoolUserWithDayKindForDinner {
  1041. // list[v.Enterprise.Name][v.Grade.Name][v.Class.Name]["dinner"] += count
  1042. //}
  1043. if v.CentralKitchenForSchoolPackageOrdForReserve.Kind == enum.CentralKitchenForSchoolUserWithDayKindForBreakfast {
  1044. list[v.Enterprise.Name][v.Grade.Name][v.Class.Name]["breakfast"] += nativeResultsMap[v.CentralKitchenForSchoolPackageOrdForReserve.OutTradeNo][enum.CentralKitchenForSchoolUserWithDayKindForBreakfast]
  1045. }
  1046. if v.CentralKitchenForSchoolPackageOrdForReserve.Kind == enum.CentralKitchenForSchoolUserWithDayKindForLunch {
  1047. list[v.Enterprise.Name][v.Grade.Name][v.Class.Name]["lunch"] += nativeResultsMap[v.CentralKitchenForSchoolPackageOrdForReserve.OutTradeNo][enum.CentralKitchenForSchoolUserWithDayKindForLunch]
  1048. }
  1049. if v.CentralKitchenForSchoolPackageOrdForReserve.Kind == enum.CentralKitchenForSchoolUserWithDayKindForDinner {
  1050. list[v.Enterprise.Name][v.Grade.Name][v.Class.Name]["dinner"] += nativeResultsMap[v.CentralKitchenForSchoolPackageOrdForReserve.OutTradeNo][enum.CentralKitchenForSchoolUserWithDayKindForDinner]
  1051. }
  1052. }
  1053. for k1, v1 := range list {
  1054. xlsx := excelize.NewFile()
  1055. tmpFileName := k1 + "学校预定统计表_" + utils.Int64ToStr(now.UnixMilli()) + ".xlsx"
  1056. tmpDownloadPath := "./static/export/central_kitchen_for_school/" + tmpFileName
  1057. startDate, _ := time.ParseInLocation("2006-01-02 15:04:05", req.StartDate, time.Local)
  1058. endDate, _ := time.ParseInLocation("2006-01-02 15:04:05", req.EndDate, time.Local)
  1059. titleList = []string{startDate.Format("2006-01-02") + "~~" + endDate.Format("2006-01-02") + " (" + k1 + ") "}
  1060. xlsx.SetSheetRow("Sheet1", "A1", &titleList)
  1061. titleList = []string{"年级", "班级", "早餐", "午餐", "晚餐"}
  1062. xlsx.SetSheetRow("Sheet1", "A2", &titleList)
  1063. xlsx.MergeCell("Sheet1", "A1", "E1") //合并单元格
  1064. style, err := xlsx.NewStyle(`{"fill":{"type":"pattern","color":["FFFF00"],"pattern":1}, "alignment":{"horizontal":"center"}}`)
  1065. if err != nil {
  1066. logx.Error(err)
  1067. println("<<<<CentralKitchenForSchoolDataStatisticsExport3>>>>>Error:::", err.Error())
  1068. panic(err)
  1069. }
  1070. xlsx.SetCellStyle("Sheet1", "A1", "E1", style)
  1071. xlsx.SetColWidth("Sheet1", "A", "E", 20)
  1072. j := 3 //表头前两行用了,只能从第三行开始
  1073. var sumClassNum, sumBreakfast, sumLunch, sumDinner = int64(0), int64(0), int64(0), int64(0)
  1074. tmpEnterpriseDb := db.EnterpriseDb{}
  1075. tmpEnterpriseDb.Set()
  1076. tmpEnterprise, err11 := tmpEnterpriseDb.GetEnterpriseByName(k1)
  1077. if err11 != nil {
  1078. logx.Error(err11)
  1079. println("<<<<CentralKitchenForSchoolDataStatisticsExport3>>>>>Error:::", err11.Error())
  1080. panic(err11)
  1081. }
  1082. tmpClassDb := db.ClassDb{}
  1083. tmpClassDb.Set(0)
  1084. classList, err := tmpClassDb.FindClassAscByEnterprise(tmpEnterprise.Id)
  1085. if err != nil {
  1086. logx.Error(err)
  1087. println("<<<<CentralKitchenForSchoolDataStatisticsExport4>>>>>Error:::", err.Error())
  1088. panic(err)
  1089. }
  1090. for _, class := range *classList {
  1091. for k2, v2 := range v1 {
  1092. if k2 == "" {
  1093. //若年级为空,则为教师餐
  1094. k2 = "教师餐"
  1095. }
  1096. for k3, v3 := range v2 {
  1097. if class.Name != k3 {
  1098. continue
  1099. }
  1100. if k3 == "" {
  1101. //若班级为空,则为教师餐
  1102. k3 = "教师餐"
  1103. }
  1104. xlsx.SetSheetRow("Sheet1", "A"+strconv.Itoa(j), &[]interface{}{
  1105. k2, k3, v3["breakfast"], v3["lunch"], v3["dinner"]})
  1106. sumClassNum++
  1107. sumBreakfast += v3["breakfast"]
  1108. sumLunch += v3["lunch"]
  1109. sumDinner += v3["dinner"]
  1110. j++
  1111. }
  1112. }
  1113. }
  1114. xlsx.SetSheetRow("Sheet1", "A"+strconv.Itoa(j), &[]interface{}{
  1115. "合计", sumClassNum, sumBreakfast, sumLunch, sumDinner})
  1116. //将文件保存至服务器
  1117. err1 := xlsx.SaveAs(tmpDownloadPath)
  1118. if err1 != nil {
  1119. logx.Error(err1)
  1120. println("<<<<CentralKitchenForSchoolDataStatisticsExport3>>>>>Error:::", err1.Error())
  1121. panic(err1)
  1122. }
  1123. xlsDownloadPath = append(xlsDownloadPath, tmpDownloadPath)
  1124. xlsFileName = append(xlsFileName, tmpFileName)
  1125. }
  1126. archive, err := os.Create("./static" + downloadPath) //第一步,创建 zip 文件
  1127. if err != nil {
  1128. logx.Error(err)
  1129. println("<<<<CentralKitchenForSchoolDataStatisticsExport3>>>>>Error:::", err.Error())
  1130. panic(err)
  1131. }
  1132. defer archive.Close()
  1133. zipWriter := zip.NewWriter(archive) //第二步,创建一个新的 *Writer 对象
  1134. for k, v := range xlsDownloadPath {
  1135. w, err1 := zipWriter.Create("学校预定统计/" + xlsFileName[k]) //向 zip 文件中添加一个文件,返回一个待压缩的文件内容应写入的 Writer
  1136. if err1 != nil {
  1137. logx.Error(err1)
  1138. println("<<<<CentralKitchenForSchoolDataStatisticsExport3>>>>>Error:::", err1.Error())
  1139. panic(err1)
  1140. }
  1141. f, err1 := os.Open(v) //打开待压缩的文件
  1142. if err1 != nil {
  1143. logx.Error(err1)
  1144. println("<<<<CentralKitchenForSchoolDataStatisticsExport3>>>>>Error:::", err1.Error())
  1145. panic(err1)
  1146. }
  1147. if _, err1 = io.Copy(w, f); err != nil {
  1148. logx.Error(err1)
  1149. println("<<<<CentralKitchenForSchoolDataStatisticsExport3>>>>>Error:::", err1.Error())
  1150. panic(err1)
  1151. }
  1152. if err1 != nil {
  1153. logx.Error(err1)
  1154. println("<<<<CentralKitchenForSchoolDataStatisticsExport3>>>>>Error:::", err1.Error())
  1155. panic(err1)
  1156. }
  1157. f.Close()
  1158. os.RemoveAll(v) //TODO::移除源文件
  1159. }
  1160. zipWriter.Close() // 第四步,关闭 zip writer,将所有数据写入指向基础 zip 文件的数据流
  1161. if err != nil {
  1162. panic(err)
  1163. }
  1164. break
  1165. case 4:
  1166. //班级明细表
  1167. fileName = "班级明细表_" + utils.Int64ToStr(now.UnixMilli()) + ".zip"
  1168. downloadPath = "/export/central_kitchen_for_school/" + fileName
  1169. var m []*db.CentralKitchenForSchoolPackageOrdForReserveWithUserIdentity
  1170. sess := db.Db.Desc("central_kitchen_for_school_package_ord_for_reserve.id")
  1171. if req.EnterpriseId != 0 {
  1172. sess.And("central_kitchen_for_school_package_ord_for_reserve.enterprise_id =?", req.EnterpriseId)
  1173. }
  1174. var months []time.Month
  1175. if req.StartDate != "" {
  1176. sDate, _ := time.ParseInLocation("2006-01-02 15:04:05", req.StartDate, time.Local)
  1177. eDate, _ := time.ParseInLocation("2006-01-02 15:04:05", req.EndDate, time.Local)
  1178. _, sMonth, _ := sDate.Date()
  1179. _, eMonth, _ := eDate.Date()
  1180. diffMonth := sMonth - eMonth
  1181. if diffMonth == 0 {
  1182. //TODO::查询一个月
  1183. months = append(months, sMonth)
  1184. } else {
  1185. for i := sMonth; i <= eMonth; i++ {
  1186. months = append(months, i)
  1187. }
  1188. }
  1189. }
  1190. if len(months) > 0 {
  1191. sess.In("central_kitchen_for_school_package_ord_for_reserve.reserve_month", months)
  1192. }
  1193. //if req.EndDate != "" {
  1194. // sess.And("central_kitchen_for_school_package_ord_for_reserve.meal_time_start <= ?", req.EndDate)
  1195. //}
  1196. if len(req.MealKindList) > 0 {
  1197. sess.In("central_kitchen_for_school_package_ord_for_reserve.kind", req.MealKindList)
  1198. }
  1199. if req.EnterPriseKind != 0 {
  1200. sess.And("enterprise.kind = ?", req.EnterPriseKind)
  1201. }
  1202. _, err := sess.
  1203. 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").
  1204. Join("LEFT", "user_identity", "central_kitchen_for_school_package_ord_for_reserve.user_identity_id = user_identity.id").
  1205. Join("LEFT", "enterprise", "enterprise.id = user_identity.enterprise_id").
  1206. Join("LEFT", "user", "user.id = user_identity.uid").
  1207. Join("LEFT", "class_with_user", "class_with_user.user_identity_id = user_identity.id").
  1208. Join("LEFT", "class", "class_with_user.class_id = class.id").
  1209. Join("LEFT", "grade", "class.grade_id = grade.id").
  1210. Asc("class.id").
  1211. FindAndCount(&m)
  1212. if err != nil {
  1213. logx.Error(err)
  1214. println("<<<<CentralKitchenForSchoolDataStatisticsExport4>>>>>Error:::", err.Error())
  1215. return
  1216. }
  1217. var ordNoArr, mealKindListArr []string
  1218. for _, v := range m {
  1219. ordNoArr = append(ordNoArr, v.CentralKitchenForSchoolPackageOrdForReserve.OutTradeNo)
  1220. }
  1221. var nativeSql = "SELECT COUNT(*) AS total, kind , ord_no FROM `central_kitchen_for_school_user_with_day` WHERE `ord_no` IN (%s) AND (state !=%d) AND (state !=%d) AND (DATE >= '%s') AND (DATE <= '%s') AND `kind` IN (%s) GROUP BY `ord_no`, `kind`"
  1222. for _, v := range req.MealKindList {
  1223. mealKindListArr = append(mealKindListArr, utils.IntToStr(v))
  1224. }
  1225. sDate, _ := time.ParseInLocation("2006-01-02 15:04:05", req.StartDate, time.Local)
  1226. eDate, _ := time.ParseInLocation("2006-01-02 15:04:05", req.EndDate, time.Local)
  1227. nativeSql = fmt.Sprintf(nativeSql, strings.Join(ordNoArr, ","), enum.CentralKitchenForSchoolUserWithDayStateForCancel, enum.CentralKitchenForSchoolUserWithDayStateForCanceling, sDate.Format("2006-01-02"), eDate.Format("2006-01-02"), strings.Join(mealKindListArr, ","))
  1228. nativeResults, err := db.QueryNativeString(db.Db, nativeSql)
  1229. var nativeResultsMap = map[string]map[int]int64{}
  1230. for _, v := range nativeResults {
  1231. if nativeResultsMap[v["ord_no"]] == nil {
  1232. nativeResultsMap[v["ord_no"]] = make(map[int]int64)
  1233. }
  1234. nativeResultsMap[v["ord_no"]][utils.StrToInt(v["kind"])] = utils.StrToInt64(v["total"])
  1235. }
  1236. var list = map[string]map[string]map[string]map[string]map[string]int64{}
  1237. var xlsFileName, xlsDownloadPath []string
  1238. centralKitchenForSchoolUserWithDayDb := db.CentralKitchenForSchoolUserWithDayDb{}
  1239. centralKitchenForSchoolUserWithDayDb.Set(0)
  1240. for _, v := range m {
  1241. if list[v.Enterprise.Name] == nil {
  1242. list[v.Enterprise.Name] = make(map[string]map[string]map[string]map[string]int64)
  1243. }
  1244. if list[v.Enterprise.Name][v.Grade.Name] == nil {
  1245. list[v.Enterprise.Name][v.Grade.Name] = make(map[string]map[string]map[string]int64)
  1246. }
  1247. if list[v.Enterprise.Name][v.Grade.Name][v.Class.Name] == nil {
  1248. list[v.Enterprise.Name][v.Grade.Name][v.Class.Name] = make(map[string]map[string]int64)
  1249. }
  1250. if list[v.Enterprise.Name][v.Grade.Name][v.Class.Name][v.UserIdentity.Name] == nil {
  1251. list[v.Enterprise.Name][v.Grade.Name][v.Class.Name][v.UserIdentity.Name] = make(map[string]int64)
  1252. }
  1253. //count, err2 := centralKitchenForSchoolUserWithDayDb.CountCentralKitchenForSchoolUserWithDayByOrdNo(
  1254. // v.CentralKitchenForSchoolPackageOrdForReserve.OutTradeNo,
  1255. // enum.CentralKitchenForSchoolUserWithDayStateForCancel, enum.CentralKitchenForSchoolUserWithDayStateForCanceling,
  1256. // req.StartDate, req.EndDate, []int{v.CentralKitchenForSchoolPackageOrdForReserve.Kind})
  1257. //if err2 != nil {
  1258. // logx.Error(err2)
  1259. // println("<<<<SelfSupportForSchoolDataStatistics Export3>>>>>Error:::", err2.Error())
  1260. // return
  1261. //}
  1262. //if count == 0 {
  1263. // continue
  1264. //}
  1265. //time.Sleep(2 * time.Millisecond) // 2毫秒
  1266. //
  1267. //if v.CentralKitchenForSchoolPackageOrdForReserve.Kind == enum.CentralKitchenForSchoolUserWithDayKindForBreakfast {
  1268. // list[v.Enterprise.Name][v.Grade.Name][v.Class.Name][v.UserIdentity.Name]["breakfast"] += count
  1269. //}
  1270. //if v.CentralKitchenForSchoolPackageOrdForReserve.Kind == enum.CentralKitchenForSchoolUserWithDayKindForLunch {
  1271. // list[v.Enterprise.Name][v.Grade.Name][v.Class.Name][v.UserIdentity.Name]["lunch"] += count
  1272. //}
  1273. //if v.CentralKitchenForSchoolPackageOrdForReserve.Kind == enum.CentralKitchenForSchoolUserWithDayKindForDinner {
  1274. // list[v.Enterprise.Name][v.Grade.Name][v.Class.Name][v.UserIdentity.Name]["dinner"] += count
  1275. //}
  1276. if v.CentralKitchenForSchoolPackageOrdForReserve.Kind == enum.CentralKitchenForSchoolUserWithDayKindForBreakfast {
  1277. list[v.Enterprise.Name][v.Grade.Name][v.Class.Name][v.UserIdentity.Name]["breakfast"] += nativeResultsMap[v.CentralKitchenForSchoolPackageOrdForReserve.OutTradeNo][enum.CentralKitchenForSchoolUserWithDayKindForBreakfast]
  1278. }
  1279. if v.CentralKitchenForSchoolPackageOrdForReserve.Kind == enum.CentralKitchenForSchoolUserWithDayKindForLunch {
  1280. list[v.Enterprise.Name][v.Grade.Name][v.Class.Name][v.UserIdentity.Name]["lunch"] += nativeResultsMap[v.CentralKitchenForSchoolPackageOrdForReserve.OutTradeNo][enum.CentralKitchenForSchoolUserWithDayKindForLunch]
  1281. }
  1282. if v.CentralKitchenForSchoolPackageOrdForReserve.Kind == enum.CentralKitchenForSchoolUserWithDayKindForDinner {
  1283. list[v.Enterprise.Name][v.Grade.Name][v.Class.Name][v.UserIdentity.Name]["dinner"] += nativeResultsMap[v.CentralKitchenForSchoolPackageOrdForReserve.OutTradeNo][enum.CentralKitchenForSchoolUserWithDayKindForDinner]
  1284. }
  1285. }
  1286. for k1, v1 := range list {
  1287. xlsx := excelize.NewFile()
  1288. tmpFileName := k1 + "班级明细_" + utils.Int64ToStr(now.UnixMilli()) + ".xlsx"
  1289. tmpDownloadPath := "./static/export/central_kitchen_for_school/" + tmpFileName
  1290. startDate, _ := time.ParseInLocation("2006-01-02 15:04:05", req.StartDate, time.Local)
  1291. endDate, _ := time.ParseInLocation("2006-01-02 15:04:05", req.EndDate, time.Local)
  1292. //查询当前学校下的班级升序
  1293. tmpEnterpriseDb := db.EnterpriseDb{}
  1294. tmpEnterpriseDb.Set()
  1295. tmpEnterprise, err11 := tmpEnterpriseDb.GetEnterpriseByName(k1)
  1296. if err11 != nil {
  1297. logx.Error(err11)
  1298. println("<<<<CentralKitchenForSchoolDataStatisticsExport4>>>>>Error:::", err11.Error())
  1299. panic(err11)
  1300. }
  1301. tmpClassDb := db.ClassDb{}
  1302. tmpClassDb.Set(0)
  1303. classList, err := tmpClassDb.FindClassAscByEnterprise(tmpEnterprise.Id)
  1304. if err != nil {
  1305. logx.Error(err)
  1306. println("<<<<CentralKitchenForSchoolDataStatisticsExport4>>>>>Error:::", err.Error())
  1307. panic(err)
  1308. }
  1309. for _, class := range *classList {
  1310. for k2, v2 := range v1 {
  1311. if k2 == "" {
  1312. //若年级为空,则为教师餐
  1313. continue
  1314. }
  1315. for k3, v3 := range v2 {
  1316. if class.Name != k3 {
  1317. continue
  1318. }
  1319. xlsx.NewSheet(k3)
  1320. titleList = []string{startDate.Format("2006-01-02") + "~~" + endDate.Format("2006-01-02") + " [" + k1 + ", " + k3 + "] 班级明细"}
  1321. xlsx.SetSheetRow(k3, "A1", &titleList)
  1322. titleList = []string{"姓名", "早餐", "午餐", "晚餐"}
  1323. xlsx.SetSheetRow(k3, "A2", &titleList)
  1324. xlsx.MergeCell(k3, "A1", "D1") //合并单元格
  1325. style, err := xlsx.NewStyle(`{"fill":{"type":"pattern","color":["FFFF00"],"pattern":1}, "alignment":{"horizontal":"center"}}`)
  1326. if err != nil {
  1327. logx.Error(err)
  1328. println("<<<<CentralKitchenForSchoolDataStatisticsExport3>>>>>Error:::", err.Error())
  1329. panic(err)
  1330. }
  1331. xlsx.SetCellStyle(k3, "A1", "D1", style)
  1332. xlsx.SetColWidth(k3, "A", "D", 20)
  1333. var sumBreakfast, sumLunch, sumDinner = int64(0), int64(0), int64(0)
  1334. j := 3 //表头前两行用了,只能从第三行开始
  1335. for k4, v4 := range v3 {
  1336. if v4["breakfast"] == 0 && v4["lunch"] == 0 && v4["dinner"] == 0 {
  1337. continue
  1338. }
  1339. xlsx.SetSheetRow(k3, "A"+strconv.Itoa(j), &[]interface{}{
  1340. k4, v4["breakfast"], v4["lunch"], v4["dinner"]})
  1341. sumBreakfast += v4["breakfast"]
  1342. sumLunch += v4["lunch"]
  1343. sumDinner += v4["dinner"]
  1344. j++
  1345. }
  1346. xlsx.SetSheetRow(k3, "A"+strconv.Itoa(j), &[]interface{}{
  1347. "合计", sumBreakfast, sumLunch, sumDinner})
  1348. }
  1349. }
  1350. }
  1351. //将文件保存至服务器
  1352. xlsx.DeleteSheet("Sheet1") //TODO::只能放这里删除
  1353. err1 := xlsx.SaveAs(tmpDownloadPath)
  1354. if err1 != nil {
  1355. logx.Error(err1)
  1356. println("<<<<CentralKitchenForSchoolDataStatisticsExport4>>>>>Error:::", err1.Error())
  1357. panic(err1)
  1358. }
  1359. xlsDownloadPath = append(xlsDownloadPath, tmpDownloadPath)
  1360. xlsFileName = append(xlsFileName, tmpFileName)
  1361. }
  1362. archive, err := os.Create("./static" + downloadPath) //第一步,创建 zip 文件
  1363. if err != nil {
  1364. logx.Error(err)
  1365. println("<<<<CentralKitchenForSchoolDataStatisticsExport4>>>>>Error:::", err.Error())
  1366. panic(err)
  1367. }
  1368. defer archive.Close()
  1369. zipWriter := zip.NewWriter(archive) //第二步,创建一个新的 *Writer 对象
  1370. for k, v := range xlsDownloadPath {
  1371. w, err1 := zipWriter.Create("班级明细/" + xlsFileName[k]) //向 zip 文件中添加一个文件,返回一个待压缩的文件内容应写入的 Writer
  1372. if err1 != nil {
  1373. logx.Error(err1)
  1374. println("<<<<CentralKitchenForSchoolDataStatisticsExport4>>>>>Error:::", err1.Error())
  1375. panic(err1)
  1376. }
  1377. f, err1 := os.Open(v) //打开待压缩的文件
  1378. if err1 != nil {
  1379. logx.Error(err1)
  1380. println("<<<<CentralKitchenForSchoolDataStatisticsExport4>>>>>Error:::", err1.Error())
  1381. panic(err1)
  1382. }
  1383. if _, err1 = io.Copy(w, f); err != nil {
  1384. logx.Error(err1)
  1385. println("<<<<CentralKitchenForSchoolDataStatisticsExport4>>>>>Error:::", err1.Error())
  1386. panic(err1)
  1387. }
  1388. if err1 != nil {
  1389. logx.Error(err1)
  1390. println("<<<<CentralKitchenForSchoolDataStatisticsExport4>>>>>Error:::", err1.Error())
  1391. panic(err1)
  1392. }
  1393. f.Close()
  1394. os.RemoveAll(v) //TODO::移除源文件
  1395. }
  1396. zipWriter.Close() // 第四步,关闭 zip writer,将所有数据写入指向基础 zip 文件的数据流
  1397. if err != nil {
  1398. panic(err)
  1399. }
  1400. break
  1401. case 5:
  1402. xlsx := excelize.NewFile()
  1403. //退款明细表
  1404. fileName = "退款明细表_" + utils.Int64ToStr(now.UnixMilli()) + ".zip"
  1405. downloadPath = "/export/central_kitchen_for_school/" + fileName
  1406. sess := db.Db.Where("central_kitchen_for_school_user_refund_day.state =?", enum.CentralKitchenForSchoolUserRefundDayStateForAuditComplete).Desc("central_kitchen_for_school_user_refund_day.id")
  1407. if req.EnterpriseId != 0 {
  1408. sess.And("enterprise.id =?", req.EnterpriseId)
  1409. }
  1410. if req.StartDate != "" {
  1411. sess.And("central_kitchen_for_school_user_refund_day.create_at >= ?", req.StartDate)
  1412. }
  1413. if req.EndDate != "" {
  1414. sess.And("central_kitchen_for_school_user_refund_day.create_at <= ?", req.EndDate)
  1415. }
  1416. if len(req.MealKindList) > 0 {
  1417. sess.In("central_kitchen_for_school_user_with_day.kind", req.MealKindList)
  1418. }
  1419. if req.EnterPriseKind != 0 {
  1420. sess.And("enterprise.kind = ?", req.EnterPriseKind)
  1421. }
  1422. var m []*db.CentralKitchenForSchoolUserRefundDayWithData
  1423. _, err := sess.
  1424. 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").
  1425. 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").
  1426. Join("LEFT", "user_identity", "central_kitchen_for_school_user_with_day.identity_id = user_identity.id").
  1427. Join("LEFT", "user", "user_identity.uid = user.id").
  1428. Join("LEFT", "enterprise", "user_identity.enterprise_id = enterprise.id").
  1429. Join("LEFT", "class_with_user", "user_identity.id = class_with_user.user_identity_id").
  1430. Join("LEFT", "class", "class_with_user.class_id = class.id").
  1431. Join("LEFT", "grade", "class.grade_id = grade.id").
  1432. Desc("central_kitchen_for_school_user_refund_day.id").
  1433. FindAndCount(&m)
  1434. if err != nil {
  1435. logx.Error(err)
  1436. println("<<<<CentralKitchenForSchoolDataStatisticsExport5>>>>>Error:::", err.Error())
  1437. return
  1438. }
  1439. var list = map[string]map[string]map[string]map[string]map[string]int{}
  1440. var xlsFileName, xlsDownloadPath []string
  1441. for _, v := range m {
  1442. if list[v.Enterprise.Name] == nil {
  1443. list[v.Enterprise.Name] = make(map[string]map[string]map[string]map[string]int)
  1444. }
  1445. if list[v.Enterprise.Name][v.Grade.Name] == nil {
  1446. list[v.Enterprise.Name][v.Grade.Name] = make(map[string]map[string]map[string]int)
  1447. }
  1448. if list[v.Enterprise.Name][v.Grade.Name][v.Class.Name] == nil {
  1449. list[v.Enterprise.Name][v.Grade.Name][v.Class.Name] = make(map[string]map[string]int)
  1450. }
  1451. if list[v.Enterprise.Name][v.Grade.Name][v.Class.Name][v.UserIdentity.Name] == nil {
  1452. list[v.Enterprise.Name][v.Grade.Name][v.Class.Name][v.UserIdentity.Name] = make(map[string]int)
  1453. }
  1454. //就餐类型(1:早餐 2:午餐 3:晚餐)
  1455. if v.CentralKitchenForSchoolUserWithDay.Kind == enum.CentralKitchenForSchoolUserWithDayKindForBreakfast {
  1456. list[v.Enterprise.Name][v.Grade.Name][v.Class.Name][v.UserIdentity.Name]["breakfast"]++
  1457. }
  1458. if v.CentralKitchenForSchoolUserWithDay.Kind == enum.CentralKitchenForSchoolUserWithDayKindForLunch {
  1459. list[v.Enterprise.Name][v.Grade.Name][v.Class.Name][v.UserIdentity.Name]["lunch"]++
  1460. }
  1461. if v.CentralKitchenForSchoolUserWithDay.Kind == enum.CentralKitchenForSchoolUserWithDayKindForDinner {
  1462. list[v.Enterprise.Name][v.Grade.Name][v.Class.Name][v.UserIdentity.Name]["dinner"]++
  1463. }
  1464. }
  1465. for k1, v1 := range list {
  1466. tmpFileName := k1 + "退款明细_" + utils.Int64ToStr(now.UnixMilli()) + ".xlsx"
  1467. tmpDownloadPath := "./static/export/central_kitchen_for_school/" + tmpFileName
  1468. startDate, _ := time.ParseInLocation("2006-01-02 15:04:05", req.StartDate, time.Local)
  1469. endDate, _ := time.ParseInLocation("2006-01-02 15:04:05", req.EndDate, time.Local)
  1470. for k2, v2 := range v1 {
  1471. if k2 == "" {
  1472. //若年级为空,则为教师餐
  1473. continue
  1474. }
  1475. for k3, v3 := range v2 {
  1476. xlsx.NewSheet(k3)
  1477. titleList = []string{startDate.Format("2006-01-02") + "~~" + endDate.Format("2006-01-02") + " [" + k1 + ", " + k3 + "] 退款明细"}
  1478. xlsx.SetSheetRow(k3, "A1", &titleList)
  1479. titleList = []string{"姓名", "早餐", "午餐", "晚餐"}
  1480. xlsx.SetSheetRow(k3, "A2", &titleList)
  1481. xlsx.MergeCell(k3, "A1", "D1") //合并单元格
  1482. style, err := xlsx.NewStyle(`{"fill":{"type":"pattern","color":["FFFF00"],"pattern":1}, "alignment":{"horizontal":"center"}}`)
  1483. if err != nil {
  1484. logx.Error(err)
  1485. println("<<<<CentralKitchenForSchoolDataStatisticsExport3>>>>>Error:::", err.Error())
  1486. panic(err)
  1487. }
  1488. xlsx.SetCellStyle(k3, "A1", "D1", style)
  1489. xlsx.SetColWidth(k3, "A", "D", 20)
  1490. var sumBreakfast, sumLunch, sumDinner = 0, 0, 0
  1491. j := 3 //表头前两行用了,只能从第三行开始
  1492. for k4, v4 := range v3 {
  1493. xlsx.SetSheetRow(k3, "A"+strconv.Itoa(j), &[]interface{}{
  1494. k4, v4["breakfast"], v4["lunch"], v4["dinner"]})
  1495. sumBreakfast += v4["breakfast"]
  1496. sumLunch += v4["lunch"]
  1497. sumDinner += v4["dinner"]
  1498. j++
  1499. }
  1500. xlsx.SetSheetRow(k3, "A"+strconv.Itoa(j), &[]interface{}{
  1501. "合计", sumBreakfast, sumLunch, sumDinner})
  1502. }
  1503. }
  1504. //将文件保存至服务器
  1505. xlsx.DeleteSheet("Sheet1") //TODO::只能放这里删除
  1506. err1 := xlsx.SaveAs(tmpDownloadPath)
  1507. if err1 != nil {
  1508. logx.Error(err1)
  1509. println("<<<<CentralKitchenForSchoolDataStatisticsExport5>>>>>Error:::", err1.Error())
  1510. panic(err1)
  1511. }
  1512. xlsDownloadPath = append(xlsDownloadPath, tmpDownloadPath)
  1513. xlsFileName = append(xlsFileName, tmpFileName)
  1514. }
  1515. archive, err := os.Create("./static" + downloadPath) //第一步,创建 zip 文件
  1516. if err != nil {
  1517. logx.Error(err)
  1518. println("<<<<CentralKitchenForSchoolDataStatisticsExport5>>>>>Error:::", err.Error())
  1519. panic(err)
  1520. }
  1521. defer archive.Close()
  1522. zipWriter := zip.NewWriter(archive) //第二步,创建一个新的 *Writer 对象
  1523. for k, v := range xlsDownloadPath {
  1524. w, err1 := zipWriter.Create("退款明细/" + xlsFileName[k]) //向 zip 文件中添加一个文件,返回一个待压缩的文件内容应写入的 Writer
  1525. if err1 != nil {
  1526. logx.Error(err1)
  1527. println("<<<<CentralKitchenForSchoolDataStatisticsExport5>>>>>Error:::", err1.Error())
  1528. panic(err1)
  1529. }
  1530. f, err1 := os.Open(v) //打开待压缩的文件
  1531. if err1 != nil {
  1532. logx.Error(err1)
  1533. println("<<<<CentralKitchenForSchoolDataStatisticsExport5>>>>>Error:::", err1.Error())
  1534. panic(err1)
  1535. }
  1536. if _, err1 = io.Copy(w, f); err != nil {
  1537. logx.Error(err1)
  1538. println("<<<<CentralKitchenForSchoolDataStatisticsExport5>>>>>Error:::", err1.Error())
  1539. panic(err1)
  1540. }
  1541. if err1 != nil {
  1542. logx.Error(err1)
  1543. println("<<<<CentralKitchenForSchoolDataStatisticsExport5>>>>>Error:::", err1.Error())
  1544. panic(err1)
  1545. }
  1546. f.Close()
  1547. os.RemoveAll(v) //TODO::移除源文件
  1548. }
  1549. zipWriter.Close() // 第四步,关闭 zip writer,将所有数据写入指向基础 zip 文件的数据流
  1550. if err != nil {
  1551. panic(err)
  1552. }
  1553. break
  1554. case 6:
  1555. xlsx := excelize.NewFile()
  1556. //收款统计表
  1557. fileName = "收款统计表_" + utils.Int64ToStr(now.UnixMilli()) + ".xlsx"
  1558. downloadPath = "/export/central_kitchen_for_school/" + fileName
  1559. startDate, _ := time.ParseInLocation("2006-01-02 15:04:05", req.StartDate, time.Local)
  1560. endDate, _ := time.ParseInLocation("2006-01-02 15:04:05", req.EndDate, time.Local)
  1561. //查找数据
  1562. var m []model.Enterprise
  1563. sess := db.Db.Desc("id")
  1564. if req.EnterPriseKind != 0 {
  1565. sess.And("kind = ?", req.EnterPriseKind)
  1566. }
  1567. if req.EnterpriseId != 0 {
  1568. sess.And("id = ?", req.EnterpriseId)
  1569. }
  1570. err := sess.Find(&m)
  1571. if err != nil {
  1572. logx.Error(err)
  1573. println("<<<<CentralKitchenForSchoolDataStatisticsExport6>>>>>Error:::", err.Error())
  1574. return
  1575. }
  1576. titleList = []string{startDate.Format("2006-01-02") + "~~" + endDate.Format("2006-01-02") + " 收款统计表"}
  1577. xlsx.SetSheetRow("Sheet1", "A1", &titleList)
  1578. titleList = []string{"学校名称", "预定总费用", "退款总计", "实收费用", "预估费率"}
  1579. xlsx.SetSheetRow("Sheet1", "A2", &titleList)
  1580. xlsx.MergeCell("Sheet1", "A1", "E1") //合并单元格
  1581. style, err := xlsx.NewStyle(`{"fill":{"type":"pattern","color":["FFFF00"],"pattern":1}, "alignment":{"horizontal":"center"}}`)
  1582. if err != nil {
  1583. logx.Error(err)
  1584. println("<<<<CentralKitchenForSchoolDataStatisticsExport3>>>>>Error:::", err.Error())
  1585. panic(err)
  1586. }
  1587. xlsx.SetCellStyle("Sheet1", "A1", "E1", style)
  1588. xlsx.SetColWidth("Sheet1", "A", "E", 20)
  1589. j := 3 //表头前两行用了,只能从第三行开始
  1590. for _, v := range m {
  1591. var income, refund string
  1592. //统计收入
  1593. 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)
  1594. mapArr1, err := db.QueryNativeString(db.Db, sql1)
  1595. if err != nil {
  1596. logx.Error(err)
  1597. println("<<<<CentralKitchenForSchoolDataStatisticsExport6>>>>>Error:::", err.Error())
  1598. return
  1599. }
  1600. if mapArr1 != nil {
  1601. income = mapArr1[0]["total"]
  1602. }
  1603. //统计退款
  1604. //查询出当前学校下所有的学生
  1605. userIdentityDb := db.UserIdentityDb{}
  1606. userIdentityDb.Set(0)
  1607. userIdentities, _ := userIdentityDb.FindUserIdentityForEnterprise(v.Id)
  1608. var userIdentityIds []string
  1609. for _, v := range *userIdentities {
  1610. userIdentityIds = append(userIdentityIds, utils.IntToStr(v.Id))
  1611. }
  1612. if len(userIdentityIds) != 0 {
  1613. userIdentityStr := php2go.Implode(",", userIdentityIds)
  1614. 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'",
  1615. enum.CentralKitchenForSchoolUserRefundDayStateForAuditComplete, userIdentityStr, req.StartDate, req.EndDate)
  1616. mapArr2, err := db.QueryNativeString(db.Db, sql2)
  1617. if err != nil {
  1618. logx.Error(err)
  1619. println("<<<<CentralKitchenForSchoolDataStatisticsExport6>>>>>Error:::", err.Error())
  1620. return
  1621. }
  1622. if mapArr2 != nil {
  1623. refund = mapArr2[0]["total"]
  1624. }
  1625. }
  1626. realityAmount := utils.StrToFloat64(income) - utils.StrToFloat64(refund)
  1627. rateFee := realityAmount * 0.0038
  1628. xlsx.SetSheetRow("Sheet1", "A"+strconv.Itoa(j), &[]interface{}{v.Name, income, refund, realityAmount, rateFee})
  1629. j++
  1630. }
  1631. //将文件保存至服务器
  1632. xlsx.SaveAs("./static" + downloadPath)
  1633. break
  1634. case 7:
  1635. xlsx := excelize.NewFile()
  1636. //班级收款明细表
  1637. fileName = "班级收款明细表_" + utils.Int64ToStr(now.UnixMilli()) + ".zip"
  1638. downloadPath = "/export/central_kitchen_for_school/" + fileName
  1639. //查找订单数据
  1640. sess := db.Db.Desc("central_kitchen_for_school_package_ord.id")
  1641. sess.And("central_kitchen_for_school_package_ord.state =?", enum.CentralKitchenForSchoolPackageOrdStateForSuccess).And("user_identity.identity = ?", enum.UserIdentityForCentralKitchenForStudent)
  1642. if req.EnterpriseId != 0 {
  1643. sess.And("central_kitchen_for_school_package_ord.enterprise_id =?", req.EnterpriseId)
  1644. }
  1645. if req.StartDate != "" {
  1646. sess.And("central_kitchen_for_school_package_ord.create_at >= ?", req.StartDate)
  1647. }
  1648. if req.EndDate != "" {
  1649. sess.And("central_kitchen_for_school_package_ord.create_at <= ?", req.EndDate)
  1650. }
  1651. if req.EnterPriseKind != 0 {
  1652. sess.And("enterprise.kind = ?", req.EnterPriseKind)
  1653. }
  1654. var m []*db.CentralKitchenForSchoolPackageOrdWithUserIdentity
  1655. _, err := sess.
  1656. Join("LEFT", "user_identity", "central_kitchen_for_school_package_ord_for_reserve.user_identity_id = user_identity.id").
  1657. Join("LEFT", "enterprise", "enterprise.id = user_identity.enterprise_id").
  1658. Join("LEFT", "user", "user.id = user_identity.uid").
  1659. Join("LEFT", "class_with_user", "class_with_user.user_identity_id = user_identity.id").
  1660. Join("LEFT", "class", "class_with_user.class_id = class.id").
  1661. Join("LEFT", "grade", "class.grade_id = grade.id").
  1662. FindAndCount(&m)
  1663. if err != nil {
  1664. logx.Error(err)
  1665. println("<<<<CentralKitchenForSchoolDataStatisticsExport7>>>>>Error:::", err.Error())
  1666. return
  1667. }
  1668. 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")
  1669. if req.EnterpriseId != 0 {
  1670. sess1.And("enterprise.id =?", req.EnterpriseId)
  1671. }
  1672. if req.StartDate != "" {
  1673. sess1.And("central_kitchen_for_school_user_refund_day.create_at >= ?", req.StartDate)
  1674. }
  1675. if req.EndDate != "" {
  1676. sess1.And("central_kitchen_for_school_user_refund_day.create_at <= ?", req.EndDate)
  1677. }
  1678. var mm []*db.CentralKitchenForSchoolUserRefundDayWithData
  1679. _, err = sess1.
  1680. 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").
  1681. 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").
  1682. Join("LEFT", "user_identity", "central_kitchen_for_school_user_with_day.identity_id = user_identity.id").
  1683. Join("LEFT", "user", "user_identity.uid = user.id").
  1684. Join("LEFT", "enterprise", "user_identity.enterprise_id = enterprise.id").
  1685. Join("LEFT", "class_with_user", "user_identity.id = class_with_user.user_identity_id").
  1686. Join("LEFT", "class", "class_with_user.class_id = class.id").
  1687. Join("LEFT", "grade", "class.grade_id = grade.id").
  1688. Desc("central_kitchen_for_school_user_refund_day.id").
  1689. FindAndCount(&mm)
  1690. if err != nil {
  1691. logx.Error(err)
  1692. println("<<<<CentralKitchenForSchoolDataStatisticsExport7>>>>>Error:::", err.Error())
  1693. return
  1694. }
  1695. var list = map[string]map[string]map[string]map[string]float64{}
  1696. for _, v := range m {
  1697. if list[v.Enterprise.Name] == nil {
  1698. list[v.Enterprise.Name] = make(map[string]map[string]map[string]float64)
  1699. }
  1700. if list[v.Enterprise.Name][v.Grade.Name] == nil {
  1701. list[v.Enterprise.Name][v.Grade.Name] = make(map[string]map[string]float64)
  1702. }
  1703. if list[v.Enterprise.Name][v.Grade.Name][v.Class.Name] == nil {
  1704. list[v.Enterprise.Name][v.Grade.Name][v.Class.Name] = make(map[string]float64)
  1705. }
  1706. list[v.Enterprise.Name][v.Grade.Name][v.Class.Name]["income"] += utils.StrToFloat64(v.CentralKitchenForSchoolPackageOrd.TotalPrice)
  1707. }
  1708. for _, v := range mm {
  1709. if list[v.Enterprise.Name] == nil {
  1710. list[v.Enterprise.Name] = make(map[string]map[string]map[string]float64)
  1711. }
  1712. if list[v.Enterprise.Name][v.Grade.Name] == nil {
  1713. list[v.Enterprise.Name][v.Grade.Name] = make(map[string]map[string]float64)
  1714. }
  1715. if list[v.Enterprise.Name][v.Grade.Name][v.Class.Name] == nil {
  1716. list[v.Enterprise.Name][v.Grade.Name][v.Class.Name] = make(map[string]float64)
  1717. }
  1718. list[v.Enterprise.Name][v.Grade.Name][v.Class.Name]["refund"] += utils.StrToFloat64(v.CentralKitchenForSchoolUserRefundDay.Amount)
  1719. }
  1720. var xlsFileName, xlsDownloadPath []string
  1721. for k1, v1 := range list {
  1722. tmpFileName := k1 + "班级收款明细_" + utils.Int64ToStr(now.UnixMilli()) + ".xlsx"
  1723. tmpDownloadPath := "./static/export/central_kitchen_for_school/" + tmpFileName
  1724. startDate, _ := time.ParseInLocation("2006-01-02 15:04:05", req.StartDate, time.Local)
  1725. endDate, _ := time.ParseInLocation("2006-01-02 15:04:05", req.EndDate, time.Local)
  1726. titleList = []string{startDate.Format("2006-01-02") + "~~" + endDate.Format("2006-01-02") + " (" + k1 + ") 班级收款明细"}
  1727. xlsx.SetSheetRow("Sheet1", "A1", &titleList)
  1728. titleList = []string{"年级", "班级", "预定总费用", "退款总计"}
  1729. xlsx.SetSheetRow("Sheet1", "A2", &titleList)
  1730. xlsx.MergeCell("Sheet1", "A1", "D1") //合并单元格
  1731. style, err := xlsx.NewStyle(`{"fill":{"type":"pattern","color":["FFFF00"],"pattern":1}, "alignment":{"horizontal":"center"}}`)
  1732. if err != nil {
  1733. logx.Error(err)
  1734. println("<<<<CentralKitchenForSchoolDataStatisticsExport3>>>>>Error:::", err.Error())
  1735. panic(err)
  1736. }
  1737. xlsx.SetCellStyle("Sheet1", "A1", "D1", style)
  1738. xlsx.SetColWidth("Sheet1", "A", "D", 20)
  1739. j := 3 //表头前两行用了,只能从第三行开始
  1740. var sumClassNum = int64(0)
  1741. var sumIncome, sumRefund = float64(0), float64(0)
  1742. for k2, v2 := range v1 {
  1743. if k2 == "" {
  1744. //若年级为空,则为教师餐
  1745. continue
  1746. }
  1747. for k3, v3 := range v2 {
  1748. xlsx.SetSheetRow("Sheet1", "A"+strconv.Itoa(j), &[]interface{}{k2, k3, utils.Float64ToStr(v3["income"]), utils.Float64ToStr(v3["refund"])})
  1749. sumClassNum++
  1750. sumIncome += v3["income"]
  1751. sumRefund += v3["refund"]
  1752. j++
  1753. }
  1754. }
  1755. xlsx.SetSheetRow("Sheet1", "A"+strconv.Itoa(j), &[]interface{}{
  1756. "合计", sumClassNum, sumIncome, sumRefund})
  1757. //将文件保存至服务器
  1758. err1 := xlsx.SaveAs(tmpDownloadPath)
  1759. if err1 != nil {
  1760. logx.Error(err1)
  1761. println("<<<<CentralKitchenForSchoolDataStatisticsExport7>>>>>Error:::", err1.Error())
  1762. panic(err1)
  1763. }
  1764. xlsDownloadPath = append(xlsDownloadPath, tmpDownloadPath)
  1765. xlsFileName = append(xlsFileName, tmpFileName)
  1766. }
  1767. archive, err := os.Create("./static" + downloadPath) //第一步,创建 zip 文件
  1768. if err != nil {
  1769. logx.Error(err)
  1770. println("<<<<CentralKitchenForSchoolDataStatisticsExport7>>>>>Error:::", err.Error())
  1771. panic(err)
  1772. }
  1773. defer archive.Close()
  1774. zipWriter := zip.NewWriter(archive) //第二步,创建一个新的 *Writer 对象
  1775. for k, v := range xlsDownloadPath {
  1776. w, err1 := zipWriter.Create("班级收款明细/" + xlsFileName[k]) //向 zip 文件中添加一个文件,返回一个待压缩的文件内容应写入的 Writer
  1777. if err1 != nil {
  1778. logx.Error(err1)
  1779. println("<<<<CentralKitchenForSchoolDataStatisticsExport7>>>>>Error:::", err1.Error())
  1780. panic(err1)
  1781. }
  1782. f, err1 := os.Open(v) //打开待压缩的文件
  1783. if err1 != nil {
  1784. logx.Error(err1)
  1785. println("<<<<CentralKitchenForSchoolDataStatisticsExport7>>>>>Error:::", err1.Error())
  1786. panic(err1)
  1787. }
  1788. if _, err1 = io.Copy(w, f); err != nil {
  1789. logx.Error(err1)
  1790. println("<<<<CentralKitchenForSchoolDataStatisticsExport7>>>>>Error:::", err1.Error())
  1791. panic(err1)
  1792. }
  1793. if err1 != nil {
  1794. logx.Error(err1)
  1795. println("<<<<CentralKitchenForSchoolDataStatisticsExport7>>>>>Error:::", err1.Error())
  1796. panic(err1)
  1797. }
  1798. f.Close()
  1799. os.RemoveAll(v) //TODO::移除源文件
  1800. }
  1801. zipWriter.Close() // 第四步,关闭 zip writer,将所有数据写入指向基础 zip 文件的数据流
  1802. if err != nil {
  1803. panic(err)
  1804. }
  1805. break
  1806. case 8:
  1807. xlsx := excelize.NewFile()
  1808. //学校预定数量统计表
  1809. fileName = "学校预定数量统计表_" + utils.Int64ToStr(now.UnixMilli()) + ".xlsx"
  1810. downloadPath = "/export/central_kitchen_for_school/" + fileName
  1811. startDate, _ := time.ParseInLocation("2006-01-02 15:04:05", req.StartDate, time.Local)
  1812. endDate, _ := time.ParseInLocation("2006-01-02 15:04:05", req.EndDate, time.Local)
  1813. var m []*db.CentralKitchenForSchoolPackageOrdForReserveWithUserIdentity
  1814. sess := db.Db.Desc("central_kitchen_for_school_package_ord_for_reserve.id")
  1815. if req.EnterpriseId != 0 {
  1816. sess.And("central_kitchen_for_school_package_ord_for_reserve.enterprise_id =?", req.EnterpriseId)
  1817. }
  1818. var months []time.Month
  1819. if req.StartDate != "" {
  1820. sDate, _ := time.ParseInLocation("2006-01-02 15:04:05", req.StartDate, time.Local)
  1821. eDate, _ := time.ParseInLocation("2006-01-02 15:04:05", req.EndDate, time.Local)
  1822. _, sMonth, _ := sDate.Date()
  1823. _, eMonth, _ := eDate.Date()
  1824. diffMonth := sMonth - eMonth
  1825. if diffMonth == 0 {
  1826. //TODO::查询一个月
  1827. months = append(months, sMonth)
  1828. } else {
  1829. for i := sMonth; i <= eMonth; i++ {
  1830. months = append(months, i)
  1831. }
  1832. }
  1833. }
  1834. if len(months) > 0 {
  1835. sess.In("central_kitchen_for_school_package_ord_for_reserve.reserve_month", months)
  1836. }
  1837. //if req.EndDate != "" {
  1838. // sess.And("central_kitchen_for_school_package_ord_for_reserve.meal_time_start <= ?", req.EndDate)
  1839. //}
  1840. if len(req.MealKindList) > 0 {
  1841. sess.In("central_kitchen_for_school_package_ord_for_reserve.kind", req.MealKindList)
  1842. }
  1843. if req.EnterPriseKind != 0 {
  1844. sess.And("enterprise.kind = ?", req.EnterPriseKind)
  1845. }
  1846. _, err := sess.
  1847. 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").
  1848. Join("LEFT", "user_identity", "central_kitchen_for_school_package_ord_for_reserve.user_identity_id = user_identity.id").
  1849. Join("LEFT", "enterprise", "enterprise.id = user_identity.enterprise_id").
  1850. FindAndCount(&m)
  1851. if err != nil {
  1852. logx.Error(err)
  1853. println("<<<<SelfSupportForSchoolDataStatisticsExport3>>>>>Error:::", err.Error())
  1854. return
  1855. }
  1856. var list = map[string]map[string]int64{}
  1857. centralKitchenForSchoolUserWithDayDb := db.CentralKitchenForSchoolUserWithDayDb{}
  1858. centralKitchenForSchoolUserWithDayDb.Set(0)
  1859. for _, v := range m {
  1860. if list[v.Enterprise.Name] == nil {
  1861. list[v.Enterprise.Name] = map[string]int64{}
  1862. }
  1863. count, err2 := centralKitchenForSchoolUserWithDayDb.CountCentralKitchenForSchoolUserWithDayByOrdNo(
  1864. v.CentralKitchenForSchoolPackageOrdForReserve.OutTradeNo,
  1865. enum.CentralKitchenForSchoolUserWithDayStateForCancel, enum.CentralKitchenForSchoolUserWithDayStateForCanceling,
  1866. req.StartDate, req.EndDate, []int{v.CentralKitchenForSchoolPackageOrdForReserve.Kind})
  1867. if err2 != nil {
  1868. logx.Error(err)
  1869. println("<<<<SelfSupportForSchoolDataStatisticsExport3>>>>>Error:::", err.Error())
  1870. return
  1871. }
  1872. for _, value := range req.MealKindList {
  1873. if value == enum.CentralKitchenForSchoolUserWithDayKindForBreakfast {
  1874. count1, err2 := centralKitchenForSchoolUserWithDayDb.CountCentralKitchenForSchoolUserWithDayByOrdNoAndKind(v.CentralKitchenForSchoolPackageOrdForReserve.OutTradeNo, req.StartDate,
  1875. req.EndDate, enum.CentralKitchenForSchoolUserWithDayKindForBreakfast,
  1876. enum.CentralKitchenForSchoolUserWithDayStateForCancel,
  1877. enum.CentralKitchenForSchoolUserWithDayStateForCanceling,
  1878. )
  1879. if err2 != nil {
  1880. logx.Error(err)
  1881. println("<<<<SelfSupportForSchoolDataStatisticsExport3>>>>>Error:::", err.Error())
  1882. return
  1883. }
  1884. list[v.Enterprise.Name][enum.CentralKitchenForSchoolUserWithDayKind(enum.CentralKitchenForSchoolUserWithDayKindForBreakfast).String()] += count1
  1885. }
  1886. if value == enum.CentralKitchenForSchoolUserWithDayKindForLunch {
  1887. count2, err2 := centralKitchenForSchoolUserWithDayDb.CountCentralKitchenForSchoolUserWithDayByOrdNoAndKind(v.CentralKitchenForSchoolPackageOrdForReserve.OutTradeNo, req.StartDate, req.EndDate,
  1888. enum.CentralKitchenForSchoolUserWithDayKindForLunch,
  1889. enum.CentralKitchenForSchoolUserWithDayStateForCancel,
  1890. enum.CentralKitchenForSchoolUserWithDayStateForCanceling,
  1891. )
  1892. if err2 != nil {
  1893. logx.Error(err)
  1894. println("<<<<SelfSupportForSchoolDataStatisticsExport3>>>>>Error:::", err.Error())
  1895. return
  1896. }
  1897. list[v.Enterprise.Name][enum.CentralKitchenForSchoolUserWithDayKind(enum.CentralKitchenForSchoolUserWithDayKindForLunch).String()] += count2
  1898. }
  1899. if value == enum.CentralKitchenForSchoolUserWithDayKindForDinner {
  1900. count3, err2 := centralKitchenForSchoolUserWithDayDb.CountCentralKitchenForSchoolUserWithDayByOrdNoAndKind(v.CentralKitchenForSchoolPackageOrdForReserve.OutTradeNo, req.StartDate, req.EndDate,
  1901. enum.CentralKitchenForSchoolUserWithDayKindForDinner,
  1902. enum.CentralKitchenForSchoolUserWithDayStateForCancel,
  1903. enum.CentralKitchenForSchoolUserWithDayStateForCanceling,
  1904. )
  1905. if err2 != nil {
  1906. logx.Error(err)
  1907. println("<<<<SelfSupportForSchoolDataStatisticsExport3>>>>>Error:::", err.Error())
  1908. return
  1909. }
  1910. list[v.Enterprise.Name][enum.CentralKitchenForSchoolUserWithDayKind(enum.CentralKitchenForSchoolUserWithDayKindForDinner).String()] += count3
  1911. }
  1912. }
  1913. list[v.Enterprise.Name]["count"] += count
  1914. time.Sleep(20 * time.Millisecond) // 20毫秒
  1915. }
  1916. titleList = []string{startDate.Format("2006-01-02") + "~~" + endDate.Format("2006-01-02") + " 预定数量统计表"}
  1917. xlsx.SetSheetRow("Sheet1", "A1", &titleList)
  1918. titleList = []string{"学校名称", "预定总数",
  1919. enum.CentralKitchenForSchoolUserWithDayKind(enum.CentralKitchenForSchoolUserWithDayKindForBreakfast).String(),
  1920. enum.CentralKitchenForSchoolUserWithDayKind(enum.CentralKitchenForSchoolUserWithDayKindForLunch).String(),
  1921. enum.CentralKitchenForSchoolUserWithDayKind(enum.CentralKitchenForSchoolUserWithDayKindForDinner).String(),
  1922. }
  1923. xlsx.SetSheetRow("Sheet1", "A2", &titleList)
  1924. xlsx.MergeCell("Sheet1", "A1", "E1") //合并单元格
  1925. style, err := xlsx.NewStyle(`{"fill":{"type":"pattern","color":["FFFF00"],"pattern":1}, "alignment":{"horizontal":"center"}}`)
  1926. if err != nil {
  1927. logx.Error(err)
  1928. println("<<<<CentralKitchenForSchoolDataStatisticsExport3>>>>>Error:::", err.Error())
  1929. panic(err)
  1930. }
  1931. xlsx.SetCellStyle("Sheet1", "A1", "E1", style)
  1932. xlsx.SetColWidth("Sheet1", "A", "A", 30)
  1933. xlsx.SetColWidth("Sheet1", "B", "E", 15)
  1934. j := 3 //表头前两行用了,只能从第三行开始
  1935. total := int64(0)
  1936. total1 := int64(0)
  1937. total2 := int64(0)
  1938. total3 := int64(0)
  1939. for k, v := range list {
  1940. xlsx.SetSheetRow("Sheet1", "A"+strconv.Itoa(j), &[]interface{}{k, v["count"],
  1941. v[enum.CentralKitchenForSchoolUserWithDayKind(enum.CentralKitchenForSchoolUserWithDayKindForBreakfast).String()],
  1942. v[enum.CentralKitchenForSchoolUserWithDayKind(enum.CentralKitchenForSchoolUserWithDayKindForLunch).String()],
  1943. v[enum.CentralKitchenForSchoolUserWithDayKind(enum.CentralKitchenForSchoolUserWithDayKindForDinner).String()]})
  1944. total += v["count"]
  1945. total1 += v[enum.CentralKitchenForSchoolUserWithDayKind(enum.CentralKitchenForSchoolUserWithDayKindForBreakfast).String()]
  1946. total2 += v[enum.CentralKitchenForSchoolUserWithDayKind(enum.CentralKitchenForSchoolUserWithDayKindForLunch).String()]
  1947. total3 += v[enum.CentralKitchenForSchoolUserWithDayKind(enum.CentralKitchenForSchoolUserWithDayKindForDinner).String()]
  1948. j++
  1949. }
  1950. xlsx.SetSheetRow("Sheet1", "A"+strconv.Itoa(j), &[]interface{}{
  1951. "合计", total, total1, total2, total3})
  1952. //将文件保存至服务器
  1953. xlsx.SaveAs("./static" + downloadPath)
  1954. break
  1955. case 10:
  1956. //就餐数据统计表
  1957. sTime, _ := time.ParseInLocation("2006-01-02 15:04:05", req.StartDate, time.Local)
  1958. eTime, _ := time.ParseInLocation("2006-01-02 15:04:05", req.EndDate, time.Local)
  1959. xls, _ := excelize.OpenFile("./static/template/央厨学校(就餐数据统计表).xlsx")
  1960. fileName = "【" + sTime.Format("2006-01-02") + "~" + eTime.Format("2006-01-02") + "】就餐数据统计表_" + utils.Int64ToStr(now.UnixMilli()) + ".xlsx"
  1961. downloadPath = "/export/central_kitchen_for_school/" + fileName
  1962. var list = map[string]map[string]map[string]float64{}
  1963. var dates []string
  1964. if req.StartDate != "" {
  1965. centralKitchenForSchoolUserWithDayDb := db.CentralKitchenForSchoolUserWithDayDb{}
  1966. centralKitchenForSchoolUserWithDayDb.Set(0)
  1967. diffDays := int(eTime.Sub(sTime).Hours() / 24)
  1968. for i := 0; i <= diffDays; i++ {
  1969. date := sTime.AddDate(0, 0, i)
  1970. dateName := date.Format("2006-01-02")
  1971. dates = append(dates, dateName)
  1972. for _, vv := range req.MealKindList {
  1973. tmpSum, err := centralKitchenForSchoolUserWithDayDb.SumCentralKitchenForSchoolUserWithDayByKind(req.EnterpriseId, dateName, dateName, vv, enum.CentralKitchenForSchoolUserWithDayStateForCancel, enum.CentralKitchenForSchoolUserWithDayStateForCanceling)
  1974. if err != nil {
  1975. logx.Error(err)
  1976. println("<<<<CentralKitchenForSchoolDataStatisticsExport9>>>>>Error:::", err.Error())
  1977. panic(err)
  1978. }
  1979. tmpCount, err := centralKitchenForSchoolUserWithDayDb.CountCentralKitchenForSchoolUserWithDayByKind(req.EnterpriseId, dateName, dateName, vv, enum.CentralKitchenForSchoolUserWithDayStateForCancel, enum.CentralKitchenForSchoolUserWithDayStateForCanceling)
  1980. if err != nil {
  1981. logx.Error(err)
  1982. println("<<<<CentralKitchenForSchoolDataStatisticsExport9>>>>>Error:::", err.Error())
  1983. panic(err)
  1984. }
  1985. if list[dateName] == nil {
  1986. list[dateName] = map[string]map[string]float64{}
  1987. }
  1988. if list[dateName][enum.CentralKitchenForSchoolUserWithDayKind(vv).String()] == nil {
  1989. list[dateName][enum.CentralKitchenForSchoolUserWithDayKind(vv).String()] = map[string]float64{}
  1990. }
  1991. if list[dateName]["合计"] == nil {
  1992. list[dateName]["合计"] = make(map[string]float64)
  1993. }
  1994. list[dateName][enum.CentralKitchenForSchoolUserWithDayKind(vv).String()]["sum"] = tmpSum
  1995. list[dateName][enum.CentralKitchenForSchoolUserWithDayKind(vv).String()]["count"] = float64(tmpCount)
  1996. list[dateName]["合计"]["sum"] += tmpSum
  1997. list[dateName]["合计"]["count"] += float64(tmpCount)
  1998. }
  1999. }
  2000. }
  2001. //xls.SetSheetRow("", "A1", &titleList)
  2002. //sheetIndex := 2 //第一个表是模板表
  2003. //xls.NewSheet("sheet1") //1、新建表格
  2004. //xls.CopySheet(1, sheetIndex)
  2005. j := 4 //表头前三"早餐"行被占用了,只能从第四行开始
  2006. var totalMap = map[string]float64{}
  2007. for _, date := range dates {
  2008. for kk, v := range list {
  2009. if date == kk {
  2010. xls.SetSheetRow("template", "A"+strconv.Itoa(j), &[]interface{}{kk,
  2011. v["早餐"]["count"], v["早餐"]["sum"],
  2012. v["午餐"]["count"], v["午餐"]["sum"],
  2013. v["晚餐"]["count"], v["晚餐"]["sum"],
  2014. v["合计"]["count"], v["合计"]["sum"],
  2015. })
  2016. totalMap["breakfast_count"] += v["早餐"]["count"]
  2017. totalMap["breakfast_sum"] += v["早餐"]["sum"]
  2018. totalMap["lunch_count"] += v["午餐"]["count"]
  2019. totalMap["lunch_sum"] += v["午餐"]["sum"]
  2020. totalMap["dinner_count"] += v["晚餐"]["count"]
  2021. totalMap["dinner_sum"] += v["晚餐"]["sum"]
  2022. totalMap["total_count"] += v["合计"]["count"]
  2023. totalMap["total_sum"] += v["合计"]["sum"]
  2024. j++
  2025. }
  2026. }
  2027. }
  2028. //xls.DeleteSheet("template") //删除模板表
  2029. xls.SetSheetRow("template", "A"+strconv.Itoa(j), &[]interface{}{"总计",
  2030. totalMap["breakfast_count"], totalMap["breakfast_sum"],
  2031. totalMap["lunch_count"], totalMap["lunch_sum"],
  2032. totalMap["dinner_count"], totalMap["dinner_sum"],
  2033. totalMap["total_count"], totalMap["total_sum"],
  2034. })
  2035. //将文件保存至服务器
  2036. xls.SaveAs("./static" + downloadPath)
  2037. break
  2038. }
  2039. //新增数据
  2040. centralKitchenForSchoolExportRecordsDb := db.CentralKitchenForSchoolExportRecordsDb{}
  2041. centralKitchenForSchoolExportRecordsDb.Set()
  2042. marshal, _ := json.Marshal(req)
  2043. centralKitchenForSchoolExportRecordsDb.CentralKitchenForSchoolExportRecordsInsert(&model.CentralKitchenForSchoolExportRecords{
  2044. Name: fileName,
  2045. DownloadPath: downloadPath,
  2046. Kind: kind,
  2047. ReqContent: string(marshal),
  2048. CreateAt: now.Format("2006-01-02 15:04:05"),
  2049. UpdateAt: now.Format("2006-01-02 15:04:05"),
  2050. })
  2051. }
  2052. }
  2053. func CentralKitchenForSchoolDataStatisticsContrast(req md.CentralKitchenForSchoolDataStatisticsContrastReq) {
  2054. var titleList []string
  2055. var fileName, downloadPath string
  2056. var now = time.Now()
  2057. xlsx := excelize.NewFile()
  2058. //数据对比
  2059. fileName = "数据对比_" + utils.Int64ToStr(now.UnixMilli()) + ".zip"
  2060. downloadPath = "/export/central_kitchen_for_school/" + fileName
  2061. var m []*db.CentralKitchenForSchoolPackageOrdForReserveWithUserIdentity
  2062. sess := db.Db.Desc("central_kitchen_for_school_package_ord_for_reserve.id")
  2063. if req.EnterpriseId != 0 {
  2064. sess.And("central_kitchen_for_school_package_ord_for_reserve.enterprise_id =?", req.EnterpriseId)
  2065. }
  2066. if req.Date1[0] != "" {
  2067. startDate, _ := time.ParseInLocation("2006-01-02 15:04:05", req.Date1[0], time.Local)
  2068. sess.And("central_kitchen_for_school_package_ord_for_reserve.meal_time_start >= ?", startDate.Format("2006-01-02"))
  2069. }
  2070. if req.Date1[1] != "" {
  2071. endDate, _ := time.ParseInLocation("2006-01-02 15:04:05", req.Date1[1], time.Local)
  2072. sess.And("central_kitchen_for_school_package_ord_for_reserve.meal_time_start <= ?", endDate.Format("2006-01-02"))
  2073. }
  2074. if len(req.MealKindList) > 0 {
  2075. sess.In("central_kitchen_for_school_package_ord_for_reserve.kind", req.MealKindList)
  2076. }
  2077. if req.EnterPriseKind != 0 {
  2078. sess.And("enterprise.kind = ?", req.EnterPriseKind)
  2079. }
  2080. _, err := sess.
  2081. 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").
  2082. Join("LEFT", "user_identity", "central_kitchen_for_school_package_ord_for_reserve.user_identity_id = user_identity.id").
  2083. Join("LEFT", "enterprise", "enterprise.id = user_identity.enterprise_id").
  2084. Join("LEFT", "user", "user.id = user_identity.uid").
  2085. Join("LEFT", "class_with_user", "class_with_user.user_identity_id = user_identity.id").
  2086. Join("LEFT", "class", "class_with_user.class_id = class.id").
  2087. Join("LEFT", "grade", "class.grade_id = grade.id").
  2088. FindAndCount(&m)
  2089. if err != nil {
  2090. logx.Error(err)
  2091. println("<<<<CentralKitchenForSchoolDataStatisticsContrast>>>>>Error:::", err.Error())
  2092. return
  2093. }
  2094. var m1 []*db.CentralKitchenForSchoolPackageOrdForReserveWithUserIdentity
  2095. sess1 := db.Db.Desc("central_kitchen_for_school_package_ord_for_reserve.id")
  2096. if req.EnterpriseId != 0 {
  2097. sess1.And("central_kitchen_for_school_package_ord_for_reserve.enterprise_id =?", req.EnterpriseId)
  2098. }
  2099. if req.Date2[0] != "" {
  2100. startDate, _ := time.ParseInLocation("2006-01-02 15:04:05", req.Date2[0], time.Local)
  2101. sess1.And("central_kitchen_for_school_package_ord_for_reserve.meal_time_start >= ?", startDate.Format("2006-01-02"))
  2102. }
  2103. if req.Date2[1] != "" {
  2104. endDate, _ := time.ParseInLocation("2006-01-02 15:04:05", req.Date2[1], time.Local)
  2105. sess1.And("central_kitchen_for_school_package_ord_for_reserve.meal_time_start <= ?", endDate.Format("2006-01-02"))
  2106. }
  2107. if len(req.MealKindList) > 0 {
  2108. sess1.In("central_kitchen_for_school_package_ord_for_reserve.kind", req.MealKindList)
  2109. }
  2110. _, err1 := sess1.
  2111. 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").
  2112. Join("LEFT", "user_identity", "central_kitchen_for_school_package_ord_for_reserve.user_identity_id = user_identity.id").
  2113. Join("LEFT", "enterprise", "enterprise.id = user_identity.enterprise_id").
  2114. Join("LEFT", "user", "user.id = user_identity.uid").
  2115. Join("LEFT", "class_with_user", "class_with_user.user_identity_id = user_identity.id").
  2116. Join("LEFT", "class", "class_with_user.class_id = class.id").
  2117. Join("LEFT", "grade", "class.grade_id = grade.id").
  2118. FindAndCount(&m1)
  2119. if err1 != nil {
  2120. logx.Error(err1)
  2121. println("<<<<CentralKitchenForSchoolDataStatisticsContrast>>>>>Error:::", err.Error())
  2122. return
  2123. }
  2124. var list = map[string]map[string]map[string]map[string]string{}
  2125. for _, v := range m {
  2126. if list[v.Enterprise.Name] == nil {
  2127. list[v.Enterprise.Name] = make(map[string]map[string]map[string]string)
  2128. }
  2129. if list[v.Enterprise.Name][v.Grade.Name] == nil {
  2130. list[v.Enterprise.Name][v.Grade.Name] = make(map[string]map[string]string)
  2131. }
  2132. if list[v.Enterprise.Name][v.Grade.Name][v.Class.Name] == nil {
  2133. list[v.Enterprise.Name][v.Grade.Name][v.Class.Name] = make(map[string]string)
  2134. }
  2135. list[v.Enterprise.Name][v.Grade.Name][v.Class.Name][v.UserIdentity.Name] = v.User.Phone
  2136. }
  2137. var list1 = map[string]map[string]map[string]map[string]string{}
  2138. for _, v := range m1 {
  2139. if list1[v.Enterprise.Name] == nil {
  2140. list1[v.Enterprise.Name] = make(map[string]map[string]map[string]string)
  2141. }
  2142. if list1[v.Enterprise.Name][v.Grade.Name] == nil {
  2143. list1[v.Enterprise.Name][v.Grade.Name] = make(map[string]map[string]string)
  2144. }
  2145. if list1[v.Enterprise.Name][v.Grade.Name][v.Class.Name] == nil {
  2146. list1[v.Enterprise.Name][v.Grade.Name][v.Class.Name] = make(map[string]string)
  2147. }
  2148. list1[v.Enterprise.Name][v.Grade.Name][v.Class.Name][v.UserIdentity.Name] = v.User.Phone
  2149. }
  2150. var xlsFileName, xlsDownloadPath []string
  2151. for k1, v1 := range list {
  2152. tmpFileName := k1 + "数据对比_" + utils.Int64ToStr(now.UnixMilli()) + ".xlsx"
  2153. tmpDownloadPath := "./static/export/central_kitchen_for_school/" + tmpFileName
  2154. startDate, _ := time.ParseInLocation("2006-01-02 15:04:05", req.Date1[0], time.Local)
  2155. endDate, _ := time.ParseInLocation("2006-01-02 15:04:05", req.Date2[0], time.Local)
  2156. //查询当前学校下的班级升序
  2157. tmpEnterpriseDb := db.EnterpriseDb{}
  2158. tmpEnterpriseDb.Set()
  2159. tmpEnterprise, err11 := tmpEnterpriseDb.GetEnterpriseByName(k1)
  2160. if err11 != nil {
  2161. logx.Error(err11)
  2162. println("<<<<CentralKitchenForSchoolDataStatisticsExport4>>>>>Error:::", err11.Error())
  2163. panic(err11)
  2164. }
  2165. tmpClassDb := db.ClassDb{}
  2166. tmpClassDb.Set(0)
  2167. classList, err := tmpClassDb.FindClassAscByEnterprise(tmpEnterprise.Id)
  2168. if err != nil {
  2169. logx.Error(err)
  2170. println("<<<<CentralKitchenForSchoolDataStatisticsExport4>>>>>Error:::", err.Error())
  2171. panic(err)
  2172. }
  2173. for _, class := range *classList {
  2174. for k2, v2 := range v1 {
  2175. if k2 == "" {
  2176. //若年级为空,则为教师
  2177. continue
  2178. }
  2179. for k3, v3 := range v2 {
  2180. if class.Name != k3 {
  2181. continue
  2182. }
  2183. xlsx.NewSheet(k3)
  2184. titleList = []string{startDate.Format("2006-01") + "~~" + endDate.Format("2006-01") + " [" + k1 + ", " + k3 + "] 订餐数据对比"}
  2185. xlsx.SetSheetRow(k3, "A1", &titleList)
  2186. titleList = []string{"姓名", "家长联系电话"}
  2187. xlsx.SetSheetRow(k3, "A2", &titleList)
  2188. xlsx.MergeCell(k3, "A1", "B1") //合并单元格
  2189. style, err := xlsx.NewStyle(`{"fill":{"type":"pattern","color":["FFFF00"],"pattern":1}, "alignment":{"horizontal":"center"}}`)
  2190. if err != nil {
  2191. logx.Error(err)
  2192. println("<<<<CentralKitchenForSchoolDataStatisticsExport3>>>>>Error:::", err.Error())
  2193. panic(err)
  2194. }
  2195. xlsx.SetCellStyle(k3, "A1", "B1", style)
  2196. xlsx.SetColWidth(k3, "A", "B", 40)
  2197. j := 2 //表头被第一行用了,只能从第二行开始
  2198. for k4, v4 := range v3 {
  2199. //TODO::判断当前用户是否在新的月份里面
  2200. fmt.Println("11111111111111", list1[k1])
  2201. fmt.Println("22222222222222", list1[k1][k2])
  2202. fmt.Println("33333333333333", list1[k1][k2][k3])
  2203. fmt.Println("44444444444444", list1[k1][k2][k3][k4])
  2204. if list1[k1] == nil || list1[k1][k2] == nil || list1[k1][k2][k3] == nil || list1[k1][k2][k3][k4] == "" {
  2205. xlsx.SetSheetRow(k3, "A"+strconv.Itoa(j), &[]interface{}{
  2206. k4, v4})
  2207. j++
  2208. }
  2209. }
  2210. }
  2211. }
  2212. }
  2213. //将文件保存至服务器
  2214. xlsx.DeleteSheet("Sheet1") //TODO::只能放这里删除
  2215. err1 := xlsx.SaveAs(tmpDownloadPath)
  2216. if err1 != nil {
  2217. logx.Error(err1)
  2218. println("<<<<CentralKitchenForSchoolDataStatisticsContrast>>>>>Error:::", err1.Error())
  2219. panic(err1)
  2220. }
  2221. xlsDownloadPath = append(xlsDownloadPath, tmpDownloadPath)
  2222. xlsFileName = append(xlsFileName, tmpFileName)
  2223. }
  2224. archive, err := os.Create("./static" + downloadPath) //第一步,创建 zip 文件
  2225. if err != nil {
  2226. logx.Error(err)
  2227. println("<<<<CentralKitchenForSchoolDataStatisticsContrast>>>>>Error:::", err.Error())
  2228. panic(err)
  2229. }
  2230. defer archive.Close()
  2231. zipWriter := zip.NewWriter(archive) //第二步,创建一个新的 *Writer 对象
  2232. for k, v := range xlsDownloadPath {
  2233. w, err1 := zipWriter.Create("数据对比/" + xlsFileName[k]) //向 zip 文件中添加一个文件,返回一个待压缩的文件内容应写入的 Writer
  2234. if err1 != nil {
  2235. logx.Error(err1)
  2236. println("<<<<CentralKitchenForSchoolDataStatisticsContrast>>>>>Error:::", err1.Error())
  2237. panic(err1)
  2238. }
  2239. f, err1 := os.Open(v) //打开待压缩的文件
  2240. if err1 != nil {
  2241. logx.Error(err1)
  2242. println("<<<<CentralKitchenForSchoolDataStatisticsContrast>>>>>Error:::", err1.Error())
  2243. panic(err1)
  2244. }
  2245. if _, err1 = io.Copy(w, f); err != nil {
  2246. logx.Error(err1)
  2247. println("<<<<CentralKitchenForSchoolDataStatisticsContrast>>>>>Error:::", err1.Error())
  2248. panic(err1)
  2249. }
  2250. if err1 != nil {
  2251. logx.Error(err1)
  2252. println("<<<<CentralKitchenForSchoolDataStatisticsContrast>>>>>Error:::", err1.Error())
  2253. panic(err1)
  2254. }
  2255. f.Close()
  2256. os.RemoveAll(v) //TODO::移除源文件
  2257. }
  2258. zipWriter.Close() // 第四步,关闭 zip writer,将所有数据写入指向基础 zip 文件的数据流
  2259. if err != nil {
  2260. panic(err)
  2261. }
  2262. //新增数据
  2263. centralKitchenForSchoolExportRecordsDb := db.CentralKitchenForSchoolExportRecordsDb{}
  2264. centralKitchenForSchoolExportRecordsDb.Set()
  2265. marshal, _ := json.Marshal(req)
  2266. centralKitchenForSchoolExportRecordsDb.CentralKitchenForSchoolExportRecordsInsert(&model.CentralKitchenForSchoolExportRecords{
  2267. Name: fileName,
  2268. DownloadPath: downloadPath,
  2269. Kind: 9, //数据对比
  2270. ReqContent: string(marshal),
  2271. CreateAt: now.Format("2006-01-02 15:04:05"),
  2272. UpdateAt: now.Format("2006-01-02 15:04:05"),
  2273. })
  2274. }
  2275. func JudgeSelfSupportForMealTime(mealDate string) (mealZh string) {
  2276. mealZh = "未知"
  2277. mealTime, _ := time.ParseInLocation("2006-01-02 15:04:05", mealDate, time.Local)
  2278. for _, v := range md.SelfSupportForMealTimeMapping {
  2279. //判断时间大小
  2280. tmpFromTime, _ := time.ParseInLocation("2006-01-02 15:04:05", mealTime.Format("2006-01-02")+" "+v["from"]+":00", time.Local)
  2281. tmpToTime, _ := time.ParseInLocation("2006-01-02 15:04:05", mealTime.Format("2006-01-02")+" "+v["to"]+":00", time.Local)
  2282. fmt.Println(">>>>>>>>>>>>>", mealTime.After(tmpFromTime))
  2283. fmt.Println("<<<<<<<<<<<<<", mealTime.Before(tmpToTime))
  2284. if mealTime.After(tmpFromTime) && mealTime.Before(tmpToTime) {
  2285. mealZh = v["name"]
  2286. }
  2287. }
  2288. return
  2289. }