AuctionOrder.php 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. <?php
  2. /**
  3. *
  4. * @author: xaboy<365615158@qq.com>
  5. * @day: 2017/11/02
  6. */
  7. namespace app\admin\model\auction;
  8. use app\admin\model\order\StoreOrderCartInfo;
  9. use app\admin\model\user\User;
  10. use app\admin\model\user\UserBill;
  11. use app\admin\model\wechat\WechatUser;
  12. use app\models\auction\AuctionProduct;
  13. use crmeb\services\PHPExcelService;
  14. use crmeb\services\product\Product;
  15. use crmeb\traits\ModelTrait;
  16. use crmeb\basic\BaseModel;
  17. /**
  18. * 预约 Model
  19. * Class WechatNews
  20. * @package app\admin\model\wechat
  21. */
  22. class AuctionOrder extends BaseModel
  23. {
  24. use ModelTrait;
  25. protected $pk = 'id';
  26. protected $name = 'auction_order';
  27. protected $autoWriteTimestamp = true;
  28. public static function list($where)
  29. {
  30. $model = self::alias('a')
  31. ->order('a.id', 'desc')
  32. ->field('a.*, u.account, u.nickname,us.account as us_account,p.name, p.image, au.nickname as au_name, gu.name as gu_name')
  33. ->leftJoin('user u', 'a.uid = u.uid')
  34. ->leftJoin('user us', 'us.uid = a.collection_id')
  35. ->leftJoin('auction_product p', 'a.product_id = p.id')
  36. ->leftJoin('auction au', 'au.id = p.auction_id')
  37. ->leftJoin('auction_gu gu', 'gu.id = au.auction_gu_id');
  38. if (trim($where['store_name']) != '') $model->where('a.id|u.account|u.nickname|a.order_id', 'like', '%'.$where['store_name'].'%');
  39. if (trim($where['status']) != '') $model->where('a.status', $where['status']);
  40. if (trim($where['auction_id']) != '') $model->where('au.id', $where['auction_id']);
  41. if (trim($where['gu_id']) != '') $model->where('gu.id', $where['gu_id']);
  42. if (trim($where['product_name']) != '') $model->where('p.name', $where['product_name']);
  43. if (trim($where['data']) != '') $model = self::getModelTime($where, $model, 'a.create_time');
  44. if (isset($where['excel']) && $where['excel'] == 1) {
  45. $list = ($list = $model->select()) && count($list) ? $list->toArray() : [];
  46. } else {
  47. $list = ($list = $model->page((int)$where['page'], (int)$where['limit'])->select()) && count($list) ? $list->toArray() : [];
  48. }
  49. if (isset($where['excel']) && $where['excel'] == 1) {
  50. self::SaveExcel($list);
  51. }
  52. if ($where['page'] && $where['limit']){
  53. $model->page($where['page'], $where['limit']);
  54. }else{
  55. $model->page(20, 1);
  56. }
  57. $data['count'] = $model->count();
  58. $list = $model->select();
  59. $data['data'] = $list;
  60. return $data;
  61. }
  62. /*
  63. * 保存并下载excel
  64. * $list array
  65. * return
  66. */
  67. public static function SaveExcel($list)
  68. {
  69. $export = [];
  70. foreach ($list as $index => $item) {
  71. $status = $item['status']== 0 ? '过期' : $item['status']== 1 ? '待上传':$item['status']== 2 ? '待审核':$item['status']== 3 ? '完成': '未知';
  72. $export[] = [
  73. $item['order_id'],
  74. $item['name'],
  75. $item['au_name'],
  76. $item['gu_name'],
  77. $item['account'],
  78. $item['nickname'],
  79. $item['us_account'],
  80. $item['price'],
  81. $status,
  82. $item['create_time'],
  83. ];
  84. }
  85. PHPExcelService::setExcelHeader(['订单号', '商品名', '场馆', '场次', '买家账号', '买家昵称', '卖家账号','商品价格', '状态',
  86. '订单时间'])
  87. ->setExcelTile('订单导出' . date('YmdHis', time()), '订单信息' . time(), ' 生成时间:' . date('Y-m-d H:i:s', time()))
  88. ->setExcelContent($export)
  89. ->ExcelSave();
  90. }
  91. /**
  92. * 卖家操作
  93. * @param $id //商品所属人
  94. * @param $price //卖出价格
  95. * @param $product //商品详情
  96. * @return void
  97. * @throws \think\db\exception\DataNotFoundException
  98. * @throws \think\db\exception\DbException
  99. * @throws \think\db\exception\ModelNotFoundException
  100. */
  101. public static function earn($id, $price,$product)
  102. {
  103. $userModel = new \app\models\user\User();
  104. $productModel = new AuctionProduct();
  105. $user = $userModel->find($id);
  106. if ($user['spread_uid'] > 0){
  107. $s_price = number_format(($price - $product['price']) * 0.1, 2); // 卖出价格减去购买价格的百分之十 为上级直推奖励
  108. $spread = $userModel->find($user['spread_uid']);
  109. $spread['integral'] = $spread['integral'] + $s_price; //积分增加
  110. $spread->save();
  111. \app\models\user\UserBill::income('直推奖励', $spread['uid'], 'anticipate', 'add_anticipate', $s_price, 0, $spread['integral'], '奖励趣豆');
  112. }
  113. // $user['anticipate'] = $user['anticipate']-$price*($product['deduct']/100); // 扣除当前卖出价格百分比的预约卷
  114. // $user->save();
  115. // UserBill::expend('预约卷扣除', $user['uid'], 'anticipate','reduce_anticipate', $price*($product['deduct']/100), 0, $user['anticipate'] ,'卖出扣除预约卷');
  116. $productModel->where('id', $product['id'])->save(['is_show' => 0]); //下架等待挂售
  117. }
  118. /**
  119. * 购买成功退预约卷
  120. * @param $id
  121. * @return void
  122. * @throws \think\db\exception\DataNotFoundException
  123. * @throws \think\db\exception\DbException
  124. * @throws \think\db\exception\ModelNotFoundException
  125. */
  126. public static function return($id)
  127. {
  128. $data = self::find($id);
  129. $userModel = new User();
  130. $productModel = new AuctionProduct();
  131. $auctionModel = new Auction();
  132. $bookingModel = new AuctionBooking();
  133. $user = $userModel->find($data['uid']);
  134. if ($user['is_new'] == 1){
  135. if ($user['spread_uid']) {
  136. $spread = $userModel->where('uid', $user['spread_uid'])->find();
  137. $spread['green_time'] = strtotime(date('Y-m-d', strtotime('+1 day'))); // 开启明天的绿色通道
  138. $spread->save();
  139. }
  140. $orderCount =AuctionOrder::where([['uid', '=', $user['uid']], ['status', '=', 3]])->count();
  141. if ($orderCount >= 5){
  142. $user['is_new'] = 0;
  143. }
  144. }
  145. $product = $productModel->where('id', $data['product_id'])->find();
  146. $auction = $auctionModel->where('id', $product['auction_id'])->find();
  147. $time = strtotime(date('Y-m-d' ,strtotime($data['create_time']))); // 订单当天时间段
  148. $totime = strtotime(date('Y-m-d' ,strtotime($data['create_time']))) + 86400; // 订单凌晨时间段
  149. $booking = $bookingModel->where('auction_id', $auction['id'])->where('uid', $user['uid'])->whereBetweenTime('create_time', $time, $totime)->find();
  150. if ($booking['status'] > 0){
  151. $booking['status'] = 0;
  152. $booking->save();
  153. $anticipate = $booking['anticipate'] - $booking['deduction'];
  154. $user['anticipate'] = $user['anticipate'] + $anticipate;// 退还预约卷
  155. $user->save();
  156. UserBill::create([
  157. 'uid' => $user['uid'],
  158. 'pm' => 1,
  159. 'title' => '预约卷退还',
  160. 'category' => 'anticipate',
  161. 'type' => 'add_anticipate',
  162. 'mark' => '退还'.$anticipate.'预约卷扣除'.$booking['deduction'],
  163. 'add_time' => time(),
  164. 'number' => $anticipate,
  165. 'balance' => $user['anticipate']
  166. ]);
  167. }
  168. }
  169. }