StoreOrder.php 57 KB

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