Order.php 38 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | 萤火商城系统 [ 致力于通过产品和服务,帮助商家高效化开拓市场 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2017~2021 https://www.yiovo.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed 这不是一个自由软件,不允许对程序代码以任何形式任何目的的再发行
  8. // +----------------------------------------------------------------------
  9. // | Author: 萤火科技 <admin@yiovo.com>
  10. // +----------------------------------------------------------------------
  11. declare (strict_types = 1);
  12. namespace app\store\controller;
  13. use app\api\model\za\ZaActivityRelation as ZaActivityRelationModel;
  14. use app\api\service\order\Order as OrderService;
  15. use app\common\enum\order\Color as ColorEnum;
  16. use app\common\enum\order\orderGoods\DeliveryStatus as DeliveryStatusEnum;
  17. use app\common\enum\order\orderGoods\ExportStatus as ExportStatusEnum;
  18. use app\common\library\helper;
  19. use app\common\service\Export as ExportService;
  20. use app\store\model\groupbuy\GroupBuyHelp;
  21. use app\store\model\groupbuy\GroupBuyJoin;
  22. use app\store\model\OrderGoods as OrderGoodsModel;
  23. use app\store\model\Express as ExpressModel;
  24. use app\store\model\Order as OrderModel;
  25. use app\store\model\OrderGoodsPackage as OrderGoodsPackageModel;
  26. use app\store\model\OrderRefund as OrderRefundModel;
  27. use app\store\model\ActivityDiscount as ActivityDiscountModel;
  28. use app\common\model\OrderTj;
  29. use app\store\model\OrderAddress as OrderAddressModel;
  30. use app\store\model\shop\Shops;
  31. use think\facade\Db;
  32. use app\store\model\OrderRefund;
  33. /**
  34. * 订单管理
  35. * Class Order
  36. * @package app\store\controller
  37. */
  38. class Order extends Controller
  39. {
  40. /**
  41. * 订单列表
  42. * @param string $dataType
  43. * @return array
  44. */
  45. public function list(string $dataType = OrderModel::LIST_TYPE_ALL)
  46. {
  47. // 订单列表
  48. $model = new OrderModel;
  49. $list = $model->getList($dataType, $this->request->param());
  50. //获取订单颜色列表
  51. $colorArray = ColorEnum::data();
  52. return $this->renderSuccess(compact('dataType', 'list', 'colorArray'));
  53. }
  54. //订单详情
  55. public function getDetail(string $orderNo){
  56. $order_id = OrderModel::where('order_no',$orderNo)->value('order_id');
  57. if($order_id>0){
  58. $detail = $this->detail($order_id,false);
  59. foreach($detail['goods'] as &$row){
  60. $pass_refund_time = false;
  61. $refund_time = $row['refund_time'];
  62. if($refund_time>0&&time()>$refund_time){
  63. $pass_refund_time = true;
  64. }
  65. $row['pass_refund_time'] = $pass_refund_time;
  66. $refund = OrderRefundModel::where(['order_goods_id'=>$row['order_goods_id']])->find();
  67. $refund_state_text ='-';
  68. $is_refund = false;
  69. $is_refund_close = false;
  70. if($refund){
  71. $is_refund = true;
  72. $refund_state_text = $refund['state_text'];
  73. if($refund['status']==40||$refund['status']==30){
  74. $is_refund_close = true;
  75. }
  76. }
  77. $row['is_refund_close'] = $is_refund_close;
  78. $row['is_refund'] = $is_refund;
  79. $row['refund_state_text'] = $refund_state_text;
  80. }
  81. return $this->renderSuccess(compact('detail'));
  82. }
  83. return $this->renderError('未找到该订单记录');
  84. }
  85. /**
  86. * 导出全部订单列表
  87. * @return array
  88. * @throws \PhpOffice\PhpSpreadsheet\Exception
  89. * @throws \PhpOffice\PhpSpreadsheet\Writer\Exception
  90. * @author: zjwhust
  91. * @Time: 2022/6/28 15:29
  92. */
  93. public function orderAllExport(){
  94. $param = $this->request->param();
  95. // if(isset($param['order_ids']) && empty($param['order_ids'])){
  96. // return $this->renderError('请勾选订单后再导出');
  97. // }
  98. $model = new OrderModel;
  99. $data = $model->orderExport($param);
  100. $res = ExportService::export($data['data'],$data['header'],$data['filename'],'列表','Xls');
  101. return $this->renderSuccess($res,'导出成功');
  102. }
  103. /**
  104. * 订单详情
  105. * @param int $orderId
  106. * @return array
  107. */
  108. public function detail(int $orderId,bool $isJsonReturn = true)
  109. {
  110. // 订单详情
  111. $model = new OrderModel;
  112. if (!$detail = $model->getDetail($orderId)) {
  113. return $this->renderError('未找到该订单记录');
  114. }
  115. if ($detail->delivery_type == 20){
  116. $detail->shops = Shops::get($detail->shop_id);
  117. }
  118. //获取平台让利的 分润金额
  119. $detail['pool_money'] = 0;
  120. $detail['commissions_content'] = '';
  121. foreach($detail['commissionsDetail'] as $c_detail){
  122. $detail['pool_money'] += $c_detail['clearing_money'];
  123. switch ($c_detail['role']){
  124. case 1:
  125. $detail['commissions_content'] .= '普通用户佣金:¥ ';
  126. break;
  127. case 4:
  128. $detail['commissions_content'] .= ' , 店员佣金:¥ ';
  129. break;
  130. case 3:
  131. $detail['commissions_content'] .= ' , 店长佣金:¥ ';
  132. break;
  133. case 2:
  134. $detail['commissions_content'] .= '店老板佣金:¥ ';
  135. break;
  136. case 5:
  137. $detail['commissions_content'] .= ' , 蒸饭厨师佣金:¥ ';
  138. break;
  139. case 99:
  140. if($c_detail['commission_level']==1){
  141. $detail['commissions_content'] .= '推荐官佣金(一级):¥ ';
  142. }else{
  143. $detail['commissions_content'] .= '推荐官佣金(二级):¥ ';
  144. }
  145. break;
  146. }
  147. $detail['commissions_content'] .= helper::bcadd($c_detail['clearing_money'],'0',2).' '.($c_detail['user']['nick_name']??'').' '.($c_detail['user']['mobile']??'');
  148. }
  149. $detail['pool_money'] = helper::bcadd($detail['pool_money'],'0',2);
  150. unset($detail['commissionsDetail']);
  151. $res = [];
  152. $i = 0;
  153. $mjActIds = [];
  154. foreach ($detail['goods'] as &$goods) {
  155. if ($goods['is_mj_send'] && $goods['mj_send_activity_id']){
  156. $mjActIds[] = $goods['mj_send_activity_id'];
  157. }
  158. if($goods['is_split_package']){
  159. foreach ($goods['package'] as $pack){
  160. $res[$i]['express_company'] = $pack['express_company'];
  161. $res[$i]['express_no'] = $pack['express_no'];
  162. $res[$i]['goods_id'] = $goods['goods_id'];
  163. $res[$i]['goods_name'] = $goods['goods_name'];
  164. $res[$i]['goods_sku_no'] = $goods['goods_sku_no'];
  165. $res[$i]['goods_image'] = $goods['goods_image'];
  166. $res[$i]['delivery_time'] = $pack['delivery_time'];
  167. $res[$i]['is_split_package'] = $goods['is_split_package'];
  168. $res[$i]['total_num'] = $pack['total_num'];
  169. $i++;
  170. };
  171. }else{
  172. if($goods['express_no']){
  173. $res[$i]['express_company'] = $goods['express_company'];
  174. $res[$i]['express_no'] = $goods['express_no'];
  175. $res[$i]['goods_id'] = $goods['goods_id'];
  176. $res[$i]['goods_name'] = $goods['goods_name'];
  177. $res[$i]['goods_sku_no'] = $goods['goods_sku_no'];
  178. $res[$i]['goods_image'] = $goods['goods_image'];
  179. $res[$i]['delivery_time'] = $goods['delivery_time'];
  180. $res[$i]['is_split_package'] = $goods['is_split_package'];
  181. $res[$i]['total_num'] = $goods['total_num'];
  182. }
  183. $i++;
  184. }
  185. unset($goods['package']);
  186. }
  187. $detail['express_list'] = array_values($res);
  188. $detail['full_send_activity'] = null;
  189. //添加满就送活动信息
  190. if ($detail->is_full_send == 1){
  191. $detail['full_send_activity'] = \app\store\model\fullsend\FullSendActivity::where('id',$detail->full_send_activity_id)
  192. ->field('name,code,start_time,end_time,status,"满就送" as type')->find();
  193. }
  194. //添加满件送活动信息
  195. $detail['mj_send_activities'] = null;
  196. if (count($mjActIds)){
  197. $detail['mj_send_activities'] = \app\store\model\mj\MjSendActivity::whereIn('id',$mjActIds)
  198. ->field('name,code,start_time,end_time,status,"满件送" as type')->select();
  199. }
  200. //添加全场满件送活动信息
  201. $detail['qc_mj_send_activities'] = null;
  202. if($detail->is_qc_send_send == 1){
  203. $detail['qc_mj_send_activities'] = \app\store\model\qc\QcMjSendActivity::where('id',$detail->qc_send_activity_id)
  204. ->field('name,code,start_time,end_time,status,"全场满件送" as type')->find();
  205. }
  206. //拼团活动
  207. $detail['group_buy_activity'] = null;
  208. if ($detail['order_source'] == 40 and $detail['order_source_id']){
  209. $group_buy_help = GroupBuyHelp::where('id',$detail['order_source_id'])->field('id,group_buy_activity_id')->find();
  210. $group_buy_id = $group_buy_help->group_buy_activity_id;
  211. $detail['group_buy_activity'] = \app\api\model\groupbuy\GroupBuyActivity::with('goodsExcept')
  212. ->where('id',$group_buy_id)
  213. ->field('id,name,code,start_time,end_time,status,"拼团" as type')->find();
  214. }
  215. //添加秒杀活动信息
  216. $detail['miaosha_activities'] = null;
  217. if($detail['order_source']==60){
  218. $ms_activity_id = \app\common\model\ms\MsActivityGood::where('id',$detail->order_source_id)->value('ms_activity_id');
  219. if($ms_activity_id){
  220. $detail['miaosha_activities'] = \app\common\model\ms\MsActivity::where('id',$ms_activity_id)->field('name,code,start_time,end_time,"限时秒杀" as type')->find();
  221. }
  222. }
  223. //添加秒杀活动信息
  224. $detail['za_activities'] = null;
  225. if($detail['order_source']==70){
  226. $za_activity_id = \app\common\model\za\ZaActivityGood::where('id',$detail->order_source_id)->value('za_activity_id');
  227. if($za_activity_id){
  228. $detail['za_activities'] = \app\common\model\za\ZaActivity::where('id',$za_activity_id)->field('id,name,code,start_time,end_time,"买一赠一" as type')->find();
  229. }
  230. // 获取活动领取人信息
  231. // 1. 改订单是否在[yoshop_za_activity_relations]表中存在child_order_id(领取人的订单号)
  232. $zrModel = ZaActivityRelationModel::where('order_id', '=', $orderId)->find();
  233. if ($zrModel['is_pay'] == 1) { // 已经支付, 才能显示领取人
  234. $getOrderId = $zrModel['child_order_id'];
  235. $detail['za_activities']['get_order_id'] = $getOrderId;
  236. if ($getOrderId > 0) {
  237. // 2. 查询地址
  238. $getAddress = OrderAddressModel::where('order_id', '=', $getOrderId)->find();
  239. $detail['za_activities']['get_order_addr'] = $getAddress;
  240. }
  241. }
  242. }
  243. //添加拼团裂变活动信息
  244. $detail['group_buy_lb_activities'] = null;
  245. if($detail['order_source']==50){
  246. $group_buy_lb_activity_id = \app\common\model\groupbuylb\GroupBuyLbHelp::where('id',$detail->order_source_id)->value('group_buy_lb_activity_id');
  247. if($group_buy_lb_activity_id){
  248. $detail['group_buy_lb_activities'] = \app\common\model\groupbuylb\GroupBuyLbActivity::where('id',$group_buy_lb_activity_id)->field('name,code,start_time,end_time,"拼团裂变" as type')->find();
  249. }
  250. }
  251. //砍价活动
  252. $detail['kj_activity'] = null;
  253. if($detail['order_source']==30){
  254. $activity_id = \app\common\model\kjactivity\KjActivityJoin::where('order_id',$detail['order_id'])->value("activity_id")??0;
  255. if($activity_id>0){
  256. $detail['kj_activity'] = \app\common\model\kjactivity\KjActivity::where('id',$activity_id)
  257. ->field('name,code,start_time,end_time,"砍价" as type')->select();
  258. }
  259. }
  260. //找出赠品信息
  261. $gifts = [];
  262. foreach ($detail['goods'] as $k=>$g) {
  263. //if ($g['is_full_send'] == 1){
  264. if ($g['goods_type'] == 20){
  265. $gifts[] = $g;
  266. unset($detail['goods'][$k]);
  267. }
  268. }
  269. $detail = $detail->toArray();
  270. $activity_discount_ids = helper::getArrayColumn($detail['goods'],'activity_discount_id');
  271. $activity_discount_ids = array_unique($activity_discount_ids);
  272. $detail['activity_discount_list'] = ActivityDiscountModel::where('id','in',$activity_discount_ids)->select();
  273. $detail['gifts'] = $gifts;
  274. if($isJsonReturn){
  275. return $this->renderSuccess(compact('detail'));
  276. }else{
  277. return $detail;
  278. }
  279. }
  280. /**
  281. * 待发货列表
  282. * @param string
  283. * @return array
  284. */
  285. public function deliverylList()
  286. {
  287. // 订单列表
  288. return $this->listss(OrderModel::LIST_TYPE_DELIVERY);
  289. }
  290. /**
  291. * 已发货列表
  292. * @param string
  293. * @return array
  294. */
  295. public function receiptlList()
  296. {
  297. // 订单列表
  298. return $this->listss(OrderModel::LIST_TYPE_RECEIPT);
  299. }
  300. public function listss($dataType){
  301. // 订单列表
  302. $model = new OrderGoodsModel();
  303. $list = $model->getList($dataType,$this->request->param());
  304. return $this->renderSuccess(compact('list'));
  305. }
  306. /**
  307. * 常规发货
  308. * @return array
  309. * @throws \Exception
  310. * @author: zjwhust
  311. * @Time: 2021/10/12 15:24
  312. */
  313. public function delivery(){
  314. $model = new OrderGoodsModel();
  315. if ($model->delivery($this->request->param())) {
  316. return $this->renderSuccess('确认发货成功');
  317. }
  318. return $this->renderError($model->getError());
  319. }
  320. /**
  321. * 拆分发货
  322. * @return array
  323. * @throws \Exception
  324. * @author: zjwhust
  325. * @Time: 2021/11/22 13:44
  326. */
  327. public function splitDelivery(){
  328. $model = new OrderGoodsModel();
  329. if ($model->splitDelivery($this->request->param())) {
  330. return $this->renderSuccess('确认发货成功');
  331. }
  332. return $this->renderError($model->getError());
  333. }
  334. /**
  335. * 标签备注
  336. * @param $order_id
  337. * @param string $color_remark
  338. * @param string $color
  339. * @return array
  340. * @author: zjwhust
  341. * @Time: 2021/10/12 18:02
  342. */
  343. public function updColor(){
  344. $model = new OrderModel;
  345. if ($model->updColor($this->request->param())) {
  346. return $this->renderSuccess('添加备注成功');
  347. }
  348. return $this->renderError($model->getError());
  349. }
  350. /**
  351. * 修改订单收货信息
  352. * @return array
  353. * @author: zjwhust
  354. * @Time: 2021/10/13 10:55
  355. */
  356. public function updAddress(){
  357. $model = new OrderModel;
  358. if ($model->updAddress($this->request->param())) {
  359. return $this->renderSuccess('修改收货地址成功');
  360. }
  361. return $this->renderError($model->getError());
  362. }
  363. /**
  364. * 查看物流详情
  365. * @param string $express_no
  366. * @param int $is_split_package 是否拆分包裹 0未拆分 1已拆分
  367. * @return array
  368. * @throws BaseException
  369. * @throws \think\db\exception\DataNotFoundException
  370. * @throws \think\db\exception\DbException
  371. * @throws \think\db\exception\ModelNotFoundException
  372. * @author: zjwhust
  373. * @Time: 2021/10/9 9:13
  374. */
  375. public function express(string $express_no,int $is_split_package=1)
  376. {
  377. if($is_split_package==0){
  378. $model = new OrderGoodsModel();
  379. $detail = $model->where('express_no',$express_no)->find();
  380. if (!$detail || $detail['delivery_status']!=DeliveryStatusEnum::DELIVERED) {
  381. return $this->renderError('未发货');
  382. }
  383. }else{
  384. $model = new OrderGoodsPackageModel();
  385. $detail = $model->where('express_no',$express_no)->find();
  386. if (!$detail || $detail['delivery_status']!=DeliveryStatusEnum::DELIVERED) {
  387. return $this->renderError('未发货');
  388. }
  389. }
  390. $res['order'] = [
  391. "express_no" => $detail['express_no'],
  392. "express_company" => $detail['express_company'],
  393. ];
  394. $res['delivery'] = (new ExpressModel)->deliverylist($express_no, $detail['delivery_time'], 1);
  395. return $this->renderSuccess(compact('res'));
  396. }
  397. /**
  398. * 导出待发货订单功能
  399. * @return array
  400. * @author: zjwhustf
  401. * @Time: 2021/10/15 13:43
  402. */
  403. public function orderExport(){
  404. $param = $this->request->param();
  405. if(isset($param['order_goods_id']) && empty($param['order_goods_id'])){
  406. return $this->renderError('请勾选订单商品后再导出');
  407. }
  408. $model = new OrderGoodsModel;
  409. if ($param['gtype'] == 2){
  410. $data = $model->orderBagExport($param);
  411. }else{
  412. $data = $model->orderExport($param);
  413. }
  414. // dd($data['data']);
  415. $res = ExportService::export($data['data'],$data['header'],$data['filename'],'列表','Xls',true);
  416. //批量修改导出状态为导出
  417. $model->updateBase(['is_export' => ExportStatusEnum::EXPORT,'export_time' => time()], [['order_goods_id', 'in', $param['order_goods_id']]]);
  418. return $this->renderSuccess($res,'导出成功');
  419. }
  420. /**
  421. * 批量导入发货,导入运单号
  422. * @return array
  423. */
  424. public function expressImport(){
  425. $file = request()->file('file');
  426. if (empty($file)){
  427. return $this->renderError('请先上传导入的文件');
  428. }
  429. // 上传到本地服务器
  430. $fileName = \think\facade\Filesystem::putFile( 'delivery', $file);
  431. $res = ExportService::import($fileName);
  432. return $this->renderSuccess($res,'导入成功');
  433. }
  434. //订单统计表
  435. public function ordertj(){
  436. $model = new OrderTj;
  437. $param = $this->request->param();
  438. $list = $model->getList($param);
  439. return $this->renderSuccess(compact('list'));
  440. }
  441. //订单统计导出
  442. public function ordertjExport(){
  443. $model = new OrderTj;
  444. $param = $this->request->param();
  445. $data = $model->listExport($param);
  446. $res = ExportService::export($data['data'],$data['header'],$data['filename'],'列表','Xls',true);
  447. return $this->renderSuccess($res,'导出成功');
  448. }
  449. //复购周期分析
  450. public function repurchase(){
  451. $params = $this->request->param();
  452. if (!empty($params['betweenTime'])) {
  453. $times = between_time_format($params['betweenTime']);
  454. /* $start_time = $times['start_time'];
  455. $end_time = $times['end_time'];*/
  456. $start_time = strtotime(date('Y-m-d 00:00:00',$times['start_time']));
  457. $end_time = strtotime(date('Y-m-d 23:59:59',$times['end_time']));
  458. }else{
  459. $start_time = time()-86400*180;
  460. $end_time = time();
  461. }
  462. $sql = "SELECT
  463. m.user_id,
  464. FROM_UNIXTIME(sfo.create_time) AS t1,
  465. FROM_UNIXTIME(m.create_time) AS t2,
  466. DATEDIFF(
  467. FROM_UNIXTIME(m.create_time),
  468. FROM_UNIXTIME(sfo.create_time)
  469. ) AS days
  470. FROM
  471. yoshop_order AS m
  472. LEFT JOIN yoshop_order AS sfo ON m.user_id = sfo.user_id
  473. AND sfo.create_time < m.create_time
  474. WHERE
  475. m.create_time BETWEEN ".$start_time."
  476. AND ".$end_time."
  477. AND m.pay_status = 20
  478. AND sfo.create_time BETWEEN ".$start_time."
  479. AND ".$end_time."
  480. AND sfo.pay_status = 20
  481. AND (
  482. SELECT
  483. count(*)
  484. FROM
  485. yoshop_order AS n
  486. WHERE
  487. m.user_id = n.user_id
  488. AND m.create_time > n.create_time
  489. AND sfo.create_time <= n.create_time
  490. ) = 1
  491. GROUP BY m.user_id,days";
  492. $result = Db::query($sql);
  493. $data = ['day1'=>0,'day2'=>0,'day3'=>0];
  494. $count = [];
  495. foreach ($result as $item){
  496. if($item['days']<=30){//0≤D≤30
  497. if(!isset($count[$item['user_id']])){
  498. $data['day1'] += 1;
  499. }
  500. $count[$item['user_id']] = 1;
  501. }elseif($item['days']>30 && $item['days']<=60){//30<D≤60
  502. $data['day2'] += 1;
  503. }else{//D>60
  504. $data['day3'] += 1;
  505. }
  506. }
  507. return $this->renderSuccess(compact('data'));
  508. }
  509. //统计rf
  510. public function rf(){
  511. $r1f1 = "select user_id,count(order_id) as c from yoshop_order
  512. where pay_status=20
  513. and pay_time>unix_timestamp(now())-7*86400
  514. GROUP BY user_id HAVING count(order_id)=1";
  515. $r1f2 = "select user_id,count(order_id) as c from yoshop_order
  516. where pay_status=20
  517. and pay_time>unix_timestamp(now())-7*86400
  518. GROUP BY user_id HAVING count(order_id)>=2 and count(order_id)<5";
  519. $r1f3 = "select user_id,count(order_id) as c from yoshop_order
  520. where pay_status=20
  521. and pay_time>unix_timestamp(now())-7*86400
  522. GROUP BY user_id HAVING count(order_id)>=5";
  523. $r2f1 = "select user_id,count(order_id) as c from yoshop_order
  524. where pay_status=20
  525. and pay_time<unix_timestamp(now())-7*86400
  526. and pay_time>unix_timestamp(now())-60*86400
  527. GROUP BY user_id HAVING count(order_id)=1";
  528. $r2f2 = "select user_id,count(order_id) as c from yoshop_order
  529. where pay_status=20
  530. and pay_time<unix_timestamp(now())-7*86400
  531. and pay_time>unix_timestamp(now())-60*86400
  532. GROUP BY user_id HAVING count(order_id)>=2 and count(order_id)<5";
  533. $r2f3 = "select user_id,count(order_id) as c from yoshop_order
  534. where pay_status=20
  535. and pay_time<unix_timestamp(now())-7*86400
  536. and pay_time>unix_timestamp(now())-60*86400
  537. GROUP BY user_id HAVING count(order_id)>=5";
  538. $r3f1 = "select user_id,count(order_id) as c from yoshop_order
  539. where pay_status=20
  540. and pay_time<unix_timestamp(now())-60*86400
  541. GROUP BY user_id HAVING count(order_id)=1";
  542. $r3f2 = "select user_id,count(order_id) as c from yoshop_order
  543. where pay_status=20
  544. and pay_time<unix_timestamp(now())-60*86400
  545. GROUP BY user_id HAVING count(order_id)>=2 and count(order_id)<5";
  546. $r3f3 = "select user_id,count(order_id) as c from yoshop_order
  547. where pay_status=20
  548. and pay_time<unix_timestamp(now())-60*86400
  549. GROUP BY user_id HAVING count(order_id)>=5";
  550. $result11 = Db::query($r1f1);
  551. $result12 = Db::query($r1f2);
  552. $result13 = Db::query($r1f3);
  553. $result21 = Db::query($r2f1);
  554. $result22 = Db::query($r2f2);
  555. $result23 = Db::query($r2f3);
  556. $result31 = Db::query($r3f1);
  557. $result32 = Db::query($r3f2);
  558. $result33 = Db::query($r3f3);
  559. // var_dump(count($result11));
  560. // var_dump(count($result12));
  561. // var_dump(count($result13));
  562. $r11 = count($result11);
  563. $r12 = count($result12);
  564. $r13 = count($result13);
  565. // echo json_encode($result12);
  566. // echo "---";
  567. // echo json_encode($result13);
  568. // die();
  569. $r21 = count($result21);
  570. $r22 = count($result22);
  571. $r23 = count($result23);
  572. // var_dump(count($result21));
  573. // var_dump(count($result22));
  574. // var_dump(count($result23));
  575. // var_dump(count($result31));
  576. // var_dump(count($result32));
  577. // var_dump(count($result33));
  578. $r31 = count($result31);
  579. $r32 = count($result32);
  580. $r33 = count($result33);
  581. $data[] = ['r1'=>$r11,"r2"=>$r12,"r3"=>$r13,'r4'=>$r11+$r12+$r13];
  582. $data[] = ['r1'=>$r21,"r2"=>$r22,"r3"=>$r23,'r4'=>$r21+$r22+$r23];
  583. $data[] = ['r1'=>$r31,"r2"=>$r32,"r3"=>$r33,'r4'=>$r31+$r32+$r33];
  584. return $this->renderSuccess(compact('data'));
  585. }
  586. //统计rm
  587. public function rm(){
  588. $r1f1 = "select user_id,sum(pay_price+rice_card_money) from yoshop_order
  589. where pay_status=20
  590. and pay_time>unix_timestamp(now())-7*86400
  591. GROUP BY user_id HAVING sum(pay_price+rice_card_money)<200";
  592. $r1f2 = "select user_id,count(order_id) as c from yoshop_order
  593. where pay_status=20
  594. and pay_time>unix_timestamp(now())-7*86400
  595. GROUP BY user_id HAVING sum(pay_price+rice_card_money)>=200 and sum(pay_price+rice_card_money)<1000";
  596. $r1f3 = "select user_id,count(order_id) as c from yoshop_order
  597. where pay_status=20
  598. and pay_time>unix_timestamp(now())-7*86400
  599. GROUP BY user_id HAVING sum(pay_price+rice_card_money)>=1000";
  600. $r2f1 = "select user_id,sum(pay_price+rice_card_money) from yoshop_order
  601. where pay_status=20
  602. and pay_time>unix_timestamp(now())-60*86400
  603. and pay_time<unix_timestamp(now())-7*86400
  604. GROUP BY user_id HAVING sum(pay_price+rice_card_money)<200";
  605. $r2f2 ="select user_id,sum(pay_price+rice_card_money) from yoshop_order
  606. where pay_status=20
  607. and pay_time>unix_timestamp(now())-60*86400
  608. and pay_time<unix_timestamp(now())-7*86400
  609. GROUP BY user_id HAVING sum(pay_price+rice_card_money)>=200 and sum(pay_price+rice_card_money)<1000";
  610. $r2f3 = "select user_id,sum(pay_price+rice_card_money) from yoshop_order
  611. where pay_status=20
  612. and pay_time>unix_timestamp(now())-60*86400
  613. and pay_time<unix_timestamp(now())-7*86400
  614. GROUP BY user_id HAVING sum(pay_price+rice_card_money)>=1000";
  615. $r3f1 = "select user_id,sum(pay_price+rice_card_money) from yoshop_order
  616. where pay_status=20
  617. and pay_time<unix_timestamp(now())-60*86400
  618. GROUP BY user_id HAVING sum(pay_price+rice_card_money)<200";
  619. $r3f2 ="select user_id,sum(pay_price+rice_card_money) from yoshop_order
  620. where pay_status=20
  621. and pay_time<unix_timestamp(now())-60*86400
  622. GROUP BY user_id HAVING sum(pay_price+rice_card_money)>=200 and sum(pay_price+rice_card_money)<1000";
  623. $r3f3 = "select user_id,sum(pay_price+rice_card_money) from yoshop_order
  624. where pay_status=20
  625. and pay_time<unix_timestamp(now())-60*86400
  626. GROUP BY user_id HAVING sum(pay_price+rice_card_money)>=1000";
  627. $result11 = Db::query($r1f1);
  628. $result12 = Db::query($r1f2);
  629. $result13 = Db::query($r1f3);
  630. $result21 = Db::query($r2f1);
  631. $result22 = Db::query($r2f2);
  632. $result23 = Db::query($r2f3);
  633. $result31 = Db::query($r3f1);
  634. $result32 = Db::query($r3f2);
  635. $result33 = Db::query($r3f3);
  636. // var_dump(count($result11));
  637. // var_dump(count($result12));
  638. // var_dump(count($result13));
  639. $r11 = count($result11);
  640. $r12 = count($result12);
  641. $r13 = count($result13);
  642. $r21 = count($result21);
  643. $r22 = count($result22);
  644. $r23 = count($result23);
  645. // var_dump(count($result21));
  646. // var_dump(count($result22));
  647. // var_dump(count($result23));
  648. // var_dump(count($result31));
  649. // var_dump(count($result32));
  650. // var_dump(count($result33));
  651. $r31 = count($result31);
  652. $r32 = count($result32);
  653. $r33 = count($result33);
  654. $data[] = ['r1'=>$r11,"r2"=>$r12,"r3"=>$r13,'r4'=>$r11+$r12+$r13];
  655. $data[] = ['r1'=>$r21,"r2"=>$r22,"r3"=>$r23,'r4'=>$r21+$r22+$r23];
  656. $data[] = ['r1'=>$r31,"r2"=>$r32,"r3"=>$r33,'r4'=>$r31+$r32+$r33];
  657. return $this->renderSuccess(compact('data'));
  658. }
  659. //消费力
  660. public function clist(){
  661. $start = 10000000;
  662. $end = time();
  663. $params = $this->request->param();
  664. //$ftype = $param['ftype']??0;
  665. if (!empty($params['betweenTime'])) {
  666. $times = between_time_format($params['betweenTime']);
  667. $start = strtotime(date('Y-m-d 00:00:00',$times['start_time']));
  668. $end = strtotime(date('Y-m-d 23:59:59',$times['end_time']));
  669. //$start = $times['start_time'];
  670. //$end = $times['end_time'];
  671. }
  672. log_record('start::'.date('Y-m-d H:i:s',$start),'error');
  673. log_record('end::'.date('Y-m-d H:i:s',$end),'error');
  674. //dd($times);
  675. //1 日 2 周 3 月
  676. /* if($ftype==1){
  677. $start = strtotime(date("Y-m-d 00:00:00",time()));
  678. $end = time();
  679. }
  680. if($ftype==2){
  681. $start = mktime(0,0,0,intval(date("m")),date("d")-date("N")+1,intval(date("y")));
  682. $end = time();
  683. }
  684. if($ftype==3){
  685. $start = mktime(0,0,0,intval(date("m")),1,intval(date("Y")));
  686. $end = time();
  687. }
  688. if($ftype==4){
  689. $start = strtotime(Date("Y",time())."-1"."-1");
  690. $end = time();
  691. }*/
  692. $sqlo1 = "select user_id,sum(pay_price+rice_card_money) from yoshop_order
  693. where pay_status=20 and tag=0
  694. and pay_time>$start and pay_time<$end
  695. GROUP BY user_id HAVING sum(pay_price+rice_card_money)<200";
  696. $sqlo2 = "select user_id,sum(pay_price+rice_card_money) from yoshop_order
  697. where pay_status=20 and tag=0
  698. and pay_time>$start and pay_time<$end
  699. GROUP BY user_id HAVING sum(pay_price+rice_card_money)>=200
  700. and sum(pay_price+rice_card_money)<1000";
  701. $sqlo3 = "select user_id,sum(pay_price+rice_card_money) from yoshop_order
  702. where pay_status=20 and tag=0
  703. and pay_time>$start and pay_time<$end
  704. GROUP BY user_id HAVING sum(pay_price+rice_card_money)>=1000";
  705. $sqln1 = "select user_id,sum(pay_price+rice_card_money) from yoshop_order
  706. where pay_status=20 and tag=1
  707. and pay_time>$start and pay_time<$end
  708. GROUP BY user_id HAVING sum(pay_price+rice_card_money)<200";
  709. $sqln2 = "select user_id,sum(pay_price+rice_card_money) from yoshop_order
  710. where pay_status=20 and tag=1
  711. and pay_time>$start and pay_time<$end
  712. GROUP BY user_id HAVING sum(pay_price+rice_card_money)>=200
  713. and sum(pay_price+rice_card_money)<1000";
  714. $sqln3 = "select user_id,sum(pay_price+rice_card_money) from yoshop_order
  715. where pay_status=20 and tag=1
  716. and pay_time>$start and pay_time<$end
  717. GROUP BY user_id HAVING sum(pay_price+rice_card_money)>=1000";
  718. $resulto1 = Db::query($sqlo1);
  719. $resulto2 = Db::query($sqlo2);
  720. $resulto3 = Db::query($sqlo3);
  721. $resultn1 = Db::query($sqln1);
  722. $resultn2 = Db::query($sqln2);
  723. $resultn3 = Db::query($sqln3);
  724. $data[] = ['title'=>'老客','value'=>[count($resulto1),count($resulto2),count($resulto3)]];
  725. $data[] = ['title'=>'新客','value'=>[count($resultn1),count($resultn2),count($resultn3)]];
  726. return $this->renderSuccess(compact('data'));
  727. }
  728. //消费频次
  729. public function flist(){
  730. $start = 10000000;
  731. $end = time();
  732. $params = $this->request->param();
  733. $ftype = $param['ftype']??0;
  734. if (!empty($params['betweenTime'])) {
  735. $times = between_time_format($params['betweenTime']);
  736. $start = strtotime(date('Y-m-d 00:00:00',$times['start_time']));
  737. $end = strtotime(date('Y-m-d 23:59:59',$times['end_time']));
  738. /* $start = $times['start_time'];
  739. $end = $times['end_time'];*/
  740. }
  741. //1 日 2 周 3 月
  742. /* if($ftype==1){
  743. $start = strtotime(date("Y-m-d 00:00:00",time()));
  744. $end = time();
  745. }
  746. if($ftype==2){
  747. $start = mktime(0,0,0,intval(date("m")),date("d")-date("N")+1,intval(date("y")));
  748. $end = time();
  749. }
  750. if($ftype==3){
  751. $start = mktime(0,0,0,intval(date("m")),1,intval(date("Y")));
  752. $end = time();
  753. }
  754. if($ftype==4){
  755. $start = strtotime(Date("Y",time())."-1"."-1");
  756. $end = time();
  757. }*/
  758. $sqlo1 = "select user_id,count(order_id) as c from yoshop_order
  759. where pay_status=20 and tag=0
  760. and pay_time>$start and pay_time<$end
  761. GROUP BY user_id HAVING count(order_id)=1";
  762. $sqlo2 = "select user_id,count(order_id) as c from yoshop_order
  763. where pay_status=20 and tag=0
  764. and pay_time>$start and pay_time<$end
  765. GROUP BY user_id HAVING count(order_id)>=2 and count(order_id)<5";
  766. $sqlo3 = "select user_id,count(order_id) as c from yoshop_order
  767. where pay_status=20 and tag=0
  768. and pay_time>$start and pay_time<$end
  769. GROUP BY user_id HAVING count(order_id)>=5";
  770. $sqln1 = "select user_id,count(order_id) as c from yoshop_order
  771. where pay_status=20 and tag=1
  772. and pay_time>$start and pay_time<$end
  773. GROUP BY user_id HAVING count(order_id)=1";
  774. $sqln2 = "select user_id,count(order_id) as c from yoshop_order
  775. where pay_status=20 and tag=1
  776. and pay_time>$start and pay_time<$end
  777. GROUP BY user_id HAVING count(order_id)>=2 and count(order_id)<5";
  778. $sqln3 = "select user_id,count(order_id) as c from yoshop_order
  779. where pay_status=20 and tag=1
  780. and pay_time>$start and pay_time<$end
  781. GROUP BY user_id HAVING count(order_id)>=5";
  782. $resulto1 = Db::query($sqlo1);
  783. $resulto2 = Db::query($sqlo2);
  784. $resulto3 = Db::query($sqlo3);
  785. $resultn1 = Db::query($sqln1);
  786. $resultn2 = Db::query($sqln2);
  787. $resultn3 = Db::query($sqln3);
  788. $data[] = ['title'=>'老客','value'=>[count($resulto1),count($resulto2),count($resulto3)]];
  789. $data[] = ['title'=>'新客','value'=>[count($resultn1),count($resultn2),count($resultn3)]];
  790. return $this->renderSuccess(compact('data'));
  791. }
  792. //订单导出
  793. public function orderFxExport(){
  794. $start = 10000000;
  795. $end = time();
  796. $param = $this->request->param();
  797. $ftype = $param['ftype']??0;
  798. if (!empty($param['betweenTime'])) {
  799. $times = between_time_format($param['betweenTime']);
  800. $start = $times['start_time'];
  801. $end = $times['end_time'];
  802. }
  803. //1 日 2 周 3 月
  804. if($ftype==1){
  805. $start = strtotime(date("Y-m-d 00:00:00",time()));
  806. $end = time();
  807. }
  808. if($ftype==2){
  809. $start = mktime(0,0,0,intval(date("m")),date("d")-date("N")+1,intval(date("y")));
  810. $end = time();
  811. }
  812. if($ftype==3){
  813. $start = mktime(0,0,0,intval(date("m")),1,intval(date("Y")));
  814. $end = time();
  815. }
  816. if($ftype==4){
  817. $start = strtotime(Date("Y",time())."-1"."-1");
  818. $end = time();
  819. }
  820. $filter[] = ['create_time', '>=', $start];
  821. $filter[] = ['create_time', '<', $end];
  822. $list = OrderModel::field('count(order_id) as c,sum(pay_price+rice_card_money) as amount,user_id,create_time,group_concat(order_id separator ",") as order_ids')->where($filter)->where("pay_status",20)->group('user_id')->with(['user','goods'])->select();
  823. // return $list;
  824. $data['header'] = ['时间', '用户ID', '昵称', '手机号','实际支付件数','支付成功次数','支付总金额'];
  825. $data['filename'] = '用户分析';
  826. $data['data'] = [];
  827. //'实际支付件数','支付金额'
  828. $newdata = [];
  829. foreach($list as $row){
  830. $newrow['create_time'] = $row['create_time'];
  831. $newrow['user_id'] = $row['user_id'];
  832. $newrow['nick_name'] = $row['user']['nick_name'];
  833. $newrow['mobile'] = $row['user']['mobile'];
  834. // $newrow['order_ids'] = $row['order_ids'];`
  835. $newrow['sku'] = OrderGoodsModel::where("order_id",'in',explode(",",$row['order_ids']))->count();
  836. $newrow['order_cnt'] = count(explode(",",$row['order_ids']));
  837. $newrow['amount'] = $row['amount'];
  838. $newdata[] = $newrow;
  839. }
  840. $data['data'] = $newdata;
  841. $res = ExportService::export($data['data'],$data['header'],$data['filename'],'列表','Xls',true);
  842. return $this->renderSuccess($res,'导出成功');
  843. // return $this->renderSuccess(compact("data"));
  844. }
  845. }