TradeService.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | LARAVEL8.0 框架 [ LARAVEL ][ RXThinkCMF ]
  4. // +----------------------------------------------------------------------
  5. // | 版权所有 2017~2021 LARAVEL研发中心
  6. // +----------------------------------------------------------------------
  7. // | 官方网站: http://www.laravel.cn
  8. // +----------------------------------------------------------------------
  9. // | Author: laravel开发员 <laravel.qq.com>
  10. // +----------------------------------------------------------------------
  11. namespace App\Services\Api;
  12. use App\Models\AccountLogModel;
  13. use App\Models\MemberBankModel;
  14. use App\Models\MemberModel;
  15. use App\Models\TradeModel;
  16. use App\Models\VideoCollectModel;
  17. use App\Models\VideoModel;
  18. use App\Services\BaseService;
  19. use App\Services\ConfigService;
  20. use App\Services\RedisService;
  21. use Illuminate\Support\Facades\DB;
  22. /**
  23. * 承兑商交易管理-服务类
  24. * @author laravel开发员
  25. * @since 2020/11/11
  26. * @package App\Services\Api
  27. */
  28. class TradeService extends BaseService
  29. {
  30. // 静态对象
  31. protected static $instance = null;
  32. /**
  33. * 构造函数
  34. * @author laravel开发员
  35. * @since 2020/11/11
  36. * GoodsService constructor.
  37. */
  38. public function __construct()
  39. {
  40. $this->model = new TradeModel();
  41. }
  42. /**
  43. * 静态入口
  44. * @return static|null
  45. */
  46. public static function make()
  47. {
  48. if (!self::$instance) {
  49. self::$instance = (new static());
  50. }
  51. return self::$instance;
  52. }
  53. /**
  54. * 列表数据
  55. * @param $params
  56. * @param int $pageSize
  57. * @return array
  58. */
  59. public function getDataList($params, $pageSize = 18, $field = '', $userId = 0)
  60. {
  61. $where = ['a.mark' => 1, 'b.mark' => 1];
  62. $field = $field ? $field : 'lev_a.*';
  63. $order = 'lev_a.id desc';
  64. $list = $this->model->with(['member', 'acceptor'])->from('trade as a')
  65. ->leftJoin('member as b', 'b.id', '=', 'a.user_id')
  66. ->where($where)
  67. ->where(function ($query) use ($params, $userId) {
  68. $type = isset($params['type']) ? $params['type'] : 0;
  69. if ($type > 0) {
  70. $query->where('a.type', $type);
  71. }
  72. $uid = isset($params['user_id']) ? $params['user_id'] : 0;
  73. if ($uid > 0) {
  74. $query->where('a.user_id', $uid);
  75. }
  76. $appectorUid = isset($params['acceptor_uid']) ? $params['acceptor_uid'] : 0;
  77. if ($appectorUid > 0) {
  78. $query->where('a.acceptor_uid', $appectorUid);
  79. }
  80. $appectorId = isset($params['acceptor_id']) ? $params['acceptor_id'] : 0;
  81. if ($appectorId > 0) {
  82. $query->where('a.acceptor_id', $appectorId);
  83. }
  84. })
  85. ->where(function ($query) use ($params) {
  86. $keyword = isset($params['kw']) ? $params['kw'] : '';
  87. if ($keyword) {
  88. $query->where('a.order_no', 'like', "%{$keyword}%")
  89. ->orWhere('b.nickname', 'like', "%{$keyword}%")
  90. ->orWhere('b.id', '=', "%{$keyword}%");
  91. }
  92. })
  93. ->selectRaw($field)
  94. ->orderByRaw($order)
  95. ->paginate($pageSize > 0 ? $pageSize : 9999999);
  96. $list = $list ? $list->toArray() : [];
  97. if ($list && $list['data']) {
  98. foreach ($list['data'] as &$item) {
  99. $item['time_text'] = isset($item['create_time']) ? dateFormat($item['create_time'], 'Y-m-d H:i') : '';
  100. $member = isset($item['member']) ? $item['member'] : [];
  101. if ($member) {
  102. $member['avatar'] = isset($member['avatar']) && $member['avatar'] ? get_image_url($member['avatar']) : get_image_url('/images/member/logo.png');
  103. }
  104. $item['member'] = $member;
  105. }
  106. }
  107. return [
  108. 'pageSize' => $pageSize,
  109. 'total' => isset($list['total']) ? $list['total'] : 0,
  110. 'list' => isset($list['data']) ? $list['data'] : []
  111. ];
  112. }
  113. /**
  114. * 详情
  115. * @param $id
  116. * @return array
  117. */
  118. public function getInfo($id, $userId = 0, $field = [])
  119. {
  120. $field = $field ? $field : ['a.*'];
  121. $info = $this->model->with(['member', 'acceptor'])->from('trade as a')
  122. ->leftJoin('member as b', 'b.id', '=', 'a.user_id')
  123. ->where(['a.id' => $id, 'a.mark' => 1, 'b.mark' => 1])
  124. ->select($field)
  125. ->first();
  126. $info = $info ? $info->toArray() : [];
  127. if ($info) {
  128. $member = isset($info['member']) ? $info['member'] : [];
  129. if ($member) {
  130. $member['avatar'] = isset($member['avatar']) && $member['avatar'] ? get_image_url($member['avatar']) : get_image_url('/images/member/logo.png');
  131. }
  132. $info['member'] = $member;
  133. $info['time_text'] = isset($info['create_time']) ? dateFormat($info['create_time'], 'Y-m-d H:i') : '';
  134. $info['memberBank'] = MemberBankModel::where(['id' => $info['account_id']])->first();
  135. if (isset($info['memberBank'])) {
  136. $info['memberBank']['qrcode'] = get_image_url($info['memberBank']['qrcode']);
  137. }
  138. }
  139. return $info;
  140. }
  141. /**
  142. * 卖出
  143. * @param $goodsId
  144. * @return bool
  145. */
  146. public function sell($userId, $params, $request)
  147. {
  148. $goodsId = isset($params['goods_id']) ? intval($params['goods_id']) : 0;
  149. $type = isset($params['type']) ? intval($params['type']) : 2;
  150. $userInfo = MemberModel::where(['id' => $userId, 'mark' => 1])
  151. ->select(['id', 'nickname', 'status'])
  152. ->first();
  153. $status = isset($userInfo['status']) ? $userInfo['status'] : 0;
  154. $nickname = isset($userInfo['nickname']) ? $userInfo['nickname'] : '';
  155. if (empty($userInfo) || $status != 1) {
  156. $this->error = 2017;
  157. return false;
  158. }
  159. $thumb = isset($params['thumb']) ? trim($params['thumb']) : '';
  160. if ($thumb) {
  161. $result = upload_base64($thumb);
  162. $thumb = isset($result['file_path']) ? $result['file_path'] : '';
  163. if (empty($thumb)) {
  164. $this->error = 2208;
  165. return false;
  166. }
  167. }
  168. // 音乐
  169. $musicUrl = isset($params['music_url']) ? trim($params['music_url']) : '';
  170. if ($musicUrl) {
  171. $result = upload_remote_image($musicUrl, 'mp3', 'music');
  172. $musicUrl = isset($result['file_path']) ? $result['file_path'] : '';
  173. if (empty($musicUrl)) {
  174. $this->error = 2209;
  175. return false;
  176. }
  177. }
  178. // 视频
  179. $fileUrl = isset($params['file_url']) ? trim($params['file_url']) : '';
  180. $albums = isset($params['album_urls']) ? json_decode($params['album_urls'], true) : [];
  181. if ($type == 1) {
  182. if ($fileUrl) {
  183. $result = upload_video($request, 'video');
  184. $data = isset($result['data']) ? $result['data'] : [];
  185. $fileUrl = isset($data['file_path']) ? $data['file_path'] : '';
  186. if (empty($fileUrl)) {
  187. $this->error = 2212;
  188. return false;
  189. }
  190. } else {
  191. $this->error = 2213;
  192. return false;
  193. }
  194. } // 相册
  195. else if ($type == 2) {
  196. if (empty($albums) || !is_array($albums)) {
  197. $this->error = 2211;
  198. return false;
  199. }
  200. $albums = get_format_images($albums);
  201. }
  202. $title = isset($params['title']) && $params['title'] ? trim($params['title']) : "{$nickname} 发布的短视频";
  203. $description = isset($params['description']) && $params['description'] ? trim($params['description']) : "{$nickname} 发布的短视频";
  204. $publishCheck = ConfigService::make()->getConfigByCode('video_publish_check', 0);
  205. $publishCheck = $publishCheck > 0 ? $publishCheck : 0;
  206. $tags = isset($params['tags']) && $params['tags'] ? $params['tags'] : '';
  207. $data = [
  208. 'user_id' => $userId,
  209. 'title' => $title,
  210. 'type' => $type,
  211. 'description' => $description,
  212. 'tags' => $tags && is_array($tags) ? implode(',', $tags) : $tags,
  213. 'file_url' => $fileUrl,
  214. 'thumb' => $thumb,
  215. 'albums' => $albums ? $albums : '',
  216. 'music_hash' => isset($params['music_hash']) ? trim($params['music_hash']) : '',
  217. 'music_name' => isset($params['music_name']) ? trim($params['music_name']) : '',
  218. 'music_url' => $musicUrl,
  219. 'goods_id' => $goodsId,
  220. 'visible_type' => isset($params['visible_type']) ? intval($params['visible_type']) : 1,
  221. 'is_comment' => isset($params['is_comment']) ? intval($params['is_comment']) : 1,
  222. 'status' => $publishCheck ? 1 : 2,
  223. 'mark' => 1,
  224. 'publish_at' => date('Y-m-d H:i:s'),
  225. 'create_time' => time(),
  226. ];
  227. RedisService::set('caches:videos:publish', ['params' => $params, 'data' => $data], 600);
  228. if ($id = $this->model->insertGetId($data)) {
  229. $this->error = 1023;
  230. // 发布视频任务处理
  231. TaskService::make()->updateTask($userId, 5, $id);
  232. return ['id' => $id];
  233. }
  234. $this->error = 1024;
  235. return false;
  236. }
  237. /**
  238. * 状态设置
  239. * @return bool
  240. */
  241. public function status()
  242. {
  243. $id = request()->post('id', 0);
  244. $status = request()->post('status', 1);
  245. if ($id && !$this->model->where(['id' => $id, 'mark' => 1])->value('id')) {
  246. $this->error = 2981;
  247. return false;
  248. }
  249. if ($this->model->where(['id' => $id, 'mark' => 1])->update(['status' => $status, 'update_time' => time()])) {
  250. $this->error = 1002;
  251. return true;
  252. }
  253. $this->error = 1003;
  254. return true;
  255. }
  256. /**
  257. * 删除
  258. * @return bool
  259. */
  260. public function delete()
  261. {
  262. // 参数
  263. $param = request()->all();
  264. $id = getter($param, "id");
  265. if (empty($id)) {
  266. $this->error = 2014;
  267. return false;
  268. }
  269. if (!$this->model->where(['id' => $id])->value('id')) {
  270. $this->error = 1039;
  271. return false;
  272. }
  273. if ($this->model->where(['id' => $id])->update(['mark' => 0, 'update_time' => time()])) {
  274. $this->model->where(['mark' => 0])->where('update_time', '<=', time() - 3 * 86400)->delete();
  275. $this->error = 1025;
  276. return true;
  277. } else {
  278. $this->error = 1026;
  279. return false;
  280. }
  281. }
  282. /**
  283. * C2C交易,确认
  284. * @param $userId
  285. * @param $params
  286. * @return array|false
  287. */
  288. public function confirm($userId, $params)
  289. {
  290. $cacheKey = "caches:trade:sellxd:lock_{$userId}";
  291. if (RedisService::get($cacheKey)) {
  292. $this->error = 1034;
  293. return false;
  294. }
  295. // 交易订单记录
  296. $info = $this->getInfo($params['id']);
  297. if ($info['status'] != 1) {
  298. $this->error = 1003;
  299. return false;
  300. }
  301. // 星豆
  302. DB::beginTransaction();
  303. RedisService::set($cacheKey, $info, rand(2, 3));
  304. // 买入
  305. // 更新trade状态
  306. // 更新account 状态
  307. // 更新member.balance
  308. TradeModel::where(['id' => $info['id']])->update(['update_time' => time(), 'status' => 2]);
  309. AccountLogModel::where(['source_id' => $info['id']])->update(['update_time' => time(), 'status' => 1]);
  310. if ($info['type'] == 1) {
  311. $updateData = ['balance' => DB::raw("balance + {$info['num']}"), 'update_time' => time()];
  312. if (!MemberModel::where(['id' => $info['user_id']])->update($updateData)) {
  313. $this->error = 2036;
  314. DB::rollBack();
  315. return false;
  316. }
  317. } else {
  318. $updateData = ['balance' => DB::raw("balance - {$info['num']}"), 'update_time' => time()];
  319. if (!MemberModel::where(['id' => $info['user_id']])->update($updateData)) {
  320. $this->error = 2036;
  321. DB::rollBack();
  322. return false;
  323. }
  324. }
  325. // 更新trade状态
  326. // 消息
  327. if ($info['type'] == 1) {
  328. MessageService::make()->pushMessage($userId, '星豆购买已确认', "您在{$info['create_time']}(UTC+8)下单¥{$info['total']}购买{$info['num']}星豆,已成功到账", 3);
  329. }else{
  330. MessageService::make()->pushMessage($userId, '星豆卖出已确认', "您在{$info['create_time']}(UTC+8)下单¥{$info['total']}卖出{$info['num']}星豆,已成功扣减", 3);
  331. }
  332. DB::commit();
  333. $this->error = 2037;
  334. RedisService::clear($cacheKey);
  335. return ['trade_id' => $info['id']];
  336. }
  337. }