123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235 |
- <?php
- namespace app\common\model;
- use think\Db;
- use think\facade\Cache;
- use think\facade\Log;
- class Orders extends Base
- {
- protected $pk = 'id';
- public function getStatusAttr($value)
- {
- $status = [0 => "已取消", 1 => "未付款", 2 => "交易关闭", 3 => "已付款", 4 => "代缴中", 5 => "交易成功"];
- return $status[$value];
- }
- public function getChargeDetailsAttr($value)
- {
- return json_decode($value, true);
- }
- public function orderInfo()
- {
- return $this->hasOne('OrderInfo', 'order_id');
- }
- public function payInfo()
- {
- return $this->hasOne('PayInfo', 'order_id');
- }
- /**
- * 创建订单及支付订单
- * @param $order_data
- * @param $order_info
- * @return bool
- * @throws \think\exception\PDOException
- */
- public function createOrder($order_data, $order_info, $coupon = null, $discounts = false)
- {
- $this->startTrans();
- try {
- //订单信息
- $this->save($order_data);
- $this->orderInfo()->save($order_info);
- Log::record($order_info);
- //是否使用优惠券
- // $coupon_id = 0;
- // $model = model('Coupon');
- // if ($order_info['service_charge'] != 0) {
- // // 打折券
- // $params = array(
- // 'uid' => $order_data['uid'],
- // 'discounts' => $discounts,//是否满足折扣券
- // );
- // $model->grantDiscountsCoupon($params);
- // }
- $this->payInfo()->save(array(
- 'uid' => $order_data['uid'],
- 'pay_status' => 0,
- ));
- // 提交事务
- $result = true;
- $this->commit();
- } catch (\Exception $e) {
- // 回滚事务
- $result = false;
- $this->rollback();
- }
- return $result;
- }
- /**
- * @param $payinfo
- * @return bool
- * @throws \think\exception\PDOException
- */
- public function checkOrderAndChangeStatus($payinfo)
- {
- $this->startTrans();
- $order = $this->where('id', $payinfo['order_id'])->find();
- $payinfo['uid'] = $order['uid'];
- $payinfo['order_sn'] = $order['order_sn'];
- try {
- #TODO 发放缴费券并扣除一张
- $params = array(
- 'uid' => $order['uid'],
- 'cid' => $this->orderInfo()->value('fund_basic') == '0' ? 2 : 3,
- 'oid' => $order->id,
- 'service_type' => $order['service_type'],//缴费类型
- );
- $coupon_id = model('Coupon')->grantDeductionCoupon($params);
- if($order['user_discount_id'] != ''){
- Db::name('user_discount')->where(['id'=>$order['user_discount_id']])->update(['status'=>1]);
- }
- #TODO 检查邀请信息
-
- if($this->orderInfo()->value('fund_basic') != 0){
-
- $inviter = Db::name('invite_relation')->where(['uid' => $payinfo['uid']])->value('inviter');
- if ($inviter && $inviter > 0) {
- $award = config('award');
- $balance = [
- 'uid' => $inviter,
- 'num' => $award['one_award'],
- 'from_uid' => $payinfo['uid'],
- 'type' => 1,
- 'ct_time' => time()
- ];
- Db::name('user_balance')->insert($balance);
- Db::name('user')->where(['uid'=>$inviter])->setInc('balance',$award['one_award']);
- if (($inviter_two = Db::name('invite_relation')->where(['uid' => $inviter])->value('inviter')) && $inviter_two > 0) {
- $balance = [
- 'uid' => $inviter_two,
- 'num' => $award['two_award'],
- 'from_uid' => $payinfo['uid'],
- 'type' => 1,
- 'ct_time' => time()
- ];
- Db::name('user_balance')->insert($balance);
- Db::name('user')->where(['uid'=>$inviter_two])->setInc('balance',$award['two_award']);
- }
- }
-
- }
-
- #TODO 改变订单状态
- $this->save(array(
- 'pay_timestamp' => date('Y-m-d H:i:s'),
- 'status' => 3,
- ), ['order_sn' => $payinfo['order_sn']]);
- $order->payInfo->save(array(
- 'pay_platform' => $payinfo['pay_platform'],
- 'coupon' => $coupon_id,
- 'pay_no' => $payinfo['pay_no'],
- 'pay_money' => $payinfo['pay_money'],
- 'pay_status' => 1,
- ));
- #TODO 记录订单状态
- $log = array(
- 'uid' => $payinfo['uid'],
- 'title' => '订单支付',
- 'remark' => $payinfo['order_sn'] . '订单支付成功'
- );
- // 提交事务
- $result = true;
- $this->commit();
- } catch (\Exception $e) {
- // 回滚事务
- $log = array(
- 'uid' => $payinfo['uid'],
- 'title' => '订单支付',
- 'remark' => $payinfo['order_sn'] . '订单支付失败'
- );
- $result = false;
- $this->rollback();
- }
- model('Action')->save($log);
- return $result;
- }
- /**
- * @param $params
- * @return array|\PDOStatement|string|\think\Collection
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\ModelNotFoundException
- * @throws \think\exception\DbException
- */
- public function getOrderList($params)
- {
- $field = array(
- 'id',
- 'order_sn',
- 'payment',
- 'status',
- 'create_timestamp'
- );
- $order = ['update_timestamp' => 'desc', 'create_timestamp' => 'desc', 'status' => 'desc'];
- $map = [['uid', '=', $params['uid']]];
- $page = isset($params['page']) && $params['page'] > 1 ? $params['page'] - 1 : 0;
- $pageSize = isset($params['pageSize']) ? $params['pageSize'] : 10;
- $limit = ($page * $pageSize) . ',' . $pageSize;
- if (isset($params['status'])) {
- $map[] = ['status', '=', $params['status']];
- }
- if (isset($params['order'])) {
- $order = array_merge($order, $params['order']);
- }
- return $this->field($field)->where($map)->order($order)->limit($limit)->select();
- }
- /**
- * 获取订单列表
- *
- * @param $params
- * @return array|\PDOStatement|string|\think\Collection
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\ModelNotFoundException
- * @throws \think\exception\DbException
- */
- public function getOrderViewList($params)
- {
- $map = [];
- $order = ['create_timestamp'=> 'desc'];
- $page = isset($params['page']) && $params['page'] > 1 ? $params['page'] - 1 : 0;
- $pageSize = isset($params['pageSize']) ? $params['pageSize'] : 10;
- $limit = ($page * $pageSize) . ',' . $pageSize;
- if (isset($params['status'])) {
- $map[] = ['status', '=', $params['status']];
- }
- if (isset($params['order'])) {
- $order = array_merge($order, $params['order']);
- }
- if (isset($params['uid'])) {
- $map[] = ['Orders.uid', '=', $params['uid']];
- }
- return $this->db()->view('Orders', [
- 'id', 'uid', 'order_sn', 'payment', 'service_type', 'status', 'update_timestamp', 'create_timestamp'
- ])->view('User', [
- 'nickname', 'avatar', 'mobile', 'realname', 'id_card', 'social_type'
- ], 'User.uid = Orders.uid', 'LEFT')->view('OrderInfo', [
- 'social_basic', 'fund_basic', 'charge_details', 'service_charge', 'total_charge'
- ], 'Orders.id = OrderInfo.order_id', 'LEFT')->view('PayInfo', [
- 'pay_platform', 'pay_no', 'pay_status', 'pay_money'
- ], 'Orders.id = PayInfo.order_id', 'LEFT')->view('CouponReceive', [
- 'name', 'type', 'value'
- ], 'CouponReceive.id = PayInfo.coupon', 'LEFT')
- ->where($map)->order($order)->limit($limit)->select();
- }
- }
|