OrderModel.php 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. <?php
  2. namespace app\common\model;
  3. use app\common\service\helperService;
  4. use think\Db;
  5. /**
  6. * 订单管理表
  7. * Class GoodsTypeModel
  8. * @package app\common\model
  9. */
  10. class OrderModel extends BaseModel
  11. {
  12. protected $table = 'order';
  13. /**
  14. * 获得用户购买商品数量
  15. * @param $condition
  16. * @return false|\PDOStatement|string|\think\Collection
  17. */
  18. public function getProduct($condition){
  19. return $this->alias('order')
  20. ->field('order.order_id,order.order_no,ol.number,ol.product_no')
  21. ->join('order_list ol','ol.order_no = order.order_no','LEFT')
  22. ->where($condition)
  23. ->select();
  24. }
  25. /**
  26. * 获取总条数据信息
  27. * @param array $condition
  28. * @return array|false|\PDOStatement|string|Model
  29. */
  30. public function getCounts($condition=[]){
  31. if($condition){
  32. return $this->alias('order')->where($condition)->join('order_sales os','os.order_no = order.order_no','LEFT')->count();
  33. }
  34. return $this->count();
  35. }
  36. /**
  37. * 获得订单详情
  38. * @param $condition
  39. * @return false|\PDOStatement|string|\think\Collection
  40. */
  41. // public function getOrderAndProduct($condition){
  42. // return $this->alias('order')
  43. // ->field('order.*,ol.number,ol.product_no,ol.product_price as ol_price,ol.number as ol_num,product.product_name,product.images,product.product_id')
  44. // ->join('order_list ol','ol.order_no = order.order_no','LEFT')
  45. // ->join('product product','product.product_no = ol.product_no','LEFT')
  46. // ->where($condition)
  47. // ->select();
  48. // }
  49. /**
  50. * 获得商品
  51. * @param $condition
  52. * @return false|\PDOStatement|string|\think\Collection
  53. */
  54. public function getOrderAndProduct($condition = []){
  55. $list = $this->alias('order')
  56. ->field('order.*,product.product_name,product.market_price,product.member_price,ol.actual_price,ol.number as ol_num,product.images,product.product_id,dg.price as dg_price,promo.price as promo_price,product_type.type_name,logistics_basic.e_name,logistics_basic.logistics_name')
  57. ->join('order_list ol','ol.order_no = order.order_no','LEFT')
  58. ->join('product product','product.product_no=ol.product_no','left')
  59. ->join('doctor_guide dg','dg.doctor_no = ol.doctor_no and dg.product_no = ol.product_no','LEFT')
  60. ->join('product_promo promo','promo.product_no = ol.product_no','LEFT')
  61. ->join('logistics_basic logistics_basic','logistics_basic.logistics_id=order.logistics_id','LEFT')
  62. ->join('product_type product_type','product.type_id = product_type.type_id and product_type.status=1','left')
  63. ->where($condition)
  64. ->select();
  65. foreach ($list as &$pro){
  66. if($pro['dg_price'] != null){
  67. $member_price = $pro['dg_price'];
  68. }elseif($pro['promo_price'] != null){
  69. $member_price = $pro['promo_price'];
  70. }else{
  71. $member_price = $pro['member_price'];
  72. }
  73. $intPrice = intval($member_price/100);
  74. $dotPrice = intval($member_price%100);
  75. $dotPrice = str_pad($dotPrice,2,0);
  76. $pro['sales_price'] = $intPrice.".".$dotPrice;
  77. }
  78. return $list;
  79. }
  80. /**
  81. * 检查半小时未付款的订单
  82. */
  83. public static function checkOrder(){
  84. $start = time()-30*60;
  85. $orderInfo = Db::table('order')->alias('order')
  86. ->field('order.order_id,order.order_no,ol.number,ol.product_no')
  87. ->join('order_list ol','ol.order_no = order.order_no','LEFT')
  88. ->where(['order_status'=>'10','add_time'=>['lt',$start]])->column('*');
  89. foreach ($orderInfo as $order){
  90. $productInfo = Db::table('product')->where(['product_no'=>"{$order['product_no']}"])->find();
  91. $productInfo = helperService::modelDataToArr($productInfo);
  92. if(empty($productInfo)){
  93. continue;
  94. }
  95. $number = intval($order['number']) + intval($productInfo['number']);
  96. $sales_volume = intval($productInfo['sales_volume']) - intval($order['number']);
  97. Db::table('order')->startTrans();
  98. Db::table('product')->where(['product_no'=>"{$order['product_no']}",'sales_volume'=>"{$productInfo['sales_volume']}"])->update(['number'=>"{$number}",'sales_volume'=>"{$sales_volume}"]);
  99. $res = Db::table('order')->where(['order_id'=>"{$order['order_id']}",'order_status'=>10])->update(['order_status'=>11]);
  100. if($res === false){
  101. Db::table('order')->rollback();
  102. continue;
  103. }
  104. Db::table('order')->commit();
  105. }
  106. }
  107. //后台订单分页
  108. public function getOrderPage($page,$pageSize,$condition=[],$is_count = false,$order=''){
  109. $join=[
  110. ['user user','user.user_no=order.user_no','LEFT'],
  111. ];
  112. $this->alias('order')
  113. ->field('order.*,user.user_name,user.mobile')
  114. ->join($join);
  115. if($condition){
  116. $this->where($condition);
  117. }
  118. if($is_count){
  119. return $this->count();
  120. }
  121. if($order){
  122. $this->order($order);
  123. }
  124. return $this->page($page,$pageSize)->select();
  125. }
  126. //获取订单单个信息
  127. public function getOrderOne($condition=[]){
  128. $join=[
  129. ['user user','user.user_no=order.user_no','LEFT'],
  130. ];
  131. $this->alias('order')
  132. ->field('order.*,user.user_name,user.mobile,user.gender,user.total_score,user.usable_score')
  133. ->join($join);
  134. if($condition){
  135. $this->where($condition);
  136. }
  137. return $this->find();
  138. }
  139. //获取物流信息
  140. public function getLogisticsInfo($condition=[]){
  141. $join=[
  142. ['logistics_basic logistics_basic','logistics_basic.logistics_id=order.logistics_id','LEFT'],
  143. ];
  144. if($condition){
  145. $this->where($condition);
  146. }
  147. $this->alias('order')
  148. ->field('order.*,logistics_basic.logistics_name')
  149. ->join($join);
  150. return $this->find();
  151. }
  152. /**
  153. * 得到订单表和售后表数据
  154. * @param array $condition
  155. * @return false|\PDOStatement|string|\think\Collection
  156. */
  157. public function getOrderAndSales($condition=[]){
  158. return $this->alias('order')
  159. ->field('order.*,os.mobile as sa_mobile,os.sales_price,os.sales_type,os.sales_userNote,os.sales_adminNote,os.status as sa_status,os.is_shows,os.add_ts as sa_add_ts,logistics_basic.e_name')
  160. ->join('order_sales os','os.order_no = order.order_no','LEFT')
  161. ->join('logistics_basic logistics_basic','logistics_basic.logistics_id=order.logistics_id','LEFT')
  162. ->order('order_status asc,os.status desc')
  163. ->where($condition)
  164. ->select();
  165. }
  166. /**
  167. *@param array 查询条件
  168. *@return array 查询结果
  169. *获取用户订单个状态数量
  170. */
  171. public function getUserOrderNums($where){
  172. $this->field("count(order_no) as nums,order_status");
  173. $this->where($where);
  174. $this->group('order_status');
  175. return $this->select();
  176. }
  177. }