MarketLogic.php 3.0 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 || empty($minPrice)) $minPrice = $price;
  20. //添加市场
  21. $rows = ProductMarket::where('product_id', $productId)->find();
  22. if(empty($rows)){
  23. ProductMarket::create([
  24. 'product_id' => $productId,
  25. 'type_id' => ProductLists::getProductTypeById($productId),
  26. 'price' => $minPrice,'status' => ProductMarket::Normal]);
  27. }else{
  28. $isUpdate = false;
  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. //添加寄售订单
  42. $fee = getConfig('transfer_fee');
  43. $feeAmount = bcmul($price, $fee, 2) ;
  44. return ProductTransfer::setTransferOrder($userId, $productId, $areaId, $feeAmount, $orderNo, $params);
  45. }
  46. //取消寄售更新最低价
  47. public static function cancelTransferOrder(int $orderId, int $userId, $orderInfo)
  48. {
  49. //市场
  50. $market = ProductMarket::where('product_id', $orderInfo['product_id'])->find();
  51. //是否全部取消
  52. $rows = ProductTransfer::where('product_id', $orderInfo['product_id'])->where('status', ProductTransfer::Normal)->count();
  53. if($rows == 1){
  54. $market->status = ProductMarket::Hidden;
  55. }else{
  56. //取消最小价格
  57. if($rows->price < $market->price){
  58. $market->price = ProductTransfer::getTransferMinPriceByProduct($orderInfo['product_id'], $rows->price);
  59. }
  60. }
  61. //市场状态
  62. $market->save();
  63. //转让列表取消
  64. ProductTransfer::where('order_id',$orderId)->setField('status', ProductTransfer::Stop);
  65. //新增记录
  66. return ProductOrder::setCreateOrder($orderId, $orderInfo, ProductOrder::Popular, $userId, $orderInfo['from_user'], getOrderSN('R'.$orderInfo['order_id']), 0, $orderInfo->popular_price);
  67. }
  68. }