ActiveGoods.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. <?php
  2. /**
  3. *
  4. * @copyright ©2020 点小铺
  5. * @author hanj
  6. * @link: https://dyuit.com
  7. * Created by VSCode
  8. */
  9. namespace app\common\model\sharp;
  10. use app\common\library\helper;
  11. use app\common\model\BaseModel;
  12. use app\common\model\sharp\Goods as GoodsModel;
  13. /**
  14. * 整点秒杀-活动会场与商品关联模型
  15. * Class ActiveGoods
  16. * @package app\common\model\sharp
  17. */
  18. class ActiveGoods extends BaseModel
  19. {
  20. protected $name = 'sharp_active_goods';
  21. /**
  22. * 关联活动会场表
  23. * @return \think\model\relation\BelongsTo
  24. */
  25. public function active()
  26. {
  27. return $this->belongsTo('Active', 'active_id');
  28. }
  29. /**
  30. * 关联活动会场场次表
  31. * @return \think\model\relation\BelongsTo
  32. */
  33. public function activeTime()
  34. {
  35. return $this->belongsTo('ActiveTime', 'active_time_id');
  36. }
  37. /**
  38. * 根据活动场次ID获取商品列表
  39. * @param int $activeTimeId
  40. * @param array $goodsParam
  41. * @return false|\PDOStatement|string|\think\Collection
  42. */
  43. public static function getGoodsListByActiveTimeId($activeTimeId, $goodsParam = [])
  44. {
  45. // 商品关联列表
  46. $model = new static;
  47. $activeGoodsList = $model->getSharpGoodsByActiveTimeId($activeTimeId);
  48. // 将列表的索引值设置为商品ID
  49. $activeGoodsList = helper::arrayColumn2Key($activeGoodsList, 'sharp_goods_id');
  50. // 获取商品列表
  51. $sharpGoodsList = $model->getGoodsListByIds(array_keys($activeGoodsList), $goodsParam);
  52. // 整理活动商品信息
  53. foreach ($sharpGoodsList as &$item) {
  54. // 活动商品的销量
  55. $item['sales_actual'] = $activeGoodsList[$item['sharp_goods_id']]['sales_actual'];
  56. // 商品销售进度
  57. $item['progress'] = $model->getProgress($item['sales_actual'], $item['seckill_stock']);
  58. }
  59. /* @var $sharpGoodsList \think\model\Collection */
  60. return $sharpGoodsList->hidden(['sku', 'goods']);
  61. }
  62. /**
  63. * 计算商品销售进度
  64. * @param $value1
  65. * @param $value2
  66. * @return mixed
  67. */
  68. protected function getProgress($value1, $value2)
  69. {
  70. if ($value2 <= 0) return 100;
  71. $progress = helper::bcdiv($value1, $value2);
  72. return min(100, (int)helper::bcmul($progress, 100, 0));
  73. }
  74. /**
  75. * 获取秒杀商品模型
  76. * @return Goods
  77. */
  78. protected function getGoodsModel()
  79. {
  80. return new GoodsModel;
  81. }
  82. /**
  83. * 根据活动场次ID获取商品集
  84. * @param $activeTimeId
  85. * @return false|\PDOStatement|string|\think\Collection
  86. * @throws \think\db\exception\DataNotFoundException
  87. * @throws \think\db\exception\ModelNotFoundException
  88. * @throws \think\exception\DbException
  89. */
  90. private function getSharpGoodsByActiveTimeId($activeTimeId)
  91. {
  92. return $this->where('active_time_id', '=', $activeTimeId)->select();
  93. }
  94. /**
  95. * 根据商品ID集获取商品列表
  96. * @param array $sharpGoodsIds
  97. * @param array $param
  98. * @return false|\PDOStatement|string|\think\Collection
  99. * @throws \think\db\exception\DataNotFoundException
  100. * @throws \think\db\exception\ModelNotFoundException
  101. * @throws \think\exception\DbException
  102. */
  103. private function getGoodsListByIds($sharpGoodsIds, $param = [])
  104. {
  105. return $this->getGoodsModel()->getListByIds($sharpGoodsIds, $param);
  106. }
  107. }