StoreOrder.php 56 KB

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