AuctionProduct.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. <?php
  2. /**
  3. *
  4. * @author: xaboy<365615158@qq.com>
  5. * @day: 2017/11/02
  6. */
  7. namespace app\models\auction;
  8. use app\admin\model\system\SystemConfig;
  9. use crmeb\traits\ModelTrait;
  10. use crmeb\basic\BaseModel;
  11. use http\Env\Request;
  12. use think\facade\Db;
  13. /**
  14. * 竞拍上坪 Model
  15. * Class WechatNews
  16. * @package app\admin\model\wechat
  17. */
  18. class AuctionProduct extends BaseModel
  19. {
  20. use ModelTrait;
  21. protected $pk = 'id';
  22. protected $name = 'auction_product';
  23. protected $autoWriteTimestamp = true;
  24. /**
  25. * 用户商品
  26. * @param $data
  27. * @param $uid
  28. * @return array
  29. * @throws \think\db\exception\DataNotFoundException
  30. * @throws \think\db\exception\DbException
  31. * @throws \think\db\exception\ModelNotFoundException
  32. */
  33. public static function user_product($data, $uid){
  34. $model = self::where('uid', $uid)->order('sort DESC,id DESC');
  35. $model->page($data['page'], $data['limit']);
  36. $list = $model->select()->toArray();
  37. return $list;
  38. }
  39. public static function random($id, $uid, $section)
  40. {
  41. $auction = Auction::find($id);
  42. AuctionOrder::startTrans();
  43. $order = AuctionOrder::where([['auction_id', '=', $id], ['frequency', '=', $auction['frequency']]])->column('product_id');
  44. $product = self::where('auction_id', $auction['id'])
  45. ->where('hanging_price', '>=', $section['low'])
  46. ->where('hanging_price','<=',$section['high'])
  47. ->where('id', 'notIn', $order)
  48. ->where('uid', '<>', $uid)
  49. ->orderRaw('rand()')
  50. ->limit(1)->find(); // 随机出来一个商品
  51. if (empty($product)){
  52. $config = SystemConfig::where('config_tab_id', 24)->select();
  53. $data = [
  54. 'auction_id' => $auction['id'],
  55. 'create_time' => time()
  56. ];
  57. foreach ($config as $v)
  58. {
  59. if ($v['menu_name'] == 'product_name') $data['name'] = json_decode($v['value']);
  60. if ($v['menu_name'] == 'product_image') $data['image'] = json_decode($v['value']);
  61. if ($v['menu_name'] == 'product_slider_image') $data['slider_image'] = $v['value'];
  62. if ($v['menu_name'] == 'product_price') $data['price'] = json_decode($v['value']);
  63. if ($v['menu_name'] == 'product_hanging_price') $data['hanging_price'] = json_decode($v['value']);
  64. }
  65. $productSave = AuctionProduct::insertGetId($data);// 如果商品卖完,没达到预约量自动创建商品
  66. $product = self::find($productSave);
  67. // 创建订单
  68. $orderData = [
  69. 'uid' => $uid,
  70. 'collection_id' => $product['uid'],
  71. 'order_id' => getNewOrderId(),
  72. 'auction_id' => $id,
  73. 'product_id' => $product['id'],
  74. 'name' => $product['name'],
  75. 'image' => $product['image'],
  76. 'price' => $product['hanging_price'],
  77. 'actual_price' => $product['hanging_price'] - $product['price'],
  78. 'create_time' => time(),
  79. 'frequency' => $auction['frequency']
  80. ];
  81. $res = AuctionOrder::create($orderData);
  82. if ($res) {
  83. AuctionOrder::commitTrans();
  84. return $orderData;
  85. }else{
  86. AuctionOrder::rollbackTrans();
  87. return 'false';
  88. }
  89. }else{
  90. // 创建订单
  91. $orderData = [
  92. 'uid' => $uid,
  93. 'collection_id' => $product['uid'],
  94. 'order_id' => getNewOrderId(),
  95. 'auction_id' => $id,
  96. 'product_id' => $product['id'],
  97. 'name' => $product['name'],
  98. 'image' => $product['image'],
  99. 'price' => $product['hanging_price'],
  100. 'actual_price' => $product['hanging_price'] - $product['price'],
  101. 'create_time' => time(),
  102. 'frequency' => $auction['frequency']
  103. ];
  104. $res = AuctionOrder::create($orderData);
  105. if ($res) {
  106. AuctionOrder::commitTrans();
  107. return $orderData;
  108. }else{
  109. AuctionOrder::rollbackTrans();
  110. return 'false';
  111. }
  112. }
  113. }
  114. }