rent_computer.go 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691
  1. package rentComputer
  2. import (
  3. "context"
  4. "errors"
  5. "github.com/xuri/excelize/v2"
  6. "go.uber.org/zap"
  7. "gorm.io/gorm"
  8. "log-server/global"
  9. "log-server/model/rentComputer"
  10. "log-server/model/rentComputer/request"
  11. "log-server/model/rentComputer/response"
  12. "math"
  13. "os"
  14. "strconv"
  15. "time"
  16. )
  17. type ServiceRentComputer struct {
  18. }
  19. func (s *ServiceRentComputer) RentComputerList(ctx context.Context, api rentComputer.RentComputer, info request.PageInfo, order string, desc bool) (interface{}, int64, error) {
  20. db := global.GVA_DB.Model(&rentComputer.RentComputer{})
  21. db = db.Select("rent_computer.id,rent_computer.update_time,rent_computer.create_time,rent_computer.pc_num,rent_computer.set_meal_id,rent_computer.director_name," +
  22. "rent_computer.pc_name,rent_computer.shop_id,rent_computer.rent_start,rent_computer.rent_duration,rent_computer.is_off_shelf,rent_computer.rent_end," +
  23. "rent_computer.remark,rent_computer.todesk_id,rent_computer.todesk_password,rent_computer.sunflower_id,rent_computer.sunflower_password,rent_computer.rent_price_used,rent_computer.is_expire," +
  24. "s.name as shop_name, r.name as set_meal_name,r.rent_price,r.price_type,r.rent_price_day")
  25. db = db.Joins("left join rent_computer_shop s on s.id = rent_computer.shop_id")
  26. db = db.Joins("left join rent_set_meal r on r.id = rent_computer.set_meal_id")
  27. if api.PcNum != "" {
  28. db = db.Where("rent_computer.pc_num = ?", api.PcNum)
  29. }
  30. if api.ShopId != 0 {
  31. db = db.Where("rent_computer.shop_id = ?", api.ShopId)
  32. }
  33. if api.SetMealId != 0 {
  34. db = db.Where("rent_computer.set_meal_id = ?", api.SetMealId)
  35. }
  36. if api.DirectorName != "ALL" {
  37. if api.DirectorName == "" {
  38. db = db.Where("rent_computer.director_name IS NULL")
  39. } else {
  40. db = db.Where("rent_computer.director_name = ?", api.DirectorName)
  41. }
  42. }
  43. //
  44. //global.GVA_LOG.Info(strconv.Itoa(int(api.IsOffShelf)))
  45. //global.GVA_LOG.Info(strconv.Itoa(int(api.IsExpire)))
  46. //if api.IsOffShelf != -1 {
  47. // db = db.Where("rent_computer.is_off_shelf = ?", api.IsOffShelf)
  48. //}
  49. if api.IsExpire != -1 {
  50. db = db.Where("rent_computer.is_expire = ?", api.IsExpire)
  51. }
  52. var total int64
  53. err := db.Count(&total).Error
  54. if err != nil {
  55. return nil, 0, err
  56. }
  57. limit := info.PageSize
  58. offset := info.PageSize * (info.Page - 1)
  59. //var statisticsLogs []*log.LogComputer
  60. var statisticscomputers []*response.ComputerStatisticsReply2
  61. db = db.Limit(limit).Offset(offset)
  62. if order != "" {
  63. var OrderStr string
  64. // 设置有效排序key 防止sql注入
  65. // 感谢 Tom4t0 提交漏洞信息
  66. orderMap := make(map[string]bool, 3)
  67. orderMap["pc_num"] = true
  68. //orderMap["game_id"] = true
  69. //orderMap["operator"] = true
  70. if orderMap[order] {
  71. if desc {
  72. OrderStr = order + " desc"
  73. } else {
  74. OrderStr = order
  75. }
  76. } else { // didn't matched any order key in `orderMap`
  77. global.GVA_LOG.Error("获取失败!", zap.Error(err))
  78. return statisticscomputers, total, err
  79. }
  80. err = db.Order(OrderStr).Find(&statisticscomputers).Error
  81. } else {
  82. err = db.Order("id").Find(&statisticscomputers).Error
  83. }
  84. if err != nil {
  85. return nil, 0, err
  86. }
  87. var statisticsLogsComputer []*response.ComputerStatisticsReply2
  88. for _, statisticsLog := range statisticscomputers {
  89. statisticscomputer := new(response.ComputerStatisticsReply2)
  90. statisticscomputer.Id = statisticsLog.Id
  91. statisticscomputer.UpdateTime = statisticsLog.UpdateTime
  92. statisticscomputer.CreateTime = statisticsLog.CreateTime
  93. statisticscomputer.PcNum = statisticsLog.PcNum
  94. statisticscomputer.PcName = statisticsLog.PcName
  95. statisticscomputer.ShopId = statisticsLog.ShopId
  96. statisticscomputer.RentStart = statisticsLog.RentStart
  97. statisticscomputer.RentDuration = statisticsLog.RentDuration
  98. statisticscomputer.RentEnd = statisticsLog.RentEnd
  99. statisticscomputer.Remark = statisticsLog.Remark
  100. statisticscomputer.TodeskId = statisticsLog.TodeskId
  101. statisticscomputer.TodeskPassword = statisticsLog.TodeskPassword
  102. statisticscomputer.SunflowerId = statisticsLog.SunflowerId
  103. statisticscomputer.SunflowerPassword = statisticsLog.SunflowerPassword
  104. statisticscomputer.RentPrice = statisticsLog.RentPrice
  105. statisticscomputer.RentPriceDay = statisticsLog.RentPriceDay
  106. statisticscomputer.RentPriceUsed = statisticsLog.RentPriceUsed
  107. statisticscomputer.IsExpire = statisticsLog.IsExpire
  108. statisticscomputer.SetMealId = statisticsLog.SetMealId
  109. statisticscomputer.PriceType = statisticsLog.PriceType
  110. statisticscomputer.IsOffShelf = statisticsLog.IsOffShelf
  111. statisticscomputer.ShopName = statisticsLog.ShopName
  112. statisticscomputer.SetMealName = statisticsLog.SetMealName
  113. statisticscomputer.DirectorName = statisticsLog.DirectorName
  114. statisticsLogsComputer = append(statisticsLogsComputer, statisticscomputer)
  115. }
  116. return statisticsLogsComputer, total, err
  117. }
  118. func (s *ServiceRentComputer) GetRentComputerNum(ctx context.Context, api rentComputer.RentComputer) int64 {
  119. var total int64
  120. db := global.GVA_DB.Model(&rentComputer.RentComputer{})
  121. db = db.Distinct("id")
  122. //db = db.Where("is_off_shelf = 0 and is_expire != 1")
  123. if api.PcNum != "" {
  124. db = db.Where("rent_computer.pc_num = ?", api.PcNum)
  125. }
  126. if api.ShopId != 0 {
  127. db = db.Where("rent_computer.shop_id = ?", api.ShopId)
  128. }
  129. if api.SetMealId != 0 {
  130. db = db.Where("rent_computer.set_meal_id = ?", api.SetMealId)
  131. }
  132. if api.DirectorName != "ALL" {
  133. if api.DirectorName == "" {
  134. db = db.Where("rent_computer.director_name IS NULL")
  135. } else {
  136. db = db.Where("rent_computer.director_name = ?", api.DirectorName)
  137. }
  138. }
  139. if api.IsExpire != -1 {
  140. db = db.Where("rent_computer.is_expire = ?", api.IsExpire)
  141. }
  142. _ = db.Count(&total).Error
  143. return total
  144. }
  145. func (s *ServiceRentComputer) AddRentComputer(requestCoding request.ComputerRequest) (err error) {
  146. if !errors.Is(global.GVA_DB.Where("pc_num = ? and shop_id = ?", requestCoding.PcNum, requestCoding.ShopId).First(&rentComputer.RentComputer{}).Error, gorm.ErrRecordNotFound) {
  147. return errors.New("租机编号已存在")
  148. }
  149. computer := new(rentComputer.RentComputer)
  150. //computer.Id = requestCoding.Id
  151. computer.PcNum = requestCoding.PcNum
  152. computer.PcName = requestCoding.PcName
  153. computer.ShopId = requestCoding.ShopId
  154. computer.RentStart = requestCoding.RentStart
  155. computer.RentDuration = requestCoding.RentDuration
  156. computer.RentEnd = requestCoding.RentEnd
  157. computer.Remark = requestCoding.Remark
  158. computer.TodeskId = requestCoding.TodeskId
  159. computer.TodeskPassword = requestCoding.TodeskPassword
  160. computer.SunflowerId = requestCoding.SunflowerId
  161. computer.SunflowerPassword = requestCoding.SunflowerPassword
  162. computer.RentPriceUsed = requestCoding.RentPriceUsed
  163. computer.IsExpire = requestCoding.IsExpire
  164. computer.SetMealId = requestCoding.SetMealId
  165. computer.IsOffShelf = requestCoding.IsOffShelf
  166. computer.DirectorName = requestCoding.DirectorName
  167. computer.CreateTime = time.Now().Format("2006-01-02 15:04:05")
  168. computer.UpdateTime = time.Now().Format("2006-01-02 15:04:05")
  169. //key := codeListCacheKey
  170. //_ = s.cache.DelBatheHsCache(context.Background(), key)
  171. return global.GVA_DB.Create(&computer).Error
  172. }
  173. func (s *ServiceRentComputer) GetRentComputerById(id int) (computerShop response.ComputerStatisticsReply2, err error) {
  174. db := global.GVA_DB.Model(&rentComputer.RentComputer{})
  175. var api response.ComputerStatisticsReply2
  176. db = db.Select("rent_computer.id,rent_computer.update_time,rent_computer.create_time,rent_computer.pc_num,rent_computer.set_meal_id,rent_computer.director_name," +
  177. "rent_computer.pc_name,rent_computer.shop_id,rent_computer.rent_start,rent_computer.rent_duration,rent_computer.is_off_shelf,rent_computer.rent_end," +
  178. "rent_computer.remark,rent_computer.todesk_id,rent_computer.todesk_password,rent_computer.sunflower_id,rent_computer.sunflower_password,rent_computer.rent_price_used,rent_computer.is_expire," +
  179. "s.name as shop_name, r.name as set_meal_name,r.rent_price,r.price_type,r.rent_price_day")
  180. db = db.Joins("left join rent_computer_shop s on s.id = rent_computer.shop_id")
  181. db = db.Joins("left join rent_set_meal r on r.id = rent_computer.set_meal_id")
  182. //db = db.Joins("left join responsible_person p on p.name = rent_computer.director_name")
  183. err = db.Where("rent_computer.id = ?", id).Order(id).Limit(1).Find(&api).Error
  184. if err != nil {
  185. return
  186. }
  187. computerShop.Id = api.Id
  188. computerShop.PcNum = api.PcNum
  189. computerShop.PcName = api.PcName
  190. computerShop.RentStart = api.RentStart
  191. computerShop.RentDuration = api.RentDuration
  192. computerShop.RentEnd = api.RentEnd
  193. computerShop.TodeskId = api.TodeskId
  194. computerShop.TodeskPassword = api.TodeskPassword
  195. computerShop.SunflowerId = api.SunflowerId
  196. computerShop.SunflowerPassword = api.SunflowerPassword
  197. computerShop.RentPrice = api.RentPrice
  198. computerShop.RentPriceDay = api.RentPriceDay
  199. computerShop.RentPriceUsed = api.RentPriceUsed
  200. computerShop.IsExpire = api.IsExpire
  201. computerShop.SetMealId = api.SetMealId
  202. computerShop.PriceType = api.PriceType
  203. computerShop.IsOffShelf = api.IsOffShelf
  204. //global.GVA_LOG.Info(strconv.Itoa(api.IsOffShelf))
  205. //global.GVA_LOG.Info(strconv.Itoa(computerShop.IsOffShelf))
  206. computerShop.ShopName = api.ShopName
  207. computerShop.SetMealName = api.SetMealName
  208. computerShop.Remark = api.Remark
  209. computerShop.ShopId = api.ShopId
  210. computerShop.DirectorName = api.DirectorName
  211. computerShop.UpdateTime = api.UpdateTime
  212. computerShop.CreateTime = api.CreateTime
  213. return
  214. }
  215. func (s *ServiceRentComputer) EditRentComputer(computer rentComputer.RentComputer) (err error) {
  216. err = global.GVA_DB.Save(computer).Error
  217. return err
  218. }
  219. func (s *ServiceRentComputer) DeleteRentComputerByIds(ids request.IdsReq) (err error) {
  220. err = global.GVA_DB.Delete(&[]rentComputer.RentComputer{}, "id in ?", ids.Ids).Error
  221. return err
  222. }
  223. func (s *ServiceRentComputer) DeleteRentComputerById(api rentComputer.RentComputer) (err error) {
  224. var entity rentComputer.RentComputer
  225. //global.GVA_LOG.Info(strconv.Itoa(int(api.Id)))
  226. err = global.GVA_DB.Where("id = ?", api.Id).First(&entity).Error // 根据id查询api记录
  227. if errors.Is(err, gorm.ErrRecordNotFound) { // api记录不存在
  228. return err
  229. }
  230. return global.GVA_DB.Delete(&entity).Error
  231. }
  232. // RenewRentComputer 租机续费
  233. func (s *ServiceRentComputer) RenewRentComputer(api request.RenewRentComputerRequest) (err error) {
  234. var info rentComputer.RentComputer
  235. if errors.Is(global.GVA_DB.Model(&rentComputer.RentComputer{}).Where("id = ?", api.PcId).First(&info).Error, gorm.ErrRecordNotFound) {
  236. return err
  237. }
  238. //global.GVA_LOG.Info(info.PcNum)
  239. remark := time.Now().Format("2006-01-02 15:04:05") + ",租机编号" + api.PcNum + "续费" + strconv.Itoa(api.Day) + "天,续费至" + api.RentRenew + ";\n"
  240. global.GVA_LOG.Info(remark)
  241. api.UpdateTime = time.Now().Format("2006-01-02 15:04:05")
  242. nowUnix := time.Now().Unix()
  243. rentRenew, _ := time.ParseInLocation("2006-01-02 15:04:05", api.RentRenew, time.Local)
  244. if rentRenew.Unix() < nowUnix {
  245. api.IsExpire = 1
  246. } else if rentRenew.Unix()-nowUnix <= 24*60*60 {
  247. api.IsExpire = 2
  248. } else {
  249. api.IsExpire = 0
  250. }
  251. //global.GVA_LOG.Info(strconv.Itoa(api.IsExpire))
  252. api.Remark = info.Remark + remark
  253. var updateInfo rentComputer.RentComputer
  254. updateInfo.UpdateTime = api.UpdateTime
  255. updateInfo.Id = api.PcId
  256. updateInfo.IsExpire = api.IsExpire
  257. updateInfo.RentDuration = info.RentDuration + api.Day
  258. updateInfo.RentEnd = api.RentRenew
  259. updateInfo.Remark = api.Remark
  260. err = global.GVA_DB.Model(&rentComputer.RentComputer{}).Where("id = ?", api.PcId).Updates(updateInfo).Error
  261. return err
  262. }
  263. // RentingOutRentComputer 租机退租
  264. func (s *ServiceRentComputer) RentingOutRentComputer(api request.RentingOutRentComputerRequest) (errMessage string, err error, okMessage string) {
  265. var info rentComputer.RentComputer
  266. if errors.Is(global.GVA_DB.Model(&rentComputer.RentComputer{}).Where("id = ? ", api.PcId).First(&info).Error, gorm.ErrRecordNotFound) {
  267. return "待退租的租机编号不存在", err, ""
  268. }
  269. nowUnix := time.Now().Unix()
  270. now := time.Now().Format("2006-01-02 15:04:05")
  271. const oneDayUnix = 24 * 60 * 60
  272. rentEnd1, _ := time.ParseInLocation("2006-01-02 15:04:05", info.RentEnd, time.Local)
  273. rentEnd2, _ := time.ParseInLocation("2006-01-02 15:04:05", api.RentEnd, time.Local)
  274. if rentEnd1.Unix() < nowUnix {
  275. return "待退租的租机已到期", err, ""
  276. }
  277. surplusDay := math.Ceil(float64((rentEnd1.Unix() - rentEnd2.Unix()) / oneDayUnix))
  278. remark := now + ",租机编号" + info.PcNum + "退租,剩余" + strconv.Itoa(int(surplusDay)) + "天,"
  279. for i, v := range api.AddList {
  280. //global.GVA_LOG.Info(v.PcNum)
  281. //global.GVA_LOG.Info(strconv.Itoa(v.AddDay))
  282. var infoTemp rentComputer.RentComputer
  283. if errors.Is(global.GVA_DB.Model(&rentComputer.RentComputer{}).Where("pc_num = ? and shop_id = ?", v.PcNum, info.ShopId).First(&infoTemp).Error, gorm.ErrRecordNotFound) {
  284. return "增加时间的租机编号不存在", err, ""
  285. }
  286. if i < len(api.AddList) {
  287. remark += "编号" + v.PcNum + "加" + strconv.Itoa(v.AddDay) + "天,"
  288. } else {
  289. remark += "编号" + v.PcNum + "加" + strconv.Itoa(v.AddDay) + "天;\n"
  290. }
  291. infoTemp.UpdateTime = now
  292. infoTemp.RentDuration = infoTemp.RentDuration + v.AddDay
  293. rentEndTemp, _ := time.ParseInLocation("2006-01-02 15:04:05", infoTemp.RentEnd, time.Local)
  294. infoTemp.RentEnd = rentEndTemp.AddDate(0, 0, v.AddDay).Format("2006-01-02 15:04:05")
  295. err = global.GVA_DB.Model(&rentComputer.RentComputer{}).Where("id = ?", infoTemp.Id).Updates(infoTemp).Error
  296. if err != nil {
  297. return "增加时间的租机信息修改失败!", err, ""
  298. }
  299. }
  300. //修改退租机器信息
  301. info.RentEnd = api.RentEnd
  302. info.RentDuration = info.RentDuration - int(surplusDay)
  303. info.UpdateTime = time.Now().Format("2006-01-02 15:04:05")
  304. info.Remark = info.Remark + remark
  305. err = global.GVA_DB.Model(&rentComputer.RentComputer{}).Where("id = ?", info.Id).Updates(info).Error
  306. if err != nil {
  307. return "退租机器信息修改失败!", err, ""
  308. }
  309. return "", err, remark
  310. }
  311. // ReplaceNumRentComputer 租机换编号
  312. func (s *ServiceRentComputer) ReplaceNumRentComputer(api request.ReplaceNumRentComputerRequest) (errMessage string, err error, okMessage string) {
  313. var infoOld rentComputer.RentComputer
  314. if errors.Is(global.GVA_DB.Model(&rentComputer.RentComputer{}).Where("pc_num = ? and shop_id = ?", api.PcNumOld, api.ShopId).First(&infoOld).Error, gorm.ErrRecordNotFound) {
  315. return "待更换的租机编号不存在", err, ""
  316. }
  317. nowUnix := time.Now().Unix()
  318. const oneDayUnix = 24 * 60 * 60
  319. rentEndOld, _ := time.ParseInLocation("2006-01-02 15:04:05", infoOld.RentEnd, time.Local)
  320. if rentEndOld.Unix() < nowUnix {
  321. return "待更换的租机已到期", err, ""
  322. }
  323. var infoNew rentComputer.RentComputer
  324. if !errors.Is(global.GVA_DB.Model(&rentComputer.RentComputer{}).Where("pc_num = ? and shop_id = ?", api.PcNumNew, api.ShopId).First(&infoNew).Error, gorm.ErrRecordNotFound) {
  325. return "要更换的租机编号已存在,不允许更换", err, ""
  326. }
  327. remark := time.Now().Format("2006-01-02 15:04:05") + ",租机编号" + api.PcNumOld + "更换为租机编号" + api.PcNumNew + ";\n"
  328. okMessage = remark
  329. rentDurationNew := math.Ceil(float64((rentEndOld.Unix() - nowUnix) / oneDayUnix))
  330. infoNew.PcNum = api.PcNumNew
  331. infoNew.PcName = api.PcNumNew
  332. infoNew.ShopId = api.ShopId
  333. infoNew.RentStart = time.Now().Format("2006-01-02 15:04:05")
  334. infoNew.RentDuration = int(rentDurationNew)
  335. infoNew.RentEnd = infoOld.RentEnd
  336. infoNew.Remark = remark
  337. infoNew.TodeskId = ""
  338. infoNew.TodeskPassword = ""
  339. infoNew.SunflowerId = ""
  340. infoNew.SunflowerPassword = ""
  341. infoNew.RentPriceUsed = 0
  342. infoNew.IsExpire = 0
  343. infoNew.SetMealId = infoOld.SetMealId
  344. infoNew.IsOffShelf = 0
  345. infoNew.CreateTime = time.Now().Format("2006-01-02 15:04:05")
  346. infoNew.UpdateTime = time.Now().Format("2006-01-02 15:04:05")
  347. // 修改旧编号信息
  348. if infoOld.Remark != "" {
  349. remark = infoOld.Remark + ";" + remark
  350. }
  351. rentStartOld, _ := time.ParseInLocation("2006-01-02 15:04:05", infoOld.RentStart, time.Local)
  352. rentDurationOld := math.Ceil(float64((rentEndOld.Unix() - rentStartOld.Unix()) / oneDayUnix))
  353. infoOld.RentDuration = int(rentDurationOld)
  354. infoOld.RentEnd = time.Now().Format("2006-01-02 15:04:05")
  355. infoOld.Remark = remark
  356. infoOld.UpdateTime = time.Now().Format("2006-01-02 15:04:05")
  357. err = global.GVA_DB.Model(&rentComputer.RentComputer{}).Where("id = ?", infoOld.Id).Updates(infoOld).Error
  358. if err != nil {
  359. return "旧编号租机修改失败!", err, ""
  360. }
  361. // 插入新编号至数据库
  362. err = global.GVA_DB.Create(&infoNew).Error
  363. if err != nil {
  364. return "新编号租机创建失败!", err, ""
  365. }
  366. return "", err, okMessage
  367. }
  368. func (exa *ServiceRentComputer) ParseExcel2InfoList(filePath string) (err error) {
  369. skipHeader := true
  370. fixedHeader := []string{"租机编码", "供应商", "所属套餐", "套餐类型", "起租日", "到期日", "todesk号", "todesk密码", "向日葵号", "向日葵密码"}
  371. file, err := excelize.OpenFile(filePath)
  372. if err != nil {
  373. return err
  374. }
  375. var insertComputers []*rentComputer.RentComputer
  376. //var updateComputers []*rentComputer.RentComputer
  377. rows, err := file.Rows("Sheet1")
  378. if err != nil {
  379. return err
  380. }
  381. //查询数据库里的套餐与供应商=====================================
  382. var setMeals []*response.SetMealStatisticsReply1
  383. db := global.GVA_DB.Model(&rentComputer.RentSetMeal{})
  384. db = db.Select("rent_set_meal.id, rent_set_meal.name," +
  385. "rent_set_meal.price_type,rent_set_meal.shop_id,s.name as shop_name")
  386. db = db.Joins("left join rent_computer_shop s on s.id = rent_set_meal.shop_id")
  387. err = db.Order("id").Find(&setMeals).Error
  388. if err != nil {
  389. return err
  390. }
  391. // =========================================================
  392. index := 0
  393. for rows.Next() {
  394. index++
  395. row, err := rows.Columns()
  396. if err != nil {
  397. return err
  398. }
  399. if skipHeader {
  400. if exa.compareStrSlice(row, fixedHeader) {
  401. skipHeader = false
  402. continue
  403. } else {
  404. return errors.New("Excel格式错误")
  405. }
  406. }
  407. if len(row) == 0 {
  408. //global.GVA_LOG.Info("数组为空")
  409. continue
  410. }
  411. c := new(rentComputer.RentComputer)
  412. c.PcNum = row[0]
  413. c.PcName = row[0]
  414. flagShopId := false
  415. flagSetMealName := false
  416. for _, setMeal := range setMeals {
  417. if row[1] == setMeal.ShopName {
  418. flagShopId = true
  419. c.ShopId = setMeal.ShopId
  420. //break
  421. }
  422. priceType := 0
  423. if row[3] == "天卡" {
  424. priceType = 0
  425. } else if row[3] == "周卡" {
  426. priceType = 1
  427. } else {
  428. priceType = 2
  429. }
  430. if row[2] == setMeal.Name && priceType == setMeal.PriceType {
  431. flagSetMealName = true
  432. c.SetMealId = setMeal.Id
  433. break
  434. }
  435. }
  436. if flagShopId == false || flagSetMealName == false {
  437. return errors.New("供应商错误")
  438. }
  439. // 时间统一设置为日期+12:00:00
  440. rentStart, _ := time.ParseInLocation("2006-01-02 15:04:05", row[4]+" 12:00:00", time.Local)
  441. rentEnd, _ := time.ParseInLocation("2006-01-02 15:04:05", row[5]+" 12:00:00", time.Local)
  442. //global.GVA_LOG.Info(strconv.FormatInt(rentStart.Unix(), 10))
  443. //global.GVA_LOG.Info(strconv.FormatInt(rentEnd.Unix(), 10))
  444. c.RentStart = row[4] + " 00:00:00"
  445. c.RentEnd = row[5] + " 00:00:00"
  446. c.RentDuration = int(math.Abs(float64(SubDays(rentStart, rentEnd)))) + 1
  447. c.IsExpire = 0
  448. c.IsOffShelf = 0
  449. // 获取工作表中指定单元格的值
  450. todeskId, _ := file.GetCellValue("Sheet1", "G"+strconv.Itoa(index))
  451. todeskPassword, _ := file.GetCellValue("Sheet1", "H"+strconv.Itoa(index))
  452. sunflowerId, _ := file.GetCellValue("Sheet1", "I"+strconv.Itoa(index))
  453. sunflowerPassword, _ := file.GetCellValue("Sheet1", "J"+strconv.Itoa(index))
  454. c.TodeskId = todeskId //todesk账号密码
  455. c.TodeskPassword = todeskPassword //todesk账号密码
  456. c.SunflowerId = sunflowerId //向日葵账号密码
  457. c.SunflowerPassword = sunflowerPassword //向日葵账号密码
  458. c.CreateTime = time.Now().Format("2006-01-02 15:04:05")
  459. c.UpdateTime = time.Now().Format("2006-01-02 15:04:05")
  460. // 租机编号已存在,存入更新数组中
  461. if !errors.Is(global.GVA_DB.Where("pc_num = ? and shop_id = ?", c.PcNum, c.ShopId).First(&rentComputer.RentComputer{}).Error, gorm.ErrRecordNotFound) {
  462. // 租机编号已存在,存入更新数组中
  463. //updateComputers = append(updateComputers, c)
  464. continue
  465. } else {
  466. insertComputers = append(insertComputers, c)
  467. }
  468. }
  469. defer func() {
  470. global.GVA_LOG.Info("defer 删除文件")
  471. err = os.RemoveAll(filePath)
  472. if err != nil {
  473. return
  474. }
  475. }()
  476. //if updateComputers != nil {
  477. // err = global.GVA_DB.Updates(updateComputers).Error
  478. // if err != nil {
  479. // return
  480. // }
  481. //}
  482. if insertComputers != nil {
  483. err = global.GVA_DB.Create(insertComputers).Error
  484. if err != nil {
  485. return
  486. }
  487. }
  488. //var cp log.Computer
  489. //err = cp.DelAllOnlinePcCodeCache()
  490. return
  491. }
  492. func (exa *ServiceRentComputer) compareStrSlice(a, b []string) bool {
  493. if len(a) != len(b) {
  494. return false
  495. }
  496. if (b == nil) != (a == nil) {
  497. return false
  498. }
  499. for key, value := range a {
  500. if value != b[key] {
  501. return false
  502. }
  503. }
  504. return true
  505. }
  506. // CheckIsExpire 定时检查是否有电脑到期,修改租机状态
  507. func (s *ServiceRentComputer) CheckIsExpire() {
  508. global.GVA_LOG.Info("检查是否有电脑到期开始执行:" + time.Now().Format("2006-01-02 15:04:05"))
  509. db := global.GVA_DB.Model(&rentComputer.RentComputer{})
  510. //SELECT pc_num,rent_start,rent_end,is_expire FROM shuyou_rent_computer
  511. //db = db.Where("is_off_shelf = 0")
  512. var computers []*rentComputer.RentComputer
  513. err := db.Order("id").Find(&computers).Error
  514. if err != nil {
  515. global.GVA_LOG.Error("检查是否有电脑到期执行失败:" + time.Now().Format("2006-01-02 15:04:05"))
  516. return
  517. }
  518. const overtime = 24 * 60 * 60
  519. nowUnix := time.Now().Unix()
  520. //global.GVA_LOG.Info(strconv.FormatInt(nowUnix, 10))
  521. //global.GVA_LOG.Info("---------------------------------------------------------------------------------")
  522. computer := new(rentComputer.RentComputer)
  523. for _, c := range computers {
  524. endUnix, _ := time.ParseInLocation("2006-01-02 15:04:05", c.RentEnd, time.Local)
  525. //global.GVA_LOG.Info(strconv.FormatInt(endUnix.Unix(), 10))
  526. if endUnix.Unix() < nowUnix {
  527. computer.IsExpire = 1
  528. } else if endUnix.Unix()-nowUnix <= overtime {
  529. computer.IsExpire = 2
  530. } else {
  531. computer.IsExpire = 0
  532. }
  533. if c.IsExpire == computer.IsExpire {
  534. continue
  535. }
  536. computer.Id = c.Id
  537. computer.UpdateTime = c.UpdateTime
  538. computer.CreateTime = c.CreateTime
  539. computer.PcNum = c.PcNum
  540. computer.PcName = c.PcName
  541. computer.ShopId = c.ShopId
  542. computer.RentStart = c.RentStart
  543. computer.RentDuration = c.RentDuration
  544. computer.RentEnd = c.RentEnd
  545. computer.Remark = c.Remark
  546. computer.TodeskId = c.TodeskId
  547. computer.TodeskPassword = c.TodeskPassword
  548. computer.SunflowerId = c.SunflowerId
  549. computer.SunflowerPassword = c.SunflowerPassword
  550. computer.RentPriceUsed = c.RentPriceUsed
  551. computer.SetMealId = c.SetMealId
  552. computer.IsOffShelf = c.IsOffShelf
  553. err = global.GVA_DB.Save(computer).Error
  554. }
  555. global.GVA_LOG.Info("检查是否有电脑到期执行成功:" + time.Now().Format("2006-01-02 15:04:05"))
  556. return
  557. }
  558. // CreateRentComputerLedger 定时生成今日租机台账表(财务)
  559. func (s *ServiceRentComputer) CreateRentComputerLedger() {
  560. global.GVA_LOG.Info("定时生成今日租机台账表开始执行:" + time.Now().Format("2006-01-02 15:04:05"))
  561. //查询当前所有在租电脑
  562. db := global.GVA_DB.Model(&rentComputer.RentComputer{})
  563. var computers []*rentComputer.RentComputerSetMeal
  564. db = db.Select("rent_computer.pc_num, rent_computer.pc_name,rent_computer.shop_id, rent_computer.rent_start, rent_computer.rent_duration, rent_computer.rent_end, rent_computer.is_expire, " +
  565. "r.rent_price,r.rent_price_day,rent_computer.set_meal_id")
  566. db = db.Joins("left join rent_set_meal r on r.id = rent_computer.set_meal_id")
  567. db = db.Where("rent_computer.is_expire != 1")
  568. err := db.Order("rent_computer.id").Find(&computers).Error
  569. if err != nil {
  570. global.GVA_LOG.Error("定时生成今日租机台账表执行失败:"+time.Now().Format("2006-01-02 15:04:05"), zap.Error(err))
  571. return
  572. }
  573. //查询昨日租机台账表中的rent_price_used_then参数
  574. var computersYest []*rentComputer.RentComputerLedger
  575. dbYest := global.GVA_DB.Model(&rentComputer.RentComputerLedger{})
  576. dbYest = dbYest.Select("rent_price_used_then").Order("id")
  577. err = dbYest.Find(&computersYest).Error
  578. if err != nil {
  579. global.GVA_LOG.Error("定时生成今日租机台账表执行失败,查询昨日数据失败:"+time.Now().Format("2006-01-02 15:04:05"), zap.Error(err))
  580. return
  581. }
  582. nowTime := time.Now().Format("2006-01-02 15:04:05")
  583. nowDate := time.Now().Format("2006-01-02")
  584. var insertInfos []rentComputer.RentComputerLedger
  585. for _, computer := range computers {
  586. var info rentComputer.RentComputerLedger
  587. info.UpdateTime = nowTime
  588. info.CreateTime = nowTime
  589. info.NewDate = nowDate
  590. info.PcNum = computer.PcNum
  591. info.PcName = computer.PcName
  592. info.ShopId = computer.ShopId
  593. info.RentStartThen = computer.RentStart
  594. info.RentDurationThen = computer.RentDuration
  595. info.RentEndThen = computer.RentEnd
  596. info.RentPriceThen = computer.RentPrice
  597. info.RentPriceDayThen = computer.RentPriceDay
  598. info.IsExpire = computer.IsExpire
  599. info.SetMealId = computer.SetMealId
  600. info.Remark = computer.Remark
  601. flag := false
  602. for _, yest := range computersYest {
  603. if info.PcNum == yest.PcNum {
  604. flag = true
  605. info.RentPriceUsedThen = yest.RentPriceUsedThen + computer.RentPriceDay
  606. break
  607. }
  608. }
  609. if flag == false {
  610. info.RentPriceUsedThen = computer.RentPriceDay
  611. }
  612. if !errors.Is(global.GVA_DB.Where("pc_num = ? and shop_id = ? and new_date = ?", computer.PcNum, computer.ShopId, nowDate).First(&rentComputer.RentComputerLedger{}).Error, gorm.ErrRecordNotFound) {
  613. // 已存在,更新
  614. err = global.GVA_DB.Model(&rentComputer.RentComputerLedger{}).Where("pc_num = ? and shop_id = ? and new_date = ?", computer.PcNum, computer.ShopId, nowDate).Updates(info).Error
  615. if err != nil {
  616. global.GVA_LOG.Error("定时生成今日租机台账表执行失败,更新数据失败:"+time.Now().Format("2006-01-02 15:04:05"), zap.Error(err))
  617. return
  618. }
  619. } else {
  620. insertInfos = append(insertInfos, info)
  621. }
  622. }
  623. if insertInfos != nil {
  624. err = global.GVA_DB.Create(&insertInfos).Error
  625. if err != nil {
  626. global.GVA_LOG.Error("定时生成今日租机台账表执行失败,插入数据失败:"+time.Now().Format("2006-01-02 15:04:05"), zap.Error(err))
  627. return
  628. }
  629. }
  630. global.GVA_LOG.Info("定时生成今日租机台账表执行成功:" + time.Now().Format("2006-01-02 15:04:05"))
  631. return
  632. }
  633. // SubDays 计算日期相差多少天
  634. // 返回值day>0, t1晚于t2; day<0, t1早于t2
  635. func SubDays(t1, t2 time.Time) (day int) {
  636. swap := false
  637. if t1.Unix() < t2.Unix() {
  638. t_ := t1
  639. t1 = t2
  640. t2 = t_
  641. swap = true
  642. }
  643. //global.GVA_LOG.Info(strconv.Itoa(int(t1.Unix())))
  644. //global.GVA_LOG.Info(strconv.Itoa(int(t2.Unix())))
  645. day = int(t1.Sub(t2).Hours() / 24)
  646. // 计算在被24整除外的时间是否存在跨自然日
  647. if int(t1.Sub(t2).Milliseconds())%86400000 > int(86400000-t2.Unix()%86400000) {
  648. day += 1
  649. }
  650. if swap {
  651. day = -day
  652. }
  653. //global.GVA_LOG.Info(strconv.Itoa(day))
  654. return
  655. }