IntegralController.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  1. <?php
  2. namespace App\Http\Controllers;
  3. use App\Models\Store;
  4. use App\Models\OrderW;
  5. use App\Models\GiftW;
  6. use App\Models\GiftOrderW;
  7. use App\Models\IntegralW;
  8. use App\Models\ExchangeW;
  9. use App\Models\User;
  10. use Carbon\Carbon;
  11. use Illuminate\Http\Request;
  12. //use Maatwebsite\Excel\Facades\Excel;
  13. use Illuminate\Support\Facades\Auth;
  14. use Illuminate\Support\Facades\DB;
  15. use Illuminate\Support\Facades\Cache;
  16. use Illuminate\Support\Facades\Log;
  17. class IntegralController extends Controller
  18. {
  19. public function getStoreIntegralOrderBy(Request $request){
  20. $page_index=$request->input('page_index');
  21. $page_size=$request->input('page_size');
  22. $num=$page_size*($page_index-1);
  23. $type=$request->input('type');
  24. $where=[];
  25. if($type=='yesterday'){
  26. $where=function($query){
  27. $date=date("Y-m-d",strtotime('-1 days'));
  28. $query->whereDate('inte.created_at',$date);
  29. };
  30. }
  31. $count=Store::where('status',0)->count();
  32. // $list=Store::where('status',0)->orderByDesc('integral')
  33. // ->skip($num)->take($page_size)->get();
  34. $list=Store::leftJoin('integral as inte',function($query)use($where){
  35. $query->on('store.id','=','inte.store_id')
  36. ->where($where);
  37. })->where('store.status',0)
  38. ->groupBy('store.id')
  39. ->select('store.id','store.name','store.img',DB::raw('sum(ifnull(inte.integral_double,0)) as integral'))
  40. ->orderByDesc('integral')->orderBy('store.id')
  41. ->skip($num)->take($page_size)->get();
  42. return $this->success_list($list,'',$count);
  43. }
  44. public function getStoreOrderBy(Request $request){
  45. $type=$request->input('type');
  46. $agent_id=Auth::user()->id;
  47. $store=Store::where('user_id',$agent_id)->first();
  48. $store_id=$store->id;
  49. $where=[];
  50. $date=date("Y-m-d",strtotime('-1 days'));
  51. $where=function($query)use($date){
  52. $query->whereDate('inte.created_at',$date);
  53. };
  54. $yes_list=array_flip($this->getOrderBy($where));
  55. $list=array_flip($this->getOrderBy([]));
  56. $yes_integral=Store::leftJoin('integral as inte',function($query)use($where){
  57. $query->on('store.id','=','inte.store_id')->where($where);
  58. })->where('store.status',0)
  59. ->where('store_id',$store_id)
  60. ->select('store.id','store.name','store.img',DB::raw('sum(ifnull(inte.integral_double,0)) as integral'))
  61. ->value('integral');
  62. $integral=Store::leftJoin('integral as inte',function($query)use($where){
  63. $query->on('store.id','=','inte.store_id');
  64. })->where('store.status',0)
  65. ->where('store_id',$store_id)
  66. ->groupBy('store.id')
  67. ->select('store.id','store.name','store.img',DB::raw('sum(ifnull(inte.integral_double,0)) as integral'))
  68. ->value('integral');
  69. // $array=[15238617326,15138929587,18838980968,13030314383,15286818088];
  70. // $array=[];
  71. // $data['exc_gifts']=0;
  72. // if(in_array($store->phone,$array)){
  73. // $data['exc_gifts']=1;
  74. // }
  75. // $exchange=ExchangeW::where('status',1)->first();
  76. // if(empty($exchange)){
  77. // $data['exc_gifts']=0;
  78. // }else{
  79. // $date=date("Y-m-d H:i:s");
  80. // if($date<$exchange->start_time || $date>$exchange->end_time){
  81. // $data['exc_gifts']=0;
  82. // }else{
  83. // $data['exc_gifts']=1;
  84. // }
  85. // }
  86. $data['exc_gifts']=1;
  87. $data['yes_sort']=$yes_list[$store_id]+1;
  88. $data['sort']=$list[$store_id] + 1;
  89. $data['store']=Store::where('id',$store_id)->select('id','name','img','integral')->first();
  90. $data['integral']=$integral;
  91. $data['yes_integral']=$yes_integral;
  92. return $this->success($data);
  93. }
  94. protected function getOrderBy($where){
  95. $data=Store::leftJoin('integral as inte',function($query)use($where){
  96. $query->on('store.id','=','inte.store_id')
  97. ->where($where);
  98. })->where('store.status',0)
  99. ->groupBy('store.id')
  100. ->select('store.id','store.name','store.img',DB::raw('sum(ifnull(inte.integral_double,0)) as integral'))
  101. ->orderByDesc('integral')->orderBy('store.id')
  102. ->pluck('id')->toArray();
  103. return $data;
  104. }
  105. public function getStoreDetail(Request $request){
  106. $page_index=$request->input('page_index');
  107. $page_size=$request->input('page_size');
  108. $num=$page_size*($page_index-1);
  109. $agent_id=Auth::user()->id;
  110. $store_id=Store::where('user_id',$agent_id)->value('id');
  111. $count=IntegralW::where('store_id',$store_id)->count();
  112. $list=IntegralW::where('store_id',$store_id)->skip($num)->take($page_size)
  113. ->orderByDesc('created_at')->get();
  114. return $this->success_list($list,'',$count);
  115. }
  116. //我的积分与兑换礼品
  117. public function getInteGifts(){
  118. $store=Store::where('user_id',Auth::user()->id)->select('id','name','phone','img','integral')->first();
  119. $gifts=GiftW::where('status',1)->orderBy('id')->get();
  120. $data['is_exc']=1;
  121. $exchange=ExchangeW::where('status',1)->first();
  122. if(empty($exchange)){
  123. $data['is_exc']=0;
  124. }else{
  125. $date=date("Y-m-d H:i:s");
  126. if($date<$exchange->start_time || $date>$exchange->end_time){
  127. $data['is_exc']=0;
  128. }
  129. }
  130. $data['integral']=$store->integral;
  131. $data['gifts']=$gifts;
  132. return $this->success($data);
  133. }
  134. //兑换礼品
  135. public function exchangeGifts(Request $request){
  136. $date=date("Y-m-d H:i:s");
  137. $exchange=ExchangeW::where('status',1)->first();
  138. if(empty($exchange)){
  139. return $this->error('450001','当前时间不可兑换');
  140. }
  141. if($date<$exchange->start_time || $date>$exchange->end_time){
  142. return $this->error('450001','当前时间不可兑换');
  143. }
  144. $store=Store::where('user_id',Auth::user()->id)->select('id','name','phone','img','integral')->first();
  145. $gift_id=$request->input('gift_id');
  146. $num=$request->input('num');
  147. $gift=GiftW::where('id',$gift_id)->first();
  148. if($gift->gift_num - $gift->exchange_num < $num){
  149. return $this->error('450001','兑换礼品库存不足,停止兑换');
  150. }
  151. $total=$num*$gift->integral;
  152. if($total>$store->integral){
  153. return $this->error('450001','兑换礼品总积分超出现有积分,不能兑换');
  154. }
  155. try{
  156. DB::transaction(function()use($gift_id,$num,$exchange){
  157. $giftInfo=GiftW::where('id',$gift_id)->lockForUpdate()->first();
  158. // $giftInfo->gift_num -= $num;
  159. $giftInfo->exchange_num += $num;
  160. $giftInfo->save();
  161. $storeInfo=Store::where('user_id',Auth::user()->id)->lockForUpdate()->first();
  162. $storeInfo->integral -= $num*$giftInfo->integral;
  163. $storeInfo->save();
  164. $order_no='DH'.date('ymdHis').substr(microtime(), 2, 6).mt_rand(10000, 99999);
  165. GiftOrderW::create([
  166. 'store_id'=>$storeInfo->id,
  167. 'uid'=>$storeInfo->user_id,
  168. 'gift_id'=>$gift_id,
  169. 'shipping_status'=>0,
  170. 'num'=>$num,
  171. 'season'=>$exchange->season,
  172. 'order_number'=>$order_no,
  173. 'bonus'=>$giftInfo->integral,
  174. 'name'=>$giftInfo->name,
  175. 'type'=>0
  176. ]);
  177. IntegralW::create([
  178. 'store_id'=>$storeInfo->id,
  179. 'order_no'=>$order_no,
  180. 'num'=> $num,
  181. 'integral'=> 0-$num*$giftInfo->integral,
  182. 'integral_double'=> 0-$num*$giftInfo->integral,
  183. 'type'=>3,
  184. 'remark'=>'兑换礼品',
  185. ]);
  186. },5);
  187. return $this->success([]);
  188. }catch(\Exception $e){
  189. return $this->error($e->getMessage());
  190. }
  191. }
  192. //取消兑换礼品
  193. public function cancelExchangeGifts(Request $request){
  194. $id=$request->input('id');
  195. $date=date("Y-m-d H:i:s");
  196. $exchange=ExchangeW::where('status',1)->first();
  197. if(empty($exchange)){
  198. return $this->error('450001','当前时间不可取消兑换');
  199. }
  200. $gift_order=GiftOrderW::where('id',$id)->first();
  201. if($exchange->season != $gift_order->season){
  202. return $this->error('450001','当前时间该兑换不可取消');
  203. }
  204. if($date<$exchange->start_time || $date>$exchange->end_time){
  205. return $this->error('450001','当前时间该兑换不可取消');
  206. }
  207. if($gift_order->shipping_status==1){
  208. return $this->error('450001','礼品已发出,不能取消兑换');
  209. }
  210. try{
  211. DB::transaction(function()use($id){
  212. $gift_order=GiftOrderW::where('id',$id)->lockForUpdate()->first();
  213. $gift_order->type=1;
  214. $gift_order->save();
  215. $giftInfo=GiftW::where('id',$gift_order->gift_id)->lockForUpdate()->first();
  216. // $giftInfo->gift_num += $gift_order->num;
  217. $giftInfo->exchange_num -= $gift_order->num;
  218. $giftInfo->save();
  219. $storeInfo=Store::where('user_id',Auth::user()->id)->lockForUpdate()->first();
  220. $storeInfo->integral += $gift_order->num*$giftInfo->integral;
  221. $storeInfo->save();
  222. IntegralW::create([
  223. 'store_id'=>$storeInfo->id,
  224. 'order_no'=>$gift_order->order_number,
  225. 'num'=> $gift_order->num,
  226. 'integral'=> $gift_order->num*$giftInfo->integral,
  227. 'integral_double'=> $gift_order->num*$giftInfo->integral,
  228. 'type'=>4,
  229. 'remark'=>'取消兑换',
  230. ]);
  231. },5);
  232. return $this->success([]);
  233. }catch(\Exception $e){
  234. return $this->error($e->getMessage());
  235. }
  236. }
  237. //我的兑换
  238. public function myExchange(Request $request){
  239. $page_index=$request->input('page_index');
  240. $page_size=$request->input('page_size');
  241. $num=$page_size*($page_index-1);
  242. $store=Store::where('user_id',Auth::user()->id)->first();
  243. $gift_count=GiftOrderW::where('store_id',$store->id)->count();
  244. $gift_order=GiftOrderW::where('store_id',$store->id)
  245. ->with('gift')
  246. ->orderByDesc('id')
  247. ->skip($num)->take($page_size)->get();
  248. return $this->success_list($gift_order,'',$gift_count);
  249. }
  250. //我的礼品
  251. public function myGifts(Request $request){
  252. $page_index=$request->input('page_index');
  253. $page_size=$request->input('page_size');
  254. $num=$page_size*($page_index-1);
  255. $store=Store::where('user_id',Auth::user()->id)->first();
  256. $gift_count=GiftOrderW::where('store_id',$store->id)->where('type',0)->sum('num');
  257. $gift_order=GiftOrderW::where('store_id',$store->id)->where('type',0)
  258. ->with('gift')
  259. ->groupBy('gift_id')
  260. ->select('id','name','gift_id',DB::raw('sum(num) as num'))
  261. ->skip($num)->take($page_size)->get();
  262. return $this->success_list($gift_order,'',$gift_count);
  263. }
  264. //我的下级兑换
  265. public function myAgentExchange(Request $request){
  266. $page_index=$request->input('page_index');
  267. $page_size=$request->input('page_size');
  268. $num=$page_size*($page_index-1);
  269. $user_id=Auth::user()->id;
  270. // if($user_id=='32531'){
  271. // $user_id=14;
  272. // }
  273. $user=User::where('id',$user_id)->first();
  274. $childIds = [];
  275. if ($user->level == '3') {
  276. $childAgentIds = User::where('agent_id', $user->id)->pluck('id')->toArray();
  277. if ($childAgentIds) {
  278. $childChildAgentIds = User::whereIn('agent_id', $childAgentIds)->pluck('id')->toArray();
  279. $childIds = array_merge($childAgentIds, $childChildAgentIds);
  280. } else {
  281. $childIds = $childAgentIds;
  282. }
  283. } elseif ($user->level == '2') {
  284. $childIds = User::where('agent_id', $user->id)->pluck('id');
  285. } else {
  286. $childIds = [];
  287. }
  288. $data['gifts_num']=GiftOrderW::whereIn('uid',$childIds)->sum('num');//->where('type',0)
  289. $count=GiftOrderW::whereIn('uid',$childIds)
  290. ->leftJoin('store as s','gift_orders.store_id','=','s.id')
  291. ->select('s.id','s.name','s.phone',DB::raw('sum(num) as num'))
  292. ->groupBy('uid')->get()->count();
  293. $data['user_gifts']=GiftOrderW::whereIn('uid',$childIds)
  294. ->leftJoin('store as s','gift_orders.store_id','=','s.id')
  295. ->select('s.id','s.name','s.phone','s.img',DB::raw('sum(num) as num'))
  296. ->groupBy('gift_orders.uid')->orderByDesc('num')->orderBy('gift_orders.store_id')
  297. ->skip($num)->take($page_size)->get();
  298. return $this->success_list($data,'',$count);//->where('type',0)
  299. }
  300. //下级礼品
  301. public function getAgentGifts(Request $request){
  302. $id=$request->input('id');
  303. $gifts=GiftOrderW::where('store_id',$id)->where('type',0)
  304. ->leftJoin('store as s','gift_orders.store_id','=','s.id')
  305. ->select('store_id','gift_id','gift_orders.name',DB::raw('sum(num) as num'))
  306. ->groupBy('gift_id')->orderByDesc('num')->get();
  307. return $this->success($gifts);
  308. }
  309. public function integral_2(){
  310. $date=date("Y-m-d",strtotime('-1 days'));
  311. $where=function($query)use($date){
  312. $query->where('order.is_pay',1)->whereIn('order.apply_cancel',[0,1,3])->whereDate('order.created_at',$date);
  313. };
  314. $store_ids=OrderW::where($where)->leftjoin('store','order.store_id','=','store.id')
  315. ->select('store.name','store.phone','store.img','store_id',DB::raw('sum(total) as total'),DB::raw('sum(account) as account'))
  316. ->groupBy('store_id')->having('total','>=',2)->orderByDesc('total')->get();
  317. return $store_ids;
  318. $date=date("Y-m-d",strtotime('-1 days'));
  319. $where=function($query)use($date){
  320. $query->where('is_pay',1)->whereIn('apply_cancel',[0,1,3])->whereDate('created_at',$date);
  321. };
  322. $store_ids=OrderW::where($where)
  323. ->select('store_id',DB::raw('sum(total) as total'))
  324. ->groupBy('store_id')->having('total','>=',2)->pluck('store_id');
  325. $order_nos=OrderW::where($where)->whereIn('store_id',$store_ids)->pluck('order_no');
  326. IntegralW::whereDate('created_at',$date)->whereIn('order_no',$order_nos)->where('type',1)
  327. ->update(['integral_double'=>DB::raw('2*integral')]);
  328. $cancel=IntegralW::whereDate('created_at',$date)->where('type',2)->get();
  329. foreach($cancel as $key=>$val){
  330. $inte_2=IntegralW::where('order_no',$val->order_no)->where('type',1)->value('integral_double');
  331. IntegralW::where('id',$val->id)->update([
  332. 'integral_double'=>0-$inte_2
  333. ]);
  334. }
  335. return '12222222222';
  336. // $store=Store::leftJoin('integral as inte',function($query)use($date){
  337. // $query->on('store.id','=','inte.store_id')->whereDate('created_at',$date);
  338. // })->where('store.status',0)
  339. // ->groupBy('store.id')
  340. // ->select('store.id','store.name','store.img',DB::raw('sum(ifnull(inte.num,0)) as num'),DB::raw('sum(ifnull(inte.integral,0)) as integral'))
  341. // ->having('num','>=',2)
  342. // ->get();
  343. $inte=IntegralW::whereDate('created_at',$date)
  344. ->select('store_id',DB::raw('sum(ifnull(inte.num,0)) as num'),DB::raw('sum(ifnull(inte.integral,0)) as integral'))
  345. ->groupBy('store_id')
  346. ->having('num','>=',2)
  347. ->get();
  348. foreach($inte as $key=>$val){
  349. IntegralW::whereDate('created_at',$date)->where('store_id',$val->store_id)->where('type',1)->update([
  350. 'integral_double'=>DB::raw('2*integral')
  351. ]);
  352. }
  353. }
  354. }