rent_computer.go 28 KB

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