StoreOrder.php 56 KB

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