StoreOrder.php 60 KB

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