Order.php 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771
  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. // app()->make(ExcelRepository::class)->create($list, $this->request->adminId(), 'order', $this->request->merId());
  312. $adminId = $this->request->adminId();
  313. $excelLog = app()->make(ExcelRepository::class)->createNoJob($list, $adminId, 'order', $where['mer_id']);
  314. app()->make(ExcelService::class)->order($list,$excelLog->excel_id);
  315. return app('json')->success('开始导出数据');
  316. }
  317. public function orderList2(array $data){
  318. $result = [];
  319. if(empty($data)) return $result;
  320. $i = 1;
  321. // '序号','订单编号','收货人','商品信息','实际支付', '支付类型','支付方式','支付状态',订单状态'下单时间','养老金','养老金状态',
  322. foreach ($data as $item){
  323. if($item['status']==5){
  324. $item['pay_price']='0';
  325. }
  326. $item['leixing']='';
  327. $item['ddzt']='';
  328. $item['is_red_ylj_text']='未结算';
  329. if($item['paid']==1){
  330. if($item['pay_type']==0){
  331. $item['leixing']='虚拟支付';
  332. }else if ($item['pay_type']==1){
  333. $item['leixing']='微信支付';
  334. }else if ($item['pay_type']==5){
  335. $item['leixing']='红积分兑换';
  336. }
  337. }
  338. if($item['status'] == 5) $item['ddzt']='待付款';
  339. if($item['status'] == 0) $item['ddzt']='待发货';
  340. if($item['status'] == 1) $item['ddzt']='待收货';
  341. if($item['status'] == 2) $item['ddzt']='待评价';
  342. if($item['status'] == 3) $item['ddzt']='已完成';
  343. if($item['status'] == 4) $item['ddzt']='已申请退款';
  344. if($item['status'] == -1) $item['ddzt']='已退款';
  345. if($item['status'] == 6) $item['ddzt']='已取消';
  346. if($item['is_red_ylj']==1){
  347. $item['is_red_ylj_text']='待审核';
  348. }
  349. if($item['is_red_ylj']==2){
  350. $item['is_red_ylj_text']='已结算';
  351. }
  352. $item['sp']='';
  353. $item['sp_num']='';
  354. // <div v-for="(val, i ) in scope.row.orderProduct" :key="i" class="tabBox acea-row row-middle">
  355. // <div class="demo-image__preview">
  356. // <el-image :src="val.cart_info.product.image" :preview-src-list="[val.cart_info.product.image]" />
  357. // </div>
  358. // <span class="tabBox_tit">{{ val.cart_info.product.store_name + ' | ' }}{{ val.cart_info.productAttr.sku }}</span>
  359. // <span class="tabBox_pice">
  360. // {{ '¥'+ val.cart_info.productAttr.price + ' x '+ val.product_num }}
  361. // <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>
  362. // </span>
  363. // </div>
  364. foreach ($item['orderProduct'] as $vv){
  365. $item['sp'].=$vv['cart_info']['product']['store_name'].' ';
  366. $item['sp_num'].=$vv['cart_info']['productAttr']['price'].' x' . $vv['product_num'].' ';
  367. }
  368. $result[] = [
  369. $i,
  370. $item['order_sn'],
  371. $item['real_name'],
  372. $item['user_phone'],
  373. $item['sp'],
  374. $item['sp_num'],
  375. $item['pay_price'],
  376. $item['leixing'],
  377. $item['paid'] ? '已支付':'未支付',
  378. $item['ddzt'],
  379. $item['create_time'] ,
  380. $item['ylj'] ,
  381. $item['is_red_ylj_text'] ,
  382. ];
  383. $i++;
  384. }
  385. return $result;
  386. }
  387. /**
  388. * TODO 打印小票
  389. * @param $id
  390. * @return mixed
  391. * @author Qinii
  392. * @day 2020-07-30
  393. */
  394. public function printer($id)
  395. {
  396. $merId = $this->request->merId();
  397. if(!merchantConfig($merId,'printing_status'))
  398. return app('json')->fail('未开启打印机设置');
  399. if(!$this->repository->getOne($id,$merId))
  400. return app('json')->fail('数据不存在');
  401. $this->repository->printer($id,$merId);
  402. return app('json')->success('打印成功');
  403. }
  404. /**
  405. * @param $id
  406. * @param StoreGroupOrderRepository $groupOrderRepository
  407. * @param repository $orderRepository
  408. * @return mixed
  409. * @throws \think\db\exception\DataNotFoundException
  410. * @throws \think\db\exception\DbException
  411. * @throws \think\db\exception\ModelNotFoundException
  412. * @author xaboy
  413. * @day 2020/6/10
  414. */
  415. public function cancelGroupOrder($id, StoreGroupOrderRepository $groupOrderRepository, StoreOrderRepository $orderRepository)
  416. {
  417. $data = $this->request->params(['change_message', 'remark']);
  418. $merId = $this->request->merId();
  419. $order = $orderRepository->getWhere(['group_order_id' => $id]);
  420. if ($order['mer_id'] != $merId)
  421. return app('json')->fail('数据不存在');
  422. try {
  423. $groupOrderRepository->cancel((int)$id, 0, $data['change_message'], $data['remark']);
  424. } catch (\Throwable $e) {
  425. Log::error('关闭订单失败:' . json_encode([
  426. 'error_message' => $e->getMessage(),
  427. 'error_file' => $e->getFile(),
  428. 'error_line' => $e->getLine(),
  429. 'error_code' => $e->getCode(),
  430. 'exception_class' => get_class($e),
  431. 'trace' => $e->getTraceAsString()
  432. ], JSON_UNESCAPED_UNICODE));
  433. return app('json')->fail('关闭订单失败');
  434. }
  435. return app('json')->success('取消成功');
  436. }
  437. //设置成支付成功
  438. public function order_zhifu($id)
  439. {
  440. $admin_id = $this->request->adminId();
  441. $admin_info = Db::name('merchant_admin')->where('merchant_admin_id',$admin_id)->find();
  442. if(!$admin_info){
  443. return app('json')->fail('没有权限');
  444. }
  445. if($admin_info['roles'] && !in_array('53',explode(',',$admin_info['roles']))){
  446. return app('json')->fail('没有权限');
  447. }
  448. $domain = $this->request->domain();
  449. $order=Db::name('store_order')->find($id);
  450. if(!$order){
  451. return app('json')->fail('数据不存在');
  452. }
  453. if($order['paid']!=0 || $order['status']!=5){
  454. return app('json')->fail('订单不是待支付状态');
  455. }
  456. // $pay_type=5;
  457. $group_order=Db::name('store_group_order') -> where('group_order_id',$order['group_order_id']) -> find();
  458. Db::name('store_group_order') -> where('group_order_id',$order['group_order_id']) -> update(['rand'=>mt_rand(1000,9999)]);
  459. $orderSn = $group_order['group_order_sn'];
  460. $groupOrder = app()->make(StoreGroupOrderRepository::class)->getWhere(['group_order_sn' => $orderSn]);
  461. if (!$groupOrder || $groupOrder->paid == 1) return;
  462. $total_price=$order['total_price']+$order['youhui_price'];
  463. $pay_price=$order['pay_price']+$order['youhui_price'];
  464. Db::name('store_order')->where('order_id',$id)->update(['total_price'=>$total_price,'pay_price'=>$pay_price,'youhui_price'=>0]);
  465. app()->make(StoreOrderRepository::class)->paySuccess($groupOrder);
  466. return app('json')->success('设置成功');
  467. // event('pay_success_order', ['order_sn' => $requestId[0],'pay_type'=>$pay_type]);
  468. // dump('成功');
  469. // dump($this->randomFromDev(20));
  470. }
  471. /**
  472. * 判断是不是红积分支付的订单
  473. * @return mixed
  474. */
  475. public function order_red_num()
  476. {
  477. $order_ids=input('order_ids','');
  478. $merId = $this->request->merId();
  479. $where['o.paid']=1;
  480. // $where['o.pay_type'] = 5;
  481. $where['o.is_red_ylj']=0;
  482. $where['o.mer_id']=$merId;
  483. $where['u.commission_type']=5;
  484. $where['u.gzc']=3;
  485. $where['u.status']=0;
  486. $order=Db::name('store_order')
  487. ->alias('o')
  488. ->join('user_bill u','u.order_sn=o.order_sn')
  489. ->where($where)
  490. ->whereIN('o.pay_type',[0,5])
  491. ->whereIn('o.order_id',$order_ids)
  492. ->column('o.order_id,o.order_sn,u.number,u.mark');
  493. if($order){
  494. $data['num']=count($order);
  495. $order_ids2=[];
  496. $ylj=0;
  497. foreach ($order as $v){
  498. $order_ids2[]=$v['order_id'];
  499. $ylj+=$v['number'];
  500. }
  501. $data['order_ids']=implode(',',$order_ids2);
  502. $data['ylj']=$ylj;
  503. $data['company_name']='北京天地博爱';
  504. $data['company_bank_name']='开户行开户行开户行开户行';
  505. $data['company_bank_card']='银行卡号银行卡号银行卡号银行卡号';
  506. return app('json')->success('ok',$data);
  507. }
  508. return app('json')->fail('只能结算红积分和虚拟支付类型的订单~');
  509. }
  510. /**
  511. * 养老金提交打款凭证
  512. * @return mixed
  513. */
  514. public function order_red_add()
  515. {
  516. $order_ids=input('order_ids','');
  517. $pics=input('pics','');
  518. if($order_ids==''){
  519. return app('json')->fail('只能结算红积分和虚拟支付类型的订单~');
  520. }
  521. if($pics==''){
  522. return app('json')->fail('请上传打款凭证');
  523. }
  524. $merId = $this->request->merId();
  525. $where['o.paid']=1;
  526. // $where['o.pay_type'] = 5;
  527. $where['o.is_red_ylj']=0;
  528. $where['o.mer_id']=$merId;
  529. $where['u.commission_type']=5;
  530. $where['u.gzc']=3;
  531. $where['u.status']=0;
  532. $order=Db::name('store_order')
  533. ->alias('o')
  534. ->join('user_bill u','u.order_sn=o.order_sn')
  535. ->where($where)
  536. ->whereIN('o.pay_type',[0,5])
  537. ->whereIn('o.order_id',$order_ids)
  538. ->column('o.order_id,o.order_sn,u.number,u.mark');
  539. if($order){
  540. $data['num']=count($order);
  541. $order_ids2=[];
  542. $ylj=0;
  543. foreach ($order as $v){
  544. $order_ids2[]=$v['order_id'];
  545. $ylj+=$v['number'];
  546. }
  547. $data['order_ids']=implode(',',$order_ids2);
  548. $data['ylj']=$ylj;
  549. $data['pics']=$pics;
  550. $data['mer_id']=$merId;
  551. $re=Db::name('enterprise_red_order_voucher')->insert($data);
  552. Db::name('store_order')->whereIn('order_id',$order_ids2)->where('is_red_ylj',0)->update(['is_red_ylj'=>1]);
  553. if($re){
  554. return app('json')->success('提交成功');
  555. }
  556. return app('json')->fail('提交失败');
  557. }
  558. return app('json')->fail('只能结算红积分和虚拟支付类型的订单~');
  559. }
  560. /**
  561. * 养老金打款凭证 列表
  562. * @return mixed
  563. * @throws \think\db\exception\DataNotFoundException
  564. * @throws \think\db\exception\DbException
  565. * @throws \think\db\exception\ModelNotFoundException
  566. */
  567. public function order_red_list()
  568. {
  569. [$page, $limit] = $this->getPage();
  570. $where = $this->request->params(['status', 'date']);
  571. $where['mer_id'] = $this->request->merId();
  572. $query =Db::name('enterprise_red_order_voucher')->when(isset($where['date']) && $where['date'] !== '', function ($query) use ($where) {
  573. getModelTime($query, $where['date']);
  574. })->when(isset($where['status']) && $where['status'] !== '', function ($query) use ($where) {
  575. $query->where('status',$where['status']);
  576. });
  577. $data['count'] = $query->count();
  578. $data['list'] = $query->page($page, $limit)->order('id desc')->select();
  579. return app('json')->success('ok',$data);
  580. }
  581. public function reward_list()
  582. {
  583. [$page, $limit] = $this->getPage();
  584. $where = $this->request->params(['type', 'account', 'date','identity']);
  585. $where['b.mer_id'] = $this->request->merId();
  586. $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')
  587. ->when(isset($where['date']) && $where['date'] !== '', function ($query) use ($where) {
  588. getModelTime($query, $where['date'],'b.create_time');
  589. })->when(isset($where['type']) && $where['type'] !== '', function ($query) use ($where) {
  590. $query->where('b.type',$where['type']);
  591. })->when(isset($where['identity']) && $where['identity'] !== '', function ($query) use ($where) {
  592. $query->where('eu.identity',$where['identity']);
  593. })->when(isset($where['account']) && $where['account'] !== '', function ($query) use ($where) {
  594. $query->whereLike('u.account','%'.$where['account'].'%');
  595. });
  596. $query->where('type','<>',8);
  597. $data['count'] = $query->count();
  598. $data['list'] = $query->page($page, $limit)->order('b.enterprise_user_bill desc')->select()->each(function ($v) use ($where){
  599. if($v['identity']==0){
  600. $identity_name='普通用户';
  601. }else if ($v['identity']==100){
  602. $identity_name='合伙人';
  603. }else{
  604. $identity_name=Db::name('enterprise_performance_divvy')->where('state',1)->where('mer_id', $where['b.mer_id'])->where('id',$v['identity'])->value('name');
  605. }
  606. $v['identity_name']=$identity_name?$identity_name:'普通用户';
  607. return $v;
  608. });
  609. $count = $query->whereIn('type', [1,2,3,4,6,7])->sum('money');
  610. $query_sql_1 = clone $query;
  611. $query_sql_2 = clone $query;
  612. $query_sql_3 = clone $query;
  613. $query_sql_4 = clone $query;
  614. $query_sql_6 = clone $query;
  615. $query_sql_7 = clone $query;
  616. //type 1-推荐奖 2-业绩奖 3-服务中心分红 4-推荐代理商收益 5- 消费增值
  617. $count1 = $query_sql_1->where('type', 1)->sum('money');
  618. $count2 = $query_sql_2->where('type', 2)->sum('money');
  619. $count3 = $query_sql_3->where('type', 3)->sum('money');
  620. $count4 = $query_sql_4->where('type', 4)->sum('money');
  621. $count6 = $query_sql_6->where('type', 6)->sum('money');
  622. $count7 = $query_sql_7->where('type', 7)->sum('money');
  623. $data['countmoyer']=[
  624. ['className' => 'el-icon-s-goods', 'count' => $count, 'field' => '元', 'name' => '奖励总额', 'info' => '奖励总额 = 分享奖+业绩奖+代理商分红+推荐代理商收益+运营中心收益+人工奖励佣金'],
  625. ['className' => 'el-icon-s-goods', 'count' => $count1, 'field' => '元', 'name' => '分享奖', 'info' => ''],
  626. ['className' => 'el-icon-s-goods', 'count' => $count2, 'field' => '元', 'name' => '业绩奖', 'info' => ''],
  627. ['className' => 'el-icon-s-goods', 'count' => $count3, 'field' => '元', 'name' => '代理商分红', 'info' => ''],
  628. ['className' => 'el-icon-s-goods', 'count' => $count4, 'field' => '元', 'name' => '推荐代理商收益', 'info' => ''],
  629. ['className' => 'el-icon-s-goods', 'count' => $count6, 'field' => '元', 'name' => '运营中心收益', 'info' => ''],
  630. ['className' => 'el-icon-s-goods', 'count' => $count7, 'field' => '元', 'name' => '人工奖励佣金', 'info' => ''],
  631. ];
  632. return app('json')->success('ok', $data);
  633. }
  634. //商户后台-奖励统计 导出功能
  635. public function reward_excel()
  636. {
  637. $where = $this->request->params(['type', 'account', 'date', 'identity']);
  638. $where['b.mer_id'] = $this->request->merId();
  639. $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')
  640. ->when(isset($where['date']) && $where['date'] !== '', function ($query) use ($where) {
  641. getModelTime($query, $where['date'], 'b.create_time');
  642. })->when(isset($where['type']) && $where['type'] !== '', function ($query) use ($where) {
  643. $query->where('b.type', $where['type']);
  644. })->when(isset($where['identity']) && $where['identity'] !== '', function ($query) use ($where) {
  645. $query->where('eu.identity', $where['identity']);
  646. })->when(isset($where['account']) && $where['account'] !== '', function ($query) use ($where) {
  647. $query->whereLike('u.account', '%' . $where['account'] . '%');
  648. });
  649. $list = $query->order('b.enterprise_user_bill desc')->select();
  650. foreach ($list as $k=>$v){
  651. $v['type_name']='';
  652. $v['identity_name']='';
  653. if($v['type'] == 1) $v['type_name']='分享奖';
  654. if($v['type'] == 2) $v['type_name']='业绩奖';
  655. if($v['type'] == 3) $v['type_name']='代理商分红';
  656. if($v['type'] == 4) $v['type_name']='推荐代理商收益';
  657. if($v['type'] == 5) $v['type_name']='消费增值系统佣金';
  658. if($v['identity'] == 0){
  659. $v['identity_name']='普通用户';
  660. }else if($v['identity'] == 100){
  661. $v['identity_name']='合伙人';
  662. }else{
  663. $identity_name=Db::name('enterprise_performance_divvy')->where('state',1)->where('mer_id', $where['b.mer_id'])->where('id',$v['identity'])->value('name');
  664. $v['identity_name']=$identity_name?$identity_name:'普通用户';
  665. }
  666. $arr[]=array(
  667. $v['enterprise_user_bill'],
  668. $v['create_time'],
  669. $v['nickname'],
  670. $v['account'],
  671. $v['type_name'],
  672. $v['money'],
  673. $v['identity_name'],
  674. $v['title'],
  675. );
  676. }
  677. $header = [
  678. 'ID','创建时间','用户昵称','用户账号(手机号)','佣金名称','金额(元)','当前身份','奖励来源'
  679. ];
  680. $filename = '奖励统计'.date('YmdHis').'_'.rand(10000,99999);
  681. $title = '奖励统计';
  682. $name = '奖励统计';
  683. $info = '生成时间:' . date('Y-m-d H:i:s',time());
  684. $suffix='xlsx';
  685. $path='extract';
  686. unset($_instance);
  687. $_instance = SpreadsheetExcelService::instance_xin();
  688. $_path =$_instance
  689. ->createOrActive()
  690. ->setExcelHeader($header)
  691. ->setExcelTile($title, $name, $info)
  692. ->setExcelContent($arr)
  693. ->excelSave($filename, $suffix, $path);
  694. $data= [
  695. 'name' => $filename.'.'.$suffix,
  696. 'status' => 1,
  697. 'path' => '/'.$_path
  698. ];
  699. return app('json')->success('ok',$data);
  700. }
  701. }