StoreOrder.php 47 KB

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