StoreOrder.php 56 KB

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