CommonPresellLogic.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. <?php
  2. namespace app\common\logic;
  3. use app\common\enum\PresellEnum;
  4. use app\common\model\Presell;
  5. use app\common\model\PresellGoods;
  6. use think\db\exception\DataNotFoundException;
  7. use think\db\exception\DbException;
  8. use think\db\exception\ModelNotFoundException;
  9. use think\facade\Db;
  10. class CommonPresellLogic extends BaseLogic
  11. {
  12. /**
  13. * @notes 检查商品是否在参与预售
  14. * @param $goods_id
  15. * @return bool
  16. * @author lbzy
  17. * @datetime 2024-04-25 09:21:14
  18. */
  19. static function checkGoodsHas($goods_id) : bool
  20. {
  21. $presell = Presell::alias('p')
  22. ->join('presell_goods pg', 'p.id=pg.presell_id')
  23. ->where('p.status', 'in', PresellEnum::STATUS_GOODS_HAS)
  24. ->where('pg.goods_id', $goods_id)
  25. ->field('p.id')
  26. ->findOrEmpty()->toArray();
  27. return ! empty($presell);
  28. }
  29. /**
  30. * @notes 获取商品详情 预售信息
  31. * @param $goods
  32. * @return object
  33. * @throws DbException
  34. * @throws ModelNotFoundException
  35. * @throws DataNotFoundException
  36. * @author lbzy
  37. * @datetime 2024-04-26 10:42:29
  38. */
  39. static function goodsDetailInfo($goods)
  40. {
  41. $goods_id = $goods->id;
  42. $id = PresellGoods::alias('pg')
  43. ->join('presell p', 'pg.presell_id=p.id')
  44. ->where('p.status', PresellEnum::STATUS_START)
  45. ->where('p.start_time', '<=', time())
  46. ->where('p.end_time', '>=', time())
  47. ->where('pg.goods_id', $goods_id)->value('p.id');
  48. PresellGoods::update([ 'click' => Db::raw('click+1') ], [
  49. [ 'goods_id' ,'=', $goods_id ],
  50. [ 'presell_id', '=', $id ],
  51. ]);
  52. $detail = $id ? Presell::with([
  53. 'goods_detail' => function ($query) use ($goods_id) {
  54. $query->hidden([ 'content' ])
  55. ->where('goods_id', $goods_id)
  56. ->field([ 'id', 'goods_id', 'presell_id', 'click', 'virtual_click', 'virtual_sale', 'max_price', 'min_price' ])
  57. ->with([
  58. 'items' => function ($query) {
  59. $query->hidden([ 'content' ])
  60. ->field([ 'id', 'item_id', 'presell_id', 'presell_goods_id', 'price', 'goods_id', 'sale_nums' ]);
  61. }
  62. ]);
  63. }
  64. ])
  65. ->field([
  66. 'id',
  67. 'name',
  68. 'type',
  69. 'start_time',
  70. 'end_time',
  71. 'remark',
  72. 'send_type',
  73. 'send_type_day',
  74. 'buy_limit',
  75. 'buy_limit_num',
  76. 'status',
  77. 'end_time as end_time_origin'
  78. ])
  79. ->append([ 'status_text', 'type_text', 'send_type_text', 'end_time_remain' ])
  80. ->findOrEmpty($id)
  81. ->toArray() : null;
  82. if (!empty($detail)) {
  83. $detail['goods_detail']['sale_nums'] = array_sum(array_column($detail['goods_detail']['items'], 'sale_nums'));
  84. }
  85. $goods->presell = !empty($detail) ? $detail : null;
  86. if ($goods->presell) {
  87. $presell_items = array_column($goods->presell['goods_detail']['items'], null, 'item_id');
  88. foreach ($goods->spec_value_list as $key => $spec_value) {
  89. $goods->spec_value_list[$key]['presell_price'] = $presell_items[$spec_value['id']]['price'] ?? null;
  90. }
  91. }
  92. return $goods;
  93. }
  94. /**
  95. * @notes 订单详情
  96. * @param $order
  97. * @return array|Presell|\think\Model
  98. * @author lbzy
  99. * @datetime 2024-04-26 17:36:54
  100. */
  101. static function orderInfo($order)
  102. {
  103. $detail = Presell::withTrashed()
  104. ->where('id', $order['presell_id'])
  105. ->field([
  106. 'id',
  107. 'name',
  108. 'type',
  109. 'start_time',
  110. 'end_time',
  111. 'remark',
  112. 'send_type',
  113. 'send_type_day',
  114. 'buy_limit',
  115. 'buy_limit_num',
  116. 'status',
  117. ])
  118. ->append([ 'status_text', 'type_text', 'send_type_text' ])
  119. ->findOrEmpty()
  120. ->toArray();
  121. if ($detail) {
  122. $detail['order_send_text'] = '';
  123. if ($detail['send_type'] == PresellEnum::SEND_TYPE_PAY_SUCCESS) {
  124. if ($order['pay_status'] > 0) {
  125. $detail['order_send_text'] = date('Y-m-d H:i:s', strtotime($order['pay_time']) + $detail['send_type_day'] * 24 * 3600) . '前发货';
  126. } else {
  127. $detail['order_send_text'] = '等待支付完成';
  128. }
  129. }
  130. if ($detail['send_type'] == PresellEnum::SEND_TYPE_END) {
  131. $detail['order_send_text'] = date('Y-m-d H:i:s', strtotime($detail['end_time']) + $detail['send_type_day'] * 24 * 3600) . '前发货';
  132. }
  133. }
  134. return $detail;
  135. }
  136. }