AuctionProduct.php 4.0 KB

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