AuctionProduct.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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. ->where('is_show', '=', 1)
  50. ->orderRaw('rand()')
  51. ->limit(1)->find(); // 随机出来一个商品
  52. if (empty($product)){
  53. $config = SystemConfig::where('config_tab_id', 24)->select();
  54. $data = [
  55. 'auction_id' => $auction['id'],
  56. 'create_time' => time()
  57. ];
  58. foreach ($config as $v)
  59. {
  60. if ($v['menu_name'] == 'product_name') $data['name'] = json_decode($v['value']);
  61. if ($v['menu_name'] == 'product_image') $data['image'] = json_decode($v['value']);
  62. if ($v['menu_name'] == 'product_slider_image') $data['slider_image'] = $v['value'];
  63. if ($v['menu_name'] == 'product_price') $data['price'] = json_decode($v['value']);
  64. if ($v['menu_name'] == 'product_hanging_price') $data['hanging_price'] = json_decode($v['value']);
  65. }
  66. $productSave = AuctionProduct::insertGetId($data);// 如果商品卖完,没达到预约量自动创建商品
  67. $product = self::find($productSave);
  68. // 创建订单
  69. $orderData = [
  70. 'uid' => $uid,
  71. 'collection_id' => $product['uid'],
  72. 'order_id' => getNewOrderId(),
  73. 'auction_id' => $id,
  74. 'product_id' => $product['id'],
  75. 'name' => $product['name'],
  76. 'image' => $product['image'],
  77. 'price' => $product['hanging_price'],
  78. 'actual_price' => $product['hanging_price'] - $product['price'],
  79. 'create_time' => time(),
  80. 'frequency' => $auction['frequency']
  81. ];
  82. $res = AuctionOrder::create($orderData);
  83. if ($res) {
  84. AuctionOrder::commitTrans();
  85. return $orderData;
  86. }else{
  87. AuctionOrder::rollbackTrans();
  88. return 'false';
  89. }
  90. }else{
  91. // 创建订单
  92. $orderData = [
  93. 'uid' => $uid,
  94. 'collection_id' => $product['uid'],
  95. 'order_id' => getNewOrderId(),
  96. 'auction_id' => $id,
  97. 'product_id' => $product['id'],
  98. 'name' => $product['name'],
  99. 'image' => $product['image'],
  100. 'price' => $product['hanging_price'],
  101. 'actual_price' => $product['hanging_price'] - $product['price'],
  102. 'create_time' => time(),
  103. 'frequency' => $auction['frequency']
  104. ];
  105. $res = AuctionOrder::create($orderData);
  106. if ($res) {
  107. AuctionOrder::commitTrans();
  108. return $orderData;
  109. }else{
  110. AuctionOrder::rollbackTrans();
  111. return 'false';
  112. }
  113. }
  114. }
  115. }