MarketLogic.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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. $isUpdate = false;
  52. //是否全部取消
  53. $rows = ProductTransfer::where('order_id', $orderId)->find();
  54. $count = ProductTransfer::where('product_id', $orderInfo['product_id'])->where('status', ProductTransfer::Normal)->count();
  55. if($count == 1){
  56. $isUpdate = true;
  57. $market->status = ProductMarket::Hidden;
  58. }else{
  59. //取消最小价格
  60. if($rows->price < $market->price){
  61. $isUpdate = true;
  62. $market->price = ProductTransfer::getTransferMinPriceByProduct($orderInfo['product_id'], $rows->price);
  63. }
  64. }
  65. //市场状态
  66. if($isUpdate) $market->save();
  67. //转让列表取消
  68. $rows->status = ProductTransfer::Stop;
  69. $rows->save();
  70. //新增记录
  71. return ProductOrder::setCreateOrder($orderInfo['order_id'], $orderInfo, ProductOrder::Popular, $userId, $orderInfo['from_user'], getOrderSN('R'.$orderInfo['order_id']), 0, $orderInfo->popular_price);
  72. }
  73. //判断是否锁定
  74. public static function setTransferLock(object $productTransfer, int $endTime, array $params)
  75. {
  76. $time = time();
  77. $lockCount = $productTransfer
  78. ->whereIn('id', $params['transfer_id'])
  79. ->where('is_lock', $productTransfer::Lock)
  80. ->whereTime('lock_time','>', - $endTime)
  81. ->count();
  82. if(!empty($lockCount)) throw new Exception(__("茶权已被他人锁定,无法操作"));
  83. return $productTransfer->whereIn('id', $params['transfer_id'])
  84. ->update(['lock_time'=> $time, 'is_lock' => $productTransfer::Lock]);
  85. }
  86. }