StoreOrder.php 56 KB

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