StoreOrder.php 57 KB

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