StoreOrder.php 56 KB

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