MarketLogic.php 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. <?php
  2. namespace app\api\logic;
  3. use Exception;
  4. use think\Env;
  5. use think\Cache;
  6. use think\Loader ;
  7. use app\common\model\ProductOrder;
  8. use app\common\model\ProductLists;
  9. use app\common\model\ProductTransfer;
  10. use app\common\model\ProductMarket;
  11. //自由市场
  12. class MarketLogic
  13. {
  14. // 添加市场寄售订单
  15. public static function createTransferOrder(float $price, int $productId, int $areaId, string $orderNo, int $userId, array $params)
  16. {
  17. //获取市场价
  18. $minPrice = ProductTransfer::getTransferMinPrice($productId);
  19. if($price < $minPrice) $minPrice = $price;
  20. $isUpdate = false;
  21. //添加市场
  22. $rows = ProductMarket::where('product_id', $productId)->find();
  23. if(empty($rows)){
  24. ProductMarket::create([
  25. 'product_id' => $productId,
  26. 'type_id' => ProductLists::getProductTypeById($productId),
  27. 'price' => $minPrice,'status' => ProductMarket::Normal]);
  28. }
  29. //更新市场状态
  30. if($rows->status != ProductMarket::Normal ){
  31. $rows->status = ProductMarket::Normal;
  32. $isUpdate = true;
  33. }
  34. //更新最低价
  35. if($rows->price != $minPrice){
  36. $rows->price = $minPrice;
  37. $isUpdate = true;
  38. }
  39. if($isUpdate) $rows->save();
  40. //添加寄售订单
  41. $fee = getConfig('transfer_fee');
  42. $feeAmount = bcmul($price, $fee, 2) ;
  43. return ProductTransfer::setTransferOrder($userId, $productId, $areaId, $feeAmount, $orderNo, $params);
  44. }
  45. //取消寄售更新最低价
  46. public static function cancelTransferOrder(int $orderId, int $userId, $orderInfo)
  47. {
  48. //市场
  49. $market = ProductMarket::where('product_id', $orderInfo['product_id'])->find();
  50. //是否全部取消
  51. $rows = ProductTransfer::where('product_id', $orderInfo['product_id'])->where('status', ProductTransfer::Normal)->count();
  52. if($rows == 1){
  53. $market->status = ProductMarket::Hidden;
  54. }else{
  55. //取消最小价格
  56. if($rows->price < $market->price){
  57. $market->price = ProductTransfer::getTransferMinPriceByProduct($orderInfo['product_id'], $rows->price);
  58. }
  59. }
  60. //市场状态
  61. $market->save();
  62. //转让列表取消
  63. ProductTransfer::where('order_id',$orderId)->setField('status', ProductTransfer::Stop);
  64. //新增记录
  65. return ProductOrder::setCreateOrder($orderId, $orderInfo, ProductOrder::Popular, $userId, $orderInfo['from_user'], getOrderSN('R'.$orderInfo['order_id']), 0, $orderInfo->popular_price);
  66. }
  67. }