AuctionOrder.php 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  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,us.nickname as us_nickname,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 ($where['uid']){
  45. $auctionGu = AuctionGu::where('uid', $where['uid'])->column('id');
  46. $auction = Auction::where('auction_gu_id', 'in', $auctionGu)->column('id');
  47. $model->where('a.auction_id', 'in', $auction);
  48. }
  49. if ($where['is_gs'] == 2){
  50. $model->where('a.is_gs', 0);
  51. }else if($where['is_gs'] == 1){
  52. $model->where('a.is_gs', 1);
  53. }
  54. $data['count'] = $model->count();
  55. if (isset($where['excel']) && $where['excel'] == 1) {
  56. $list = ($list = $model->select()) && count($list) ? $list->toArray() : [];
  57. } else {
  58. $list = ($list = $model->page((int)$where['page'], (int)$where['limit'])->select()) && count($list) ? $list->toArray() : [];
  59. }
  60. if (isset($where['excel']) && $where['excel'] == 1) {
  61. self::SaveExcel($list);
  62. }
  63. if ($where['page'] && $where['limit']){
  64. $model->page($where['page'], $where['limit']);
  65. }else{
  66. $model->page(20, 1);
  67. }
  68. $list = $model->select();
  69. $data['data'] = $list;
  70. return $data;
  71. }
  72. /*
  73. * 保存并下载excel
  74. * $list array
  75. * return
  76. */
  77. public static function SaveExcel($list)
  78. {
  79. $export = [];
  80. foreach ($list as $index => $item) {
  81. if ($item['status'] == 0 ){
  82. $status = '过期订单';
  83. }elseif ($item['status'] == 1){
  84. $status ='待支付';
  85. }elseif ($item['status'] == 2){
  86. $status ='待审核';
  87. }elseif ($item['status'] == 3){
  88. $status ='完成';
  89. }
  90. if ($item['status'] == 0){
  91. $is_gs = '过期订单';
  92. }else{
  93. if ($item['is_gs'] == 0){
  94. $is_gs ='未挂售';
  95. }elseif ($item['is_gs'] == 1){
  96. $is_gs ='已挂售';
  97. }
  98. }
  99. $export[] = [
  100. $item['order_id'],
  101. $item['name'],
  102. $item['au_name'],
  103. $item['gu_name'],
  104. $item['account'],
  105. $item['nickname'],
  106. $item['us_account'],
  107. $item['us_nickname'],
  108. $item['price'],
  109. $status,
  110. $is_gs,
  111. $item['create_time'],
  112. ];
  113. }
  114. PHPExcelService::setExcelHeader(['订单号', '商品名', '场馆', '场次', '买家账号', '买家昵称', '卖家账号','卖家昵称','商品价格', '状态','挂售状态',
  115. '订单时间'])
  116. ->setExcelTile('订单导出' . date('YmdHis', time()), '订单信息' . time(), ' 生成时间:' . date('Y-m-d H:i:s', time()))
  117. ->setExcelContent($export)
  118. ->ExcelSave();
  119. }
  120. /**
  121. * 卖家操作
  122. * @param $id //商品所属人
  123. * @param $price //卖出价格
  124. * @param $product //商品详情
  125. * @return void
  126. * @throws \think\db\exception\DataNotFoundException
  127. * @throws \think\db\exception\DbException
  128. * @throws \think\db\exception\ModelNotFoundException
  129. */
  130. public static function earn($id, $price,$product)
  131. {
  132. $userModel = new \app\models\user\User();
  133. $productModel = new AuctionProduct();
  134. $user = $userModel->find($id);
  135. if ($user['spread_uid'] > 0){
  136. $s_price = number_format(($price - $product['price']) * 0.1, 2); // 卖出价格减去购买价格的百分之十 为上级直推奖励
  137. $spread = $userModel->find($user['spread_uid']);
  138. $spread['integral'] = $spread['integral'] + $s_price; //积分增加
  139. $spread->save();
  140. \app\models\user\UserBill::income('直推奖励', $spread['uid'], 'anticipate', 'add_anticipate', $s_price, 0, $spread['integral'], '奖励趣豆');
  141. }
  142. // $user['anticipate'] = $user['anticipate']-$price*($product['deduct']/100); // 扣除当前卖出价格百分比的广告值
  143. // $user->save();
  144. // UserBill::expend('广告值扣除', $user['uid'], 'anticipate','reduce_anticipate', $price*($product['deduct']/100), 0, $user['anticipate'] ,'卖出扣除广告值');
  145. $productModel->where('id', $product['id'])->save(['is_show' => 0]); //下架等待挂售
  146. }
  147. /**
  148. * 购买成功退广告值
  149. * @param $id
  150. * @return void
  151. * @throws \think\db\exception\DataNotFoundException
  152. * @throws \think\db\exception\DbException
  153. * @throws \think\db\exception\ModelNotFoundException
  154. */
  155. public static function return($id)
  156. {
  157. $data = self::find($id);
  158. $userModel = new User();
  159. $productModel = new AuctionProduct();
  160. $auctionModel = new Auction();
  161. $bookingModel = new AuctionBooking();
  162. $user = $userModel->find($data['uid']);
  163. if ($user['is_new'] == 1){
  164. if ($user['spread_uid']) {
  165. $spread = $userModel->where('uid', $user['spread_uid'])->find();
  166. $spread['green_time'] = strtotime(date('Y-m-d', strtotime('+1 day'))); // 开启明天的绿色通道
  167. $spread->save();
  168. }
  169. $orderCount =AuctionOrder::where([['uid', '=', $user['uid']], ['status', '=', 3]])->count();
  170. if ($orderCount >= 5){
  171. $user['is_new'] = 0;
  172. }
  173. }
  174. $product = $productModel->where('id', $data['product_id'])->find();
  175. $auction = $auctionModel->where('id', $product['auction_id'])->find();
  176. $time = strtotime(date('Y-m-d' ,strtotime($data['create_time']))); // 订单当天时间段
  177. $totime = strtotime(date('Y-m-d' ,strtotime($data['create_time']))) + 86400; // 订单凌晨时间段
  178. $booking = $bookingModel->where('auction_id', $auction['id'])->where('uid', $user['uid'])->whereBetweenTime('create_time', $time, $totime)->find();
  179. if ($booking['status'] > 0){
  180. $booking['status'] = 0;
  181. $booking->save();
  182. $anticipate = $booking['anticipate'] - $booking['deduction'];
  183. $user['anticipate'] = $user['anticipate'] + $anticipate;// 退还广告值
  184. $user->save();
  185. UserBill::create([
  186. 'uid' => $user['uid'],
  187. 'pm' => 1,
  188. 'title' => '广告值退还',
  189. 'category' => 'anticipate',
  190. 'type' => 'th_anticipate',
  191. 'mark' => '退还'.$anticipate.'广告值扣除'.$booking['deduction'],
  192. 'add_time' => time(),
  193. 'number' => $anticipate,
  194. 'balance' => $user['anticipate']
  195. ]);
  196. }
  197. }
  198. }