StoreOrder.php 58 KB

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