AuctionProduct.php 4.1 KB

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