rent_computer.go 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688
  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. c := new(rentComputer.RentComputer)
  408. c.PcNum = row[0]
  409. c.PcName = row[0]
  410. flagShopId := false
  411. flagSetMealName := false
  412. for _, setMeal := range setMeals {
  413. if row[1] == setMeal.ShopName {
  414. flagShopId = true
  415. c.ShopId = setMeal.ShopId
  416. //break
  417. }
  418. priceType := 0
  419. if row[3] == "天卡" {
  420. priceType = 0
  421. } else if row[3] == "周卡" {
  422. priceType = 1
  423. } else {
  424. priceType = 2
  425. }
  426. if row[2] == setMeal.Name && priceType == setMeal.PriceType {
  427. flagSetMealName = true
  428. c.SetMealId = setMeal.Id
  429. break
  430. }
  431. }
  432. if flagShopId == false || flagSetMealName == false {
  433. return errors.New("供应商错误")
  434. }
  435. // 时间统一设置为日期+12:00:00
  436. rentStart, _ := time.ParseInLocation("2006-01-02 15:04:05", row[4]+" 12:00:00", time.Local)
  437. rentEnd, _ := time.ParseInLocation("2006-01-02 15:04:05", row[5]+" 12:00:00", time.Local)
  438. //global.GVA_LOG.Info(strconv.FormatInt(rentStart.Unix(), 10))
  439. //global.GVA_LOG.Info(strconv.FormatInt(rentEnd.Unix(), 10))
  440. c.RentStart = row[4] + " 00:00:00"
  441. c.RentEnd = row[5] + " 00:00:00"
  442. c.RentDuration = int(math.Abs(float64(SubDays(rentStart, rentEnd)))) + 1
  443. c.IsExpire = 0
  444. c.IsOffShelf = 0
  445. // 获取工作表中指定单元格的值
  446. todeskId, _ := file.GetCellValue("Sheet1", "G"+strconv.Itoa(index))
  447. todeskPassword, _ := file.GetCellValue("Sheet1", "H"+strconv.Itoa(index))
  448. sunflowerId, _ := file.GetCellValue("Sheet1", "I"+strconv.Itoa(index))
  449. sunflowerPassword, _ := file.GetCellValue("Sheet1", "J"+strconv.Itoa(index))
  450. c.TodeskId = todeskId //todesk账号密码
  451. c.TodeskPassword = todeskPassword //todesk账号密码
  452. c.SunflowerId = sunflowerId //向日葵账号密码
  453. c.SunflowerPassword = sunflowerPassword //向日葵账号密码
  454. c.CreateTime = time.Now().Format("2006-01-02 15:04:05")
  455. c.UpdateTime = time.Now().Format("2006-01-02 15:04:05")
  456. // 租机编号已存在,存入更新数组中
  457. if !errors.Is(global.GVA_DB.Where("pc_num = ? and shop_id = ?", c.PcNum, c.ShopId).First(&rentComputer.RentComputer{}).Error, gorm.ErrRecordNotFound) {
  458. // 租机编号已存在,存入更新数组中
  459. //updateComputers = append(updateComputers, c)
  460. continue
  461. } else {
  462. insertComputers = append(insertComputers, c)
  463. }
  464. }
  465. defer func() {
  466. global.GVA_LOG.Info("defer 删除文件")
  467. err = os.RemoveAll(filePath)
  468. if err != nil {
  469. return
  470. }
  471. }()
  472. //if updateComputers != nil {
  473. // err = global.GVA_DB.Updates(updateComputers).Error
  474. // if err != nil {
  475. // return
  476. // }
  477. //}
  478. if insertComputers != nil {
  479. err = global.GVA_DB.Create(insertComputers).Error
  480. if err != nil {
  481. return
  482. }
  483. }
  484. //var cp log.Computer
  485. //err = cp.DelAllOnlinePcCodeCache()
  486. return
  487. }
  488. func (exa *ServiceRentComputer) compareStrSlice(a, b []string) bool {
  489. if len(a) != len(b) {
  490. return false
  491. }
  492. if (b == nil) != (a == nil) {
  493. return false
  494. }
  495. for key, value := range a {
  496. if value != b[key] {
  497. return false
  498. }
  499. }
  500. return true
  501. }
  502. // CheckIsExpire 定时检查是否有电脑到期,修改租机状态
  503. func (s *ServiceRentComputer) CheckIsExpire() {
  504. global.GVA_LOG.Info("检查是否有电脑到期开始执行:" + time.Now().Format("2006-01-02 15:04:05"))
  505. db := global.GVA_DB.Model(&rentComputer.RentComputer{})
  506. //SELECT pc_num,rent_start,rent_end,is_expire FROM shuyou_rent_computer
  507. //db = db.Where("is_off_shelf = 0")
  508. var computers []*rentComputer.RentComputer
  509. err := db.Order("id").Find(&computers).Error
  510. if err != nil {
  511. global.GVA_LOG.Error("检查是否有电脑到期执行失败:" + time.Now().Format("2006-01-02 15:04:05"))
  512. return
  513. }
  514. const overtime = 24 * 60 * 60
  515. nowUnix := time.Now().Unix()
  516. //global.GVA_LOG.Info(strconv.FormatInt(nowUnix, 10))
  517. //global.GVA_LOG.Info("---------------------------------------------------------------------------------")
  518. computer := new(rentComputer.RentComputer)
  519. for _, c := range computers {
  520. endUnix, _ := time.ParseInLocation("2006-01-02 15:04:05", c.RentEnd, time.Local)
  521. //global.GVA_LOG.Info(strconv.FormatInt(endUnix.Unix(), 10))
  522. if endUnix.Unix() < nowUnix {
  523. computer.IsExpire = 1
  524. } else if endUnix.Unix()-nowUnix <= overtime {
  525. computer.IsExpire = 2
  526. } else {
  527. computer.IsExpire = 0
  528. }
  529. if c.IsExpire == computer.IsExpire {
  530. continue
  531. }
  532. computer.Id = c.Id
  533. computer.UpdateTime = c.UpdateTime
  534. computer.CreateTime = c.CreateTime
  535. computer.PcNum = c.PcNum
  536. computer.PcName = c.PcName
  537. computer.ShopId = c.ShopId
  538. computer.RentStart = c.RentStart
  539. computer.RentDuration = c.RentDuration
  540. computer.RentEnd = c.RentEnd
  541. computer.Remark = c.Remark
  542. computer.TodeskId = c.TodeskId
  543. computer.TodeskPassword = c.TodeskPassword
  544. computer.SunflowerId = c.SunflowerId
  545. computer.SunflowerPassword = c.SunflowerPassword
  546. computer.RentPriceUsed = c.RentPriceUsed
  547. computer.SetMealId = c.SetMealId
  548. computer.IsOffShelf = c.IsOffShelf
  549. err = global.GVA_DB.Save(computer).Error
  550. }
  551. global.GVA_LOG.Info("检查是否有电脑到期执行成功:" + time.Now().Format("2006-01-02 15:04:05"))
  552. return
  553. }
  554. // CreateRentComputerLedger 定时生成今日租机台账表(财务)
  555. func (s *ServiceRentComputer) CreateRentComputerLedger() {
  556. global.GVA_LOG.Info("定时生成今日租机台账表开始执行:" + time.Now().Format("2006-01-02 15:04:05"))
  557. //查询当前所有在租电脑
  558. db := global.GVA_DB.Model(&rentComputer.RentComputer{})
  559. var computers []*rentComputer.RentComputerSetMeal
  560. 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, " +
  561. "r.rent_price,r.rent_price_day,rent_computer.set_meal_id")
  562. db = db.Joins("left join rent_set_meal r on r.id = rent_computer.set_meal_id")
  563. db = db.Where("rent_computer.is_expire != 1")
  564. err := db.Order("rent_computer.id").Find(&computers).Error
  565. if err != nil {
  566. global.GVA_LOG.Error("定时生成今日租机台账表执行失败:"+time.Now().Format("2006-01-02 15:04:05"), zap.Error(err))
  567. return
  568. }
  569. //查询昨日租机台账表中的rent_price_used_then参数
  570. var computersYest []*rentComputer.RentComputerLedger
  571. dbYest := global.GVA_DB.Model(&rentComputer.RentComputerLedger{})
  572. dbYest = dbYest.Select("rent_price_used_then").Order("id")
  573. err = dbYest.Find(&computersYest).Error
  574. if err != nil {
  575. global.GVA_LOG.Error("定时生成今日租机台账表执行失败,查询昨日数据失败:"+time.Now().Format("2006-01-02 15:04:05"), zap.Error(err))
  576. return
  577. }
  578. nowTime := time.Now().Format("2006-01-02 15:04:05")
  579. nowDate := time.Now().Format("2006-01-02")
  580. var insertInfos []rentComputer.RentComputerLedger
  581. for _, computer := range computers {
  582. var info rentComputer.RentComputerLedger
  583. info.UpdateTime = nowTime
  584. info.CreateTime = nowTime
  585. info.NewDate = nowDate
  586. info.PcNum = computer.PcNum
  587. info.PcName = computer.PcName
  588. info.ShopId = computer.ShopId
  589. info.RentStartThen = computer.RentStart
  590. info.RentDurationThen = computer.RentDuration
  591. info.RentEndThen = computer.RentEnd
  592. info.RentPriceThen = computer.RentPrice
  593. info.RentPriceDayThen = computer.RentPriceDay
  594. info.IsExpire = computer.IsExpire
  595. info.SetMealId = computer.SetMealId
  596. info.Remark = computer.Remark
  597. flag := false
  598. for _, yest := range computersYest {
  599. if info.PcNum == yest.PcNum {
  600. flag = true
  601. info.RentPriceUsedThen = yest.RentPriceUsedThen + computer.RentPriceDay
  602. break
  603. }
  604. }
  605. if flag == false {
  606. info.RentPriceUsedThen = computer.RentPriceDay
  607. }
  608. 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) {
  609. // 已存在,更新
  610. err = global.GVA_DB.Model(&rentComputer.RentComputerLedger{}).Where("pc_num = ? and shop_id = ? and new_date = ?", computer.PcNum, computer.ShopId, nowDate).Updates(info).Error
  611. if err != nil {
  612. global.GVA_LOG.Error("定时生成今日租机台账表执行失败,更新数据失败:"+time.Now().Format("2006-01-02 15:04:05"), zap.Error(err))
  613. return
  614. }
  615. } else {
  616. insertInfos = append(insertInfos, info)
  617. }
  618. }
  619. if insertInfos != nil {
  620. err = global.GVA_DB.Create(&insertInfos).Error
  621. if err != nil {
  622. global.GVA_LOG.Error("定时生成今日租机台账表执行失败,插入数据失败:"+time.Now().Format("2006-01-02 15:04:05"), zap.Error(err))
  623. return
  624. }
  625. }
  626. global.GVA_LOG.Info("定时生成今日租机台账表执行成功:" + time.Now().Format("2006-01-02 15:04:05"))
  627. return
  628. }
  629. // SubDays 计算日期相差多少天
  630. // 返回值day>0, t1晚于t2; day<0, t1早于t2
  631. func SubDays(t1, t2 time.Time) (day int) {
  632. swap := false
  633. if t1.Unix() < t2.Unix() {
  634. t_ := t1
  635. t1 = t2
  636. t2 = t_
  637. swap = true
  638. }
  639. //global.GVA_LOG.Info(strconv.Itoa(int(t1.Unix())))
  640. //global.GVA_LOG.Info(strconv.Itoa(int(t2.Unix())))
  641. day = int(t1.Sub(t2).Hours() / 24)
  642. // 计算在被24整除外的时间是否存在跨自然日
  643. if int(t1.Sub(t2).Milliseconds())%86400000 > int(86400000-t2.Unix()%86400000) {
  644. day += 1
  645. }
  646. if swap {
  647. day = -day
  648. }
  649. //global.GVA_LOG.Info(strconv.Itoa(day))
  650. return
  651. }