AuctionOrder.php 7.0 KB

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