StoreOrder.php 55 KB

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