StoreOrder.php 56 KB

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