OrderRepository.php 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  1. <?php
  2. /**
  3. * 订单相关
  4. * @author x.b
  5. * @date 2020-02-25 10:10:06
  6. */
  7. declare(strict_types=1);
  8. namespace App\Repositories;
  9. use App\Constants\ChangeLogConstant;
  10. use App\Model\CoinsOrderModel;
  11. use App\Model\Mongo\RepairOrderHistoryModel;
  12. use App\Exception\ApiException;
  13. use App\Model\CollectionsModel;
  14. use App\Model\PaymentTypeModel;
  15. use App\Model\UserModel;
  16. use App\Model\PaymentModel;
  17. use App\Model\VipCardModel;
  18. use App\Model\VipOrderModel;
  19. use App\Repositories\Service\QueueService;
  20. use App\Repositories\UserRepository as UserRepository;
  21. use Carbon\Carbon;
  22. use Hyperf\DbConnection\Db;
  23. use KZ2\Repositories\Model\Repository;
  24. use Psr\SimpleCache\CacheInterface;
  25. class OrderRepository extends Repository
  26. {
  27. public function model()
  28. {
  29. return PaymentModel::class;
  30. }
  31. /**
  32. * 查询支付方式/支付通道信息
  33. * @param array no $select 查询字段,默认*
  34. * @return array
  35. */
  36. public function getPaymentData($select = ['*'])
  37. {
  38. return PaymentModel::query()->select($select)->get();
  39. }
  40. /**
  41. * 对指定对订单-标示为查单处理状态
  42. * @param string yes $order_sn 订单号
  43. * @param int no $is_admin_action 是否已查单处理: 0-没处理 1-已处理,(默认 值为 1)
  44. * @return bool
  45. */
  46. public function setAdminAction($order_sn = '', $is_admin_action = 1)
  47. {
  48. $data = array(
  49. 'is_admin_action' => $is_admin_action
  50. );
  51. if ($is_admin_action == 1) {
  52. $data['admin_action_at'] = date('Y-m-d H:i:s');
  53. }
  54. $r = VipOrderModel::query()
  55. ->where('order_sn', $order_sn)
  56. ->update($data);
  57. if (!$r) {
  58. return false;
  59. }
  60. return true;
  61. }
  62. /**
  63. * 查询指定订单信息
  64. * @param string yes $order_sn 订单号
  65. * @return array
  66. */
  67. public function getOrderInfo($order_sn = '')
  68. {
  69. $r = VipOrderModel::query()
  70. ->where('order_sn', $order_sn)
  71. ->first();
  72. if (!$r) {
  73. return array();
  74. }
  75. return $r;
  76. }
  77. /**
  78. * 查询指定订单信息
  79. * @param string yes $order_sn 订单号
  80. * @return array
  81. */
  82. public function getCoinsOrderInfo($order_sn = '')
  83. {
  84. $r = CoinsOrderModel::query()
  85. ->where('order_sn', $order_sn)
  86. ->first();
  87. if (!$r) {
  88. return array();
  89. }
  90. return $r;
  91. }
  92. /**
  93. * 补单
  94. * @param string yes $order_sn 订单号
  95. * @param string no $trade_no 支付单号
  96. * @param string no $repair_remark 补单备注
  97. * @return bool
  98. *
  99. * 处理逻辑:vip_order 订单表标示已付款和付款日期; collections 表创建收款单记录, user表添加会员日期,
  100. * 成功之后,在mongodb中创建一个 补单记录。
  101. * 备注: 不需要去内调回调接口,不要验签!
  102. *
  103. */
  104. public function again($order_sn = '', $trade_no = '', $repair_remark = '')
  105. {
  106. $order_info = $this->getOrderInfo($order_sn);
  107. if (!$order_info) {
  108. return false;
  109. }
  110. $user_info = UserModel::query()
  111. ->where('id', $order_info['user_id'])
  112. ->first();
  113. if (!$user_info) {
  114. return false;
  115. }
  116. $card_info = VipCardModel::query()
  117. ->where('id', $order_info['vip_id'])
  118. ->first();
  119. if (!$card_info) {
  120. return false;
  121. }
  122. Db::beginTransaction();
  123. try {
  124. // 标示订单已付款
  125. $data = array(
  126. 'status' => 1,
  127. 'pay_time' => date('Y-m-d H:i:s'),
  128. 'trade_no' => $trade_no,
  129. 'repair_remark' => $repair_remark,
  130. );
  131. $result = VipOrderModel::query()
  132. ->where('order_sn', $order_sn)
  133. ->update($data);
  134. if (!$result) {
  135. throw new ApiException('标示订单已付款失败');
  136. }
  137. // 创建收款单记录
  138. $insertData = array(
  139. 'user_id' => $user_info['id'],
  140. 'user_code' => $user_info['code'],
  141. 'order_sn' => $order_sn,
  142. 'trade_sn' => $trade_no,
  143. 'order_amount' => $order_info['order_amount'],
  144. 'real_amount' => $order_info['real_amount'],
  145. 'device_type' => $user_info['device_type'],
  146. 'type' => 1, //购买会员
  147. );
  148. $result = CollectionsModel::create($insertData);
  149. if (!$result) {
  150. throw new ApiException('创建收款单记录失败');
  151. }
  152. // 对用户加会员天数
  153. $userInfo = make(UserRepository::class)
  154. ->getUserInfoFromCache($user_info['code']);
  155. if ($userInfo['is_vip'] == 'y') {
  156. $vipTime = Carbon::parse($userInfo['vip_expired'])
  157. ->addDays($card_info['day'])
  158. ->toDateTimeString();
  159. } else {
  160. $vipTime = Carbon::now()
  161. ->addDays($card_info['day'])
  162. ->toDateTimeString();
  163. }
  164. $package = json_decode($order_info['package_info'], true);
  165. $houseCount = 0;
  166. $userCoins = 0;
  167. foreach ($package['jdata']['feature'] as $packVal) {
  168. switch ($packVal['type']) {
  169. case 'house':
  170. $houseCount = $packVal['count'];
  171. break;
  172. }
  173. }
  174. if ($package['send_type'] == 2) {
  175. $userCoins = $package['send_num'];
  176. }
  177. $houseCount = $userInfo['max_open_house'] + $houseCount;
  178. $coins = $userInfo['coins'] + $userCoins;
  179. $result = UserModel::query()
  180. ->where('id', $user_info['id'])
  181. ->update(['vip_expired' => $vipTime, 'vip_level' => $order_info['vip_id'], 'max_open_house' => $houseCount, 'coins' => $coins]);
  182. if (!$result) {
  183. throw new ApiException('对用户加会员天数失败');
  184. }
  185. //创建一条mongo补单记录
  186. $data = array(
  187. '_id' => md5($order_info['user_id'] . '_' . $order_sn),
  188. 'order_sn' => $order_sn,
  189. 'trade_no' => $trade_no,
  190. 'user_id' => $order_info['user_id'],
  191. 'vip_id' => $order_info['vip_id'],
  192. 'vip_days' => $order_info['vip_days'],
  193. 'repair_remark' => $repair_remark,
  194. 'created_at' => date('Y-m-d H:i:s')
  195. );
  196. make(RepairOrderHistoryModel::class)->db()->insertOne($data);
  197. Db::commit();
  198. //清除用户信息redis缓存
  199. make(UserRepository::class)->delUserCache($user_info['code']);
  200. // 帐变记录
  201. if ($userCoins > 0) {
  202. $changeData = [
  203. 'user_uid' => $user_info['code'],
  204. 'currency_type' => ChangeLogConstant::CHANGE_COIN,
  205. 'amount' => $userCoins,
  206. 'way' => ChangeLogConstant::WAY_1,
  207. 'type' => ChangeLogConstant::TYPE_23,
  208. ];
  209. make(QueueService::class)->saveChangeLogJob($changeData);
  210. }
  211. } catch (\Exception $e) {
  212. Db::rollBack();
  213. }
  214. return true;
  215. }
  216. /**
  217. * 获取可用的自发通道
  218. *
  219. * @param $payType
  220. * @return
  221. */
  222. public function getPayChannel($payId)
  223. {
  224. $paymentData = PaymentModel::query()
  225. ->join('payment_type as pt', 'payment.uid', '=', 'pt.payment_uid')
  226. ->leftJoin('payment_attr as pa', 'pt.payment_attr_uid', '=', 'pa.uid')
  227. ->select(
  228. 'payment.pay_code',
  229. 'payment.partner_id',
  230. 'payment.uid',
  231. 'pa.code',
  232. 'pt.limit_type',
  233. 'pt.limit',
  234. 'pt.pay_type',
  235. 'pt.weights',
  236. 'pt.id',
  237. 'pt.name'
  238. )
  239. ->where(['payment.status' => 1, 'pt.status' => 1, 'pt.id' => $payId])
  240. ->first();
  241. return $paymentData;
  242. }
  243. /**
  244. * 金币补单
  245. *
  246. * @param $orderSn
  247. * @param $remark
  248. * @param $userId
  249. * @param $coins
  250. * @return string|void
  251. */
  252. public function coinsAgain($orderSn, $remark, $userId, $coins)
  253. {
  254. $userInfo = UserModel::query()
  255. ->where('id', $userId)
  256. ->first();
  257. if (!$userInfo) {
  258. return false;
  259. }
  260. Db::beginTransaction();
  261. $coinsRes = CoinsOrderModel::query()->where('order_sn', $orderSn)->update(['remark' => $remark, 'status' => 1]);
  262. if (!$coinsRes) {
  263. Db::rollBack();
  264. return false;
  265. }
  266. $userRes = UserModel::query()->where('id', $userId)->increment('coins', $coins);
  267. if (!$userRes) {
  268. Db::rollBack();
  269. return false;
  270. }
  271. Db::commit();
  272. //清除用户信息redis缓存
  273. make(UserRepository::class)->delUserCache($userInfo['code']);
  274. // 帐变记录
  275. $changeData = [
  276. 'user_uid' => $userInfo['code'],
  277. 'currency_type' => ChangeLogConstant::CHANGE_COIN,
  278. 'amount' => $coins,
  279. 'way' => ChangeLogConstant::WAY_1,
  280. 'type' => ChangeLogConstant::TYPE_19,
  281. ];
  282. make(QueueService::class)->saveChangeLogJob($changeData);
  283. return true;
  284. }
  285. }