StoreOrder.php 56 KB

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