Goods.php 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755
  1. <?php
  2. namespace app\api\controller\v1;
  3. use app\api\controller\ApiController;
  4. use app\common\validate\IDMustBePositiveInt;
  5. use Lettered\Support\Exceptions\EvidentException;
  6. use app\http\IResponse;
  7. use EasyWeChat\Factory;
  8. use think\App;
  9. use think\Db;
  10. class Goods extends ApiController
  11. {
  12. /**
  13. * 获取用户红包信息
  14. *
  15. * @author 许祖兴 < zuxing.xu@lettered.cn>
  16. * @date 2020/9/16 13:44
  17. *
  18. * @return \think\response\Json
  19. * @throws \think\db\exception\DataNotFoundException
  20. * @throws \think\db\exception\ModelNotFoundException
  21. * @throws \think\exception\DbException
  22. */
  23. public function getUnrebate()
  24. {
  25. // 红包
  26. $rebate = model('common/GoodsOrder')
  27. ->where([
  28. 'is_pin' => 1,
  29. 'pin_rebate_rec' => 0,
  30. 'status' => 6
  31. ])
  32. ->where(['user_id' => $this->auth->user()['id']])
  33. ->where('pin_rebate_expired','<>', 0)
  34. // ->orderRaw('rand()')
  35. ->field('id,is_pin,pin_rebate,pin_rebate_expired,pin_rebate_rec')
  36. ->select();
  37. return $this->ApiJson(0, '红包信息', $rebate);
  38. }
  39. /**
  40. * 首页分类
  41. *
  42. * @author 许祖兴 < zuxing.xu@lettered.cn>
  43. * @date 2020/9/15 17:44
  44. *
  45. * @return \think\response\Json
  46. * @throws \think\db\exception\DataNotFoundException
  47. * @throws \think\db\exception\ModelNotFoundException
  48. * @throws \think\exception\DbException
  49. */
  50. public function getCategory()
  51. {
  52. // 拼团产品
  53. $collages = model('common/GoodsCategory')
  54. // ->orderRaw('rand()')
  55. ->field('id,name')
  56. ->select();
  57. return $this->ApiJson(0,'获取产品分类成功', $collages);
  58. }
  59. /**
  60. * 首页搜索
  61. *
  62. * @author 许祖兴 < zuxing.xu@lettered.cn>
  63. * @date 2020/7/6 17:44
  64. *
  65. * @return \think\response\Json
  66. * @throws \think\db\exception\DataNotFoundException
  67. * @throws \think\db\exception\ModelNotFoundException
  68. * @throws \think\exception\DbException
  69. */
  70. public function getGoodsBySearch()
  71. {
  72. $param = $this->request->param();
  73. $limit = 10;
  74. // 数据校验
  75. $valid = $this->validate($param, [
  76. 'page' => 'require',
  77. ]);
  78. // 错误
  79. if (true !== $valid){
  80. return $this->ApiJson(-1,$valid);
  81. }
  82. $where = [];
  83. //组合搜索
  84. !empty($param['keywords']) && $where[]
  85. = ['name|content', 'like', '%' . $param['keywords'] . '%'];
  86. // 拼团产品
  87. $collages = model('Goods')
  88. ->where($where)
  89. ->where('stock','>=',5)
  90. ->limit((($param['page'] - 1) * $limit) . "," . $limit)
  91. ->select();
  92. return $this->ApiJson(0,'获取产品成功', $collages);
  93. }
  94. /**
  95. * 首页拼团产品
  96. *
  97. * @url /goods/collage
  98. *
  99. * @return \think\response\Json
  100. * @throws \think\exception\DbException
  101. */
  102. public function getCollageByHome()
  103. {
  104. $param = $this->request->param();
  105. $where = [];
  106. $limit = 10;
  107. // 数据校验
  108. $valid = $this->validate($param, [
  109. 'page' => 'require',
  110. ]);
  111. // 错误
  112. if (true !== $valid){
  113. return $this->ApiJson(-1,$valid);
  114. }
  115. !empty($param['cateId']) && $where[]
  116. = ['category_id', 'eq', $param['cateId']];
  117. // 拼团产品
  118. $collages = model('Goods')
  119. ->where($where)
  120. ->where([ 'is_pin' => 1, 'status' => 1])
  121. // ->orderRaw('rand()')
  122. ->limit((($param['page'] - 1) * $limit) . "," . $limit)
  123. ->select();
  124. return $this->ApiJson(0,'获取产品成功', $collages);
  125. }
  126. /**
  127. * 获取商户的产品列表
  128. *
  129. * @url /goods/by_seller/:id
  130. * @param integer $id 商户ID
  131. *
  132. * @return \think\response\Json
  133. * @throws \Lettered\Support\Exceptions\EvidentException
  134. * @throws \think\db\exception\DataNotFoundException
  135. * @throws \think\db\exception\ModelNotFoundException
  136. * @throws \think\exception\DbException
  137. */
  138. public function getGoodsBySellerId($id)
  139. {
  140. (new IDMustBePositiveInt())->valid();
  141. // 分页
  142. $page = $this->request->param('page', 1);
  143. $goods = model('Goods')
  144. ->where(['seller_id' => $id])
  145. ->limit((($page - 1) * 10) . "," . 10)
  146. // ->order(['xx' => 'xx'])
  147. ->select();
  148. if($goods->isEmpty()){
  149. //
  150. return $this->ApiJson(0,$page > 1 ? '无更多产品' :'此商户暂无商品!');
  151. }
  152. return $this->ApiJson(0, '', $goods);
  153. }
  154. /**
  155. * 获取产品详情
  156. *
  157. * @url /goods/:id
  158. * @author 许祖兴 < zuxing.xu@lettered.cn>
  159. * @date 2020/7/3 15:07
  160. *
  161. * @param $id
  162. * @return \think\response\Json
  163. * @throws \Lettered\Support\Exceptions\EvidentException
  164. * @throws \think\db\exception\DataNotFoundException
  165. * @throws \think\db\exception\ModelNotFoundException
  166. * @throws \think\exception\DbException
  167. */
  168. public function getGoodsByID($id)
  169. {
  170. (new IDMustBePositiveInt())->valid();
  171. $goods = model('common/Goods')
  172. ->with(['seller'])
  173. ->find($id);
  174. if (!$goods){
  175. return $this->ApiJson(-1, '不存在商品编号');
  176. }
  177. return $this->ApiJson(0, '获取商品信息成功', $goods);
  178. }
  179. /**
  180. * 取得商品规格
  181. *
  182. * @author 许祖兴 < zuxing.xu@lettered.cn>
  183. * @date 2020/6/29 11:45
  184. *
  185. * @param $id
  186. * @return \think\response\Json
  187. * @throws \think\db\exception\DataNotFoundException
  188. * @throws \think\db\exception\ModelNotFoundException
  189. * @throws \think\exception\DbException
  190. */
  191. public function getGoodsSkuByID($id)
  192. {
  193. // 获取规格
  194. $skus = model('common/GoodsSku')
  195. ->where(['goods_id' => $id])
  196. ->select();
  197. $spec = [];
  198. $values = [];
  199. $ret = [];
  200. // 取出这个产品的SPEC SPEC_VALUE
  201. foreach ($skus as $sku) {
  202. $ret['sku'][] = [
  203. 'ids' => dejson($sku['spec_param']),
  204. 'price' => $sku['price'],
  205. 'pin_price' => $sku['pin_price'],
  206. 'stock' => $sku['stock'],
  207. ];
  208. foreach (dejson($sku['spec_param']) as $sp) {
  209. foreach ($sp as $sid => $vid) {
  210. $spec[] = model('GoodsSpec')->where(['id' => $sid])->find();
  211. $values[] = model('GoodsSpecValue')->where(['spec_id' => $sid, 'id' => $vid])->find();
  212. }
  213. }
  214. }
  215. // 构造
  216. foreach (array_unique($spec) as $s => $k) {
  217. $sub = [];
  218. foreach (array_unique($values) as $i => $v) {
  219. if ($v['spec_id'] == $k['id']) {
  220. $sub[] = [
  221. 'id' => $v['id'],
  222. 'name' => $v['value_name']
  223. ];
  224. }
  225. }
  226. $ret['spec'][] = [
  227. 'id' => $k['id'],
  228. 'name' => $k['spec_name'],
  229. 'sub' => $sub
  230. ];
  231. }
  232. return $this->ApiJson(0, '获取商品信息成功', $ret);
  233. }
  234. /**
  235. * 获取商品正在进行拼单
  236. *
  237. * @author 许祖兴 < zuxing.xu@lettered.cn>
  238. * @date 2020/7/8 15:30
  239. *
  240. * @param $id
  241. * @return \think\response\Json
  242. * @throws \Lettered\Support\Exceptions\EvidentException
  243. * @throws \think\db\exception\DataNotFoundException
  244. * @throws \think\db\exception\ModelNotFoundException
  245. * @throws \think\exception\DbException
  246. */
  247. public function getGroupOrderByID($id)
  248. {
  249. (new IDMustBePositiveInt())->valid();
  250. $orders = model('common/GoodsOrderGroup')->with(['user'])
  251. ->where(['goods_id' => $id,'status' => 0])
  252. ->whereTime('expired_at', '>=', time())
  253. ->select();
  254. return $this->ApiJson(0, '获取商品信息成功', $orders);
  255. }
  256. /**
  257. * 下订单
  258. *
  259. * @author 许祖兴 < zuxing.xu@lettered.cn>
  260. * @date 2020/7/3 10:49
  261. *
  262. * @return \think\response\Json
  263. */
  264. public function placeOrder()
  265. {
  266. $param = $this->request->param();
  267. // 数据校验
  268. $valid = $this->validate($param, [
  269. 'addr|送货地址' => 'require',
  270. 'total_price|订单总价' => 'require',
  271. 'goods|产品列表' => 'require',
  272. 'area_id|区域信息' => 'require',
  273. ]);
  274. // 错误
  275. if (true !== $valid){
  276. return $this->ApiJson(-1,$valid);
  277. }
  278. // 地址解析
  279. $addr = dejson($param['addr']);
  280. // 事务处理
  281. Db::startTrans();
  282. try {
  283. $all_total_price = 0; // 总需支付
  284. $order_idx = ""; // 相关订单
  285. // 循环商家产品(商家单订单多产品)
  286. foreach (dejson($param['goods']) as $seller){
  287. // 创建商家订单
  288. $order = model('common/GoodsOrder')::create(
  289. [
  290. 'seller_id' => $seller['id'],
  291. 'addr_id' => $addr['id'],
  292. 'user_id' => $this->auth->user()['id'],
  293. // 订单号
  294. 'order_no' => get_order_no(),
  295. // 订单总价后续创建完更新
  296. ], true
  297. );
  298. $total_price = 0; // 订单总价
  299. $is_pin = 0; // 是否拼团
  300. $joinId = 0; // 拼单ID
  301. foreach ($seller['goods'] as $good) {
  302. // 单商品总价 数量* 单价
  303. $good['total_price'] = sprintf("%.2f", round($good['count'] * $good['price'], 2));
  304. // 1. 查询某个产品库存
  305. $sku = model('common/GoodsSku')->getBy(['goods_id' => $good['id'],'param' => enjson($good['spec'])]);
  306. // 2. 库存充足 创建 / 否则回滚
  307. if ($sku['stock'] <= 20)
  308. throw new EvidentException([
  309. 'errmsg' => '当前商品库存不足!'
  310. ]);
  311. // 创建商家订单产品
  312. model('common/GoodsOrderDetail')::create(
  313. [
  314. 'order_id' => $order['id'],
  315. 'goods_id' => $good['id'],
  316. 'title' => $good['name'] . " " . arr2str($good['spec'], " "),
  317. 'spec' => arr2str($good['spec'], ","),
  318. 'count' => $good['count'],
  319. 'price' => $good['price'],
  320. 'total_price' => $good['total_price']
  321. ], true
  322. );
  323. $total_price += $good['total_price'];
  324. // 3. 总库/单库存处理
  325. // todo 加不加锁?
  326. model('common/GoodsSku')->update([
  327. 'stock' => $sku['stock'] - $good['count'] // 减库存
  328. ], ['goods_id' => $good['id'], 'param' => enjson($good['spec'])
  329. ]);
  330. // 更新
  331. $goods = model('common/Goods')->findBy($good['id']); // 不知道这里还要不要
  332. $goods->updateBy($goods['id'], [
  333. 'stock' => $goods['stock'] - $good['count'], // 减库存
  334. 'sell_count' => $goods['sell_count'] + $good['count'] // 加销量
  335. ]);
  336. // 拼团
  337. if (isset($good['is_pin']) || isset($good['is_join'])) {
  338. // 参团先给group_id
  339. isset($good['is_join']) && $joinId = $good['joinId'];
  340. $is_pin = 1;
  341. }
  342. }
  343. // 总价
  344. $all_total_price += $total_price;
  345. $order_idx .= "," . $order['id'];
  346. // 更新订单总价/ 所属拼单iD
  347. model('common/GoodsOrder')->updateBy($order['id'], [
  348. 'group_id' => $joinId,
  349. 'area_id' => $param['area_id'],
  350. 'total_price' => $total_price,
  351. 'is_pin' => $is_pin
  352. ]);
  353. }
  354. // 创建对应支付记录
  355. $trade_no = get_order_no();
  356. model('common/OrderPaylog')->storeBy([
  357. 'out_trade_no' => $trade_no,
  358. 'total_price' => $all_total_price,
  359. 'order_idx' => ltrim($order_idx, ","), // 去逗号
  360. 'ascription' => 'goods' // 归属订单
  361. ]);
  362. Db::commit();
  363. // push_socket_data('goods',[
  364. // 'id' => $trade_no,
  365. // 'msg' => '有新的商品订单等待处理,点击前往!'
  366. // ]);
  367. return $this->ApiJson(0, '下单成功',$trade_no);
  368. }catch (\Exception $e){
  369. Db::rollback();
  370. p($e->getMessage());
  371. p($e->getTraceAsString(), 1);
  372. return $this->ApiJson(-1, '数据异常,请稍后重试' . $e->getMessage());
  373. }
  374. }
  375. /**
  376. * 获取用户订单列表
  377. *
  378. * @author 许祖兴 < zuxing.xu@lettered.cn>
  379. * @date 2020/7/6 9:43
  380. *
  381. * @param $action
  382. * @return \think\response\Json
  383. * @throws \Lettered\Support\Exceptions\FailedException
  384. * @throws \think\db\exception\DataNotFoundException
  385. * @throws \think\db\exception\ModelNotFoundException
  386. * @throws \think\exception\DbException
  387. */
  388. public function getUserOrder($action)
  389. {
  390. $param = $this->request->param();
  391. $limit = 10;
  392. // 数据校验
  393. $valid = $this->validate($param, [
  394. 'page' => 'require',
  395. ]);
  396. // 错误
  397. if (true !== $valid) {
  398. return $this->ApiJson(-1, $valid);
  399. }
  400. //
  401. $orders = model('common/GoodsOrder')->with(['seller','detail'])
  402. ->where(['user_id' => $this->auth->user()['id']]);
  403. // 条件
  404. switch ($action) {
  405. case 'unpay':
  406. $orders->where(['status' => 1]);
  407. break;
  408. case 'ship':
  409. $orders->where(['status' => 2]);
  410. break;
  411. case 'wait':
  412. $orders->where(['status' => 3]);
  413. break;
  414. case 'check':
  415. $orders->where(['status' => 4]);
  416. break;
  417. }
  418. return $this->ApiJson(0, '获取成功',
  419. $orders->limit((($param['page'] - 1) * $limit) . "," . $limit)
  420. ->order(['created_at' => 'desc'])
  421. ->select());
  422. }
  423. /**
  424. * 获取订单详情
  425. *
  426. * @author 许祖兴 < zuxing.xu@lettered.cn>
  427. * @date 2020/7/6 10:08
  428. *
  429. * @param $id
  430. * @return \think\response\Json
  431. * @throws \Lettered\Support\Exceptions\EvidentException
  432. * @throws \think\db\exception\DataNotFoundException
  433. * @throws \think\db\exception\ModelNotFoundException
  434. * @throws \think\exception\DbException
  435. */
  436. public function getOrderByID($id)
  437. {
  438. (new IDMustBePositiveInt())->valid();
  439. $order = model('common/GoodsOrder')
  440. ->with(['addr'])
  441. ->find($id);
  442. if (!$order){
  443. return $this->ApiJson(-1, '不存在订单编号');
  444. }
  445. // 查询订单产品
  446. $order['detail'] = model('common/GoodsOrderDetail')
  447. ->with(['goods'])
  448. ->where(['order_id' => $order['id']])
  449. ->select();
  450. // 查询订单产品
  451. $order['service'] = model('common/GoodsOrderService')
  452. ->where(['order_id' => $order['id']])
  453. ->find();
  454. return $this->ApiJson(0, '获取订单信息成功', $order);
  455. }
  456. /**
  457. * 申请订单售后
  458. *
  459. * @author 许祖兴 < zuxing.xu@lettered.cn>
  460. * @date 2020/9/21 10:08
  461. *
  462. * @param $id
  463. * @return \think\response\Json
  464. * @throws \Lettered\Support\Exceptions\EvidentException
  465. * @throws \think\db\exception\DataNotFoundException
  466. * @throws \think\db\exception\ModelNotFoundException
  467. * @throws \think\exception\DbException
  468. */
  469. public function applyOrderService($id)
  470. {
  471. // 获取产品信息
  472. (new IDMustBePositiveInt())->valid();
  473. $order = model('common/GoodsOrder')
  474. ->find($id);
  475. if(!$order)
  476. return $this->ApiJson( -1, '订单数据不存在');
  477. // 查找售后状态
  478. $service = model('common/GoodsOrderService')->getBy(['order_id' => $order['id']]);
  479. // 提交申请
  480. if($this->request->isPost()){
  481. // 接收参数
  482. $params = $this->request->param();
  483. // 参数校验
  484. $valid = $this->validate($params, [
  485. 'action|售后类型' => 'require',
  486. 'remarks|备注说明' => 'require',
  487. 'attach|附件照片' => 'require'
  488. ]);
  489. // 错误返回
  490. if(true !== $valid){
  491. return $this->ApiJson(-1, $valid);
  492. }
  493. if (!$service) {
  494. unset($params['id']);
  495. $params['order_id'] = $order['id'];
  496. $params['seller_id'] = $order['seller_id'];
  497. // 写入数据
  498. $ret = model('common/GoodsOrderService')::create($params, true);
  499. }else {
  500. $params['status'] = 1;
  501. // 更新数据
  502. unset($params['id']);
  503. // $ret = model('common/GoodsOrderService')->update($params,['id' => $service['id']]);
  504. $ret = model('common/GoodsOrderService')->updateBy($service['id'],$params);
  505. }
  506. // 消息
  507. if ($ret){
  508. // 订单售后状态
  509. model('common/GoodsOrder')->updateBy($order['id'],[
  510. 'in_service' => 1
  511. ]);
  512. // 发送订阅消息
  513. return $this->ApiJson(0,'提交成功,请等待商家审核!');
  514. }
  515. return $this->ApiJson(-1,'数据异常,请稍后重试!');
  516. }
  517. // 查询时间 多少天内在售后期 读后台配置 sys_config('order_service_expired_at','store')
  518. return $this->ApiJson(0, '获取信息成功', ['order' => $order,
  519. 'service' => $service]);
  520. }
  521. /**
  522. *
  523. * @author 许祖兴 < zuxing.xu@lettered.cn>
  524. * @date 2020/7/15 9:56
  525. *
  526. * @param $id
  527. * @return \think\response\Json
  528. * @throws \Lettered\Support\Exceptions\EvidentException
  529. * @throws \think\Exception
  530. * @throws \think\exception\PDOException
  531. */
  532. public function cancelOrderByID($id)
  533. {
  534. (new IDMustBePositiveInt())->valid();
  535. Db::startTrans();
  536. try {
  537. // 查订单
  538. $order = model('common/GoodsOrder')->where(['id' => $id])->lock(true)->find();
  539. if ($order['status'] == 0){
  540. return $this->ApiJson(-1, "当前订单已关闭");
  541. }
  542. // 取消状态应该是 未支付,待发货
  543. $data = [ 'status' => 0 ];
  544. // 待发货取消需要退钱
  545. if ($order['status'] == 2){
  546. model('common/Users')->changeBalance(
  547. $order['user_id'],
  548. $order['pay_price'] ,
  549. "取消订单,退还支付金额【" . $order['pay_price'] .'】',
  550. true);
  551. }
  552. // 获取订单产品
  553. $order_detail = model('common/GoodsOrderDetail')->getAll(['order_id' => $order['id']]);
  554. foreach ($order_detail as $goods){
  555. // 单产品SKU库存回增
  556. model('common/GoodsSku')
  557. ->where(['param' => enjson(str2arr($goods['spec']))])
  558. ->setInc('stock', $goods['count']);
  559. // 产品总库存更新
  560. model('common/Goods')
  561. ->where(['id' => $goods['goods_id']])
  562. ->setInc('stock', $goods['count']);
  563. // 销量减
  564. model('common/Goods')
  565. ->where(['id' => $goods['goods_id']])
  566. ->setDec('sell_count', $goods['count']);
  567. }
  568. // 更新订单
  569. $order->updateBy($order['id'], $data);
  570. Db::commit();
  571. return $this->ApiJson(0, "订单取消成功");
  572. }catch (\Exception $e){
  573. Db::rollback();
  574. return $this->ApiJson(-1, "系统异常,请稍后再试");
  575. }
  576. }
  577. /**
  578. * 确认订单
  579. *
  580. * @author 许祖兴 < zuxing.xu@lettered.cn>
  581. * @date 2020/7/25 17:35
  582. *
  583. * @param $id
  584. * @return \think\response\Json
  585. * @throws \Lettered\Support\Exceptions\EvidentException
  586. * @throws \think\db\exception\DataNotFoundException
  587. * @throws \think\db\exception\ModelNotFoundException
  588. * @throws \think\exception\DbException
  589. */
  590. public function confirmOrderByID($id)
  591. {
  592. (new IDMustBePositiveInt())->valid();
  593. // 查订单
  594. $order = model('common/GoodsOrder')->where(['id' => $id])->find();
  595. if ($order['status'] == 0){
  596. return $this->ApiJson(-1, "当前订单已关闭");
  597. }
  598. // 查商品信息 取得商品进货价
  599. // 计算利润
  600. // 利润的百分几
  601. // 利润 count * ( total_price - instock )
  602. // 总利润
  603. $cgo_total = 0;
  604. // 子单明细
  605. $its = model('common/GoodsOrderDetail')->where(['order_id' => $order['id']])->select();
  606. foreach ($its as $itsv) {
  607. // 产品
  608. $goin = model('common/GoodsSku')->where(['goods_id' => $itsv['goods_id'],'param' => enjson(str2arr($itsv['spec'],','))])->value('instock_price');
  609. $cgo_total += sprintf("%.2f", round($itsv['total_price'] - $goin, 2));
  610. }
  611. // 奖励期权条件 -- 资产消费不得期权
  612. if ($order && $order['is_pin'] !== 1 && $order['pay_type'] !== 'property'){
  613. // 用户期权
  614. // order_property_reward
  615. $order_property_reward = sys_config('order_property_reward','store');
  616. // 计算 交易金额$order['total_price'] * order_property_reward //2020 12 09改为利润$cgo_total
  617. $reward = sprintf("%.3f", round(((float)$order_property_reward / 100) * $cgo_total, 3));
  618. app()->log(enjson([$order_property_reward,$reward]),'debug');
  619. if($reward > 0){
  620. // 更新
  621. model('common/Users')->changeProperty(
  622. $order['user_id'],
  623. $reward,
  624. '订单完成,奖励资产【' . $reward . "】",
  625. true
  626. );
  627. }
  628. }
  629. // 查找上级
  630. $parent = model('common/UsersInviteRelation')->getBy(['invite_id' => $order['user_id']]);
  631. // 上级推荐奖励
  632. if ($parent['form_id'] !== 0) {
  633. // 取得收益百分几
  634. $user_spread_reward_scale = sys_config('user_spread_reward_scale','user');
  635. // 计算收益
  636. $reward = sprintf("%.2f", round(((float)$user_spread_reward_scale / 100) * $cgo_total, 2));
  637. model('common/Users')->changeBalance(
  638. $parent['form_id'],
  639. $reward,
  640. "下级消费奖励,奖金【" . $reward .'】',
  641. true
  642. );
  643. }
  644. // 更新确认订单
  645. $order->updateBy($order['id'], [
  646. 'received_at' => time(),
  647. 'status' => 4 // 结束订单
  648. ]);
  649. return $this->ApiJson(0, "订单已确认");
  650. }
  651. /**
  652. * 拼团返现领取
  653. *
  654. * @author 许祖兴 < zuxing.xu@lettered.cn>
  655. * @date 2020/7/14 15:18
  656. *
  657. * @return \think\response\Json
  658. * @throws \Lettered\Support\Exceptions\EvidentException
  659. * @throws \think\exception\PDOException
  660. */
  661. public function getRebateByID()
  662. {
  663. (new IDMustBePositiveInt())->valid();
  664. try {
  665. // 查订单
  666. $order = model('common/GoodsOrder')->getBy(['id' => input('id'),'is_pin' => 1]);
  667. if($order){
  668. // 是否领取了
  669. if ($order['pin_rebate_rec']) {
  670. return $this->ApiJson(-1, '本次返现已经领取,请勿重复领取');
  671. }
  672. // 写入记录
  673. model('common/Users')->changeBalance(
  674. $order['user_id'],
  675. $order['pin_rebate'],
  676. "参团失败,领取红包奖励金额【" . $order['pin_rebate'] . '】',
  677. true);
  678. // 更新订单
  679. $order->updateBy($order['id'], [
  680. 'pin_rebate_rec' => 1,
  681. 'status' => 4 // 完成订单
  682. ]);
  683. return $this->ApiJson(0, '领取红包成功!');
  684. }
  685. return $this->ApiJson(-1, '订单数据不存在!');
  686. }catch (\Exception $e){
  687. return $this->ApiJson(-1, '数据异常,请稍后再试!');
  688. }
  689. }
  690. }