StoreOrder.php 38 KB

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