StoreOrder.php 58 KB

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