Bill.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. <?php
  2. namespace app\common\model\plus\assemble;
  3. use app\common\enum\settings\DeliveryTypeEnum;
  4. use app\common\library\helper;
  5. use app\common\model\BaseModel;
  6. use app\common\model\order\Order as OrderModel;
  7. use app\common\model\plus\assemble\BillUser as BillUserModel;
  8. /**
  9. * 参与记录模型
  10. */
  11. class Bill extends BaseModel
  12. {
  13. protected $name = 'assemble_bill';
  14. protected $pk = 'assemble_bill_id';
  15. /**
  16. * 详情
  17. */
  18. public static function detail($assemble_bill_id, $with = [])
  19. {
  20. return (new static())->with($with)->where('assemble_bill_id', '=', $assemble_bill_id)->find();
  21. }
  22. /**
  23. * 关联活动表
  24. */
  25. public function activity()
  26. {
  27. return $this->belongsTo('Active', 'assemble_activity_id', 'assemble_activity_id');
  28. }
  29. /**
  30. * 关联创建者
  31. */
  32. public function user()
  33. {
  34. return $this->belongsTo('app\\common\\model\\user\\User', 'creator_id', 'user_id')
  35. ->field(['user_id', 'nickName', 'avatarUrl']);
  36. }
  37. /**
  38. * 关联拼团成员表
  39. */
  40. public function billUser()
  41. {
  42. return $this->hasMany('app\\common\\model\\plus\\assemble\\BillUser', 'assemble_bill_id', 'assemble_bill_id')
  43. ->field(['user_id','assemble_bill_id'])
  44. ->order(['create_time' => 'asc']);
  45. }
  46. /**
  47. * 新拼团订单
  48. */
  49. public function newOrder($product, $sku)
  50. {
  51. $active = Active::detail($sku['assemble_activity_id']);
  52. //插入主表
  53. $this->save([
  54. 'assemble_product_id' => $sku['assemble_product_id'],
  55. 'actual_people' => 1,
  56. 'creator_id' => $product['user_id'],
  57. 'assemble_activity_id' => $sku['assemble_activity_id'],
  58. 'end_time' => time() + $active['together_time'] * 60 * 60,
  59. 'app_id' => $active['app_id'],
  60. ]);
  61. //插入拼团记录表
  62. $bill_user_model = new BillUser();
  63. $bill_user_model->save([
  64. 'assemble_bill_id' => $this['assemble_bill_id'],
  65. 'order_id' => $product['order_id'],
  66. 'user_id' => $product['user_id'],
  67. 'is_creator' => 1,
  68. 'app_id' => $active['app_id'],
  69. ]);
  70. //拼团订单商品
  71. $product->save([
  72. 'bill_source_id' => $this['assemble_bill_id']
  73. ]);
  74. //更新主订单表拼团状态
  75. (new OrderModel)->where('order_id', '=', $product['order_id'])
  76. ->save([
  77. 'assemble_status' => 10
  78. ]);
  79. }
  80. /**
  81. * 参团订单
  82. */
  83. public function updateOrder($product, $sku)
  84. {
  85. //更新拼团人数
  86. $this->where('assemble_bill_id', '=', $this['assemble_bill_id'])->inc('actual_people', 1)->update();;
  87. //插入拼团记录表
  88. $bill_user_model = new BillUser();
  89. $bill_user_model->save([
  90. 'assemble_bill_id' => $this['assemble_bill_id'],
  91. 'order_id' => $product['order_id'],
  92. 'user_id' => $product['user_id'],
  93. 'is_creator' => 0,
  94. 'app_id' => $product['app_id'],
  95. ]);
  96. //开团信息
  97. $asemble_product = Product::detail($sku['assemble_product_id']);
  98. //判断拼团是否成功
  99. if($this['actual_people'] + 1 >= $asemble_product['assemble_num']){
  100. $this->save([
  101. 'status' => 20
  102. ]);
  103. $order_list = (new BillUserModel)
  104. ->field(['order_id'])
  105. ->where('assemble_bill_id', '=', $this['assemble_bill_id'])
  106. ->select();
  107. $orderIds = helper::getArrayColumn($order_list, 'order_id');
  108. //更新主订单表拼团状态
  109. (new OrderModel)->where('order_id', 'in', $orderIds)
  110. ->save([
  111. 'assemble_status' => 20
  112. ]);
  113. // 是否是虚拟商品
  114. $order = OrderModel::detail($product['order_id']);
  115. if($order['delivery_type']['value'] == DeliveryTypeEnum::NO_EXPRESS){
  116. (new OrderModel)->where('order_id', 'in', $orderIds)
  117. ->save([
  118. 'assemble_status' => 20,
  119. 'delivery_status' => 20,
  120. 'delivery_time' => time(),
  121. 'receipt_status' => 20,
  122. 'receipt_time' => time(),
  123. 'order_status' => 30
  124. ]);
  125. }
  126. }else{
  127. //更新主订单表拼团状态
  128. (new OrderModel)->where('order_id', '=', $product['order_id'])
  129. ->save([
  130. 'assemble_status' => 10
  131. ]);
  132. }
  133. }
  134. }