| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234 |
- <?php
- namespace app\api\controller;
- use app\common\model\LedgerTokenChangeModel;
- use app\common\controller\Api;
- use app\common\model\AnnouncementModel;
- use app\common\model\UserCollect;
- use app\common\model\ProductBuying;
- use app\common\model\LedgerWalletModel;
- use app\common\model\ProductTransfer;
- use app\api\logic\MarketLogic;
- use app\common\model\ProductLists;
- use app\common\model\ProductOrder;
- use app\common\model\UserBuying;
- use fast\Asset;
- use think\Db;
- use Exception;
- class Market extends Api
- {
-
- //用户收藏
- public function collect(UserCollect $userCollect)
- {
- $params = $this->request->post();
- $validate = \think\Loader::validate('Market');
- if(!$validate->scene('collect')->check($params)) $this->error($validate->getError());
- $userCollect::setUserCollect($this->auth->id, $params['market_id']);
- $this->success("ok");
- }
- /*
- * 相关公告
- */
- public function announcement(AnnouncementModel $announcementModel)
- {
- $params = $this->request->post();
- $validate = \think\Loader::validate('Market');
- if(!$validate->scene('announcement')->check($params)) $this->error($validate->getError());
- $announcement = $announcementModel
- ->where('find_in_set(:id,product_id)',['id'=>$params['product_id']])
- ->where('status', $announcementModel::Normal)
- ->where('to_lang', $this->request->getLan())
- ->order('id desc')
- ->paginate($this->pageSize);
- $this->success('ok', $announcement);
- }
- //获取产品最大求购价格
- public function getBuyingMaxPrice(ProductBuying $productBuying)
- {
- $params = $this->request->post();
- $validate = \think\Loader::validate('Market');
- if(!$validate->scene('announcement')->check($params)) $this->error($validate->getError());
- $this->success('ok', $productBuying::getProductBuyingMaxPrice($params['product_id']));
- }
-
- //锁定寄售
- public function setTransferLock(ProductTransfer $productTransfer, MarketLogic $marketLogic)
- {
- $params = $this->request->post();
- $validate = \think\Loader::validate('Market');
- if(!$validate->scene('transferlock')->check($params)) $this->error($validate->getError());
- Db::startTrans();
- try {
- $marketLogic::setTransferLock($productTransfer, config('market_transfer.lock_time'), $this->auth->id, $params['transfer_id']);
- Db::commit();
- } catch (Exception $e) {
- // 回滚事务
- Db::rollback();
- $this->error($e->getMessage());
- }
- $this->success('ok');
- }
- //求购列表
- public function buyingList(ProductBuying $productBuying)
- {
- $params = $this->request->post();
- $validate = \think\Loader::validate('Market');
- if(!$validate->scene('announcement')->check($params)) $this->error($validate->getError());
- $this->success('ok', $productBuying::getBuyingList($params['product_id']));
- }
- /**
- * 发起求购
- * @return void
- */
- public function setBuying(LedgerWalletModel $ledgerWalletModel, ProductBuying $productBuying)
- {
- $params = $this->request->post();
- $validate = \think\Loader::validate('Market');
- if(!$validate->scene('buying')->check($params)) $this->error($validate->getError());
- if($productBuying::getProductBuyingCount($this->auth->id, $params['product_id']) > config('market_transfer.max_buying_count')) $this->error('您的求购次数已达上限');
-
- $order_info = ProductLists::get($params['product_id']);
- if(!$order_info) $this->error('参数有误');
- if($params['min_price'] < $order_info->min_buying_token || $params['min_price'] > $order_info->max_buying_token) $this->error(__("当前订单求购茶宝为").$order_info->min_buying_token.'~'.$order_info->max_buying_token);
- $chabao = $ledgerWalletModel::getWalletChaBao($this->auth->id);
- $total = bcmul($params['min_price'], $params['stock'], 6); // 所需茶宝
-
- if($chabao < $total) $this->error('您的茶币不足');
- // 启动事务
- Db::startTrans();
- try {
- // 记录订单
- $productBuying::setCreateBuying($this->auth->id, $params['product_id'], $params['stock'], $params['min_price'], $total);
- // 扣除茶币
- $ledgerWalletModel->changeWalletAccount($this->auth->id, Asset::TOKEN, -$total, LedgerTokenChangeModel::Buying, $this->auth->id);
- $ledgerWalletModel->changeWalletOnly($this->auth->id, Asset::BUYING, $total); //冻结金额
- // 提交事务
- Db::commit();
- } catch (Exception $e) {
- // 回滚事务
- Db::rollback();
- return $this->error($e->getMessage());
- }
- $this->success('ok');
- }
- /**
- * 求购详情
- * @return void
- */
- public function getBuyingDetail( ProductBuying $productBuying)
- {
- $params = $this->request->post();
- $validate = \think\Loader::validate('Market');
- if(!$validate->scene('buying_info')->check($params)) $this->error($validate->getError());
- $resq = $productBuying::getProductBuyingDetail($params['buying_id'], $this->request->getLan());
- $this->success('ok', $resq);
- }
- //出售求购
- public function sellBuying(LedgerWalletModel $ledgerWalletModel, ProductBuying $productBuying, UserBuying $userBuying, ProductOrder $productOrder, LedgerTokenChangeModel $ledgerTokenChangeModel)
- {
- $params = $this->request->post();
- $validate = \think\Loader::validate('Market');
- if(!$validate->scene('sellbuying')->check($params)) $this->error($validate->getError());
- $buying = $productBuying::get($params['buying_id']);
- if(empty($buying)) $this->error('订单不存在');
- if($buying->user_id == $this->auth->id) $this->error('无权操作');
- if($buying->status != ProductBuying::Normal) $this->error('订单已完成');
-
- // 启动事务
- Db::startTrans();
- try {
- // 记录出售订单
- list($chabao, $fee)=$userBuying::getCreateUserBuying($this->auth->id, $params['buying_id'], $params['order_id'], $buying->user_id, $buying->min_price);
- // 添加扣除茶币
- $newChabao = $ledgerWalletModel->changeWalletAccount($this->auth->id, Asset::TOKEN, $chabao, $ledgerTokenChangeModel::BuySellg, $buying->user_id);
- // 扣除冻结金额
- $ledgerWalletModel->changeWalletOnly($buying->user_id, Asset::BUYING, -$buying->min_price); //解冻金额
- //添加订单
- $productOrder::setCreateOrder($params['order_id'], ['product_id' => $buying->product_id, 'area_id' => 0,'price' =>$buying->min_price], ProductOrder::Buying, $buying->user_id, $this->auth->id, getOrderSN('R'), 0, $buying->min_price);
- //记录转让手续费
- $ledgerTokenChangeModel::createChangeFees($this->auth->id, $buying->user_id, -$fee, $newChabao);
- //关闭订单
- $productOrder::where('id', $params['order_id'])->where('user_id', $this->auth->id)->setField(['status' => ProductOrder::Closure]);
- if(($buying->stock - $buying->num) == 1) $buying->status = ProductBuying::SaleOut;
- $buying->num += 1;
- $buying->save();
- // 提交事务
- Db::commit();
- } catch (Exception $e) {
- // 回滚事务
- Db::rollback();
- return $this->error($e->getMessage());
- }
- $this->success('ok');
- }
- //取消求购
- public function cancelBuying(LedgerWalletModel $ledgerWalletModel, ProductBuying $productBuying)
- {
- $params = $this->request->post();
- $validate = \think\Loader::validate('Market');
- if(!$validate->scene('cancelbuying')->check($params)) $this->error($validate->getError());
- $buying = $productBuying::get($params['buying_id']);
- if(empty($buying)) $this->error('订单不存在');
- if($buying->user_id!= $this->auth->id) $this->error('无权操作');
- if($buying->status != ProductBuying::Normal) $this->error('订单已完成');
- Db::startTrans();
- try {
- //退回金额
- $amount = bcmul($buying->min_price, ($buying->stock - $buying->num), 6);
- // 添加扣除茶币
- $ledgerWalletModel->changeWalletAccount($this->auth->id, Asset::TOKEN, $amount, LedgerTokenChangeModel::BuyCancel, $buying->user_id);
- // 扣除冻结金额
- $ledgerWalletModel->changeWalletOnly($buying->user_id, Asset::BUYING, -$amount); //解冻金额
- $buying->status = ProductBuying::Close;
- $buying->save();
- // 提交事务
- Db::commit();
- } catch (Exception $e) {
- // 回滚事务
- Db::rollback();
- return $this->error($e->getMessage());
- }
- $this->success('ok');
- }
- //获取配置信息
- public function getMarketConfig()
- {
- $this->success('ok', config('market_transfer'));
- }
-
- }
|