Order.php 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767
  1. <?php
  2. /**
  3. * @package merchant
  4. *
  5. * @author Qinii
  6. * @day 2020-06-10
  7. *
  8. *
  9. */
  10. namespace app\controller\merchant\store\order;
  11. use app\common\repositories\store\ExcelRepository;
  12. use app\common\repositories\store\order\MerchantReconciliationRepository;
  13. use app\common\repositories\store\order\StoreGroupOrderRepository;
  14. use app\common\repositories\store\order\StoreOrderRepository;
  15. use crmeb\exceptions\UploadException;
  16. use crmeb\services\ExcelService;
  17. use crmeb\services\SpreadsheetExcelService;
  18. use think\App;
  19. use crmeb\basic\BaseController;
  20. use app\common\repositories\store\order\StoreOrderRepository as repository;
  21. use think\facade\Db;
  22. use think\facade\Log;
  23. class Order extends BaseController
  24. {
  25. protected $repository;
  26. /**
  27. * Product constructor.
  28. * @param App $app
  29. * @param repository $repository
  30. */
  31. public function __construct(App $app, repository $repository)
  32. {
  33. parent::__construct($app);
  34. $this->repository = $repository;
  35. }
  36. /**
  37. * 订单列表
  38. * @return mixed
  39. * @author Qinii
  40. */
  41. public function lst()
  42. {
  43. [$page, $limit] = $this->getPage();
  44. $where = $this->request->params(['status', 'date', 'order_sn', 'username', 'order_type', 'distribution_mode', 'keywords', 'pay_type', 'paid', 'order_ids', 'is_red_ylj', 'gzc', 'ylj_status']);
  45. $where['mer_id'] = $this->request->merId();
  46. return app('json')->success($this->repository->merchantGetListYhc($where, $page, $limit));
  47. }
  48. /**
  49. * @return mixed
  50. * @throws \think\db\exception\DataNotFoundException
  51. * @throws \think\db\exception\DbException
  52. * @throws \think\db\exception\ModelNotFoundException
  53. * @author xaboy
  54. * @day 2020/6/10
  55. */
  56. public function userLst()
  57. {
  58. [$page, $limit] = $this->getPage();
  59. $where = [
  60. 'status' => (int)$this->request->get('status', 0),
  61. 'mer_id' => $this->request->merId()
  62. ];
  63. if (input('uid')) {
  64. $where['uid'] = input('uid');
  65. }
  66. return app('json')->success($this->repository->getList($where, $page, $limit));
  67. }
  68. /**
  69. * TODO 自提订单列表
  70. * @return mixed
  71. * @author Qinii
  72. * @day 2020-08-17
  73. */
  74. public function takeLst()
  75. {
  76. [$page, $limit] = $this->getPage();
  77. $where = $this->request->params(['date', 'order_sn', 'username']);
  78. $where['take_order'] = 1;
  79. $where['status'] = -1;
  80. $where['verify_date'] = $where['date'];
  81. unset($where['date']);
  82. $where['mer_id'] = $this->request->merId();
  83. return app('json')->success($this->repository->merchantGetList($where, $page, $limit));
  84. }
  85. /**
  86. * 订单头部统计
  87. * @return mixed
  88. * @author Qinii
  89. */
  90. public function chart()
  91. {
  92. return app('json')->success($this->repository->merchantOrderNumber($this->request->merId()));
  93. }
  94. /**
  95. * TODO 自提订单头部统计
  96. * @return mixed
  97. * @author Qinii
  98. * @day 2020-08-17
  99. */
  100. public function takeChart()
  101. {
  102. return app('json')->success($this->repository->merchantOrderNumber($this->request->merId(),1));
  103. }
  104. /**
  105. * TODO 订单类型
  106. * @return mixed
  107. * @author Qinii
  108. * @day 2020-08-15
  109. */
  110. public function orderType()
  111. {
  112. $where['mer_id'] = $this->request->merId();
  113. return app('json')->success($this->repository->orderType($where));
  114. }
  115. /**
  116. * @param $id
  117. * @return mixed
  118. * @author Qinii
  119. */
  120. public function deliveryForm($id)
  121. {
  122. if(!$this->repository->merDeliveryExists($id,$this->request->merId()))
  123. return app('json')->fail('订单信息或状态错误');
  124. return app('json')->success(formToData($this->repository->sendProductForm($id)));
  125. }
  126. /**
  127. * @param $id
  128. * @return mixed
  129. * @author Qinii
  130. */
  131. public function delivery($id)
  132. {
  133. if(!$this->repository->merDeliveryExists($id,$this->request->merId()))
  134. return app('json')->fail('订单信息或状态错误');
  135. $data = $this->request->params(['delivery_type','delivery_name','delivery_id']);
  136. if(preg_match('/([\x81-\xfe][\x40-\xfe])/',$data['delivery_id'])) return app('json')->fail('请输入正确的单号/电话');
  137. $this->repository->delivery($id,$data);
  138. return app('json')->success('发货成功');
  139. }
  140. /**
  141. * @param $id
  142. * @return mixed
  143. * @author Qinii
  144. * @day 2020-06-11
  145. */
  146. public function updateForm($id)
  147. {
  148. if(!$this->repository->merStatusExists($id,$this->request->merId()))
  149. return app('json')->fail('订单信息或状态错误');
  150. return app('json')->success(formToData($this->repository->form($id)));
  151. }
  152. /**
  153. * @param $id
  154. * @return mixed
  155. * @author Qinii
  156. * @day 2020-06-11
  157. */
  158. public function update($id)
  159. {
  160. $data = $this->request->params(['total_price','pay_price','total_postage','pay_postage']);
  161. if($data['total_price'] < 0 || $data['pay_price'] < 0 || $data['total_postage'] < 0 || $data['pay_postage'] < 0 )
  162. return app('json')->fail('金额不可未负数');
  163. if(!$this->repository->merStatusExists($id,$this->request->merId()))
  164. return app('json')->fail('订单信息或状态错误');
  165. $this->repository->eidt($id,$data);
  166. return app('json')->success('修改成功');
  167. }
  168. /**
  169. * @param $id
  170. * @return mixed
  171. * @author Qinii
  172. * @day 2020-06-11
  173. */
  174. public function detail($id)
  175. {
  176. $data = $this->repository->getOne($id,$this->request->merId());
  177. if(!$data)
  178. return app('json')->fail('数据不存在');
  179. return app('json')->success($data);
  180. }
  181. /**
  182. * @param $id
  183. * @return mixed
  184. * @author Qinii
  185. * @day 2020-06-11
  186. */
  187. public function status($id)
  188. {
  189. [$page, $limit] = $this->getPage();
  190. if(!$this->repository->getOne($id,$this->request->merId()))
  191. return app('json')->fail('数据不存在');
  192. return app('json')->success($this->repository->getOrderStatus($id,$page, $limit));
  193. }
  194. /**
  195. * @param $id
  196. * @return mixed
  197. * @author Qinii
  198. * @day 2020-06-11
  199. */
  200. public function remarkForm($id)
  201. {
  202. return app('json')->success(formToData($this->repository->remarkForm($id)));
  203. }
  204. /**
  205. * @param $id
  206. * @return mixed
  207. * @author Qinii
  208. * @day 2020-06-11
  209. */
  210. public function remark($id)
  211. {
  212. if(!$this->repository->getOne($id,$this->request->merId()))
  213. return app('json')->fail('数据不存在');
  214. $data = $this->request->params(['remark']);
  215. $this->repository->update($id,$data);
  216. return app('json')->success('备注成功');
  217. }
  218. /**
  219. * 核销
  220. * @param $code
  221. * @author xaboy
  222. * @day 2020/8/15
  223. */
  224. public function verify($code)
  225. {
  226. $this->repository->verifyOrder($code, $this->request->merId(), 0);
  227. return app('json')->success('订单核销成功');
  228. }
  229. /**
  230. * 自提
  231. * @param $code
  232. * @author xaboy
  233. * @day 2020/8/15
  234. */
  235. public function carry($code)
  236. {
  237. $order_id= input('order_id');
  238. $this->repository->carryOrder($code, $this->request->merId(), 0,$order_id);
  239. return app('json')->success('订单核销成功');
  240. }
  241. /**
  242. * 手动操作订单为 已自提 关单
  243. * @return mixed
  244. * @throws \think\db\exception\DataNotFoundException
  245. * @throws \think\db\exception\DbException
  246. * @throws \think\db\exception\ModelNotFoundException
  247. */
  248. public function carryByWithoutCode()
  249. {
  250. $order_id = input('order_id');
  251. $this->repository->carryOrder(null, $this->request->merId(), 0, $order_id);
  252. return app('json')->success('订单核销成功');
  253. }
  254. /**
  255. * @param $id
  256. * @return mixed
  257. * @author Qinii
  258. * @day 2020-06-11
  259. */
  260. public function delete($id)
  261. {
  262. if(!$this->repository->userDelExists($id,$this->request->merId()))
  263. return app('json')->fail('订单信息或状态错误');
  264. $data['is_system_del'] = 1;
  265. $this->repository->update($id,$data);
  266. return app('json')->success('删除成功');
  267. }
  268. /**
  269. * TODO 快递查询
  270. * @param $id
  271. * @return mixed
  272. * @author Qinii
  273. * @day 2020-06-25
  274. */
  275. public function express($id)
  276. {
  277. if(!$this->repository->getWhereCount(['order_id' => $id,'mer_id' => $this->request->merId(),'delivery_type' =>1]))
  278. return app('json')->fail('订单信息或状态错误');
  279. return app('json')->success($this->repository->express($id));
  280. }
  281. /**
  282. * TODO
  283. * @param $id
  284. * @return mixed
  285. * @author Qinii
  286. * @day 2020-07-30
  287. */
  288. public function reList($id)
  289. {
  290. [$page,$limit] = $this->getPage();
  291. $make = app()->make(MerchantReconciliationRepository::class);
  292. if(!$make->getWhereCount(['mer_id' => $this->request->merId(),'reconciliation_id' => $id]))
  293. return app('json')->fail('数据不存在');
  294. $where = ['reconciliation_id' => $id,'type' => 0];
  295. return app('json')->success($this->repository->reconList($where,$page,$limit));
  296. }
  297. /**
  298. * TODO 导出文件
  299. * @author Qinii
  300. * @day 2020-07-30
  301. */
  302. public function excel()
  303. {
  304. $where = $this->request->params(['status', 'date', 'order_sn', 'username', 'order_type', 'keywords', 'pay_type', 'paid', 'order_ids', 'is_red_ylj', 'ylj_status']);
  305. $where['mer_id'] = $this->request->merId();
  306. $data=$this->repository->merchantGetListYhc($where, 1, 10000);
  307. if($data['count']<1){
  308. return app('json')->fail('没有数据');
  309. }
  310. $list=$this->orderList2($data['list']->toArray());
  311. // var_dump($list);die;
  312. app()->make(ExcelRepository::class)->create($list, $this->request->adminId(), 'order', $this->request->merId());
  313. return app('json')->success('开始导出数据');
  314. }
  315. public function orderList2(array $data){
  316. $result = [];
  317. if(empty($data)) return $result;
  318. $i = 1;
  319. // '序号','订单编号','收货人','商品信息','实际支付', '支付类型','支付方式','支付状态',订单状态'下单时间','养老金','养老金状态',
  320. foreach ($data as $item){
  321. if($item['status']==5){
  322. $item['pay_price']='0';
  323. }
  324. $item['leixing']='';
  325. $item['ddzt']='';
  326. $item['is_red_ylj_text']='未结算';
  327. if($item['paid']==1){
  328. if($item['pay_type']==0){
  329. $item['leixing']='虚拟支付';
  330. }else if ($item['pay_type']==1){
  331. $item['leixing']='微信支付';
  332. }else if ($item['pay_type']==5){
  333. $item['leixing']='红积分兑换';
  334. }
  335. }
  336. if($item['status'] == 5) $item['ddzt']='待付款';
  337. if($item['status'] == 0) $item['ddzt']='待发货';
  338. if($item['status'] == 1) $item['ddzt']='待收货';
  339. if($item['status'] == 2) $item['ddzt']='待评价';
  340. if($item['status'] == 3) $item['ddzt']='已完成';
  341. if($item['status'] == 4) $item['ddzt']='已申请退款';
  342. if($item['status'] == -1) $item['ddzt']='已退款';
  343. if($item['status'] == 6) $item['ddzt']='已取消';
  344. if($item['is_red_ylj']==1){
  345. $item['is_red_ylj_text']='待审核';
  346. }
  347. if($item['is_red_ylj']==2){
  348. $item['is_red_ylj_text']='已结算';
  349. }
  350. $item['sp']='';
  351. $item['sp_num']='';
  352. // <div v-for="(val, i ) in scope.row.orderProduct" :key="i" class="tabBox acea-row row-middle">
  353. // <div class="demo-image__preview">
  354. // <el-image :src="val.cart_info.product.image" :preview-src-list="[val.cart_info.product.image]" />
  355. // </div>
  356. // <span class="tabBox_tit">{{ val.cart_info.product.store_name + ' | ' }}{{ val.cart_info.productAttr.sku }}</span>
  357. // <span class="tabBox_pice">
  358. // {{ '¥'+ val.cart_info.productAttr.price + ' x '+ val.product_num }}
  359. // <em v-if="val.refund_num < val.product_num && val.refund_num > 0" style="color: red;font-style: normal;">(-{{ val.product_num - val.refund_num }})</em>
  360. // </span>
  361. // </div>
  362. foreach ($item['orderProduct'] as $vv){
  363. $item['sp'].=$vv['cart_info']['product']['store_name'].' ';
  364. $item['sp_num'].=$vv['cart_info']['productAttr']['price'].' x' . $vv['product_num'].' ';
  365. }
  366. $result[] = [
  367. $i,
  368. $item['order_sn'],
  369. $item['real_name'],
  370. $item['user_phone'],
  371. $item['sp'],
  372. $item['sp_num'],
  373. $item['pay_price'],
  374. $item['leixing'],
  375. $item['paid'] ? '已支付':'未支付',
  376. $item['ddzt'],
  377. $item['create_time'] ,
  378. $item['ylj'] ,
  379. $item['is_red_ylj_text'] ,
  380. ];
  381. $i++;
  382. }
  383. return $result;
  384. }
  385. /**
  386. * TODO 打印小票
  387. * @param $id
  388. * @return mixed
  389. * @author Qinii
  390. * @day 2020-07-30
  391. */
  392. public function printer($id)
  393. {
  394. $merId = $this->request->merId();
  395. if(!merchantConfig($merId,'printing_status'))
  396. return app('json')->fail('未开启打印机设置');
  397. if(!$this->repository->getOne($id,$merId))
  398. return app('json')->fail('数据不存在');
  399. $this->repository->printer($id,$merId);
  400. return app('json')->success('打印成功');
  401. }
  402. /**
  403. * @param $id
  404. * @param StoreGroupOrderRepository $groupOrderRepository
  405. * @param repository $orderRepository
  406. * @return mixed
  407. * @throws \think\db\exception\DataNotFoundException
  408. * @throws \think\db\exception\DbException
  409. * @throws \think\db\exception\ModelNotFoundException
  410. * @author xaboy
  411. * @day 2020/6/10
  412. */
  413. public function cancelGroupOrder($id, StoreGroupOrderRepository $groupOrderRepository, StoreOrderRepository $orderRepository)
  414. {
  415. $data = $this->request->params(['change_message', 'remark']);
  416. $merId = $this->request->merId();
  417. $order = $orderRepository->getWhere(['group_order_id' => $id]);
  418. if ($order['mer_id'] != $merId)
  419. return app('json')->fail('数据不存在');
  420. try {
  421. $groupOrderRepository->cancel((int)$id, 0, $data['change_message'], $data['remark']);
  422. } catch (\Throwable $e) {
  423. Log::error('关闭订单失败:' . json_encode([
  424. 'error_message' => $e->getMessage(),
  425. 'error_file' => $e->getFile(),
  426. 'error_line' => $e->getLine(),
  427. 'error_code' => $e->getCode(),
  428. 'exception_class' => get_class($e),
  429. 'trace' => $e->getTraceAsString()
  430. ], JSON_UNESCAPED_UNICODE));
  431. return app('json')->fail('关闭订单失败');
  432. }
  433. return app('json')->success('取消成功');
  434. }
  435. //设置成支付成功
  436. public function order_zhifu($id)
  437. {
  438. $admin_id = $this->request->adminId();
  439. $admin_info = Db::name('merchant_admin')->where('merchant_admin_id',$admin_id)->find();
  440. if(!$admin_info){
  441. return app('json')->fail('没有权限');
  442. }
  443. if($admin_info['roles'] && !in_array('53',explode(',',$admin_info['roles']))){
  444. return app('json')->fail('没有权限');
  445. }
  446. $domain = $this->request->domain();
  447. $order=Db::name('store_order')->find($id);
  448. if(!$order){
  449. return app('json')->fail('数据不存在');
  450. }
  451. if($order['paid']!=0 || $order['status']!=5){
  452. return app('json')->fail('订单不是待支付状态');
  453. }
  454. // $pay_type=5;
  455. $group_order=Db::name('store_group_order') -> where('group_order_id',$order['group_order_id']) -> find();
  456. Db::name('store_group_order') -> where('group_order_id',$order['group_order_id']) -> update(['rand'=>mt_rand(1000,9999)]);
  457. $orderSn = $group_order['group_order_sn'];
  458. $groupOrder = app()->make(StoreGroupOrderRepository::class)->getWhere(['group_order_sn' => $orderSn]);
  459. if (!$groupOrder || $groupOrder->paid == 1) return;
  460. $total_price=$order['total_price']+$order['youhui_price'];
  461. $pay_price=$order['pay_price']+$order['youhui_price'];
  462. Db::name('store_order')->where('order_id',$id)->update(['total_price'=>$total_price,'pay_price'=>$pay_price,'youhui_price'=>0]);
  463. app()->make(StoreOrderRepository::class)->paySuccess($groupOrder);
  464. return app('json')->success('设置成功');
  465. // event('pay_success_order', ['order_sn' => $requestId[0],'pay_type'=>$pay_type]);
  466. // dump('成功');
  467. // dump($this->randomFromDev(20));
  468. }
  469. /**
  470. * 判断是不是红积分支付的订单
  471. * @return mixed
  472. */
  473. public function order_red_num()
  474. {
  475. $order_ids=input('order_ids','');
  476. $merId = $this->request->merId();
  477. $where['o.paid']=1;
  478. // $where['o.pay_type'] = 5;
  479. $where['o.is_red_ylj']=0;
  480. $where['o.mer_id']=$merId;
  481. $where['u.commission_type']=5;
  482. $where['u.gzc']=3;
  483. $where['u.status']=0;
  484. $order=Db::name('store_order')
  485. ->alias('o')
  486. ->join('user_bill u','u.order_sn=o.order_sn')
  487. ->where($where)
  488. ->whereIN('o.pay_type',[0,5])
  489. ->whereIn('o.order_id',$order_ids)
  490. ->column('o.order_id,o.order_sn,u.number,u.mark');
  491. if($order){
  492. $data['num']=count($order);
  493. $order_ids2=[];
  494. $ylj=0;
  495. foreach ($order as $v){
  496. $order_ids2[]=$v['order_id'];
  497. $ylj+=$v['number'];
  498. }
  499. $data['order_ids']=implode(',',$order_ids2);
  500. $data['ylj']=$ylj;
  501. $data['company_name']='北京天地博爱';
  502. $data['company_bank_name']='开户行开户行开户行开户行';
  503. $data['company_bank_card']='银行卡号银行卡号银行卡号银行卡号';
  504. return app('json')->success('ok',$data);
  505. }
  506. return app('json')->fail('只能结算红积分和虚拟支付类型的订单~');
  507. }
  508. /**
  509. * 养老金提交打款凭证
  510. * @return mixed
  511. */
  512. public function order_red_add()
  513. {
  514. $order_ids=input('order_ids','');
  515. $pics=input('pics','');
  516. if($order_ids==''){
  517. return app('json')->fail('只能结算红积分和虚拟支付类型的订单~');
  518. }
  519. if($pics==''){
  520. return app('json')->fail('请上传打款凭证');
  521. }
  522. $merId = $this->request->merId();
  523. $where['o.paid']=1;
  524. // $where['o.pay_type'] = 5;
  525. $where['o.is_red_ylj']=0;
  526. $where['o.mer_id']=$merId;
  527. $where['u.commission_type']=5;
  528. $where['u.gzc']=3;
  529. $where['u.status']=0;
  530. $order=Db::name('store_order')
  531. ->alias('o')
  532. ->join('user_bill u','u.order_sn=o.order_sn')
  533. ->where($where)
  534. ->whereIN('o.pay_type',[0,5])
  535. ->whereIn('o.order_id',$order_ids)
  536. ->column('o.order_id,o.order_sn,u.number,u.mark');
  537. if($order){
  538. $data['num']=count($order);
  539. $order_ids2=[];
  540. $ylj=0;
  541. foreach ($order as $v){
  542. $order_ids2[]=$v['order_id'];
  543. $ylj+=$v['number'];
  544. }
  545. $data['order_ids']=implode(',',$order_ids2);
  546. $data['ylj']=$ylj;
  547. $data['pics']=$pics;
  548. $data['mer_id']=$merId;
  549. $re=Db::name('enterprise_red_order_voucher')->insert($data);
  550. Db::name('store_order')->whereIn('order_id',$order_ids2)->where('is_red_ylj',0)->update(['is_red_ylj'=>1]);
  551. if($re){
  552. return app('json')->success('提交成功');
  553. }
  554. return app('json')->fail('提交失败');
  555. }
  556. return app('json')->fail('只能结算红积分和虚拟支付类型的订单~');
  557. }
  558. /**
  559. * 养老金打款凭证 列表
  560. * @return mixed
  561. * @throws \think\db\exception\DataNotFoundException
  562. * @throws \think\db\exception\DbException
  563. * @throws \think\db\exception\ModelNotFoundException
  564. */
  565. public function order_red_list()
  566. {
  567. [$page, $limit] = $this->getPage();
  568. $where = $this->request->params(['status', 'date']);
  569. $where['mer_id'] = $this->request->merId();
  570. $query =Db::name('enterprise_red_order_voucher')->when(isset($where['date']) && $where['date'] !== '', function ($query) use ($where) {
  571. getModelTime($query, $where['date']);
  572. })->when(isset($where['status']) && $where['status'] !== '', function ($query) use ($where) {
  573. $query->where('status',$where['status']);
  574. });
  575. $data['count'] = $query->count();
  576. $data['list'] = $query->page($page, $limit)->order('id desc')->select();
  577. return app('json')->success('ok',$data);
  578. }
  579. public function reward_list()
  580. {
  581. [$page, $limit] = $this->getPage();
  582. $where = $this->request->params(['type', 'account', 'date','identity']);
  583. $where['b.mer_id'] = $this->request->merId();
  584. $query =Db::name('enterprise_user_bill')->alias('b')->join('user u','u.uid=b.uid')->join('enterprise_user eu','eu.uid=b.uid')->field('b.*,u.nickname,u.account,eu.identity')
  585. ->when(isset($where['date']) && $where['date'] !== '', function ($query) use ($where) {
  586. getModelTime($query, $where['date'],'b.create_time');
  587. })->when(isset($where['type']) && $where['type'] !== '', function ($query) use ($where) {
  588. $query->where('b.type',$where['type']);
  589. })->when(isset($where['identity']) && $where['identity'] !== '', function ($query) use ($where) {
  590. $query->where('eu.identity',$where['identity']);
  591. })->when(isset($where['account']) && $where['account'] !== '', function ($query) use ($where) {
  592. $query->whereLike('u.account','%'.$where['account'].'%');
  593. });
  594. $query->where('type','<>',8);
  595. $data['count'] = $query->count();
  596. $data['list'] = $query->page($page, $limit)->order('b.enterprise_user_bill desc')->select()->each(function ($v) use ($where){
  597. if($v['identity']==0){
  598. $identity_name='普通用户';
  599. }else if ($v['identity']==100){
  600. $identity_name='合伙人';
  601. }else{
  602. $identity_name=Db::name('enterprise_performance_divvy')->where('state',1)->where('mer_id', $where['b.mer_id'])->where('id',$v['identity'])->value('name');
  603. }
  604. $v['identity_name']=$identity_name?$identity_name:'普通用户';
  605. return $v;
  606. });
  607. $count = $query->whereIn('type', [1,2,3,4,6,7])->sum('money');
  608. $query_sql_1 = clone $query;
  609. $query_sql_2 = clone $query;
  610. $query_sql_3 = clone $query;
  611. $query_sql_4 = clone $query;
  612. $query_sql_6 = clone $query;
  613. $query_sql_7 = clone $query;
  614. //type 1-推荐奖 2-业绩奖 3-服务中心分红 4-推荐代理商收益 5- 消费增值
  615. $count1 = $query_sql_1->where('type', 1)->sum('money');
  616. $count2 = $query_sql_2->where('type', 2)->sum('money');
  617. $count3 = $query_sql_3->where('type', 3)->sum('money');
  618. $count4 = $query_sql_4->where('type', 4)->sum('money');
  619. $count6 = $query_sql_6->where('type', 6)->sum('money');
  620. $count7 = $query_sql_7->where('type', 7)->sum('money');
  621. $data['countmoyer']=[
  622. ['className' => 'el-icon-s-goods', 'count' => $count, 'field' => '元', 'name' => '奖励总额', 'info' => '奖励总额 = 分享奖+业绩奖+代理商分红+推荐代理商收益+运营中心收益+人工奖励佣金'],
  623. ['className' => 'el-icon-s-goods', 'count' => $count1, 'field' => '元', 'name' => '分享奖', 'info' => ''],
  624. ['className' => 'el-icon-s-goods', 'count' => $count2, 'field' => '元', 'name' => '业绩奖', 'info' => ''],
  625. ['className' => 'el-icon-s-goods', 'count' => $count3, 'field' => '元', 'name' => '代理商分红', 'info' => ''],
  626. ['className' => 'el-icon-s-goods', 'count' => $count4, 'field' => '元', 'name' => '推荐代理商收益', 'info' => ''],
  627. ['className' => 'el-icon-s-goods', 'count' => $count6, 'field' => '元', 'name' => '运营中心收益', 'info' => ''],
  628. ['className' => 'el-icon-s-goods', 'count' => $count7, 'field' => '元', 'name' => '人工奖励佣金', 'info' => ''],
  629. ];
  630. return app('json')->success('ok', $data);
  631. }
  632. //商户后台-奖励统计 导出功能
  633. public function reward_excel()
  634. {
  635. $where = $this->request->params(['type', 'account', 'date', 'identity']);
  636. $where['b.mer_id'] = $this->request->merId();
  637. $query = Db::name('enterprise_user_bill')->alias('b')->join('user u', 'u.uid=b.uid')->join('enterprise_user eu', 'eu.uid=b.uid')->field('b.*,u.nickname,u.account,eu.identity')
  638. ->when(isset($where['date']) && $where['date'] !== '', function ($query) use ($where) {
  639. getModelTime($query, $where['date'], 'b.create_time');
  640. })->when(isset($where['type']) && $where['type'] !== '', function ($query) use ($where) {
  641. $query->where('b.type', $where['type']);
  642. })->when(isset($where['identity']) && $where['identity'] !== '', function ($query) use ($where) {
  643. $query->where('eu.identity', $where['identity']);
  644. })->when(isset($where['account']) && $where['account'] !== '', function ($query) use ($where) {
  645. $query->whereLike('u.account', '%' . $where['account'] . '%');
  646. });
  647. $list = $query->order('b.enterprise_user_bill desc')->select();
  648. foreach ($list as $k=>$v){
  649. $v['type_name']='';
  650. $v['identity_name']='';
  651. if($v['type'] == 1) $v['type_name']='分享奖';
  652. if($v['type'] == 2) $v['type_name']='业绩奖';
  653. if($v['type'] == 3) $v['type_name']='代理商分红';
  654. if($v['type'] == 4) $v['type_name']='推荐代理商收益';
  655. if($v['type'] == 5) $v['type_name']='消费增值系统佣金';
  656. if($v['identity'] == 0){
  657. $v['identity_name']='普通用户';
  658. }else if($v['identity'] == 100){
  659. $v['identity_name']='合伙人';
  660. }else{
  661. $identity_name=Db::name('enterprise_performance_divvy')->where('state',1)->where('mer_id', $where['b.mer_id'])->where('id',$v['identity'])->value('name');
  662. $v['identity_name']=$identity_name?$identity_name:'普通用户';
  663. }
  664. $arr[]=array(
  665. $v['enterprise_user_bill'],
  666. $v['create_time'],
  667. $v['nickname'],
  668. $v['account'],
  669. $v['type_name'],
  670. $v['money'],
  671. $v['identity_name'],
  672. $v['title'],
  673. );
  674. }
  675. $header = [
  676. 'ID','创建时间','用户昵称','用户账号(手机号)','佣金名称','金额(元)','当前身份','奖励来源'
  677. ];
  678. $filename = '奖励统计'.date('YmdHis').'_'.rand(10000,99999);
  679. $title = '奖励统计';
  680. $name = '奖励统计';
  681. $info = '生成时间:' . date('Y-m-d H:i:s',time());
  682. $suffix='xlsx';
  683. $path='extract';
  684. unset($_instance);
  685. $_instance = SpreadsheetExcelService::instance_xin();
  686. $_path =$_instance
  687. ->createOrActive()
  688. ->setExcelHeader($header)
  689. ->setExcelTile($title, $name, $info)
  690. ->setExcelContent($arr)
  691. ->excelSave($filename, $suffix, $path);
  692. $data= [
  693. 'name' => $filename.'.'.$suffix,
  694. 'status' => 1,
  695. 'path' => '/'.$_path
  696. ];
  697. return app('json')->success('ok',$data);
  698. }
  699. }