StoreOrder.php 47 KB

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