| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103 |
- <?php
- namespace app\api\logic;
- use Exception;
- use think\Env;
- use think\Cache;
- use think\Loader ;
- use app\common\model\ProductOrder;
- use app\common\model\ProductLists;
- use app\common\model\ProductTransfer;
- use app\common\model\ProductMarket;
- //自由市场
- class MarketLogic
- {
- // 添加市场寄售订单
- public static function createTransferOrder(float $price, int $productId, int $areaId, string $orderNo, int $userId, array $params)
- {
- //获取市场价
- $minPrice = ProductTransfer::getTransferMinPrice($productId);
- if($price < $minPrice || empty($minPrice)) $minPrice = $price;
- //添加市场
- $rows = ProductMarket::where('product_id', $productId)->find();
- if(empty($rows)){
- ProductMarket::create([
- 'product_id' => $productId,
- 'type_id' => ProductLists::getProductTypeById($productId),
- 'price' => $minPrice,'status' => ProductMarket::Normal]);
- }else{
- $isUpdate = false;
- //更新市场状态
- if($rows->status != ProductMarket::Normal ){
- $rows->status = ProductMarket::Normal;
- $isUpdate = true;
- }
- //更新最低价
- if($rows->price != $minPrice){
- $rows->price = $minPrice;
- $isUpdate = true;
- }
- if($isUpdate) $rows->save();
- }
- //添加寄售订单
- $fee = getConfig('transfer_fee');
- $feeAmount = bcmul($price, $fee, 2) ;
- return ProductTransfer::setTransferOrder($userId, $productId, $areaId, $feeAmount, $orderNo, $params);
- }
- //取消寄售更新最低价
- public static function cancelTransferOrder(int $orderId, int $userId, $orderInfo)
- {
- //市场
- $market = ProductMarket::where('product_id', $orderInfo['product_id'])->find();
- //转让
- $rows = ProductTransfer::where('order_id', $orderId)->find();
- if($market){
- $isUpdate = false;
- //是否全部取消
-
- $count = ProductTransfer::where('product_id', $orderInfo['product_id'])->where('status', ProductTransfer::Normal)->count();
- if($count == 1){
- $isUpdate = true;
- $market->status = ProductMarket::Hidden;
- }else{
- //取消最小价格
- if($rows->price < $market->price){
- $isUpdate = true;
- $market->price = ProductTransfer::getTransferMinPriceByProduct($orderInfo['product_id'], $rows->price);
- }
- }
- //市场状态
- if($isUpdate) $market->save();
- }
- //转让列表取消
- $rows->status = ProductTransfer::Stop;
- $rows->save();
- //新增记录
- return ProductOrder::setCreateOrder($orderInfo['order_id'], $orderInfo, ProductOrder::Popular, $userId, $orderInfo['from_user'], getOrderSN('R'.$orderInfo['order_id']), 0, $orderInfo->popular_price);
- }
- //判断是否锁定
- public static function setTransferLock(object $productTransfer, int $endTime, int $uid, string $transferId)
- {
- $time = time();
- $lockList = $productTransfer->whereIn('id', $transferId)->select();
- foreach ($lockList as &$item) {
- if($item->user_id == $uid) throw new Exception(__("不能锁自己的寄售单"));
- if($item->lock_uid != $uid && $item->lock_time + $endTime > $time) throw new Exception(__("茶权已被他人锁定,无法操作"));
- $item->lock_uid = $uid;
- $item->lock_time = $time;
- $item->save();
- }
- return true;
- }
- }
|