StoreOrder.php 38 KB

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