UnlockOrder.php 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | 商城系统 [ 致力于通过产品和服务,帮助商家高效化开拓市场 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2017~2021 https://www.thinkphp.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed 这不是一个自由软件,不允许对程序代码以任何形式任何目的的再发行
  8. // +----------------------------------------------------------------------
  9. // | Author: thinkphp <admin@yiovo.com>
  10. // +----------------------------------------------------------------------
  11. declare (strict_types=1);
  12. namespace app\common\model;
  13. use app\api\service\User as UserService;
  14. use app\common\service\Order as OrderService;
  15. use cores\BaseModel;
  16. /**
  17. * 解锁生源用户订单模型类
  18. * Class UnlockOrder
  19. * @package app\common\model
  20. */
  21. class UnlockOrder extends BaseModel
  22. {
  23. protected $globalScope = [''];
  24. // 定义表名
  25. protected $name = 'unlock_order';
  26. // 定义主键
  27. protected $pk = 'order_id';
  28. /**
  29. * 关联会员记录表
  30. * @return \think\model\relation\BelongsTo
  31. */
  32. public function user()
  33. {
  34. $module = self::getCalledModule();
  35. return $this->belongsTo("app\\{$module}\\model\\User");
  36. }
  37. /**
  38. * 付款时间
  39. * @param $value
  40. * @return array
  41. */
  42. public function getPayTimeAttr($value)
  43. {
  44. return format_time($value);
  45. }
  46. /**
  47. * 获取订单详情
  48. * @param $where
  49. * @return array|null|\think\Model
  50. */
  51. public static function detail($where)
  52. {
  53. return static::get($where);
  54. }
  55. /**
  56. * 获取老师已解锁的学校ID
  57. * @param $userId
  58. * @return array
  59. */
  60. public static function getSchoolsByUser($userId)
  61. {
  62. return self::where(['user_id' => $userId, 'status' => 2])
  63. ->column('school_id');
  64. }
  65. public function createOrder($params, int $lockType = 1)
  66. {
  67. // 验证解锁操作
  68. $schoolId = isset($params['school_id']) ? intval($params['school_id']) : 0;
  69. $userId = isset($params['user_id']) ? intval($params['user_id']) : 0;
  70. if ($schoolId <= 0) {
  71. $this->error = '学校参数错误';
  72. return false;
  73. }
  74. // 验证解锁类型
  75. $userIds = $lockType == 1 ? [$userId] : [];
  76. if ($lockType == 1 && $userId <= 0) {
  77. $this->error = '请选择解锁用户';
  78. return false;
  79. }
  80. // 全解
  81. $userInfo = UserService::getCurrentLoginUser(true);
  82. if ($lockType == 2) {
  83. $dayIds = \app\api\model\UserInfo::getLockedIds($userInfo['user_id']);
  84. $ids = UnlockOrder::getLockedUsersBySchool($userInfo['user_id'], $schoolId);
  85. $ids = $dayIds ? array_merge($dayIds, $ids) : [];
  86. $userIds = \app\api\model\User::getSourceUserIds($schoolId, $ids);
  87. }
  88. if (empty($userIds)) {
  89. $this->error = '该生源学校暂无可解锁生源';
  90. return false;
  91. }
  92. $platform = \app\store\model\Setting::getItem('platform');
  93. $price = isset($platform['locked']['locked_cost']) ? floatval($platform['locked']['locked_cost']) : 0;
  94. if ($price <= 0) {
  95. $this->error = '请先后台设置解锁费用';
  96. return false;
  97. }
  98. // 订单基础信息
  99. $num = count($userIds);
  100. $totalPrice = $num * $price;
  101. $data = [
  102. 'user_id' => UserService::getCurrentLoginUserId(),
  103. 'order_no' => 'LC' . OrderService::createOrderNo(),
  104. 'agent_id' => isset($userInfo['info']['agent_id']) ? $userInfo['info']['agent_id'] : 0,
  105. 'school_id' => $schoolId,
  106. 'num' => $num,
  107. 'price' => $price,
  108. 'total_price' => $totalPrice,
  109. 'create_time' => time(),
  110. 'update_time' => time(),
  111. 'status' => 1,
  112. ];
  113. $orderId = self::insertGetId($data);
  114. if (!$orderId) {
  115. $this->error = '解锁订单提交失败';
  116. return false;
  117. }
  118. // 解锁用户数据
  119. $datas = [];
  120. foreach ($userIds as $id) {
  121. $datas[] = [
  122. 'order_id' => $orderId,
  123. 'user_id' => $id,
  124. 'update_time' => time(),
  125. ];
  126. }
  127. $unlockUser = new UnlockUser;
  128. if ($datas && $unlockUser->insertAll($datas)) {
  129. $data['order_id'] = $orderId;
  130. return $data;
  131. }
  132. $this->error = '解锁订单提交失败';
  133. return false;
  134. }
  135. /**
  136. * 保存订单数据,并返回
  137. * @param $data
  138. * @return bool
  139. */
  140. public function saveOrder($data)
  141. {
  142. return self::save($data);
  143. }
  144. /**
  145. * 验证支付订单
  146. * @param $order
  147. * @return bool
  148. * @throws \think\db\exception\DataNotFoundException
  149. * @throws \think\db\exception\DbException
  150. * @throws \think\db\exception\ModelNotFoundException
  151. */
  152. public function checkPay($order)
  153. {
  154. $orderId = isset($order['order_id'])? $order['order_id'] : 0;
  155. if($orderId<=0){
  156. $this->error = '订单参数错误';
  157. return false;
  158. }
  159. $orderStatus = isset($order['status'])? $order['status'] : 0;
  160. if($orderStatus == 2){
  161. $this->error = '订单已支付';
  162. return false;
  163. }
  164. if($orderStatus != 1){
  165. $this->error = '订单未确认或状态不可支付';
  166. return false;
  167. }
  168. return true;
  169. }
  170. /**
  171. * 获取用户解锁订单详情(含关联数据)
  172. * @param int $orderId 订单ID
  173. * @return Order|array|null
  174. * @throws BaseException
  175. * @throws \think\db\exception\DataNotFoundException
  176. * @throws \think\db\exception\DbException
  177. * @throws \think\db\exception\ModelNotFoundException
  178. */
  179. public static function getUserOrderDetail(int $orderId)
  180. {
  181. // 查询订单记录
  182. $order = static::getDetail($orderId);
  183. // 该订单是否允许申请售后
  184. $order['isAllowRefund'] = 1;
  185. return $order;
  186. }
  187. /**
  188. * 获取用户解锁订单详情(仅订单记录)
  189. * @param int $orderId
  190. * @param array $with
  191. * @return Order|array|null
  192. */
  193. public static function getDetail(int $orderId, array $with = [])
  194. {
  195. // 查询订单记录
  196. $order = static::detail($orderId, $with);
  197. empty($order) && throwError('订单不存在');
  198. return $order;
  199. }
  200. /**
  201. * 获取老师该学校已经解锁的用户
  202. * @param $userId
  203. * @param $schoolId
  204. * @return mixed
  205. */
  206. public static function getLockedUsersBySchool($userId, $schoolId)
  207. {
  208. return self::alias('a')
  209. ->leftJoin('unlock_users u', 'u.order_id=a.order_id')
  210. ->where(['a.user_id' => $userId, 'a.school_id' => $schoolId, 'a.status' => 2])
  211. ->column('u.user_id');
  212. }
  213. }