MarketLogic.php 4.0 KB

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