BoxHandleService.php 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666
  1. <?php
  2. namespace app\common\service;
  3. use app\common\model\BoxHandleAllModel;
  4. use app\common\model\BoxHandleModel;
  5. use app\common\model\GreenScoreLogModel;
  6. use app\common\model\MoneyLogModel;
  7. use app\common\model\RecyclecardLogModel;
  8. use app\common\model\ShopGoods;
  9. use app\common\model\ShopOrder;
  10. use app\common\model\ShopOrderGoodsModel;
  11. use app\common\model\ShopOrderShippingModel;
  12. use app\common\model\UserAddressModel;
  13. use app\common\model\UserModel;
  14. use app\common\model\UserUnmoneyModel;
  15. use think\facade\Db;
  16. use utils\RedisCache;
  17. /**
  18. * 开出盒子服务 by wes
  19. * Class BoxHandleService
  20. * @package app\common\service
  21. */
  22. class BoxHandleService
  23. {
  24. protected static $instance = null;
  25. protected $model = null;
  26. public function __construct()
  27. {
  28. $this->model = new BoxHandleModel();
  29. }
  30. /**
  31. * 静态化入口
  32. * @return static|null
  33. */
  34. public static function make()
  35. {
  36. if (!self::$instance) {
  37. self::$instance = new static();
  38. }
  39. return self::$instance;
  40. }
  41. /**
  42. * 获取今日中奖福袋
  43. * @param $uid
  44. * @param array $params
  45. * @param int $pageSize
  46. * @param string $field
  47. * @return array|mixed
  48. * @throws \think\Exception
  49. * @throws \think\db\exception\DataNotFoundException
  50. * @throws \think\db\exception\DbException
  51. * @throws \think\db\exception\ModelNotFoundException
  52. */
  53. public function getBoxListByUser($uid, $params = [], $pageSize = 10, $field = '')
  54. {
  55. $type = isset($params['type']) ? $params['type'] : 0;
  56. $boxType = isset($params['box_type']) ? $params['box_type'] : 0;
  57. if (!in_array($type, [1, 2])) {
  58. sr_throw('订单类型参数错误');
  59. }
  60. if ($boxType && !in_array($boxType, [10, 20, 30, 40])) {
  61. sr_throw('盒子类型参数错误');
  62. }
  63. $where = ['r.uid' => $uid, 'r.box_type' => 0, 'r.status' => $type, 'r.is_delete' => 2];
  64. if ($boxType > 0) {
  65. $where['r.box_type'] = $boxType;
  66. } else {
  67. unset($where['r.box_type']);
  68. }
  69. // var_dump($where);
  70. $list = $this->model->alias('r')
  71. ->where($where)
  72. ->where(function ($query) use ($params) {
  73. $keyword = isset($params['keywords']) ? trim($params['keywords']) : '';
  74. if ($keyword) {
  75. $query->where('r.h_sn', 'like', "%{$keyword}%");
  76. }
  77. })
  78. ->where(function ($query) {
  79. $query->where('r.open_time', '<=', date('Y-m-d H:i:s'))->whereOr('r.open_time', 'null');
  80. })
  81. ->where(function ($query) use ($params) {
  82. $time = isset($params['time']) ? $params['time'] : '';
  83. $time1 = isset($params['time1']) ? $params['time1'] : '';
  84. if ($time) {
  85. $query->where('r.create_time', '>=', $time);
  86. } else if ($time1) {
  87. $query->where('r.create_time', '<', $time1);
  88. }
  89. })
  90. ->field('r.*')
  91. ->order('r.create_time desc,r.id desc')
  92. ->paginate($pageSize)
  93. ->each(function ($val, $k) {
  94. $goodsInfo = ShopGoodsService::make()->getCacheInfoById($val['goods_id'], 'goods_name,goods_sn,goods_img,price,spec_name');
  95. $val['goods_name'] = $goodsInfo['goods_name'];
  96. $val['goods_sn'] = $goodsInfo['goods_sn'];
  97. $val['goods_img'] = $goodsInfo['goods_img'];
  98. $val['price'] = $goodsInfo['price'];
  99. $val['time_text'] = $val['create_time'] ? date('H:i:s', strtotime($val['create_time'])) : '';
  100. $val['spec_name'] = $goodsInfo['spec_name'];
  101. if ($val['box_type'] == 10) {
  102. $val['is_canrecycle'] = 0;
  103. } else {
  104. $val['is_canrecycle'] = 1;
  105. }
  106. });
  107. $list = $list ? $list->toArray() : [];
  108. return isset($list['data']) ? $list['data'] : [];
  109. }
  110. /**
  111. * 获取历史中奖福袋
  112. * @param $uid
  113. * @param array $params
  114. * @param int $pageSize
  115. * @param string $field
  116. * @return array|mixed
  117. * @throws \think\Exception
  118. * @throws \think\db\exception\DataNotFoundException
  119. * @throws \think\db\exception\DbException
  120. * @throws \think\db\exception\ModelNotFoundException
  121. */
  122. public function getHistoryBoxList($uid, $params = [], $pageSize = 10, $field = '')
  123. {
  124. $type = isset($params['type']) ? $params['type'] : 0;
  125. $boxType = isset($params['box_type']) ? $params['box_type'] : 0;
  126. if (!in_array($type, [1, 2])) {
  127. sr_throw('订单类型参数错误');
  128. }
  129. if ($boxType && !in_array($boxType, [10, 20, 30, 40])) {
  130. sr_throw('盒子类型参数错误');
  131. }
  132. $where = ['r.uid' => $uid, 'r.box_type' => 0, 'r.status' => $type, 'r.is_delete' => 2];
  133. if ($boxType > 0) {
  134. $where['r.box_type'] = $boxType;
  135. } else {
  136. unset($where['r.box_type']);
  137. }
  138. $list = BoxHandleAllModel::alias('r')
  139. ->where($where)
  140. ->where(function ($query) use ($params) {
  141. $keyword = isset($params['keywords']) ? trim($params['keywords']) : '';
  142. if ($keyword) {
  143. $query->where('r.h_sn', 'like', "%{$keyword}%");
  144. }
  145. })
  146. ->where(function ($query) {
  147. $query->where('r.open_time', '<=', date('Y-m-d H:i:s'))->whereOr('r.open_time', 'null');
  148. })
  149. ->where('r.create_time', '<', date('Y-m-d'))
  150. ->field('r.*')
  151. ->order('r.create_time desc,r.id desc')
  152. ->paginate($pageSize)
  153. ->each(function ($val, $k) {
  154. $goodsInfo = ShopGoodsService::make()->getCacheInfoById($val['goods_id'], 'goods_name,goods_sn,goods_img,price,spec_name');
  155. $val['goods_name'] = $goodsInfo['goods_name'];
  156. $val['goods_sn'] = $goodsInfo['goods_sn'];
  157. $val['goods_img'] = $goodsInfo['goods_img'];
  158. $val['price'] = $goodsInfo['price'];
  159. $val['time_text'] = $val['create_time'] ? date('H:i:s', strtotime($val['create_time'])) : '';
  160. $val['spec_name'] = $goodsInfo['spec_name'];
  161. if ($val['box_type'] == 10) {
  162. $val['is_canrecycle'] = 0;
  163. } else {
  164. $val['is_canrecycle'] = 1;
  165. }
  166. });
  167. $list = $list ? $list->toArray() : [];
  168. return isset($list['data']) ? $list['data'] : [];
  169. }
  170. /**
  171. * 获取福袋统计
  172. * @param $uid
  173. * @param $status
  174. * @param int $dateType
  175. * @return mixed
  176. */
  177. public function getBoxCount($uid, $status, $dateType = 1)
  178. {
  179. $counts = ['10' => 0, '20' => 0, '30' => 0, '40' => 0];
  180. $model = new BoxHandleModel();
  181. // $model = $dateType == 1? new BoxHandleModel() : new BoxHandleAllModel();
  182. $datas = $model->where(['uid' => $uid, 'status' => $status, 'is_delete' => 2])
  183. ->where(function ($query) {
  184. $query->where('open_time', '<=', date('Y-m-d H:i:s'))->whereOr('open_time', 'null');
  185. })
  186. ->where(function ($query) use ($dateType) {
  187. if ($dateType == 1) {
  188. $query->where('create_time', '>=', date('Y-m-d'));
  189. } else {
  190. $query->where('create_time', '<', date('Y-m-d'));
  191. }
  192. })
  193. ->field(Db::raw('box_type,status,count(*) as total'))
  194. ->group('box_type')
  195. ->select();
  196. $datas = $datas ? $datas->toArray() : [];
  197. $datas = array_column($datas, 'total', 'box_type');
  198. if ($datas) {
  199. $counts = [
  200. '10' => isset($datas[10]) ? $datas[10] : 0,
  201. '20' => isset($datas[20]) ? $datas[20] : 0,
  202. '30' => isset($datas[30]) ? $datas[30] : 0,
  203. '40' => isset($datas[40]) ? $datas[40] : 0,
  204. ];
  205. }
  206. return $counts;
  207. }
  208. /**
  209. * 一键发货
  210. * @param $uid 用户ID
  211. * @param $params
  212. * @throws Exception
  213. * @throws \think\db\exception\DataNotFoundException
  214. * @throws \think\db\exception\DbException
  215. * @throws \think\db\exception\ModelNotFoundException
  216. */
  217. public function boxDelivery($uid, $params)
  218. {
  219. $handIds = $params['hand_ids'];
  220. $addressId = $params['address_id'];
  221. sr_throw('发货功能暂未开放');
  222. $catchType = isset($params['date_type']) ? $params['date_type'] : 0;
  223. $ids = explode(',', $handIds);
  224. if (empty($ids)) {
  225. sr_throw('请选择发货的商品');
  226. }
  227. if (!in_array($catchType, [1, 2])) {
  228. sr_throw('处理参数错误');
  229. }
  230. $model = new BoxHandleModel();
  231. // $model = $catchType==1? new BoxHandleModel() : new BoxHandleAllModel();
  232. $boxList = $model->whereIn('id', $ids)
  233. ->where(['uid' => $uid, 'status' => 1, 'is_delete' => 2])
  234. ->field('id,h_sn,status,uid,rid,handle_type,goods_id,box_type,goods_price')
  235. ->select();
  236. if (count($boxList) != count($ids)) {
  237. sr_throw('商品参数错误或已处理,请刷新后重试');
  238. }
  239. $addressInfo = UserAddressModel::getAddressIdDetails((int)$addressId, $uid);
  240. if (!$addressInfo) {
  241. sr_throw('地址错误');
  242. }
  243. $i = 0;
  244. $catchIds = [];
  245. $orders = $orderGoods = $orderShippings = [];
  246. $orderId = ShopOrder::max('order_id') + 1;
  247. $curTime = sr_getcurtime(time());
  248. $cacheKey = "caches:box:deliveryCatch:user_{$uid}:";
  249. foreach ($boxList as $key => $val) {
  250. $goodsInfo = ShopGoodsService::make()->getCacheInfoById($val['goods_id']);
  251. if (empty($goodsInfo)) {
  252. sr_throw('商品参数错误');
  253. }
  254. // 判断该福袋是否已经处理
  255. if ($model->where(['id' => $val['id'], 'status' => 2])->value('id')) {
  256. sr_throw('商品已处理');
  257. }
  258. // 验证是否有处理订单
  259. if (ShopOrder::where(['user_id' => $uid, 'order_source' => '福袋单号:' . $val['h_sn'], 'status' => 1])->value('order_id')) {
  260. sr_throw('存在已发货商品');
  261. }
  262. // 订单数据
  263. $orderId = $orderId + $i;
  264. $orders[] = [
  265. 'order_id' => $orderId,
  266. 'order_sn' => createdFDOrderSn(),
  267. 'payment' => 0,
  268. 'ship_postfee' => 0,
  269. 'user_id' => $uid,
  270. 'status' => 1,
  271. 'num' => 1,
  272. 'order_type' => 5,
  273. 'order_remark' => '福袋:' . $val['h_sn'],
  274. 'order_source' => '福袋单号:' . $val['h_sn'],
  275. 'supplier_name' => $goodsInfo['supplier_name'],
  276. 'total_price' => 0,
  277. 'created_time' => $curTime,
  278. 'rebate_score' => 0,
  279. ];
  280. $orderGoods[] = [
  281. 'order_id' => $orderId,
  282. 'goods_id' => $goodsInfo['goods_id'],
  283. 'goods_name' => $goodsInfo['goods_name'],
  284. 'goods_category' => $goodsInfo['category'],
  285. 'goods_img' => $goodsInfo['goods_img'],
  286. 'num' => 1,
  287. 'price' => $goodsInfo['price'],
  288. 'total_fee' => $goodsInfo['price'],
  289. 'spec_ids' => 1,
  290. 'spec_text' => $goodsInfo['spec_name'],
  291. 'uid' => $uid,
  292. 'rebate_score' => 0,
  293. 'total_rebate_score' => 0
  294. ];
  295. $orderShippings[] = [
  296. 'order_id' => $orderId,
  297. 'sp_name' => $addressInfo['name'],
  298. 'sp_mobile' => $addressInfo['mobile'],
  299. 'sp_province' => $addressInfo['sp_province'],
  300. 'sp_city' => $addressInfo['sp_city'],
  301. 'sp_county' => $addressInfo['sp_county'],
  302. 'sp_remark' => $addressInfo['remark'],
  303. 'sp_mergename' => $addressInfo['mergename'],
  304. 'created_time' => $curTime
  305. ];
  306. $catchIds[] = $val['id'];
  307. $i++;
  308. }
  309. // 验证处理数据
  310. if (empty($orders) || empty($catchIds) || empty($orderGoods) || empty($orderShippings)) {
  311. sr_throw('发货处理失败');
  312. }
  313. // 插入订单
  314. if (!ShopOrder::insertAll($orders)) {
  315. sr_throw('处理发货订单错误');
  316. }
  317. // 发货订单商品
  318. if (!ShopOrderGoodsModel::insertAll($orderGoods)) {
  319. sr_throw('处理发货订单商品数据错误');
  320. }
  321. // 发货订单信息
  322. if (!ShopOrderShippingModel::insertAll($orderShippings)) {
  323. sr_throw('处理发单数据错误');
  324. }
  325. // 今日数据处理/历史数据处理
  326. if (!$model->whereIn('id', $catchIds)->update(['status' => 2, 'handle_type' => 1, 'update_time' => $curTime])) {
  327. sr_throw('处理福袋数据失败');
  328. }
  329. return true;
  330. }
  331. /**
  332. * 一件回收 (新的处理回收到绿色积分)
  333. * @param $uid
  334. * @param $params
  335. * @return array
  336. * @throws \think\Exception
  337. * @throws \think\db\exception\DataNotFoundException
  338. * @throws \think\db\exception\DbException
  339. * @throws \think\db\exception\ModelNotFoundException
  340. */
  341. public function boxGoodsReBuy($uid, $params)
  342. {
  343. $handIds = $params['hand_ids'];
  344. $isRetrieve = SystemConfigService::make()->getConfigByName('fudai_is_retrieve', 1, 'fudai');
  345. $isRetrieve = $isRetrieve ? $isRetrieve : 0;
  346. if (!$isRetrieve) {
  347. sr_throw('回收功能已暂时关闭');
  348. }
  349. $catchType = isset($params['date_type']) ? $params['date_type'] : 0;
  350. $ids = explode(',', $handIds);
  351. if (empty($ids)) {
  352. sr_throw('请选择回收的商品');
  353. }
  354. if (!in_array($catchType, [1, 2])) {
  355. sr_throw('处理参数错误');
  356. }
  357. $num = count($ids);
  358. $model = new BoxHandleModel();
  359. // $model = $catchType==1? new BoxHandleModel() : new BoxHandleAllModel();
  360. $boxList = $model->whereIn('id', $ids)
  361. ->where(['uid' => $uid, 'status' => 1, 'is_delete' => 2])
  362. ->field('id,h_sn,status,uid,rid,handle_type,goods_id,box_type,goods_price')
  363. ->select();
  364. if (count($boxList) != count($ids)) {
  365. sr_throw('商品参数错误或已处理,请刷新后重试');
  366. }
  367. if ($model->where('box_type', 10)->where('id', 'in', $ids)->value('id')) {
  368. sr_throw('普通商品只能发货,不能回收');
  369. }
  370. if (GreenScoreLogModel::where(['ids' => $handIds])->value('id')) {
  371. sr_throw('商品已处理');
  372. }
  373. // 待处理的总金额
  374. $totalMoney = $model->where('id', 'in', $ids)->sum('goods_price');
  375. $serviceFee = $num * env('boxsetting.RECYCLE_ONBOX_PRICE');
  376. $endMoney = $totalMoney - $serviceFee;
  377. if ($endMoney < 0) {
  378. sr_throw('错误,请联系客服');
  379. }
  380. // 申请
  381. if ($params['action'] == 'apply') {
  382. return [
  383. 'total_count' => count($boxList),
  384. 'end_money' => $endMoney,
  385. 'total_money' => $totalMoney
  386. ];
  387. } // 回收
  388. else if ($params['action'] == 'surerecycle') {
  389. $userInfo = UserService::make()->getCacheInfo($uid, 'id,score,money,green_score,recycle_count,profit_money');
  390. $recycleCount = isset($userInfo['recycle_count']) ? $userInfo['recycle_count'] : 0;
  391. $userGreenScore = isset($userInfo['green_score']) ? $userInfo['green_score'] : 0;
  392. // 是否需要扣除回收卡
  393. $needRecycle = false;
  394. if ($needRecycle) {
  395. if ($recycleCount < $num) {
  396. sr_throw('回收卡不足' . $num . '个');
  397. }
  398. // 扣除回收卡
  399. if (!UserModel::where('id', $uid)->dec('recycle_count', $num)->update()) {
  400. sr_throw('回收卡扣除失败');
  401. }
  402. $data = [
  403. 'uid' => $uid,
  404. 'type' => 3,
  405. 'score' => $num,
  406. 'create_at' => sr_getcurtime(time()),
  407. 'state' => 2,
  408. 'before_score' => $recycleCount,
  409. 'after_score' => max(0, $recycleCount - $num),
  410. 'from_id' => 0,
  411. 'uid2' => $handIds
  412. ];
  413. if (!RecyclecardLogModel::insertGetId($data)) {
  414. sr_throw('回收卡扣除处理失败');
  415. }
  416. }
  417. // 扣除手续费后的钱全进绿色积分账户
  418. if (!UserModel::where('id', $uid)->inc('green_score', $endMoney)->update()) {
  419. sr_throw('回收商品结算错误');
  420. }
  421. $data = [
  422. 'uid' => $uid,
  423. 'type' => 6,
  424. 'score' => $endMoney,
  425. 'create_at' => sr_getcurtime(time()),
  426. 'state' => 1,
  427. 'before_score' => $userGreenScore,
  428. 'after_score' => floatval($userGreenScore + $endMoney),
  429. 'from_id' => 0,
  430. 'uid2' => 0,
  431. 'ids' => $handIds,
  432. 'remark' => "回收{$num}个福袋商品"
  433. ];
  434. if (!GreenScoreLogModel::insertGetId($data)) {
  435. sr_throw('回收商品结算处理失败');
  436. }
  437. // 改变处理状态
  438. if (!$model->whereIn('id', $ids)->update(['status' => 2, 'handle_type' => 2, 'update_time' => date('Y-m-d H:i:s')])) {
  439. sr_throw('更新回收处理状态失败');
  440. }
  441. $logData = ['ids' => $ids, 'totalMoney' => $totalMoney, 'fee' => $serviceFee, 'back' => $endMoney, 'data' => $data, 'date' => date('Y-m-d H:i:s')];
  442. RedisCache::set("caches:box:recycle:user_{$uid}:" . md5(json_encode($ids)), $logData, 7200);
  443. return [
  444. 'ids' => $ids,
  445. 'total_fee' => $endMoney,
  446. ];
  447. }
  448. }
  449. /**
  450. * 一件回收(原本回收到余额)
  451. * @param $uid
  452. * @param $params
  453. * @return array
  454. * @throws \think\Exception
  455. * @throws \think\db\exception\DataNotFoundException
  456. * @throws \think\db\exception\DbException
  457. * @throws \think\db\exception\ModelNotFoundException
  458. */
  459. public function boxGoodsReBuyBack($uid, $params)
  460. {
  461. $handIds = $params['hand_ids'];
  462. $isRetrieve = SystemConfigService::make()->getConfigByName('fudai_is_retrieve', 1, 'fudai');
  463. $isRetrieve = $isRetrieve ? $isRetrieve : 0;
  464. if (!$isRetrieve) {
  465. sr_throw('回收功能已暂时关闭');
  466. }
  467. $catchType = isset($params['date_type']) ? $params['date_type'] : 0;
  468. $ids = explode(',', $handIds);
  469. if (empty($ids)) {
  470. sr_throw('请选择回收的商品');
  471. }
  472. if (!in_array($catchType, [1, 2])) {
  473. sr_throw('处理参数错误');
  474. }
  475. $num = count($ids);
  476. $model = new BoxHandleModel();
  477. // $model = $catchType==1? new BoxHandleModel() : new BoxHandleAllModel();
  478. $boxList = $model->whereIn('id', $ids)
  479. ->where(['uid' => $uid, 'status' => 1, 'is_delete' => 2])
  480. ->field('id,h_sn,status,uid,rid,handle_type,goods_id,box_type,goods_price')
  481. ->select();
  482. if (count($boxList) != count($ids)) {
  483. sr_throw('商品参数错误或已处理,请刷新后重试');
  484. }
  485. if ($model->where('box_type', 10)->where('id', 'in', $ids)->value('id')) {
  486. sr_throw('普通商品只能发货,不能回收');
  487. }
  488. if (UserUnmoneyModel::where(['ids' => $handIds, 'status' => 1])->value('id')) {
  489. sr_throw('商品已处理');
  490. }
  491. // 待处理的总金额
  492. $totalMoney = $model->where('id', 'in', $ids)->sum('goods_price');
  493. $serviceFee = $num * env('boxsetting.RECYCLE_ONBOX_PRICE');
  494. $endMoney = $totalMoney - $serviceFee;
  495. if ($endMoney < 0) {
  496. sr_throw('错误,请联系客服');
  497. }
  498. // 申请
  499. if ($params['action'] == 'apply') {
  500. return [
  501. 'total_count' => count($boxList),
  502. 'end_money' => $endMoney,
  503. 'total_money' => $totalMoney
  504. ];
  505. } // 回收
  506. else if ($params['action'] == 'surerecycle') {
  507. $userInfo = UserService::make()->getCacheInfo($uid, 'id,score,money,recycle_count,profit_money');
  508. $recycleCount = isset($userInfo['recycle_count']) ? $userInfo['recycle_count'] : 0;
  509. $userProfitMoney = isset($userInfo['profit_money']) ? $userInfo['profit_money'] : 0;
  510. $userMoney = isset($userInfo['money']) ? $userInfo['money'] : 0;
  511. $userScore = isset($userInfo['score']) ? $userInfo['score'] : 0;
  512. // 回收卡扣除
  513. if ($recycleCount < $num) {
  514. sr_throw('回收卡不足' . $num . '个');
  515. }
  516. // 扣除回收卡
  517. if (!UserModel::where('id', $uid)->dec('recycle_count', $num)->update()) {
  518. sr_throw('回收卡扣除失败');
  519. }
  520. $data = [
  521. 'uid' => $uid,
  522. 'type' => 3,
  523. 'score' => $num,
  524. 'create_at' => sr_getcurtime(time()),
  525. 'state' => 2,
  526. 'before_score' => $recycleCount,
  527. 'after_score' => max(0, $recycleCount - $num),
  528. 'from_id' => 0,
  529. 'uid2' => $handIds
  530. ];
  531. if (!RecyclecardLogModel::insertGetId($data)) {
  532. sr_throw('回收卡扣除处理失败');
  533. }
  534. // 288的原价一个,累计回收到余额到钱
  535. $totalFee = $num * env('boxsetting.ONE_BOX_PRICE');
  536. // 扣除手续费后的钱除去原价为利润
  537. $profitMoney = max(0, $endMoney - $totalFee);
  538. if (!UserModel::where('id', $uid)->inc('money', $totalFee)->update()) {
  539. sr_throw('回收余额结算错误');
  540. }
  541. $data = [
  542. 'uid' => $uid,
  543. 'type' => 2,
  544. 'money' => $totalFee,
  545. 'create_at' => sr_getcurtime(time()),
  546. 'state' => 1,
  547. 'before_money' => $userMoney,
  548. 'after_money' => floatval($userMoney + $totalFee),
  549. 'from_id' => 0,
  550. 'uid2' => $handIds,
  551. 'free_type' => 0,
  552. 'remark' => "回收{$num}个福袋商品"
  553. ];
  554. if (!MoneyLogModel::insertGetId($data)) {
  555. sr_throw('回收商品金额处理失败');
  556. }
  557. // 利润
  558. if ($profitMoney > 0) {
  559. // 利润结算
  560. if (!UserModel::where('id', $uid)->inc('profit_money', $profitMoney)->update()) {
  561. sr_throw('回收商品利润结算错误');
  562. }
  563. // 添加用户利润记录
  564. $data = [
  565. 'uid' => $uid,
  566. 'money' => $profitMoney,
  567. 'status' => 2,
  568. 'type' => 1,
  569. 'ids' => $handIds,
  570. 'state' => 1,
  571. 'before_money' => $userProfitMoney,
  572. 'after_money' => floatval($userProfitMoney + $profitMoney),
  573. 'remark' => "回收{$num}个福袋结算,总金额:{$totalMoney}",
  574. 'create_time' => sr_getcurtime(time())
  575. ];
  576. if (!UserUnmoneyModel::insertGetId($data)) {
  577. sr_throw('商品回收利润处理错误');
  578. }
  579. // 更新团队利润数据
  580. }
  581. // 改变用户处理状态
  582. if (!$model->whereIn('id', $ids)->update(['status' => 2, 'handle_type' => 2, 'update_time' => date('Y-m-d H:i:s')])) {
  583. sr_throw('更新回收处理状态失败');
  584. }
  585. $logData = ['ids' => $ids, 'totalMoney' => $totalMoney, 'fee' => $serviceFee, 'profit' => $profitMoney, 'back' => $totalFee, 'data' => $data, 'date' => date('Y-m-d H:i:s')];
  586. RedisCache::set("caches:box:recycle:user_{$uid}:" . md5(json_encode($ids)), $logData, 7200);
  587. return [
  588. 'ids' => $ids,
  589. 'total_fee' => $totalFee,
  590. 'profit_money' => $profitMoney
  591. ];
  592. }
  593. }
  594. }