OrderProduct.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. <?php
  2. namespace app\common\model\order;
  3. use app\common\model\BaseModel;
  4. /**
  5. * 订单商品模型
  6. */
  7. class OrderProduct extends BaseModel
  8. {
  9. protected $name = 'order_product';
  10. protected $pk = 'order_product_id';
  11. /**
  12. * 订单商品列表
  13. * @return \think\model\relation\BelongsTo
  14. */
  15. public function image()
  16. {
  17. return $this->belongsTo('app\\common\\model\\file\\UploadFile', 'image_id', 'file_id');
  18. }
  19. /**
  20. * 关联商品表
  21. * @return \think\model\relation\BelongsTo
  22. */
  23. public function product()
  24. {
  25. return $this->belongsTo('app\\common\\model\\product\\Product');
  26. }
  27. /**
  28. * 关联商品sku表
  29. * @return \think\model\relation\BelongsTo
  30. */
  31. public function sku()
  32. {
  33. return $this->belongsTo('app\\common\\model\\product\\ProductSku', 'spec_sku_id', 'spec_sku_id');
  34. }
  35. /**
  36. * 关联订单主表
  37. * @return \think\model\relation\BelongsTo
  38. */
  39. public function orderM()
  40. {
  41. return $this->belongsTo('Order','order_id','order_id');
  42. }
  43. /**
  44. * 售后单记录表
  45. * @return \think\model\relation\HasOne
  46. */
  47. public function refund()
  48. {
  49. return $this->hasOne('app\\common\\model\\order\\OrderRefund');
  50. }
  51. /**
  52. * 关联分销商
  53. * @return \think\model\relation\BelongsTo
  54. */
  55. public function agent()
  56. {
  57. return $this->belongsTo('app\\common\\model\\agent\\Apply', 'agent_user_id', 'user_id');
  58. }
  59. /**
  60. * 订单商品详情
  61. */
  62. public static function detail($where)
  63. {
  64. return (new static())->with(['image', 'refund', 'orderM'])->find($where);
  65. }
  66. /**
  67. * 获取商品数据 (可指定某天)
  68. */
  69. public function getProductData($startDate = null, $endDate = null, $type, $shop_supplier_id)
  70. {
  71. $model = $this;
  72. if($shop_supplier_id > 0){
  73. $model = $model->where('order.shop_supplier_id', '=', $shop_supplier_id);
  74. }
  75. $model = $model->alias('order_product')
  76. ->join('order order', 'order_product.order_id = order.order_id','left');
  77. $model = $model->where('order.create_time', '>=', strtotime($startDate));
  78. if(is_null($endDate)){
  79. $model = $model->where('order.create_time', '<', strtotime($startDate) + 86400);
  80. }else{
  81. $model = $model->where('order.create_time', '<', strtotime($endDate) + 86400);
  82. }
  83. if($type == 'no_pay'){
  84. // 未支付
  85. return $model->where('order.pay_status', '=', 10)->sum('order_product.total_num');
  86. }else if($type == 'pay'){
  87. // 已支付
  88. return $model->where('order.pay_status', '=', 20)->sum('order_product.total_num');
  89. }
  90. return 0;
  91. }
  92. }