StoreOrder.php 57 KB

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