StoreOrder.php 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907
  1. <?php
  2. /**
  3. *
  4. * @author: xaboy<365615158@qq.com>
  5. * @day: 2017/11/11
  6. */
  7. namespace app\admin\model\order;
  8. use app\admin\model\wechat\WechatUser;
  9. use app\admin\model\ump\StorePink;
  10. use app\admin\model\store\StoreProduct;
  11. use service\PHPExcelService;
  12. use traits\ModelTrait;
  13. use basic\ModelBasic;
  14. use service\WechatTemplateService;
  15. use think\Url;
  16. use think\Db;
  17. /**
  18. * 订单管理Model
  19. * Class StoreOrder
  20. * @package app\admin\model\store
  21. */
  22. class StoreOrder extends ModelBasic
  23. {
  24. use ModelTrait;
  25. public static function orderCount(){
  26. $data['wz']=self::statusByWhere(0,new self())->count();
  27. $data['wf']=self::statusByWhere(1,new self())->count();
  28. $data['ds']=self::statusByWhere(2,new self())->count();
  29. $data['dp']=self::statusByWhere(3,new self())->count();
  30. $data['jy']=self::statusByWhere(4,new self())->count();
  31. $data['tk']=self::statusByWhere(-1,new self())->count();
  32. $data['yt']=self::statusByWhere(-2,new self())->count();
  33. $data['general']=self::where(['pink_id'=>0,'combination_id'=>0,'seckill_id'=>0])->count();
  34. $data['pink']=self::where('pink_id|combination_id','neq',0)->count();
  35. $data['seckill']=self::where('seckill_id','neq',0)->count();
  36. return $data;
  37. }
  38. public static function OrderList($where){
  39. $model = self::getOrderWhere($where,self::alias('a')->join('user r','r.uid=a.uid','LEFT'),'a.','r')->field('a.*,r.nickname');
  40. if($where['order']!=''){
  41. $model = $model->order(self::setOrder($where['order']));
  42. }else{
  43. $model = $model->order('a.id desc');
  44. }
  45. $data=($data=$model->page((int)$where['page'],(int)$where['limit'])->select()) && count($data) ? $data->toArray() : [];
  46. foreach ($data as &$item){
  47. $_info = Db::name('store_order_cart_info')->where('oid',$item['id'])->field('cart_info')->select();
  48. foreach ($_info as $k=>$v){
  49. $_info[$k]['cart_info'] = json_decode($v['cart_info'],true);
  50. }
  51. $item['_info'] = $_info;
  52. if($item['pink_id'] && $item['combination_id']){
  53. $pinkStatus = StorePink::where('order_id_key',$item['id'])->value('status');
  54. switch ($pinkStatus){
  55. case 1:
  56. $item['pink_name'] = '[拼团订单]正在进行中';
  57. $item['color'] = '#f00';
  58. break;
  59. case 2:
  60. $item['pink_name'] = '[拼团订单]已完成';
  61. $item['color'] = '#00f';
  62. break;
  63. case 3:
  64. $item['pink_name'] = '[拼团订单]未完成';
  65. $item['color'] = '#f0f';
  66. break;
  67. default:
  68. $item['pink_name'] = '[拼团订单]历史订单';
  69. $item['color'] = '#457856';
  70. break;
  71. }
  72. }else{
  73. if($item['seckill_id']){
  74. $item['pink_name'] = '[秒杀订单]';
  75. $item['color'] = '#32c5e9';
  76. }else{
  77. $item['pink_name'] = '[普通订单]';
  78. $item['color'] = '#895612';
  79. }
  80. }
  81. if($item['paid']==1){
  82. switch ($item['pay_type']){
  83. case 'weixin':
  84. $item['pay_type_name']='微信支付';
  85. break;
  86. case 'yue':
  87. $item['pay_type_name']='微信支付';
  88. break;
  89. case 'offline':
  90. $item['pay_type_name']='线下支付';
  91. break;
  92. default:
  93. $item['pay_type_name']='其他支付';
  94. break;
  95. }
  96. }else{
  97. switch ($item['pay_type']){
  98. default:
  99. $item['pay_type_name']='未支付';
  100. break;
  101. case 'offline':
  102. $item['pay_type_name']='线下支付';
  103. $item['pay_type_info']=1;
  104. break;
  105. }
  106. }
  107. if($item['paid']==0 && $item['status']==0){
  108. $item['status_name']='未支付';
  109. }else if($item['paid']==1 && $item['status']==0 && $item['refund_status']==0){
  110. $item['status_name']='未发货';
  111. }else if($item['paid']==1 && $item['status']==1 && $item['refund_status']==0){
  112. $item['status_name']='待收货';
  113. }else if($item['paid']==1 && $item['status']==2 && $item['refund_status']==0){
  114. $item['status_name']='待评价';
  115. }else if($item['paid']==1 && $item['status']==3 && $item['refund_status']==0){
  116. $item['status_name']='待评价';
  117. }else if($item['paid']==1 && $item['refund_status']==1){
  118. $item['status_name']=<<<HTML
  119. <b style="color:#f124c7">申请退款</b><br/>
  120. <span>退款原因:{$item['refund_reason_wap']}</span>
  121. HTML;
  122. }else if($item['paid']==1 && $item['refund_status']==2){
  123. $item['status_name']='已退款';
  124. }
  125. if($item['paid']==0 && $item['status']==0 && $item['refund_status']==0){
  126. $item['_status']=1;
  127. }else if($item['paid']==1 && $item['status']==0 && $item['refund_status']==0){
  128. $item['_status']=2;
  129. }else if($item['paid']==1 && $item['refund_status']==1){
  130. $item['_status']=3;
  131. }else if($item['paid']==1 && $item['status']==1 && $item['refund_status']==0){
  132. $item['_status']=4;
  133. }else if($item['paid']==1 && $item['status']==2 && $item['refund_status']==0){
  134. $item['_status']=5;
  135. }else if($item['paid']==1 && $item['status']==3 && $item['refund_status']==0){
  136. $item['_status']=6;
  137. }else if($item['paid']==1 && $item['refund_status']==2){
  138. $item['_status']=7;
  139. }
  140. }
  141. if(isset($where['excel']) && $where['excel']==1){
  142. self::SaveExcel($data);
  143. }
  144. $count=self::getOrderWhere($where,self::alias('a')->join('user r','r.uid=a.uid','LEFT'),'a.','r')->count();
  145. return compact('count','data');
  146. }
  147. /*
  148. * 保存并下载excel
  149. * $list array
  150. * return
  151. */
  152. public static function SaveExcel($list){
  153. $export = [];
  154. foreach ($list as $index=>$item){
  155. $_info = Db::name('store_order_cart_info')->where('oid',$item['id'])->column('cart_info');
  156. $goodsName = [];
  157. foreach ($_info as $k=>$v){
  158. $v = json_decode($v,true);
  159. $goodsName[] = implode(
  160. [$v['productInfo']['store_name'],
  161. isset($v['productInfo']['attrInfo']) ? '('.$v['productInfo']['attrInfo']['suk'].')' : '',
  162. "[{$v['cart_num']} * {$v['truePrice']}]"
  163. ],' ');
  164. }
  165. $item['cartInfo'] = $_info;
  166. $export[] = [
  167. $item['order_id'],$item['pay_type_name'],
  168. $item['total_num'],$item['total_price'],$item['total_postage'],$item['pay_price'],$item['refund_price'],
  169. $item['mark'],$item['remark'],
  170. [$item['real_name'],$item['user_phone'],$item['user_address']],
  171. $goodsName,
  172. [$item['paid'] == 1? '已支付':'未支付','支付时间: '.($item['pay_time'] > 0 ? date('Y/md H:i',$item['pay_time']) : '暂无')]
  173. ];
  174. }
  175. PHPExcelService::setExcelHeader(['订单号','支付方式','商品总数','商品总价','邮费','支付金额','退款金额','用户备注','管理员备注','收货人信息','商品信息','支付状态'])
  176. ->setExcelTile('订单导出','订单信息'.time(),' 生成时间:'.date('Y-m-d H:i:s',time()))
  177. ->setExcelContent($export)
  178. ->ExcelSave();
  179. }
  180. /**
  181. * @param $where
  182. * @return array
  183. */
  184. public static function systemPage($where,$userid=false){
  185. $model = self::getOrderWhere($where,self::alias('a')->join('user r','r.uid=a.uid','LEFT'),'a.','r')->field('a.*,r.nickname');
  186. if($where['order']){
  187. $model = $model->order('a.'.$where['order']);
  188. }else{
  189. $model = $model->order('a.id desc');
  190. }
  191. if($where['export'] == 1){
  192. $list = $model->select()->toArray();
  193. $export = [];
  194. foreach ($list as $index=>$item){
  195. if ($item['pay_type'] == 'weixin'){
  196. $payType = '微信支付';
  197. }elseif($item['pay_type'] == 'yue'){
  198. $payType = '余额支付';
  199. }elseif($item['pay_type'] == 'offline'){
  200. $payType = '线下支付';
  201. }else{
  202. $payType = '其他支付';
  203. }
  204. $_info = Db::name('store_order_cart_info')->where('oid',$item['id'])->column('cart_info');
  205. $goodsName = [];
  206. foreach ($_info as $k=>$v){
  207. $v = json_decode($v,true);
  208. $goodsName[] = implode(
  209. [$v['productInfo']['store_name'],
  210. isset($v['productInfo']['attrInfo']) ? '('.$v['productInfo']['attrInfo']['suk'].')' : '',
  211. "[{$v['cart_num']} * {$v['truePrice']}]"
  212. ],' ');
  213. }
  214. $item['cartInfo'] = $_info;
  215. $export[] = [
  216. $item['order_id'],$payType,
  217. $item['total_num'],$item['total_price'],$item['total_postage'],$item['pay_price'],$item['refund_price'],
  218. $item['mark'],$item['remark'],
  219. [$item['real_name'],$item['user_phone'],$item['user_address']],
  220. $goodsName,
  221. [$item['paid'] == 1? '已支付':'未支付','支付时间: '.($item['pay_time'] > 0 ? date('Y/md H:i',$item['pay_time']) : '暂无')]
  222. ];
  223. $list[$index] = $item;
  224. }
  225. PHPExcelService::setExcelHeader(['订单号','支付方式','商品总数','商品总价','邮费','支付金额','退款金额','用户备注','管理员备注','收货人信息','商品信息','支付状态'])
  226. ->setExcelTile('订单导出','订单信息'.time(),' 生成时间:'.date('Y-m-d H:i:s',time()))
  227. ->setExcelContent($export)
  228. ->ExcelSave();
  229. }
  230. return self::page($model,function ($item){
  231. $_info = Db::name('store_order_cart_info')->where('oid',$item['id'])->field('cart_info')->select();
  232. foreach ($_info as $k=>$v){
  233. $_info[$k]['cart_info'] = json_decode($v['cart_info'],true);
  234. }
  235. $item['_info'] = $_info;
  236. if($item['pink_id'] && $item['combination_id']){
  237. $pinkStatus = StorePink::where('order_id_key',$item['id'])->value('status');
  238. switch ($pinkStatus){
  239. case 1:
  240. $item['pink_name'] = '[拼团订单]正在进行中';
  241. $item['color'] = '#f00';
  242. break;
  243. case 2:
  244. $item['pink_name'] = '[拼团订单]已完成';
  245. $item['color'] = '#00f';
  246. break;
  247. case 3:
  248. $item['pink_name'] = '[拼团订单]未完成';
  249. $item['color'] = '#f0f';
  250. break;
  251. default:
  252. $item['pink_name'] = '[拼团订单]历史订单';
  253. $item['color'] = '#457856';
  254. break;
  255. }
  256. }else{
  257. if($item['seckill_id']){
  258. $item['pink_name'] = '[秒杀订单]';
  259. $item['color'] = '#32c5e9';
  260. }else{
  261. $item['pink_name'] = '[普通订单]';
  262. $item['color'] = '#895612';
  263. }
  264. }
  265. },$where);
  266. }
  267. public static function statusByWhere($status,$model = null,$alert='')
  268. {
  269. if($model == null) $model = new self;
  270. if('' === $status)
  271. return $model;
  272. else if($status == 0)//未支付
  273. return $model->where($alert.'paid',0)->where($alert.'status',0)->where($alert.'refund_status',0);
  274. else if($status == 1)//已支付 未发货
  275. return $model->where($alert.'paid',1)->where($alert.'status',0)->where($alert.'refund_status',0);
  276. else if($status == 2)//已支付 待收货
  277. return $model->where($alert.'paid',1)->where($alert.'status',1)->where($alert.'refund_status',0);
  278. else if($status == 3)// 已支付 已收货 待评价
  279. return $model->where($alert.'paid',1)->where($alert.'status',2)->where($alert.'refund_status',0);
  280. else if($status == 4)// 交易完成
  281. return $model->where($alert.'paid',1)->where($alert.'status',3)->where($alert.'refund_status',0);
  282. else if($status == -1)//退款中
  283. return $model->where($alert.'paid',1)->where($alert.'refund_status',1);
  284. else if($status == -2)//已退款
  285. return $model->where($alert.'paid',1)->where($alert.'refund_status',2);
  286. else if($status == -3)//退款
  287. return $model->where($alert.'paid',1)->where($alert.'refund_status','in','1,2');
  288. else
  289. return $model;
  290. }
  291. public static function timeQuantumWhere($startTime = null,$endTime = null,$model = null)
  292. {
  293. if($model === null) $model = new self;
  294. if($startTime != null && $endTime != null)
  295. $model = $model->where('add_time','>',strtotime($startTime))->where('add_time','<',strtotime($endTime));
  296. return $model;
  297. }
  298. public static function changeOrderId($orderId)
  299. {
  300. $ymd = substr($orderId,2,8);
  301. $key = substr($orderId,16);
  302. return 'wx'.$ymd.date('His').$key;
  303. }
  304. /**
  305. * 线下付款
  306. * @param $id
  307. * @return $this
  308. */
  309. public static function updateOffline($id){
  310. $orderId = self::where('id',$id)->value('order_id');
  311. $res = self::where('order_id',$orderId)->update(['paid'=>1,'pay_time'=>time()]);
  312. return $res;
  313. }
  314. /**
  315. * 退款发送模板消息
  316. * @param $oid
  317. * $oid 订单id key
  318. */
  319. public static function refundTemplate($data,$oid)
  320. {
  321. $order = self::where('id',$oid)->find();
  322. WechatTemplateService::sendTemplate(WechatUser::uidToOpenid($order['uid']),WechatTemplateService::ORDER_REFUND_STATUS, [
  323. 'first'=>'亲,您购买的商品已退款,本次退款'.$data['refund_price'].'金额',
  324. 'keyword1'=>$order['order_id'],
  325. 'keyword2'=>$order['pay_price'],
  326. 'keyword3'=>date('Y-m-d H:i:s',$order['add_time']),
  327. 'remark'=>'点击查看订单详情'
  328. ],Url::build('wap/My/order',['uni'=>$order['order_id']],true,true));
  329. }
  330. /**
  331. * 处理where条件
  332. * @param $where
  333. * @param $model
  334. * @return mixed
  335. */
  336. public static function getOrderWhere($where,$model,$aler='',$join=''){
  337. // $model = $model->where('combination_id',0);
  338. if($where['status'] != '') $model = self::statusByWhere($where['status'],$model,$aler);
  339. if($where['is_del'] != '' && $where['is_del'] != -1) $model = $model->where($aler.'is_del',$where['is_del']);
  340. if(isset($where['combination_id'])){
  341. if($where['combination_id'] =='普通订单'){
  342. $model = $model->where($aler.'combination_id',0)->where($aler.'seckill_id',0);
  343. }
  344. if($where['combination_id'] =='拼团订单'){
  345. $model = $model->where($aler.'combination_id',">",0)->where($aler.'pink_id',">",0);
  346. }
  347. if($where['combination_id'] =='秒杀订单'){
  348. $model = $model->where($aler.'seckill_id',">",0);
  349. }
  350. }
  351. if(isset($where['type'])){
  352. switch ($where['type']){
  353. case 1:
  354. $model = $model->where($aler.'combination_id',0)->where($aler.'seckill_id',0);
  355. break;
  356. case 2:
  357. $model = $model->where($aler.'combination_id',">",0)->where($aler.'pink_id',">",0);
  358. break;
  359. case 3:
  360. $model = $model->where($aler.'seckill_id',">",0);
  361. break;
  362. }
  363. }
  364. if($where['real_name'] != ''){
  365. $model = $model->where($aler.'order_id|'.$aler.'real_name|'.$aler.'user_phone'.($join ? '|'.$join.'.nickname|'.$join.'.uid':''),'LIKE',"%$where[real_name]%");
  366. }
  367. if($where['data'] !== ''){
  368. $model = self::getModelTime($where,$model,$aler.'add_time');
  369. }
  370. return $model;
  371. }
  372. public static function getBadge($where){
  373. $price=self::getOrderPrice($where);
  374. return [
  375. [
  376. 'name'=>'订单数量',
  377. 'field'=>'件',
  378. 'count'=>$price['total_num'],
  379. 'background_color'=>'layui-bg-blue',
  380. 'col'=>2
  381. ],
  382. [
  383. 'name'=>'售出商品',
  384. 'field'=>'件',
  385. 'count'=>$price['total_num'],
  386. 'background_color'=>'layui-bg-blue',
  387. 'col'=>2
  388. ],
  389. [
  390. 'name'=>'订单金额',
  391. 'field'=>'元',
  392. 'count'=>$price['pay_price'],
  393. 'background_color'=>'layui-bg-blue',
  394. 'col'=>2
  395. ],
  396. [
  397. 'name'=>'退款金额',
  398. 'field'=>'元',
  399. 'count'=>$price['refund_price'],
  400. 'background_color'=>'layui-bg-blue',
  401. 'col'=>2
  402. ],
  403. [
  404. 'name'=>'微信支付金额',
  405. 'field'=>'元',
  406. 'count'=>$price['pay_price_wx'],
  407. 'background_color'=>'layui-bg-blue',
  408. 'col'=>2
  409. ],
  410. [
  411. 'name'=>'余额支付金额',
  412. 'field'=>'元',
  413. 'count'=>$price['pay_price_yue'],
  414. 'background_color'=>'layui-bg-blue',
  415. 'col'=>2
  416. ],
  417. [
  418. 'name'=>'线下支付金额',
  419. 'field'=>'元',
  420. 'count'=>$price['pay_price_offline'],
  421. 'background_color'=>'layui-bg-blue',
  422. 'col'=>2
  423. ],
  424. [
  425. 'name'=>'积分抵扣',
  426. 'field'=>'分',
  427. 'count'=>$price['use_integral'].'(抵扣金额:¥'.$price['deduction_price'].')',
  428. 'background_color'=>'layui-bg-blue',
  429. 'col'=>2
  430. ],
  431. [
  432. 'name'=>'退回积分',
  433. 'field'=>'元',
  434. 'count'=>$price['back_integral'],
  435. 'background_color'=>'layui-bg-blue',
  436. 'col'=>2
  437. ]
  438. ];
  439. }
  440. /**
  441. * 处理订单金额
  442. * @param $where
  443. * @return array
  444. */
  445. public static function getOrderPrice($where){
  446. $model = new self;
  447. $price = array();
  448. $price['pay_price'] = 0;//支付金额
  449. $price['refund_price'] = 0;//退款金额
  450. $price['pay_price_wx'] = 0;//微信支付金额
  451. $price['pay_price_yue'] = 0;//余额支付金额
  452. $price['pay_price_offline'] = 0;//线下支付金额
  453. $price['pay_price_other'] = 0;//其他支付金额
  454. $price['use_integral'] = 0;//用户使用积分
  455. $price['back_integral'] = 0;//退积分总数
  456. $price['deduction_price'] = 0;//抵扣金额
  457. $price['total_num'] = 0; //商品总数
  458. $model = self::getOrderWhere($where,$model);
  459. $list = $model->select()->toArray();
  460. foreach ($list as $v){
  461. $price['total_num'] = bcadd($price['total_num'],$v['total_num'],0);
  462. $price['pay_price'] = bcadd($price['pay_price'],$v['pay_price'],2);
  463. $price['refund_price'] = bcadd($price['refund_price'],$v['refund_price'],2);
  464. $price['use_integral'] = bcadd($price['use_integral'],$v['use_integral'],2);
  465. $price['back_integral'] = bcadd($price['back_integral'],$v['back_integral'],2);
  466. $price['deduction_price'] = bcadd($price['deduction_price'],$v['deduction_price'],2);
  467. if ($v['pay_type'] == 'weixin'){
  468. $price['pay_price_wx'] = bcadd($price['pay_price_wx'],$v['pay_price'],2);
  469. }elseif($v['pay_type'] == 'yue'){
  470. $price['pay_price_yue'] = bcadd($price['pay_price_yue'],$v['pay_price'],2);
  471. }elseif($v['pay_type'] == 'offline'){
  472. $price['pay_price_offline'] = bcadd($price['pay_price_offline'],$v['pay_price'],2);
  473. }else{
  474. $price['pay_price_other'] = bcadd($price['pay_price_other'],$v['pay_price'],2);
  475. }
  476. }
  477. return $price;
  478. }
  479. public static function systemPagePink($where){
  480. $model = new self;
  481. $model = self::getOrderWherePink($where,$model);
  482. $model = $model->order('id desc');
  483. if($where['export'] == 1){
  484. $list = $model->select()->toArray();
  485. $export = [];
  486. foreach ($list as $index=>$item){
  487. if ($item['pay_type'] == 'weixin'){
  488. $payType = '微信支付';
  489. }elseif($item['pay_type'] == 'yue'){
  490. $payType = '余额支付';
  491. }elseif($item['pay_type'] == 'offline'){
  492. $payType = '线下支付';
  493. }else{
  494. $payType = '其他支付';
  495. }
  496. $_info = Db::name('store_order_cart_info')->where('oid',$item['id'])->column('cart_info');
  497. $goodsName = [];
  498. foreach ($_info as $k=>$v){
  499. $v = json_decode($v,true);
  500. $goodsName[] = implode(
  501. [$v['productInfo']['store_name'],
  502. isset($v['productInfo']['attrInfo']) ? '('.$v['productInfo']['attrInfo']['suk'].')' : '',
  503. "[{$v['cart_num']} * {$v['truePrice']}]"
  504. ],' ');
  505. }
  506. $item['cartInfo'] = $_info;
  507. $export[] = [
  508. $item['order_id'],$payType,
  509. $item['total_num'],$item['total_price'],$item['total_postage'],$item['pay_price'],$item['refund_price'],
  510. $item['mark'],$item['remark'],
  511. [$item['real_name'],$item['user_phone'],$item['user_address']],
  512. $goodsName,
  513. [$item['paid'] == 1? '已支付':'未支付','支付时间: '.($item['pay_time'] > 0 ? date('Y/md H:i',$item['pay_time']) : '暂无')]
  514. ];
  515. $list[$index] = $item;
  516. }
  517. ExportService::exportCsv($export,'订单导出'.time(),['订单号','支付方式','商品总数','商品总价','邮费','支付金额','退款金额','用户备注','管理员备注','收货人信息','商品信息','支付状态']);
  518. }
  519. return self::page($model,function ($item){
  520. $item['nickname'] = WechatUser::where('uid',$item['uid'])->value('nickname');
  521. $_info = Db::name('store_order_cart_info')->where('oid',$item['id'])->field('cart_info')->select();
  522. foreach ($_info as $k=>$v){
  523. $_info[$k]['cart_info'] = json_decode($v['cart_info'],true);
  524. }
  525. $item['_info'] = $_info;
  526. },$where);
  527. }
  528. /**
  529. * 处理where条件
  530. * @param $where
  531. * @param $model
  532. * @return mixed
  533. */
  534. public static function getOrderWherePink($where,$model){
  535. $model = $model->where('combination_id','GT',0);
  536. if($where['status'] != '') $model = $model::statusByWhere($where['status']);
  537. // if($where['is_del'] != '' && $where['is_del'] != -1) $model = $model->where('is_del',$where['is_del']);
  538. if($where['real_name'] != ''){
  539. $model = $model->where('order_id|real_name|user_phone','LIKE',"%$where[real_name]%");
  540. }
  541. if($where['data'] !== ''){
  542. list($startTime,$endTime) = explode(' - ',$where['data']);
  543. $model = $model->where('add_time','>',strtotime($startTime));
  544. $model = $model->where('add_time','<',strtotime($endTime));
  545. }
  546. return $model;
  547. }
  548. /**
  549. * 处理订单金额
  550. * @param $where
  551. * @return array
  552. */
  553. public static function getOrderPricePink($where){
  554. $model = new self;
  555. $price = array();
  556. $price['pay_price'] = 0;//支付金额
  557. $price['refund_price'] = 0;//退款金额
  558. $price['pay_price_wx'] = 0;//微信支付金额
  559. $price['pay_price_yue'] = 0;//余额支付金额
  560. $price['pay_price_offline'] = 0;//线下支付金额
  561. $price['pay_price_other'] = 0;//其他支付金额
  562. $price['use_integral'] = 0;//用户使用积分
  563. $price['back_integral'] = 0;//退积分总数
  564. $price['deduction_price'] = 0;//抵扣金额
  565. $price['total_num'] = 0; //商品总数
  566. $model = self::getOrderWherePink($where,$model);
  567. $list = $model->select()->toArray();
  568. foreach ($list as $v){
  569. $price['total_num'] = bcadd($price['total_num'],$v['total_num'],0);
  570. $price['pay_price'] = bcadd($price['pay_price'],$v['pay_price'],2);
  571. $price['refund_price'] = bcadd($price['refund_price'],$v['refund_price'],2);
  572. $price['use_integral'] = bcadd($price['use_integral'],$v['use_integral'],2);
  573. $price['back_integral'] = bcadd($price['back_integral'],$v['back_integral'],2);
  574. $price['deduction_price'] = bcadd($price['deduction_price'],$v['deduction_price'],2);
  575. if ($v['pay_type'] == 'weixin'){
  576. $price['pay_price_wx'] = bcadd($price['pay_price_wx'],$v['pay_price'],2);
  577. }elseif($v['pay_type'] == 'yue'){
  578. $price['pay_price_yue'] = bcadd($price['pay_price_yue'],$v['pay_price'],2);
  579. }elseif($v['pay_type'] == 'offline'){
  580. $price['pay_price_offline'] = bcadd($price['pay_price_offline'],$v['pay_price'],2);
  581. }else{
  582. $price['pay_price_other'] = bcadd($price['pay_price_other'],$v['pay_price'],2);
  583. }
  584. }
  585. return $price;
  586. }
  587. /**
  588. * 获取昨天的订单 首页在使用
  589. * @param int $preDay
  590. * @param int $day
  591. * @return $this|StoreOrder
  592. */
  593. public static function isMainYesterdayCount($preDay = 0,$day = 0){
  594. $model = new self();
  595. $model = $model->where('add_time','gt',$preDay);
  596. $model = $model->where('add_time','lt',$day);
  597. return $model;
  598. }
  599. /**
  600. * 获取用户购买次数
  601. * @param int $uid
  602. * @return int|string
  603. */
  604. public static function getUserCountPay($uid = 0){
  605. if(!$uid) return 0;
  606. return self::where('uid',$uid)->where('paid',1)->count();
  607. }
  608. /**
  609. * 获取单个用户购买列表
  610. * @param array $where
  611. * @return array
  612. */
  613. public static function getOneorderList($where){
  614. return self::where(['uid'=>$where['uid']])
  615. ->order('add_time desc')
  616. ->page((int)$where['page'],(int)$where['limit'])
  617. ->field(['order_id','real_name','total_num','total_price','pay_price',
  618. 'FROM_UNIXTIME(pay_time,"%Y-%m-%d") as pay_time','paid','pay_type',
  619. 'pink_id','seckill_id','bargain_id'
  620. ])->select()
  621. ->toArray();
  622. }
  623. /*
  624. * 设置订单统计图搜索
  625. * $where array 条件
  626. * return object
  627. */
  628. public static function setEchatWhere($where,$status=null,$time=null){
  629. $model=self::statusByWhere($where['status']);
  630. if($status!==null) $where['type']=$status;
  631. if($time===true) $where['data']='';
  632. switch ($where['type']){
  633. case 1:
  634. //普通商品
  635. $model=$model->where('combination_id',0)->where('seckill_id',0);
  636. break;
  637. case 2:
  638. //拼团商品
  639. $model=$model->where('combination_id',">",0)->where('pink_id',">",0);
  640. break;
  641. case 3:
  642. //秒杀商品
  643. $model=$model->where('seckill_id',">",0);
  644. break;
  645. case 4:
  646. //砍价商品
  647. $model=$model->where('bargain_id','>',0);
  648. break;
  649. }
  650. return self::getModelTime($where,$model);
  651. }
  652. /*
  653. * 获取订单数据统计图
  654. * $where array
  655. * $limit int
  656. * return array
  657. */
  658. public static function getEchartsOrder($where,$limit=20){
  659. $orderlist=self::setEchatWhere($where)->field([
  660. 'FROM_UNIXTIME(add_time,"%Y-%m-%d") as _add_time',
  661. 'sum(total_num) total_num',
  662. 'count(*) count',
  663. 'sum(total_price) total_price',
  664. 'sum(refund_price) refund_price',
  665. 'group_concat(cart_id SEPARATOR "|") cart_ids'
  666. ])->group('_add_time')->order('_add_time asc')->select();
  667. count($orderlist) && $orderlist=$orderlist->toArray();
  668. $legend=['商品数量','订单数量','订单金额','退款金额'];
  669. $seriesdata=[
  670. [
  671. 'name'=>$legend[0],
  672. 'type'=>'line',
  673. 'data'=>[],
  674. ],
  675. [
  676. 'name'=>$legend[1],
  677. 'type'=>'line',
  678. 'data'=>[]
  679. ],
  680. [
  681. 'name'=>$legend[2],
  682. 'type'=>'line',
  683. 'data'=>[]
  684. ],
  685. [
  686. 'name'=>$legend[3],
  687. 'type'=>'line',
  688. 'data'=>[]
  689. ]
  690. ];
  691. $xdata=[];
  692. $zoom='';
  693. foreach ($orderlist as $item){
  694. $xdata[]=$item['_add_time'];
  695. $seriesdata[0]['data'][]=$item['total_num'];
  696. $seriesdata[1]['data'][]=$item['count'];
  697. $seriesdata[2]['data'][]=$item['total_price'];
  698. $seriesdata[3]['data'][]=$item['refund_price'];
  699. }
  700. count($xdata) > $limit && $zoom=$xdata[$limit-5];
  701. $badge=self::getOrderBadge($where);
  702. $bingpaytype=self::setEchatWhere($where)->group('pay_type')->field(['count(*) as count','pay_type'])->select();
  703. count($bingpaytype) && $bingpaytype=$bingpaytype->toArray();
  704. $bing_xdata=['微信支付','余额支付','其他支付'];
  705. $color=['#ffcccc','#99cc00','#fd99cc','#669966'];
  706. $bing_data=[];
  707. foreach ($bingpaytype as $key=>$item){
  708. if($item['pay_type']=='weixin'){
  709. $value['name']=$bing_xdata[0];
  710. }else if($item['pay_type']=='yue'){
  711. $value['name']=$bing_xdata[1];
  712. }else{
  713. $value['name']=$bing_xdata[2];
  714. }
  715. $value['value']=$item['count'];
  716. $value['itemStyle']['color']=isset($color[$key]) ? $color[$key]:$color[0];
  717. $bing_data[]=$value;
  718. }
  719. return compact('zoom','xdata','seriesdata','badge','legend','bing_data','bing_xdata');
  720. }
  721. public static function getOrderBadge($where){
  722. return [
  723. [
  724. 'name'=>'拼团订单数量',
  725. 'field'=>'个',
  726. 'count'=>self::setEchatWhere($where,2)->count(),
  727. 'content'=>'拼团总订单数量',
  728. 'background_color'=>'layui-bg-cyan',
  729. 'sum'=>self::setEchatWhere($where,2,true)->count(),
  730. 'class'=>'fa fa-line-chart',
  731. 'col'=>2
  732. ],
  733. [
  734. 'name'=>'砍价订单数量',
  735. 'field'=>'个',
  736. 'count'=>self::setEchatWhere($where,4)->count(),
  737. 'content'=>'砍价总订单数量',
  738. 'background_color'=>'layui-bg-cyan',
  739. 'sum'=>self::setEchatWhere($where,4,true)->count(),
  740. 'class'=>'fa fa-line-chart',
  741. 'col'=>2
  742. ],
  743. [
  744. 'name'=>'秒杀订单数量',
  745. 'field'=>'个',
  746. 'count'=>self::setEchatWhere($where,3)->count(),
  747. 'content'=>'秒杀总订单数量',
  748. 'background_color'=>'layui-bg-cyan',
  749. 'sum'=>self::setEchatWhere($where,3,true)->count(),
  750. 'class'=>'fa fa-line-chart',
  751. 'col'=>2
  752. ],
  753. [
  754. 'name'=>'普通订单数量',
  755. 'field'=>'个',
  756. 'count'=>self::setEchatWhere($where,1)->count(),
  757. 'content'=>'普通总订单数量',
  758. 'background_color'=>'layui-bg-cyan',
  759. 'sum'=>self::setEchatWhere($where,1,true)->count(),
  760. 'class'=>'fa fa-line-chart',
  761. 'col'=>2,
  762. ],
  763. [
  764. 'name'=>'使用优惠卷金额',
  765. 'field'=>'元',
  766. 'count'=>self::setEchatWhere($where)->sum('coupon_price'),
  767. 'content'=>'普通总订单数量',
  768. 'background_color'=>'layui-bg-cyan',
  769. 'sum'=>self::setEchatWhere($where,null,true)->sum('coupon_price'),
  770. 'class'=>'fa fa-line-chart',
  771. 'col'=>2
  772. ],
  773. [
  774. 'name'=>'积分消耗数',
  775. 'field'=>'个',
  776. 'count'=>self::setEchatWhere($where)->sum('use_integral'),
  777. 'content'=>'积分消耗总数',
  778. 'background_color'=>'layui-bg-cyan',
  779. 'sum'=>self::setEchatWhere($where,null,true)->sum('use_integral'),
  780. 'class'=>'fa fa-line-chart',
  781. 'col'=>2
  782. ],
  783. [
  784. 'name'=>'积分抵扣金额',
  785. 'field'=>'个',
  786. 'count'=>self::setEchatWhere($where)->sum('deduction_price'),
  787. 'content'=>'积分抵扣总金额',
  788. 'background_color'=>'layui-bg-cyan',
  789. 'sum'=>self::setEchatWhere($where,null,true)->sum('deduction_price'),
  790. 'class'=>'fa fa-money',
  791. 'col'=>2
  792. ],
  793. [
  794. 'name'=>'在线支付金额',
  795. 'field'=>'元',
  796. 'count'=>self::setEchatWhere($where)->where('pay_type','weixin')->sum('pay_price'),
  797. 'content'=>'在线支付总金额',
  798. 'background_color'=>'layui-bg-cyan',
  799. 'sum'=>self::setEchatWhere($where,null,true)->where('pay_type','weixin')->sum('pay_price'),
  800. 'class'=>'fa fa-weixin',
  801. 'col'=>2
  802. ],
  803. [
  804. 'name'=>'余额支付金额',
  805. 'field'=>'元',
  806. 'count'=>self::setEchatWhere($where)->where('pay_type','yue')->sum('pay_price'),
  807. 'content'=>'余额支付总金额',
  808. 'background_color'=>'layui-bg-cyan',
  809. 'sum'=>self::setEchatWhere($where,null,true)->where('pay_type','yue')->sum('pay_price'),
  810. 'class'=>'fa fa-balance-scale',
  811. 'col'=>2
  812. ],
  813. [
  814. 'name'=>'赚取积分',
  815. 'field'=>'分',
  816. 'count'=>self::setEchatWhere($where)->sum('gain_integral'),
  817. 'content'=>'赚取总积分',
  818. 'background_color'=>'layui-bg-cyan',
  819. 'sum'=>self::setEchatWhere($where,null,true)->sum('gain_integral'),
  820. 'class'=>'fa fa-gg-circle',
  821. 'col'=>2
  822. ],
  823. [
  824. 'name'=>'交易额',
  825. 'field'=>'元',
  826. 'count'=>self::setEchatWhere($where)->sum('pay_price'),
  827. 'content'=>'总交易额',
  828. 'background_color'=>'layui-bg-cyan',
  829. 'sum'=>self::setEchatWhere($where,null,true)->sum('pay_price'),
  830. 'class'=>'fa fa-jpy',
  831. 'col'=>2
  832. ],
  833. [
  834. 'name'=>'订单商品数量',
  835. 'field'=>'元',
  836. 'count'=>self::setEchatWhere($where)->sum('total_num'),
  837. 'content'=>'订单商品总数量',
  838. 'background_color'=>'layui-bg-cyan',
  839. 'sum'=>self::setEchatWhere($where,null,true)->sum('total_num'),
  840. 'class'=>'fa fa-cube',
  841. 'col'=>2
  842. ]
  843. ];
  844. }
  845. /*
  846. * 退款列表
  847. * $where array
  848. * return array
  849. */
  850. // public static function getRefundList($where){
  851. // $refundlist=self::setEchatWhere($where)->field([
  852. // 'order_id','total_price','coupon_price','deduction_price',
  853. // 'use_integral','FROM_UNIXTIME(add_time,"%Y-%m-%d") as add_time','FROM_UNIXTIME(pay_time,"%Y-%m-%d") as pay_time','combination_id',
  854. // 'seckill_id','bargain_id','cost','status','cart_id','pay_price','refund_status'
  855. // ])->page((int)$where['page'],(int)$where['limit'])->select();
  856. // count($refundlist) && $refundlist=$refundlist->toArray();
  857. // foreach($refundlist as &$item){
  858. // $item['product']=StoreProduct::where('id','in',function ($quers) use($item){
  859. // $quers->name('store_cart')->where('id','in',json_decode($item['cart_id'],true))->field('product_id');
  860. // })->field(['store_name','cost','price','image'])->select()->toArray();
  861. // if($item['refund_status']==1) {
  862. // $item['_refund'] = '申请退款中';
  863. // }elseif ($item['refund_status']==2){
  864. // $item['_refund'] = '退款成功';
  865. // }
  866. // }
  867. // return $refundlist;
  868. // }
  869. /**
  870. * 获取订单总数
  871. * @param int $uid
  872. * @return int|string
  873. */
  874. public static function getOrderCount($uid = 0){
  875. if(!$uid) return 0;
  876. return self::where('uid',$uid)->where('paid',1)->where('refund_status',0)->where('status',2)->count();
  877. }
  878. }