Orders.php 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. <?php
  2. namespace app\common\model;
  3. use think\Db;
  4. use think\facade\Cache;
  5. use think\facade\Log;
  6. class Orders extends Base
  7. {
  8. protected $pk = 'id';
  9. public function getStatusAttr($value)
  10. {
  11. $status = [0 => "已取消", 1 => "未付款", 2 => "交易关闭", 3 => "已付款", 4 => "代缴中", 5 => "交易成功"];
  12. return $status[$value];
  13. }
  14. public function getChargeDetailsAttr($value)
  15. {
  16. return json_decode($value, true);
  17. }
  18. public function orderInfo()
  19. {
  20. return $this->hasOne('OrderInfo', 'order_id');
  21. }
  22. public function payInfo()
  23. {
  24. return $this->hasOne('PayInfo', 'order_id');
  25. }
  26. /**
  27. * 创建订单及支付订单
  28. * @param $order_data
  29. * @param $order_info
  30. * @return bool
  31. * @throws \think\exception\PDOException
  32. */
  33. public function createOrder($order_data, $order_info, $coupon = null, $discounts = false)
  34. {
  35. $this->startTrans();
  36. try {
  37. //订单信息
  38. $this->save($order_data);
  39. $this->orderInfo()->save($order_info);
  40. Log::record($order_info);
  41. //是否使用优惠券
  42. // $coupon_id = 0;
  43. // $model = model('Coupon');
  44. // if ($order_info['service_charge'] != 0) {
  45. // // 打折券
  46. // $params = array(
  47. // 'uid' => $order_data['uid'],
  48. // 'discounts' => $discounts,//是否满足折扣券
  49. // );
  50. // $model->grantDiscountsCoupon($params);
  51. // }
  52. $this->payInfo()->save(array(
  53. 'uid' => $order_data['uid'],
  54. 'pay_status' => 0,
  55. ));
  56. // 提交事务
  57. $result = true;
  58. $this->commit();
  59. } catch (\Exception $e) {
  60. // 回滚事务
  61. $result = false;
  62. $this->rollback();
  63. }
  64. return $result;
  65. }
  66. /**
  67. * @param $payinfo
  68. * @return bool
  69. * @throws \think\exception\PDOException
  70. */
  71. public function checkOrderAndChangeStatus($payinfo)
  72. {
  73. $this->startTrans();
  74. $order = $this->where('id', $payinfo['order_id'])->find();
  75. $payinfo['uid'] = $order['uid'];
  76. $payinfo['order_sn'] = $order['order_sn'];
  77. try {
  78. #TODO 发放缴费券并扣除一张
  79. $params = array(
  80. 'uid' => $order['uid'],
  81. 'cid' => $this->orderInfo()->value('fund_basic') == '0' ? 2 : 3,
  82. 'oid' => $order->id,
  83. 'service_type' => $order['service_type'],//缴费类型
  84. );
  85. $coupon_id = model('Coupon')->grantDeductionCoupon($params);
  86. if($order['user_discount_id'] != ''){
  87. Db::name('user_discount')->where(['id'=>$order['user_discount_id']])->update(['status'=>1]);
  88. }
  89. #TODO 检查邀请信息
  90. if($this->orderInfo()->value('fund_basic') != 0){
  91. $inviter = Db::name('invite_relation')->where(['uid' => $payinfo['uid']])->value('inviter');
  92. if ($inviter && $inviter > 0) {
  93. $award = config('award');
  94. $balance = [
  95. 'uid' => $inviter,
  96. 'num' => $award['one_award'],
  97. 'from_uid' => $payinfo['uid'],
  98. 'type' => 1,
  99. 'ct_time' => time()
  100. ];
  101. Db::name('user_balance')->insert($balance);
  102. Db::name('user')->where(['uid'=>$inviter])->setInc('balance',$award['one_award']);
  103. if (($inviter_two = Db::name('invite_relation')->where(['uid' => $inviter])->value('inviter')) && $inviter_two > 0) {
  104. $balance = [
  105. 'uid' => $inviter_two,
  106. 'num' => $award['two_award'],
  107. 'from_uid' => $payinfo['uid'],
  108. 'type' => 1,
  109. 'ct_time' => time()
  110. ];
  111. Db::name('user_balance')->insert($balance);
  112. Db::name('user')->where(['uid'=>$inviter_two])->setInc('balance',$award['two_award']);
  113. }
  114. }
  115. }
  116. #TODO 改变订单状态
  117. $this->save(array(
  118. 'pay_timestamp' => date('Y-m-d H:i:s'),
  119. 'status' => 3,
  120. ), ['order_sn' => $payinfo['order_sn']]);
  121. $order->payInfo->save(array(
  122. 'pay_platform' => $payinfo['pay_platform'],
  123. 'coupon' => $coupon_id,
  124. 'pay_no' => $payinfo['pay_no'],
  125. 'pay_money' => $payinfo['pay_money'],
  126. 'pay_status' => 1,
  127. ));
  128. #TODO 记录订单状态
  129. $log = array(
  130. 'uid' => $payinfo['uid'],
  131. 'title' => '订单支付',
  132. 'remark' => $payinfo['order_sn'] . '订单支付成功'
  133. );
  134. // 提交事务
  135. $result = true;
  136. $this->commit();
  137. } catch (\Exception $e) {
  138. // 回滚事务
  139. $log = array(
  140. 'uid' => $payinfo['uid'],
  141. 'title' => '订单支付',
  142. 'remark' => $payinfo['order_sn'] . '订单支付失败'
  143. );
  144. $result = false;
  145. $this->rollback();
  146. }
  147. model('Action')->save($log);
  148. return $result;
  149. }
  150. /**
  151. * @param $params
  152. * @return array|\PDOStatement|string|\think\Collection
  153. * @throws \think\db\exception\DataNotFoundException
  154. * @throws \think\db\exception\ModelNotFoundException
  155. * @throws \think\exception\DbException
  156. */
  157. public function getOrderList($params)
  158. {
  159. $field = array(
  160. 'id',
  161. 'order_sn',
  162. 'payment',
  163. 'status',
  164. 'create_timestamp'
  165. );
  166. $order = ['update_timestamp' => 'desc', 'create_timestamp' => 'desc', 'status' => 'desc'];
  167. $map = [['uid', '=', $params['uid']]];
  168. $page = isset($params['page']) && $params['page'] > 1 ? $params['page'] - 1 : 0;
  169. $pageSize = isset($params['pageSize']) ? $params['pageSize'] : 10;
  170. $limit = ($page * $pageSize) . ',' . $pageSize;
  171. if (isset($params['status'])) {
  172. $map[] = ['status', '=', $params['status']];
  173. }
  174. if (isset($params['order'])) {
  175. $order = array_merge($order, $params['order']);
  176. }
  177. return $this->field($field)->where($map)->order($order)->limit($limit)->select();
  178. }
  179. /**
  180. * 获取订单列表
  181. *
  182. * @param $params
  183. * @return array|\PDOStatement|string|\think\Collection
  184. * @throws \think\db\exception\DataNotFoundException
  185. * @throws \think\db\exception\ModelNotFoundException
  186. * @throws \think\exception\DbException
  187. */
  188. public function getOrderViewList($params)
  189. {
  190. $map = [];
  191. $order = ['create_timestamp'=> 'desc'];
  192. $page = isset($params['page']) && $params['page'] > 1 ? $params['page'] - 1 : 0;
  193. $pageSize = isset($params['pageSize']) ? $params['pageSize'] : 10;
  194. $limit = ($page * $pageSize) . ',' . $pageSize;
  195. if (isset($params['status'])) {
  196. $map[] = ['status', '=', $params['status']];
  197. }
  198. if (isset($params['order'])) {
  199. $order = array_merge($order, $params['order']);
  200. }
  201. if (isset($params['uid'])) {
  202. $map[] = ['Orders.uid', '=', $params['uid']];
  203. }
  204. return $this->db()->view('Orders', [
  205. 'id', 'uid', 'order_sn', 'payment', 'service_type', 'status', 'update_timestamp', 'create_timestamp'
  206. ])->view('User', [
  207. 'nickname', 'avatar', 'mobile', 'realname', 'id_card', 'social_type'
  208. ], 'User.uid = Orders.uid', 'LEFT')->view('OrderInfo', [
  209. 'social_basic', 'fund_basic', 'charge_details', 'service_charge', 'total_charge'
  210. ], 'Orders.id = OrderInfo.order_id', 'LEFT')->view('PayInfo', [
  211. 'pay_platform', 'pay_no', 'pay_status', 'pay_money'
  212. ], 'Orders.id = PayInfo.order_id', 'LEFT')->view('CouponReceive', [
  213. 'name', 'type', 'value'
  214. ], 'CouponReceive.id = PayInfo.coupon', 'LEFT')
  215. ->where($map)->order($order)->limit($limit)->select();
  216. }
  217. }