MarketLogic.php 4.0 KB

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