rent_computer.go 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437
  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," +
  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. //global.GVA_LOG.Info(strconv.Itoa(int(api.IsOffShelf)))
  37. //global.GVA_LOG.Info(strconv.Itoa(int(api.IsExpire)))
  38. if api.IsOffShelf != -1 {
  39. db = db.Where("rent_computer.is_off_shelf = ?", api.IsOffShelf)
  40. }
  41. if api.IsExpire != -1 {
  42. db = db.Where("rent_computer.is_expire = ?", api.IsExpire)
  43. }
  44. var total int64
  45. err := db.Count(&total).Error
  46. //if err != nil {
  47. // return nil, 0, err
  48. //}
  49. limit := info.PageSize
  50. offset := info.PageSize * (info.Page - 1)
  51. //var statisticsLogs []*log.LogComputer
  52. var statisticscomputers []*response.ComputerStatisticsReply2
  53. db = db.Limit(limit).Offset(offset)
  54. if order != "" {
  55. var OrderStr string
  56. // 设置有效排序key 防止sql注入
  57. // 感谢 Tom4t0 提交漏洞信息
  58. orderMap := make(map[string]bool, 3)
  59. orderMap["pc_num"] = true
  60. //orderMap["game_id"] = true
  61. //orderMap["operator"] = true
  62. if orderMap[order] {
  63. if desc {
  64. OrderStr = order + " desc"
  65. } else {
  66. OrderStr = order
  67. }
  68. } else { // didn't matched any order key in `orderMap`
  69. global.GVA_LOG.Error("获取失败!", zap.Error(err))
  70. return statisticscomputers, total, err
  71. }
  72. err = db.Order(OrderStr).Find(&statisticscomputers).Error
  73. } else {
  74. err = db.Order("id").Find(&statisticscomputers).Error
  75. }
  76. if err != nil {
  77. return nil, 0, err
  78. }
  79. var statisticsLogsComputer []*response.ComputerStatisticsReply2
  80. for _, statisticsLog := range statisticscomputers {
  81. statisticscomputer := new(response.ComputerStatisticsReply2)
  82. statisticscomputer.Id = statisticsLog.Id
  83. statisticscomputer.UpdateTime = statisticsLog.UpdateTime
  84. statisticscomputer.CreateTime = statisticsLog.CreateTime
  85. statisticscomputer.PcNum = statisticsLog.PcNum
  86. statisticscomputer.PcName = statisticsLog.PcName
  87. statisticscomputer.ShopId = statisticsLog.ShopId
  88. statisticscomputer.RentStart = statisticsLog.RentStart
  89. statisticscomputer.RentDuration = statisticsLog.RentDuration
  90. statisticscomputer.RentEnd = statisticsLog.RentEnd
  91. statisticscomputer.Remark = statisticsLog.Remark
  92. statisticscomputer.TodeskId = statisticsLog.TodeskId
  93. statisticscomputer.TodeskPassword = statisticsLog.TodeskPassword
  94. statisticscomputer.SunflowerId = statisticsLog.SunflowerId
  95. statisticscomputer.SunflowerPassword = statisticsLog.SunflowerPassword
  96. statisticscomputer.RentPrice = statisticsLog.RentPrice
  97. statisticscomputer.RentPriceDay = statisticsLog.RentPriceDay
  98. statisticscomputer.RentPriceUsed = statisticsLog.RentPriceUsed
  99. statisticscomputer.IsExpire = statisticsLog.IsExpire
  100. statisticscomputer.SetMealId = statisticsLog.SetMealId
  101. statisticscomputer.PriceType = statisticsLog.PriceType
  102. statisticscomputer.IsOffShelf = statisticsLog.IsOffShelf
  103. statisticscomputer.ShopName = statisticsLog.ShopName
  104. statisticscomputer.SetMealName = statisticsLog.SetMealName
  105. statisticsLogsComputer = append(statisticsLogsComputer, statisticscomputer)
  106. }
  107. return statisticsLogsComputer, total, err
  108. }
  109. func (s *ServiceRentComputer) GetRentComputerNum() int64 {
  110. var total int64
  111. db := global.GVA_DB.Model(&rentComputer.RentComputer{})
  112. db = db.Distinct("id")
  113. db = db.Where("is_off_shelf = 0 and is_expire != 1")
  114. _ = db.Count(&total).Error
  115. return total
  116. }
  117. func (s *ServiceRentComputer) AddRentComputer(requestCoding request.ComputerRequest) (err error) {
  118. if !errors.Is(global.GVA_DB.Where("pc_num = ? and shop_id = ?", requestCoding.PcNum, requestCoding.ShopId).First(&rentComputer.RentComputer{}).Error, gorm.ErrRecordNotFound) {
  119. return errors.New("租机编号已存在")
  120. }
  121. computer := new(rentComputer.RentComputer)
  122. //computer.Id = requestCoding.Id
  123. computer.PcNum = requestCoding.PcNum
  124. computer.PcName = requestCoding.PcName
  125. computer.ShopId = requestCoding.ShopId
  126. computer.RentStart = requestCoding.RentStart
  127. computer.RentDuration = requestCoding.RentDuration
  128. computer.RentEnd = requestCoding.RentEnd
  129. computer.Remark = requestCoding.Remark
  130. computer.TodeskId = requestCoding.TodeskId
  131. computer.TodeskPassword = requestCoding.TodeskPassword
  132. computer.SunflowerId = requestCoding.SunflowerId
  133. computer.SunflowerPassword = requestCoding.SunflowerPassword
  134. computer.RentPriceUsed = requestCoding.RentPriceUsed
  135. computer.IsExpire = requestCoding.IsExpire
  136. computer.SetMealId = requestCoding.SetMealId
  137. computer.IsOffShelf = requestCoding.IsOffShelf
  138. computer.CreateTime = time.Now().Format("2006-01-02 15:04:05")
  139. computer.UpdateTime = time.Now().Format("2006-01-02 15:04:05")
  140. //key := codeListCacheKey
  141. //_ = s.cache.DelBatheHsCache(context.Background(), key)
  142. return global.GVA_DB.Create(&computer).Error
  143. }
  144. func (s *ServiceRentComputer) GetRentComputerById(id int) (computerShop response.ComputerStatisticsReply2, err error) {
  145. db := global.GVA_DB.Model(&rentComputer.RentComputer{})
  146. var api response.ComputerStatisticsReply2
  147. db = db.Select("rent_computer.id,rent_computer.update_time,rent_computer.create_time,rent_computer.pc_num,rent_computer.set_meal_id," +
  148. "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," +
  149. "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," +
  150. "s.name as shop_name, r.name as set_meal_name,r.rent_price,r.price_type,r.rent_price_day")
  151. db = db.Joins("left join rent_computer_shop s on s.id = rent_computer.shop_id")
  152. db = db.Joins("left join rent_set_meal r on r.id = rent_computer.set_meal_id")
  153. err = db.Where("rent_computer.id = ?", id).Order(id).Limit(1).Find(&api).Error
  154. if err != nil {
  155. return
  156. }
  157. computerShop.Id = api.Id
  158. computerShop.PcNum = api.PcNum
  159. computerShop.PcName = api.PcName
  160. computerShop.RentStart = api.RentStart
  161. computerShop.RentDuration = api.RentDuration
  162. computerShop.RentEnd = api.RentEnd
  163. computerShop.TodeskId = api.TodeskId
  164. computerShop.TodeskPassword = api.TodeskPassword
  165. computerShop.SunflowerId = api.SunflowerId
  166. computerShop.SunflowerPassword = api.SunflowerPassword
  167. computerShop.RentPrice = api.RentPrice
  168. computerShop.RentPriceDay = api.RentPriceDay
  169. computerShop.RentPriceUsed = api.RentPriceUsed
  170. computerShop.IsExpire = api.IsExpire
  171. computerShop.SetMealId = api.SetMealId
  172. computerShop.PriceType = api.PriceType
  173. computerShop.IsOffShelf = api.IsOffShelf
  174. global.GVA_LOG.Info(strconv.Itoa(api.IsOffShelf))
  175. global.GVA_LOG.Info(strconv.Itoa(computerShop.IsOffShelf))
  176. computerShop.ShopName = api.ShopName
  177. computerShop.SetMealName = api.SetMealName
  178. computerShop.Remark = api.Remark
  179. computerShop.ShopId = api.ShopId
  180. computerShop.UpdateTime = api.UpdateTime
  181. computerShop.CreateTime = api.CreateTime
  182. return
  183. }
  184. func (s *ServiceRentComputer) EditRentComputer(computer rentComputer.RentComputer) (err error) {
  185. err = global.GVA_DB.Save(computer).Error
  186. return err
  187. }
  188. func (s *ServiceRentComputer) DeleteRentComputerByIds(ids request.IdsReq) (err error) {
  189. err = global.GVA_DB.Delete(&[]rentComputer.RentComputer{}, "id in ?", ids.Ids).Error
  190. return err
  191. }
  192. func (s *ServiceRentComputer) DeleteRentComputerById(api rentComputer.RentComputer) (err error) {
  193. var entity rentComputer.RentComputer
  194. //global.GVA_LOG.Info(strconv.Itoa(int(api.Id)))
  195. err = global.GVA_DB.Where("id = ?", api.Id).First(&entity).Error // 根据id查询api记录
  196. if errors.Is(err, gorm.ErrRecordNotFound) { // api记录不存在
  197. return err
  198. }
  199. return global.GVA_DB.Delete(&entity).Error
  200. }
  201. func (exa *ServiceRentComputer) ParseExcel2InfoList(filePath string) (err error) {
  202. skipHeader := true
  203. fixedHeader := []string{"租机编码", "供应商", "所属套餐", "套餐类型", "起租日", "到期日", "todesk号", "todesk密码", "向日葵号", "向日葵密码"}
  204. file, err := excelize.OpenFile(filePath)
  205. if err != nil {
  206. return err
  207. }
  208. var insertComputers []*rentComputer.RentComputer
  209. //var updateComputers []*rentComputer.RentComputer
  210. rows, err := file.Rows("Sheet1")
  211. if err != nil {
  212. return err
  213. }
  214. //查询数据库里的套餐与供应商=====================================
  215. var setMeals []*response.SetMealStatisticsReply1
  216. db := global.GVA_DB.Model(&rentComputer.RentSetMeal{})
  217. db = db.Select("rent_set_meal.id, rent_set_meal.name," +
  218. "rent_set_meal.price_type,rent_set_meal.shop_id,s.name as shop_name")
  219. db = db.Joins("left join rent_computer_shop s on s.id = rent_set_meal.shop_id")
  220. err = db.Order("id").Find(&setMeals).Error
  221. if err != nil {
  222. return err
  223. }
  224. // =========================================================
  225. index := 0
  226. for rows.Next() {
  227. index++
  228. row, err := rows.Columns()
  229. if err != nil {
  230. return err
  231. }
  232. if skipHeader {
  233. if exa.compareStrSlice(row, fixedHeader) {
  234. skipHeader = false
  235. continue
  236. } else {
  237. return errors.New("Excel格式错误")
  238. }
  239. }
  240. c := new(rentComputer.RentComputer)
  241. c.PcNum = row[0]
  242. c.PcName = row[0]
  243. flagShopId := false
  244. flagSetMealName := false
  245. for _, setMeal := range setMeals {
  246. if row[1] == setMeal.ShopName {
  247. flagShopId = true
  248. c.ShopId = setMeal.ShopId
  249. //break
  250. }
  251. priceType := 0
  252. if row[3] == "天卡" {
  253. priceType = 0
  254. } else if row[3] == "周卡" {
  255. priceType = 1
  256. } else {
  257. priceType = 2
  258. }
  259. if row[2] == setMeal.Name && priceType == setMeal.PriceType {
  260. flagSetMealName = true
  261. c.SetMealId = setMeal.Id
  262. break
  263. }
  264. }
  265. if flagShopId == false || flagSetMealName == false {
  266. return errors.New("供应商错误")
  267. }
  268. rentStart, _ := time.ParseInLocation("2006-01-02 15:04:05", row[4]+" 00:00:00", time.Local)
  269. rentEnd, _ := time.ParseInLocation("2006-01-02 15:04:05", row[5]+" 00:00:00", time.Local)
  270. //global.GVA_LOG.Info(strconv.FormatInt(rentStart.Unix(), 10))
  271. //global.GVA_LOG.Info(strconv.FormatInt(rentEnd.Unix(), 10))
  272. c.RentStart = row[4] + " 00:00:00"
  273. c.RentEnd = row[5] + " 00:00:00"
  274. c.RentDuration = int(math.Abs(float64(SubDays(rentStart, rentEnd)))) + 1
  275. c.IsExpire = 0
  276. c.IsOffShelf = 0
  277. // 获取工作表中指定单元格的值
  278. todeskId, _ := file.GetCellValue("Sheet1", "G"+strconv.Itoa(index))
  279. todeskPassword, _ := file.GetCellValue("Sheet1", "H"+strconv.Itoa(index))
  280. sunflowerId, _ := file.GetCellValue("Sheet1", "I"+strconv.Itoa(index))
  281. sunflowerPassword, _ := file.GetCellValue("Sheet1", "J"+strconv.Itoa(index))
  282. c.TodeskId = todeskId //todesk账号密码
  283. c.TodeskPassword = todeskPassword //todesk账号密码
  284. c.SunflowerId = sunflowerId //向日葵账号密码
  285. c.SunflowerPassword = sunflowerPassword //向日葵账号密码
  286. c.CreateTime = time.Now().Format("2006-01-02 15:04:05")
  287. c.UpdateTime = time.Now().Format("2006-01-02 15:04:05")
  288. // 租机编号已存在,存入更新数组中
  289. if !errors.Is(global.GVA_DB.Where("pc_num = ? and shop_id = ?", c.PcNum, c.ShopId).First(&rentComputer.RentComputer{}).Error, gorm.ErrRecordNotFound) {
  290. // 租机编号已存在,存入更新数组中
  291. //updateComputers = append(updateComputers, c)
  292. continue
  293. } else {
  294. insertComputers = append(insertComputers, c)
  295. }
  296. }
  297. defer func() {
  298. global.GVA_LOG.Info("defer 删除文件")
  299. err = os.RemoveAll(filePath)
  300. if err != nil {
  301. return
  302. }
  303. }()
  304. //if updateComputers != nil {
  305. // err = global.GVA_DB.Updates(updateComputers).Error
  306. // if err != nil {
  307. // return
  308. // }
  309. //}
  310. if insertComputers != nil {
  311. err = global.GVA_DB.Create(insertComputers).Error
  312. if err != nil {
  313. return
  314. }
  315. }
  316. //var cp log.Computer
  317. //err = cp.DelAllOnlinePcCodeCache()
  318. return
  319. }
  320. func (exa *ServiceRentComputer) compareStrSlice(a, b []string) bool {
  321. if len(a) != len(b) {
  322. return false
  323. }
  324. if (b == nil) != (a == nil) {
  325. return false
  326. }
  327. for key, value := range a {
  328. if value != b[key] {
  329. return false
  330. }
  331. }
  332. return true
  333. }
  334. // CheckIsExpire 定时检查是否有电脑到期,修改租机状态/*
  335. func (s *ServiceRentComputer) CheckIsExpire() {
  336. global.GVA_LOG.Info("检查是否有电脑到期开始执行:" + time.Now().Format("2006-01-02 15:04:05"))
  337. db := global.GVA_DB.Model(&rentComputer.RentComputer{})
  338. //SELECT pc_num,rent_start,rent_end,is_expire FROM shuyou_rent_computer
  339. db = db.Where("is_off_shelf = 0")
  340. var computers []*rentComputer.RentComputer
  341. err := db.Order("id").Find(&computers).Error
  342. if err != nil {
  343. global.GVA_LOG.Error("检查是否有电脑到期执行失败:" + time.Now().Format("2006-01-02 15:04:05"))
  344. return
  345. }
  346. const overtime = 24 * 60 * 60
  347. nowUnix := time.Now().Unix()
  348. //global.GVA_LOG.Info(strconv.FormatInt(nowUnix, 10))
  349. //global.GVA_LOG.Info("---------------------------------------------------------------------------------")
  350. computer := new(rentComputer.RentComputer)
  351. for _, c := range computers {
  352. endUnix, _ := time.ParseInLocation("2006-01-02 15:04:05", c.RentEnd, time.Local)
  353. //global.GVA_LOG.Info(strconv.FormatInt(endUnix.Unix(), 10))
  354. if endUnix.Unix() < nowUnix {
  355. computer.IsExpire = 1
  356. } else if endUnix.Unix()-nowUnix <= overtime {
  357. computer.IsExpire = 2
  358. } else {
  359. computer.IsExpire = 0
  360. }
  361. if c.IsExpire == computer.IsExpire {
  362. continue
  363. }
  364. computer.Id = c.Id
  365. computer.UpdateTime = c.UpdateTime
  366. computer.CreateTime = c.CreateTime
  367. computer.PcNum = c.PcNum
  368. computer.PcName = c.PcName
  369. computer.ShopId = c.ShopId
  370. computer.RentStart = c.RentStart
  371. computer.RentDuration = c.RentDuration
  372. computer.RentEnd = c.RentEnd
  373. computer.Remark = c.Remark
  374. computer.TodeskId = c.TodeskId
  375. computer.TodeskPassword = c.TodeskPassword
  376. computer.SunflowerId = c.SunflowerId
  377. computer.SunflowerPassword = c.SunflowerPassword
  378. computer.RentPriceUsed = c.RentPriceUsed
  379. computer.SetMealId = c.SetMealId
  380. computer.IsOffShelf = c.IsOffShelf
  381. err = global.GVA_DB.Save(computer).Error
  382. }
  383. global.GVA_LOG.Info("检查是否有电脑到期执行成功:" + time.Now().Format("2006-01-02 15:04:05"))
  384. return
  385. }
  386. // SubDays 计算日期相差多少天
  387. // 返回值day>0, t1晚于t2; day<0, t1早于t2
  388. func SubDays(t1, t2 time.Time) (day int) {
  389. swap := false
  390. if t1.Unix() < t2.Unix() {
  391. t_ := t1
  392. t1 = t2
  393. t2 = t_
  394. swap = true
  395. }
  396. //global.GVA_LOG.Info(strconv.Itoa(int(t1.Unix())))
  397. //global.GVA_LOG.Info(strconv.Itoa(int(t2.Unix())))
  398. day = int(t1.Sub(t2).Hours() / 24)
  399. // 计算在被24整除外的时间是否存在跨自然日
  400. if int(t1.Sub(t2).Milliseconds())%86400000 > int(86400000-t2.Unix()%86400000) {
  401. day += 1
  402. }
  403. if swap {
  404. day = -day
  405. }
  406. //global.GVA_LOG.Info(strconv.Itoa(day))
  407. return
  408. }