Orders.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. <?php namespace App\Models;
  2. use Illuminate\Database\Eloquent\Factories\HasFactory;
  3. use Illuminate\Database\Eloquent\Model;
  4. use App\Models\Traits\Orders\Status as OrdersStatus;
  5. /**
  6. * 需求模型
  7. *
  8. */
  9. class Orders extends Model
  10. {
  11. use HasFactory,OrdersStatus;
  12. // 与模型关联的表名
  13. protected $table = 'orders';
  14. // 是否主动维护时间戳
  15. public $timestamps = false;
  16. // 定义时间戳字段名
  17. // const CREATED_AT = 'insert_time';
  18. // const UPDATED_AT = 'update_time';
  19. /**
  20. * 添加数据
  21. *
  22. */
  23. public function add($data)
  24. {
  25. // 时间
  26. if( empty($data['insert_time']) ) $data['insert_time'] = time();
  27. if( empty($data['update_time']) ) $data['update_time'] = time();
  28. // 写入数据表
  29. $id = $this->query()->insertGetId($data);
  30. // 返回结果
  31. return $id;
  32. }
  33. /**
  34. * 添加数据
  35. *
  36. */
  37. public function edit($id,$data)
  38. {
  39. // 更新时间
  40. if( empty($data['update_time']) ) $data['update_time'] = time();
  41. // 写入数据表
  42. $result = $this->query()->where([['id','=',$id]])->update($data);
  43. // 返回结果
  44. return $id;
  45. }
  46. /**
  47. * 编码转id
  48. *
  49. * @param string $code 编码
  50. *
  51. */
  52. public function codeToId($code){
  53. return intval(str_ireplace('kldd','',$code));
  54. }
  55. /**
  56. * id转编码
  57. *
  58. * @param int $id 编码
  59. *
  60. */
  61. public function idToCode($id){
  62. return 'kldd'. str_pad($id, 9, '0', STR_PAD_LEFT);;
  63. }
  64. /**
  65. * 取消订单
  66. *
  67. * @param int $id 订单ID
  68. * @param int $status 订单状态
  69. * @param OrdersProduct $OrdersProduct 子订单模型
  70. */
  71. public function setOrderStatus($id,$status,$OrdersProduct){
  72. // 查询数据
  73. $result = $this->edit($id,['status'=>$status]);
  74. // 提示新增失败
  75. if( !$result ) {
  76. // 提示信息
  77. return ['error'=>'订单修改失败'];
  78. }
  79. // 查询数据
  80. $result = $OrdersProduct->query()->where([['order_id','=',$id]])->update(['status'=>$status,'update_time'=>time()]);
  81. // 提示新增失败
  82. if( !$result ) {
  83. // 提示信息
  84. return ['error'=>'子订单修改失败'];json_send(['code'=>'error','msg'=>'取消失败','data'=>['error'=>'']]);
  85. }
  86. // 进行操作
  87. return ['success'=>'成功'];
  88. }
  89. /**
  90. * 取消优惠券关联订单
  91. *
  92. * @param int $orderTime 订单时间
  93. * @param int $uid 客户ID
  94. * @param OrdersProduct $OrdersProduct 子订单模型
  95. * @param CustomScore $CustomScore 客户积分
  96. */
  97. public function cancelRelate($orderTime,$uid,$OrdersProduct,$CustomScore){
  98. // 模型实例
  99. $CouponRewardRule = new \App\Models\CouponRewardRule();
  100. $CustomCoupon = new \App\Models\CustomCoupon();
  101. $CouponRewardProduct = new \App\Models\CouponRewardProduct();
  102. // 获取配置列表
  103. $ruleList = $CouponRewardRule->query()->where([['start_time','<=',$orderTime],['end_time','>=',$orderTime]])->get(['id','name','std_num','remove_custom','city_ids','status','start_time','end_time','coupon_id']);
  104. // 如果有活动列表
  105. foreach ($ruleList as $value) {
  106. // 查询客户是否有该优惠券
  107. $havaCoupon = $CustomCoupon->query()->where([['custom_uid','=',$uid],['coupon_id','=',$value['coupon_id']]])->first(['id','status','order_id']);
  108. // 没有优惠券,跳过
  109. if( !$havaCoupon ) continue;
  110. // 通过配置ID获取对应的商品范围
  111. $productList = $CouponRewardProduct->getListByRule($value['id']);
  112. // 获取客户 规定时段内订单的商品ID以及购买数量
  113. $orderList = $OrdersProduct->query()->where([['custom_uid','=',$uid],['status','=',1],['insert_time','>=',$value['start_time']],['insert_time','<=',$value['end_time']]])->get(['product_id','buy_num'])->toArray();
  114. // 计算商品总量
  115. $total = 0;
  116. // 循环商品范围
  117. foreach ($productList as $scope) {
  118. // 循环订单产品
  119. foreach ($orderList as $order) {
  120. // 如果产品不相等
  121. if( $scope['product_id'] != $order['product_id'] ) continue;
  122. // 相等的计算总量
  123. $total += $scope['product_units'] * $order['buy_num'];
  124. }
  125. }
  126. // 判断总数还是达标,跳过
  127. if( $total >= $value['std_num'] ) continue;
  128. // 剩余的达不到条件,如果有优惠券,未使用的的作废
  129. if( $havaCoupon['status'] == 0 ) {
  130. // 作废
  131. $CustomCoupon->edit($havaCoupon['id'],['status'=>4]);
  132. // 作废成功
  133. continue;
  134. }
  135. // 优惠券有订单信息
  136. if( $havaCoupon['order_id'] ) {
  137. // 获取优惠券关联的订单
  138. $oldData = $this->query()->where([['id','=',$havaCoupon['order_id']],['custom_uid','=',$uid]])->first(['id','order_score','status','custom_uid','insert_time']);
  139. // 如果订单不存在
  140. if( !$oldData ) continue;
  141. // 如果订单状态已取消
  142. if( $oldData['status'] == 4 ) continue;
  143. // 取消订单
  144. $result = $this->setOrderStatus($havaCoupon['order_id'],4,$OrdersProduct);
  145. // 取消失败
  146. if( isset($result['error']) ) return ['error'=>$result['error']];
  147. // 撤回订单
  148. if( $oldData['order_score'] > 0 ) {
  149. // 如果扣减失败
  150. $result = $CustomScore->trade($oldData['custom_uid'],$oldData['id'],($oldData['order_score']*-1),6,1);
  151. // 提示新增失败
  152. if( isset($result['error']) ) return ['error'=>$result['error']];
  153. }
  154. }
  155. }
  156. // 默认成功
  157. return ['success'=>'操作成功'];
  158. }
  159. }