GoodsData.lua 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. local GoodsData = class('GoodsData')
  2. function GoodsData:ctor()
  3. self.count = 0
  4. self.buyNum = 0
  5. end
  6. function GoodsData:Dispose()
  7. self.id = nil
  8. self.price = nil
  9. self.curPrice = nil
  10. self.disPercent = nil
  11. self.hot = nil
  12. self.limitType = nil
  13. self.count = nil
  14. self.startTime = nil
  15. self.endTime = nil
  16. self.buyNum = nil
  17. self.buyLastTime = nil
  18. self.cfgData = nil
  19. end
  20. function GoodsData:SetBaseData(data, cfgData, refreshTime, dayEndTime, weekEndTime)
  21. self.id = data.goods_id
  22. self.price = data.price
  23. self.curPrice = data.cur_price
  24. self.disPercent = data.dispercent
  25. self.hot = data.hot
  26. self.limitType = data.limit_type
  27. self.count = data.count or 0
  28. self.startTime = data.start_time
  29. self.endTime = data.end_time
  30. self.cfgData = cfgData
  31. if not self:IsDiscount() then
  32. self.curPrice = self.price
  33. end
  34. if self.limitType == Enum.LimitBuyType.Forever then
  35. self.endTime = dayEndTime
  36. elseif self.limitType == Enum.LimitBuyType.Daily then
  37. self.endTime = dayEndTime
  38. elseif self.limitType == Enum.LimitBuyType.Week then
  39. self.endTime = weekEndTime
  40. elseif self.limitType == Enum.LimitBuyType.Special then
  41. self.endTime = refreshTime
  42. end
  43. self.buyNum = 0
  44. self.buyLastTime = nil
  45. end
  46. function GoodsData:SetBuyData(data)
  47. self.buyNum = data.buy_num or 0
  48. self.buyLastTime = data.buy_time
  49. end
  50. function GoodsData:GetCurTime()
  51. return ManagerContainer.LuaTimerMgr:GetTimeSecond()
  52. end
  53. function GoodsData:IsValidTime()
  54. if self.limitType == Enum.LimitBuyType.NoLimit then
  55. return true
  56. end
  57. if self.limitType == Enum.LimitBuyType.Forever then
  58. return true
  59. elseif self.limitType == Enum.LimitBuyType.Daily then
  60. local curTime = self:GetCurTime()
  61. return (curTime <= self.endTime)
  62. elseif self.limitType == Enum.LimitBuyType.Week then
  63. local curTime = self:GetCurTime()
  64. return (curTime <= self.endTime)
  65. elseif self.limitType == Enum.LimitBuyType.Time then
  66. local curTime = self:GetCurTime()
  67. return (curTime >= self.startTime and curTime <= self.endTime)
  68. elseif self.limitType == Enum.LimitBuyType.Special then
  69. local curTime = self:GetCurTime()
  70. return (curTime <= self.endTime)
  71. end
  72. return true
  73. end
  74. function GoodsData:IsCanBuy()
  75. if self.limitType == Enum.LimitBuyType.NoLimit then
  76. return true
  77. end
  78. if self:GetRemainBuyNumInternal() <= 0 then
  79. return false
  80. end
  81. if self.limitType == Enum.LimitBuyType.Forever then
  82. return true
  83. elseif self.limitType == Enum.LimitBuyType.Daily then
  84. local curTime = self:GetCurTime()
  85. return (curTime <= self.endTime)
  86. elseif self.limitType == Enum.LimitBuyType.Week then
  87. local curTime = self:GetCurTime()
  88. return (curTime <= self.endTime)
  89. elseif self.limitType == Enum.LimitBuyType.Time then
  90. local curTime = self:GetCurTime()
  91. return (curTime >= self.startTime and curTime <= self.endTime)
  92. elseif self.limitType == Enum.LimitBuyType.Special then
  93. local curTime = self:GetCurTime()
  94. return (curTime <= self.endTime)
  95. end
  96. return true
  97. end
  98. function GoodsData:GetRemainBuyNumInternal()
  99. if not self.count or self.count <= 0 then
  100. return 0
  101. end
  102. if self.buyNum then
  103. return (self.count - self.buyNum)
  104. else
  105. return self.count
  106. end
  107. end
  108. --- 返回剩余可购买次数
  109. ---@return integer -1时为无限次
  110. function GoodsData:GetRemainBuyNum()
  111. if self.limitType == Enum.LimitBuyType.NoLimit then
  112. return -1
  113. end
  114. local remainNum = self:GetRemainBuyNumInternal()
  115. if remainNum <= 0 then
  116. return 0
  117. end
  118. return remainNum
  119. -- if self.limitType == Enum.LimitBuyType.Forever then
  120. -- return remainNum
  121. -- elseif self.limitType == Enum.LimitBuyType.Daily then
  122. -- local curTime = self:GetCurTime()
  123. -- if curTime <= self.endTime then
  124. -- return remainNum
  125. -- else
  126. -- return 0
  127. -- end
  128. -- elseif self.limitType == Enum.LimitBuyType.Week then
  129. -- local curTime = self:GetCurTime()
  130. -- if curTime <= self.endTime then
  131. -- return remainNum
  132. -- else
  133. -- return 0
  134. -- end
  135. -- elseif self.limitType == Enum.LimitBuyType.Time then
  136. -- local curTime = self:GetCurTime()
  137. -- if (curTime >= self.startTime and curTime <= self.endTime) then
  138. -- return remainNum
  139. -- else
  140. -- return 0
  141. -- end
  142. -- elseif self.limitType == Enum.LimitBuyType.Special then
  143. -- local curTime = self:GetCurTime()
  144. -- if curTime <= self.endTime then
  145. -- return remainNum
  146. -- else
  147. -- return 0
  148. -- end
  149. -- end
  150. -- return 0
  151. end
  152. --- 是否卖光了
  153. function GoodsData:IsSoldout()
  154. if self.limitType == Enum.LimitBuyType.NoLimit then
  155. return false
  156. end
  157. if self.limitType == Enum.LimitBuyType.Forever
  158. or self.limitType == Enum.LimitBuyType.Daily
  159. or self.limitType == Enum.LimitBuyType.Week
  160. or self.limitType == Enum.LimitBuyType.Time
  161. or self.limitType == Enum.LimitBuyType.Special then
  162. if self:GetRemainBuyNumInternal() <= 0 then
  163. return true
  164. end
  165. return false
  166. end
  167. return true
  168. end
  169. --- 获得商品的显示规则数据
  170. ---@return boolean,int64,int64 是否显示,显示需要刷新的时间,数据需要刷新的时间
  171. function GoodsData:GetShowInfo()
  172. if self.limitType == Enum.LimitBuyType.NoLimit then
  173. return true, nil, nil
  174. elseif self.limitType == Enum.LimitBuyType.Forever then
  175. -- if self:GetRemainBuyNumInternal() > 0 then
  176. return true, nil, nil
  177. -- else
  178. -- if self.buyLastTime then
  179. -- if (self.endTime - self.buyLastTime) > ManagerContainer.LuaTimerMgr:GetOneDaySeconds() then
  180. -- return false, nil, nil
  181. -- else
  182. -- return true, self.endTime, self.endTime
  183. -- end
  184. -- else
  185. -- return true, self.endTime, self.endTime
  186. -- end
  187. -- end
  188. elseif self.limitType == Enum.LimitBuyType.Daily then
  189. return true, nil, self.endTime
  190. elseif self.limitType == Enum.LimitBuyType.Week then
  191. return true, nil, self.endTime
  192. elseif self.limitType == Enum.LimitBuyType.Time then
  193. local curTime = self:GetCurTime()
  194. if curTime < self.startTime then
  195. return false, self.startTime, self.endTime
  196. elseif curTime > self.endTime then
  197. return false, nil, nil
  198. else
  199. return true, self.endTime, self.endTime
  200. end
  201. elseif self.limitType == Enum.LimitBuyType.Special then
  202. return true, nil, self.endTime
  203. end
  204. return false, nil, nil
  205. end
  206. function GoodsData:IsDiscount()
  207. return self.disPercent and self.disPercent > 0 and self.disPercent < 100
  208. end
  209. function GoodsData:GetSortId()
  210. return self.cfgData.SortId
  211. end
  212. function GoodsData:GetGoodsCfgData()
  213. return self.cfgData
  214. end
  215. function GoodsData:CalculateLimitTime()
  216. local time = ManagerContainer.LuaTimerMgr:GetRemainSeconds(self.endTime)
  217. local timerStr, outTime = CommonUtil.FormatTimeDMS(time)
  218. return timerStr, outTime
  219. end
  220. return GoodsData