StoreOrder.php 56 KB

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