rent_computer.go 33 KB

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