StoreOrder.php 41 KB

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