OrderModel.php 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. <?php
  2. namespace app\common\model;
  3. use think\Db;
  4. /**
  5. * 订单管理表
  6. * Class GoodsTypeModel
  7. * @package app\common\model
  8. */
  9. class OrderModel extends BaseModel
  10. {
  11. protected $table = 'order';
  12. /**
  13. * 获取状态名称
  14. */
  15. public function getOrderStatusText($data)
  16. {
  17. $arr= [10=>'未付款','11'=>'关闭订单',20=>'待发货',30=>'已发货',31=>'已收货',40=>'退款成功'];
  18. return $arr[$data['order_status']];
  19. }
  20. //后台订单分页
  21. public function getOrderPage($page,$pageSize,$condition=[],$is_count = false,$order=''){
  22. $join=[
  23. ['user user','user.user_no=order.user_no','LEFT'],
  24. ['pay_list pay_list','pay_list.order_no=order.order_no','LEFT'],
  25. ['logistics_basic logistics_basic','logistics_basic.logistics_id=order.logistics_id','LEFT'],
  26. ];
  27. $this->alias('order')
  28. ->field('order.*,user.user_name,user.mobile,user.real_name,user.user_name,
  29. pay_list.wx_price,logistics_basic.logistics_name,pay_list.pay_no,pay_list.add_ts add_payTime')
  30. ->join($join);
  31. if($condition){
  32. $this->where($condition);
  33. }
  34. if($is_count){
  35. return $this->group('pay_list.order_no')->count();
  36. }
  37. if($order){
  38. $this->order($order);
  39. }
  40. return $this->page($page,$pageSize)->group('pay_list.order_no')->select();
  41. }
  42. //后台订单分页
  43. public function getOrderLockPage($page,$pageSize,$condition=[],$is_count = false,$order=''){
  44. $join=[
  45. ['user user','user.user_no=order.user_no','LEFT'],
  46. ['pay_list pay_list','pay_list.order_no=order.order_no','LEFT'],
  47. ['order_sales order_sales','order_sales.order_no=order.order_no','LEFT'],
  48. ];
  49. $this->alias('order')
  50. ->field('order.*,user.user_name,user.real_name,user.mobile,pay_list.wx_price,pay_list.pay_no,
  51. order_sales.mobile sales_mobile,sales_price,sales_type,sales_userNote,sales_adminNote,
  52. order_sales.sales_id,order_sales.status sales_status,order_sales.add_ts sales_add_ts
  53. ')
  54. ->join($join);
  55. if($condition){
  56. $this->where($condition);
  57. }
  58. if($is_count){
  59. return $this->group('pay_list.order_no')->count();
  60. }
  61. if($order){
  62. $this->order($order);
  63. }
  64. return $this->page($page,$pageSize)->group('pay_list.order_no')->select();
  65. }
  66. //获取订单单个信息
  67. public function getOrderOne($condition=[]){
  68. $join=[
  69. ['user user','user.user_no=order.user_no','LEFT'],
  70. ['logistics_basic com','com.logistics_id=order.logistics_id','LEFT'],
  71. ['pay_list pay_list','pay_list.order_no=order.order_no','LEFT'],
  72. ];
  73. $this->alias('order')
  74. ->field('order.*,user.user_name,user.mobile,user.gender,user.total_score,user.usable_score,user.real_name,user.lable,
  75. com.logistics_name,com.e_name,pay_list.pay_no
  76. ')
  77. ->join($join);
  78. if($condition){
  79. $this->where($condition);
  80. }
  81. return $this->find();
  82. }
  83. //获取物流信息
  84. public function getLogisticsInfo($condition=[]){
  85. $join=[
  86. ['logistics_basic logistics_basic','logistics_basic.logistics_id=order.logistics_id','LEFT'],
  87. ];
  88. if($condition){
  89. $this->where($condition);
  90. }
  91. $this->alias('order')
  92. ->field('order.*,logistics_basic.logistics_name')
  93. ->join($join);
  94. return $this->find();
  95. }
  96. /**
  97. * 获得用户购买商品数量
  98. * @param $condition
  99. * @return false|\PDOStatement|string|\think\Collection
  100. */
  101. public function getProduct($condition){
  102. return $this->alias('order')
  103. ->field('order.order_id,order.order_no,ol.number,ol.product_id')
  104. ->join('order_list ol','ol.order_no = order.order_no','LEFT')
  105. ->where($condition)
  106. ->select();
  107. }
  108. /**
  109. * 获得订单详情
  110. * @param $condition
  111. * @return false|\PDOStatement|string|\think\Collection
  112. */
  113. // public function getOrderAndProduct($condition){
  114. // return $this->alias('order')
  115. // ->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')
  116. // ->join('order_list ol','ol.order_no = order.order_no','LEFT')
  117. // ->join('product product','product.product_no = ol.product_no','LEFT')
  118. // ->where($condition)
  119. // ->select();
  120. // }
  121. /**
  122. * 获得商品
  123. * @param $condition
  124. * @return false|\PDOStatement|string|\think\Collection
  125. */
  126. public function getOrderAndProduct($condition = []){
  127. $list = $this->alias('order')
  128. ->field('order.*,product.product_name,product.market_price,product.member_price,ol.number as ol_num,product.images,product.product_id,dg.price as dg_price,promo.price as promo_price')
  129. ->join('order_list ol','ol.order_no = order.order_no','LEFT')
  130. ->join('product product','product.product_no=ol.product_no','left')
  131. ->join('doctor_guide dg','dg.doctor_no = ol.doctor_no and dg.product_no = ol.product_no','LEFT')
  132. ->join('product_promo promo','promo.product_no = ol.product_no','LEFT')
  133. ->where($condition)
  134. ->select();
  135. foreach ($list as &$pro){
  136. if($pro['dg_price'] != null){
  137. $member_price = $pro['dg_price'];
  138. }elseif($pro['promo_price'] != null){
  139. $member_price = $pro['promo_price'];
  140. }else{
  141. $member_price = $pro['member_price'];
  142. }
  143. $intPrice = intval($member_price/100);
  144. $dotPrice = intval($member_price%100);
  145. $dotPrice = str_pad($dotPrice,2,0);
  146. $pro['sales_price'] = $intPrice.".".$dotPrice;
  147. }
  148. return $list;
  149. }
  150. /**
  151. * 检查半小时未付款的订单
  152. */
  153. public static function checkOrder(){
  154. $start = time()-30*60;
  155. $orderInfo = Db::table('order')->alias('order')
  156. ->field('order.order_id,order.order_no,ol.number,ol.product_no')
  157. ->join('order_list ol','ol.order_no = order.order_no','LEFT')
  158. ->where(['order_status'=>'10','add_time'=>['lt',$start]])->select();
  159. foreach ($orderInfo as $order){
  160. $productInfo = Db::table('product')->where(['product_no'=>"{$order['product_no']}"])->find();
  161. if(empty($productInfo)){
  162. continue;
  163. }
  164. $number = intval($order['number']) + intval($productInfo['number']);
  165. Db::table('order')->startTrans();
  166. Db::table('product')->where(['product_no'=>"{$order['product_no']}",'number'=>"{$productInfo['number']}"])->update(['number'=>"{$number}"]);
  167. $res = Db::table('order')->where(['order_id'=>"{$order['order_id']}",'order_status'=>10])->update(['order_status'=>11]);
  168. if($res === false){
  169. Db::table('order')->rollback();
  170. continue;
  171. }
  172. Db::table('order')->commit();
  173. }
  174. }
  175. /**
  176. * 得到订单表和售后表数据
  177. * @param array $condition
  178. * @return false|\PDOStatement|string|\think\Collection
  179. */
  180. public function getOrderAndSales($condition=[]){
  181. $this->alias('order')
  182. ->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.add_ts as sa_add_ts')
  183. ->join('order_sales os','os.order_no = order.order_no','LEFT')
  184. ->order('order_status desc,os.status desc')
  185. ->where($condition)
  186. ->select();
  187. echo $this->getLastSql();exit;
  188. }
  189. /**
  190. *@param array 查询条件
  191. *@return array 查询结果
  192. *获取用户订单个状态数量
  193. */
  194. public function getUserOrderNums($where){
  195. $this->field("count(order_no) as nums,order_status");
  196. $this->where($where);
  197. $this->group('order_status');
  198. return $this->select();
  199. }
  200. //获取订单列表表格导出
  201. public function getOrderList($condition=[],$order=''){
  202. $join=[
  203. ['user user','user.user_no=order.user_no','LEFT'],
  204. ['pay_list pay','pay.order_no=order.order_no','LEFT'],
  205. ];
  206. $this->alias('order')
  207. ->field('order.*,order.address order_address,
  208. user.user_name,user.real_name,
  209. pay.wx_price,pay.pay_style,pay.add_ts pay_time,pay.pay_no
  210. ')
  211. ->join($join);
  212. if($condition){
  213. $this->where($condition);
  214. }
  215. if($order){
  216. $this->order($order);
  217. }
  218. return $this->BaseModel($this->select());
  219. }
  220. }