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