StoreOrder.php 115 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668
  1. <<<<<<< HEAD
  2. <?php
  3. /**
  4. *
  5. * @author: xaboy<365615158@qq.com>
  6. * @day: 2017/11/11
  7. */
  8. namespace app\admin\model\order;
  9. use crmeb\basic\BaseModel;
  10. use crmeb\traits\ModelTrait;
  11. use think\facade\Route as Url;
  12. use app\models\store\StoreCart;
  13. use app\admin\model\wechat\WechatUser;
  14. use app\admin\model\store\StoreProduct;
  15. use app\models\routine\RoutineTemplate;
  16. use app\admin\model\user\{User, UserBill};
  17. use app\admin\model\ump\{StoreCouponUser, StorePink};
  18. use crmeb\services\{PHPExcelService, WechatService, WechatTemplateService};
  19. /**
  20. * 订单管理Model
  21. * Class StoreOrder
  22. * @package app\admin\model\store
  23. */
  24. class StoreOrder extends BaseModel
  25. {
  26. /**
  27. * 数据表主键
  28. * @var string
  29. */
  30. protected $pk = 'id';
  31. /**
  32. * 模型名称
  33. * @var string
  34. */
  35. protected $name = 'store_order';
  36. use ModelTrait;
  37. public static function orderCount()
  38. {
  39. $data['ys'] = self::statusByWhere(9, new self())->where(['is_system_del' => 0])->count();
  40. $data['wz'] = self::statusByWhere(0, new self())->where(['is_system_del' => 0])->count();
  41. $data['wf'] = self::statusByWhere(1, new self())->where(['is_system_del' => 0, 'shipping_type' => 1])->count();
  42. $data['ds'] = self::statusByWhere(2, new self())->where(['is_system_del' => 0, 'shipping_type' => 1])->count();
  43. $data['dp'] = self::statusByWhere(3, new self())->where(['is_system_del' => 0])->count();
  44. $data['jy'] = self::statusByWhere(4, new self())->where(['is_system_del' => 0])->count();
  45. $data['tk'] = self::statusByWhere(-1, new self())->where(['is_system_del' => 0])->count();
  46. $data['yt'] = self::statusByWhere(-2, new self())->where(['is_system_del' => 0])->count();
  47. $data['del'] = self::statusByWhere(-4, new self())->where(['is_system_del' => 0])->count();
  48. $data['write_off'] = self::statusByWhere(5, new self())->where(['is_system_del' => 0])->count();
  49. $data['general'] = self::where(['pink_id' => 0, 'combination_id' => 0, 'seckill_id' => 0, 'bargain_id' => 0, 'is_system_del' => 0])->count();
  50. $data['pink'] = self::where('pink_id|combination_id', '>', 0)->where('is_system_del', 0)->count();
  51. $data['seckill'] = self::where('seckill_id', '>', 0)->where('is_system_del', 0)->count();
  52. $data['bargain'] = self::where('bargain_id', '>', 0)->where('is_system_del', 0)->count();
  53. return $data;
  54. }
  55. public static function OrderList($where)
  56. {
  57. $model = self::getOrderWhere($where, self::alias('a')
  58. ->join('user r', 'r.uid=a.uid', 'LEFT'), 'a.', 'r')
  59. ->field('a.*,r.nickname,r.phone,r.spread_uid');
  60. if ($where['order'] != '') {
  61. $model = $model->order(self::setOrder($where['order']));
  62. } else {
  63. $model = $model->order('a.id desc');
  64. }
  65. if (isset($where['excel']) && $where['excel'] == 1) {
  66. $data = ($data = $model->select()) && count($data) ? $data->toArray() : [];
  67. } else {
  68. $data = ($data = $model->page((int)$where['page'], (int)$where['limit'])->select()) && count($data) ? $data->toArray() : [];
  69. }
  70. foreach ($data as &$item) {
  71. $_info = StoreOrderCartInfo::where('oid', $item['id'])->field('cart_info')->select();
  72. $_info = count($_info) ? $_info->toArray() : [];
  73. foreach ($_info as $k => $v) {
  74. $cart_info = json_decode($v['cart_info'], true);
  75. if (!isset($cart_info['productInfo'])) $cart_info['productInfo'] = [];
  76. $_info[$k]['cart_info'] = $cart_info;
  77. unset($cart_info);
  78. }
  79. $item['_info'] = $_info;
  80. $item['spread_nickname'] = User::where('uid', $item['spread_uid'])->value('nickname');
  81. $item['add_time'] = $item['add_time'] ? date('Y-m-d H:i:s', $item['add_time']) : '';
  82. $item['back_integral'] = $item['back_integral'] ?: 0;
  83. if ($item['pink_id'] || $item['combination_id']) {
  84. $pinkStatus = StorePink::where('order_id_key', $item['id'])->value('status');
  85. switch ($pinkStatus) {
  86. case 1:
  87. $item['pink_name'] = '[拼团订单]正在进行中';
  88. $item['color'] = '#f00';
  89. break;
  90. case 2:
  91. $item['pink_name'] = '[拼团订单]已完成';
  92. $item['color'] = '#00f';
  93. break;
  94. case 3:
  95. $item['pink_name'] = '[拼团订单]未完成';
  96. $item['color'] = '#f0f';
  97. break;
  98. default:
  99. $item['pink_name'] = '[拼团订单]历史订单';
  100. $item['color'] = '#457856';
  101. break;
  102. }
  103. } elseif ($item['seckill_id']) {
  104. $item['pink_name'] = '[秒杀订单]';
  105. $item['color'] = '#32c5e9';
  106. } elseif ($item['bargain_id']) {
  107. $item['pink_name'] = '[砍价订单]';
  108. $item['color'] = '#12c5e9';
  109. } else {
  110. if ($item['shipping_type'] == 1) {
  111. $item['pink_name'] = '[普通订单]';
  112. $item['color'] = '#895612';
  113. } else if ($item['shipping_type'] == 2) {
  114. $item['pink_name'] = '[核销订单]';
  115. $item['color'] = '#8956E8';
  116. }
  117. }
  118. if ($item['paid'] == 1) {
  119. switch ($item['pay_type']) {
  120. case 'weixin':
  121. $item['pay_type_name'] = '微信支付';
  122. break;
  123. case 'yue':
  124. $item['pay_type_name'] = '余额支付';
  125. break;
  126. case 'ali':
  127. $item['pay_type_name'] = '支付宝支付';
  128. break;
  129. case 'offline':
  130. $item['pay_type_name'] = '线下支付';
  131. break;
  132. default:
  133. $item['pay_type_name'] = '其他支付';
  134. break;
  135. }
  136. } else {
  137. switch ($item['pay_type']) {
  138. default:
  139. $item['pay_type_name'] = '未支付';
  140. break;
  141. case 'offline':
  142. $item['pay_type_name'] = '线下支付';
  143. $item['pay_type_info'] = 1;
  144. break;
  145. }
  146. }
  147. if ($item['paid'] == 0 && $item['status'] == 0) {
  148. $item['status_name'] = '未支付';
  149. } else if ($item['paid'] == 1 && $item['status'] == 0 && $item['shipping_type'] == 1 && $item['refund_status'] == 0) {
  150. $item['status_name'] = '未发货';
  151. } else if ($item['paid'] == 1 && $item['status'] == 0 && $item['shipping_type'] == 2 && $item['refund_status'] == 0) {
  152. $item['status_name'] = '未核销';
  153. } else if ($item['paid'] == 1 && $item['status'] == 1 && $item['shipping_type'] == 1 && $item['refund_status'] == 0) {
  154. $item['status_name'] = '待收货';
  155. } else if ($item['paid'] == 1 && $item['status'] == 1 && $item['shipping_type'] == 2 && $item['refund_status'] == 0) {
  156. $item['status_name'] = '未核销';
  157. } else if ($item['paid'] == 1 && $item['status'] == 2 && $item['refund_status'] == 0) {
  158. $item['status_name'] = '待评价';
  159. } else if ($item['paid'] == 1 && $item['status'] == 3 && $item['refund_status'] == 0) {
  160. $item['status_name'] = '已完成';
  161. } else if ($item['paid'] == 1 && $item['refund_status'] == 1) {
  162. $refundReasonTime = date('Y-m-d H:i', $item['refund_reason_time']);
  163. $refundReasonWapImg = json_decode($item['refund_reason_wap_img'], true);
  164. $refundReasonWapImg = $refundReasonWapImg ? $refundReasonWapImg : [];
  165. $img = '';
  166. if (count($refundReasonWapImg)) {
  167. foreach ($refundReasonWapImg as $itemImg) {
  168. if (strlen(trim($itemImg)))
  169. $img .= '<img style="height:50px;" src="' . $itemImg . '" />';
  170. }
  171. }
  172. if (!strlen(trim($img))) $img = '无';
  173. if (isset($where['excel']) && $where['excel'] == 1) {
  174. $refundImageStr = implode(',', $refundReasonWapImg);
  175. $item['status_name'] = <<<TEXT
  176. 退款原因:{$item['refund_reason_wap']}
  177. 备注说明:{$item['refund_reason_wap_explain']}
  178. 退款时间:{$refundReasonTime}
  179. 凭证连接:{$refundImageStr}
  180. TEXT;
  181. unset($refundImageStr);
  182. } else {
  183. $item['status_name'] = <<<HTML
  184. <b style="color:#f124c7">申请退款</b><br/>
  185. <span>退款原因:{$item['refund_reason_wap']}</span><br/>
  186. <span>备注说明:{$item['refund_reason_wap_explain']}</span><br/>
  187. <span>退款时间:{$refundReasonTime}</span><br/>
  188. <span>退款凭证:{$img}</span>
  189. HTML;
  190. }
  191. } else if ($item['paid'] == 1 && $item['refund_status'] == 2) {
  192. $item['status_name'] = '已退款';
  193. }
  194. if ($item['paid'] == 1 && $item['status'] == 0 && $item['shipping_type'] == 2) {
  195. $item['_status'] = 0;
  196. } else if ($item['paid'] == 0 && $item['status'] == 0 && $item['refund_status'] == 0) {
  197. $item['_status'] = 1;
  198. } else if ($item['paid'] == 1 && $item['status'] == 0 && $item['refund_status'] == 0) {
  199. $item['_status'] = 2;
  200. } else if ($item['paid'] == 1 && $item['refund_status'] == 1) {
  201. $item['_status'] = 3;
  202. } else if ($item['paid'] == 1 && $item['status'] == 1 && $item['refund_status'] == 0) {
  203. $item['_status'] = 4;
  204. } else if ($item['paid'] == 1 && $item['status'] == 2 && $item['refund_status'] == 0) {
  205. $item['_status'] = 5;
  206. } else if ($item['paid'] == 1 && $item['status'] == 3 && $item['refund_status'] == 0) {
  207. $item['_status'] = 6;
  208. } else if ($item['paid'] == 1 && $item['refund_status'] == 2) {
  209. $item['_status'] = 7;
  210. }
  211. }
  212. if (isset($where['excel']) && $where['excel'] == 1) {
  213. self::SaveExcel($data);
  214. }
  215. $count = self::getOrderWhere($where, self::alias('a')->join('user r', 'r.uid=a.uid', 'LEFT'), 'a.', 'r')->count();
  216. return compact('count', 'data');
  217. }
  218. /*
  219. * 保存并下载excel
  220. * $list array
  221. * return
  222. */
  223. public static function SaveExcel($list)
  224. {
  225. $export = [];
  226. foreach ($list as $index => $item) {
  227. $_info = StoreOrderCartInfo::where('oid', $item['id'])->column('cart_info');
  228. $goodsName = [];
  229. foreach ($_info as $k => $v) {
  230. $v = json_decode($v, true);
  231. $suk = '';
  232. if (isset($v['productInfo']['attrInfo'])) {
  233. if (isset($v['productInfo']['attrInfo']['suk'])) {
  234. $suk = '(' . $v['productInfo']['attrInfo']['suk'] . ')';
  235. }
  236. }
  237. $goodsName[] = implode(
  238. [$v['productInfo']['store_name'],
  239. $suk,
  240. "[{$v['cart_num']} * {$v['truePrice']}]"
  241. ], ' ');
  242. }
  243. $item['cartInfo'] = $_info;
  244. $sex = WechatUser::where('uid', $item['uid'])->value('sex');
  245. if ($sex == 1) $sex_name = '男';
  246. else if ($sex == 2) $sex_name = '女';
  247. else $sex_name = '未知';
  248. $export[] = [
  249. $item['order_id'],
  250. $sex_name,
  251. $item['phone'],
  252. $item['real_name'],
  253. $item['user_phone'],
  254. $item['user_address'],
  255. $goodsName,
  256. $item['total_price'],
  257. $item['pay_price'],
  258. $item['pay_postage'],
  259. $item['coupon_price'],
  260. $item['pay_type_name'],
  261. $item['pay_time'] > 0 ? date('Y/m-d H:i', $item['pay_time']) : '暂无',
  262. $item['status_name'],
  263. $item['add_time'],
  264. $item['mark']
  265. ];
  266. }
  267. PHPExcelService::setExcelHeader(['订单号', '性别', '电话', '收货人姓名', '收货人电话', '收货地址', '商品信息',
  268. '总价格', '实际支付', '邮费', '优惠金额', '支付状态', '支付时间', '订单状态', '下单时间', '用户备注'])
  269. ->setExcelTile('订单导出' . date('YmdHis', time()), '订单信息' . time(), ' 生成时间:' . date('Y-m-d H:i:s', time()))
  270. ->setExcelContent($export)
  271. ->ExcelSave();
  272. }
  273. /**
  274. * @param $where
  275. * @return array
  276. */
  277. public static function systemPage($where, $userid = false)
  278. {
  279. $model = self::getOrderWhere($where, self::alias('a')
  280. ->join('user r', 'r.uid=a.uid', 'LEFT'), 'a.', 'r')
  281. ->field('a.*,r.nickname');
  282. if ($where['order']) {
  283. $model = $model->order('a.' . $where['order']);
  284. } else {
  285. $model = $model->order('a.id desc');
  286. }
  287. if ($where['export'] == 1) {
  288. $list = $model->select()->toArray();
  289. $export = [];
  290. foreach ($list as $index => $item) {
  291. if ($item['pay_type'] == 'weixin') {
  292. $payType = '微信支付';
  293. } elseif ($item['pay_type'] == 'yue') {
  294. $payType = '余额支付';
  295. } elseif ($item['pay_type'] == 'offline') {
  296. $payType = '线下支付';
  297. } else {
  298. $payType = '其他支付';
  299. }
  300. $_info = StoreOrderCartInfo::where('oid', $item['id'])->column('cart_info', 'oid');
  301. $goodsName = [];
  302. foreach ($_info as $k => $v) {
  303. $v = json_decode($v, true);
  304. $goodsName[] = implode(
  305. [$v['productInfo']['store_name'],
  306. isset($v['productInfo']['attrInfo']) ? '(' . $v['productInfo']['attrInfo']['suk'] . ')' : '',
  307. "[{$v['cart_num']} * {$v['truePrice']}]"
  308. ], ' ');
  309. }
  310. $item['cartInfo'] = $_info;
  311. $export[] = [
  312. $item['order_id'], $payType,
  313. $item['total_num'], $item['total_price'], $item['total_postage'], $item['pay_price'], $item['refund_price'],
  314. $item['mark'], $item['remark'],
  315. [$item['real_name'], $item['user_phone'], $item['user_address']],
  316. $goodsName,
  317. [$item['paid'] == 1 ? '已支付' : '未支付', '支付时间: ' . ($item['pay_time'] > 0 ? date('Y/md H:i', $item['pay_time']) : '暂无')]
  318. ];
  319. $list[$index] = $item;
  320. }
  321. PHPExcelService::setExcelHeader(['订单号', '支付方式', '商品总数', '商品总价', '邮费', '支付金额', '退款金额', '用户备注', '管理员备注', '收货人信息', '商品信息', '支付状态'])
  322. ->setExcelTile('订单导出', '订单信息' . time(), ' 生成时间:' . date('Y-m-d H:i:s', time()))
  323. ->setExcelContent($export)
  324. ->ExcelSave();
  325. }
  326. return self::page($model, function ($item) {
  327. $_info = StoreOrderCartInfo::where('oid', $item['id'])->field('cart_info')->select();
  328. foreach ($_info as $k => $v) {
  329. $_info[$k]['cart_info'] = json_decode($v['cart_info'], true);
  330. }
  331. $item['_info'] = $_info;
  332. if ($item['pink_id'] && $item['combination_id']) {
  333. $pinkStatus = StorePink::where('order_id_key', $item['id'])->value('status');
  334. switch ($pinkStatus) {
  335. case 1:
  336. $item['pink_name'] = '[拼团订单]正在进行中';
  337. $item['color'] = '#f00';
  338. break;
  339. case 2:
  340. $item['pink_name'] = '[拼团订单]已完成';
  341. $item['color'] = '#00f';
  342. break;
  343. case 3:
  344. $item['pink_name'] = '[拼团订单]未完成';
  345. $item['color'] = '#f0f';
  346. break;
  347. default:
  348. $item['pink_name'] = '[拼团订单]历史订单';
  349. $item['color'] = '#457856';
  350. break;
  351. }
  352. } else {
  353. if ($item['seckill_id']) {
  354. $item['pink_name'] = '[秒杀订单]';
  355. $item['color'] = '#32c5e9';
  356. } elseif ($item['bargain_id']) {
  357. $item['pink_name'] = '[砍价订单]';
  358. $item['color'] = '#12c5e9';
  359. } else {
  360. $item['pink_name'] = '[普通订单]';
  361. $item['color'] = '#895612';
  362. }
  363. }
  364. }, $where);
  365. }
  366. public static function statusByWhere($status, $model = null, $alert = '')
  367. {
  368. if ($model == null) $model = new self;
  369. if ('' === $status)
  370. return $model;
  371. else if ($status == 8)
  372. return $model;
  373. else if ($status == 0)//未支付
  374. return $model->where($alert . 'paid', 0)->where($alert . 'status', 0)->where($alert . 'refund_status', 0)->where($alert . 'is_del', 0);
  375. else if ($status == 1)//已支付 未发货
  376. 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);
  377. else if ($status == 2)//已支付 待收货
  378. 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);
  379. else if ($status == 5)//已支付 待核销
  380. 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);
  381. else if ($status == 3)// 已支付 已收货 待评价
  382. return $model->where($alert . 'paid', 1)->where($alert . 'status', 2)->where($alert . 'refund_status', 0)->where($alert . 'is_del', 0);
  383. else if ($status == 4)// 交易完成
  384. return $model->where($alert . 'paid', 1)->where($alert . 'status', 3)->where($alert . 'refund_status', 0)->where($alert . 'is_del', 0);
  385. else if ($status == -1)//退款中
  386. return $model->where($alert . 'paid', 1)->where($alert . 'refund_status', 1)->where($alert . 'is_del', 0);
  387. else if ($status == -2)//已退款
  388. return $model->where($alert . 'paid', 1)->where($alert . 'refund_status', 2)->where($alert . 'is_del', 0);
  389. else if ($status == -3)//退款
  390. return $model->where($alert . 'paid', 1)->where($alert . 'refund_status', 'in', '1,2')->where($alert . 'is_del', 0);
  391. else if ($status == -4)//已删除
  392. return $model->where($alert . 'is_del', 1);
  393. else if ($status == 9)//已卖出
  394. return $model->where($alert . 'paid', 1)->where($alert . 'refund_status', 0)->where($alert . 'is_del', 0);
  395. else
  396. return $model;
  397. }
  398. public static function timeQuantumWhere($startTime = null, $endTime = null, $model = null)
  399. {
  400. if ($model === null) $model = new self;
  401. if ($startTime != null && $endTime != null)
  402. $model = $model->where('add_time', '>', strtotime($startTime))->where('add_time', '<', strtotime($endTime));
  403. return $model;
  404. }
  405. public static function changeOrderId($orderId)
  406. {
  407. $ymd = substr($orderId, 2, 8);
  408. $key = substr($orderId, 16);
  409. return 'wx' . $ymd . date('His') . $key;
  410. }
  411. /**
  412. * 线下付款
  413. * @param $id
  414. * @return $this
  415. */
  416. public static function updateOffline($id)
  417. {
  418. $count = self::where('id', $id)->count();
  419. if (!$count) return self::setErrorInfo('订单不存在');
  420. $count = self::where('id', $id)->where('paid', 0)->count();
  421. if (!$count) return self::setErrorInfo('订单已支付');
  422. $res = self::where('id', $id)->update(['paid' => 1, 'pay_time' => time()]);
  423. return $res;
  424. }
  425. /**
  426. * TODO 公众号退款发送模板消息
  427. * @param $oid
  428. * $oid 订单id key
  429. */
  430. public static function refundTemplate($data, $oid)
  431. {
  432. $order = self::where('id', $oid)->find();
  433. WechatTemplateService::sendTemplate(WechatUser::where('uid', $order['uid'])->value('openid'), WechatTemplateService::ORDER_REFUND_STATUS, [
  434. 'first' => '亲,您购买的商品已退款,本次退款' . $data['refund_price'] . '金额',
  435. 'keyword1' => $order['order_id'],
  436. 'keyword2' => $order['pay_price'],
  437. 'keyword3' => date('Y-m-d H:i:s', $order['add_time']),
  438. 'remark' => '点击查看订单详情'
  439. ], Url::buildUrl('/order/detail/' . $order['order_id'])->suffix('')->domain(true)->build());
  440. }
  441. /**
  442. * TODO 小程序余额退款模板消息
  443. * @param $oid
  444. * @return bool|mixed
  445. * @throws \think\db\exception\DataNotFoundException
  446. * @throws \think\db\exception\ModelNotFoundException
  447. * @throws \think\exception\DbException
  448. */
  449. public static function refundRoutineTemplate($oid)
  450. {
  451. $order = self::where('id', $oid)->find();
  452. return RoutineTemplate::sendOrderRefundSuccess($order);
  453. }
  454. /**
  455. * 处理where条件
  456. * @param $where
  457. * @param $model
  458. * @return mixed
  459. */
  460. public static function getOrderWhere($where, $model, $aler = '', $join = '')
  461. {
  462. // $model = $model->where('combination_id',0);
  463. $model = $model->where('is_system_del', 0);
  464. if (isset($where['status']) && $where['status'] != '') {
  465. $model = self::statusByWhere($where['status'], $model, $aler);
  466. }
  467. if (isset($where['is_del']) && $where['is_del'] != '' && $where['is_del'] != -1) $model = $model->where($aler . 'is_del', $where['is_del']);
  468. if (isset($where['combination_id'])) {
  469. if ($where['combination_id'] == '普通订单') {
  470. $model = $model->where($aler . 'combination_id', 0)->where($aler . 'seckill_id', 0)->where($aler . 'bargain_id', 0);
  471. }
  472. if ($where['combination_id'] == '拼团订单') {
  473. $model = $model->where($aler . 'combination_id', ">", 0)->where($aler . 'pink_id', ">", 0);
  474. }
  475. if ($where['combination_id'] == '秒杀订单') {
  476. $model = $model->where($aler . 'seckill_id', ">", 0);
  477. }
  478. if ($where['combination_id'] == '砍价订单') {
  479. $model = $model->where($aler . 'bargain_id', ">", 0);
  480. }
  481. }
  482. if (isset($where['pay_type'])) {
  483. switch ($where['pay_type']) {
  484. case 1:
  485. $model = $model->where($aler . 'pay_type', 'weixin');
  486. break;
  487. case 2:
  488. $model = $model->where($aler . 'pay_type', 'yue');
  489. break;
  490. case 3:
  491. $model = $model->where($aler . 'pay_type', 'offline');
  492. break;
  493. }
  494. }
  495. if (isset($where['type'])) {
  496. switch ($where['type']) {
  497. case 1:
  498. $model = $model->where($aler . 'combination_id', 0)->where($aler . 'seckill_id', 0)->where($aler . 'bargain_id', 0);
  499. break;
  500. case 2:
  501. // $model = $model->where($aler.'combination_id',">",0)->where($aler.'pink_id',">",0);
  502. $model = $model->where($aler . 'combination_id', ">", 0);
  503. break;
  504. case 3:
  505. $model = $model->where($aler . 'seckill_id', ">", 0);
  506. break;
  507. case 4:
  508. $model = $model->where($aler . 'bargain_id', ">", 0);
  509. break;
  510. }
  511. }
  512. if (isset($where['real_name']) && $where['real_name'] != '') {
  513. $model = $model->where($aler . 'order_id|' . $aler . 'real_name|' . $aler . 'user_phone' . ($join ? '|' . $join . '.nickname|' . $join . '.uid|' . $join . '.phone' : ''), 'LIKE', "%$where[real_name]%");
  514. }
  515. if (isset($where['data']) && $where['data'] !== '') {
  516. $model = self::getModelTime($where, $model, $aler . 'add_time');
  517. }
  518. return $model;
  519. }
  520. public static function getBadge($where)
  521. {
  522. $price = self::getOrderPrice($where);
  523. return [
  524. [
  525. 'name' => '订单数量',
  526. 'field' => '件',
  527. 'count' => $price['count_sum'],
  528. 'background_color' => 'layui-bg-blue',
  529. 'col' => 2
  530. ],
  531. [
  532. 'name' => '售出商品',
  533. 'field' => '件',
  534. 'count' => $price['total_num'],
  535. 'background_color' => 'layui-bg-blue',
  536. 'col' => 2
  537. ],
  538. [
  539. 'name' => '订单金额',
  540. 'field' => '元',
  541. 'count' => $price['pay_price'],
  542. 'background_color' => 'layui-bg-blue',
  543. 'col' => 2
  544. ],
  545. [
  546. 'name' => '退款金额',
  547. 'field' => '元',
  548. 'count' => $price['refund_price'],
  549. 'background_color' => 'layui-bg-blue',
  550. 'col' => 2
  551. ],
  552. [
  553. 'name' => '微信支付金额',
  554. 'field' => '元',
  555. 'count' => $price['pay_price_wx'],
  556. 'background_color' => 'layui-bg-blue',
  557. 'col' => 2
  558. ],
  559. [
  560. 'name' => '余额支付金额',
  561. 'field' => '元',
  562. 'count' => $price['pay_price_yue'],
  563. 'background_color' => 'layui-bg-blue',
  564. 'col' => 2
  565. ],
  566. [
  567. 'name' => '运费金额',
  568. 'field' => '元',
  569. 'count' => $price['pay_postage'],
  570. 'background_color' => 'layui-bg-blue',
  571. 'col' => 2
  572. ],
  573. [
  574. 'name' => '分佣金额',
  575. 'field' => '元',
  576. 'count' => $price['brokerage'],
  577. 'background_color' => 'layui-bg-blue',
  578. 'col' => 2
  579. ],
  580. [
  581. 'name' => '线下支付金额',
  582. 'field' => '元',
  583. 'count' => $price['pay_price_offline'],
  584. 'background_color' => 'layui-bg-blue',
  585. 'col' => 2
  586. ],
  587. [
  588. 'name' => '积分抵扣',
  589. 'field' => '分',
  590. 'count' => $price['use_integral'] . '(抵扣金额:¥' . $price['deduction_price'] . ')',
  591. 'background_color' => 'layui-bg-blue',
  592. 'col' => 2
  593. ],
  594. [
  595. 'name' => '退回积分',
  596. 'field' => '元',
  597. 'count' => $price['back_integral'],
  598. 'background_color' => 'layui-bg-blue',
  599. 'col' => 2
  600. ]
  601. ];
  602. }
  603. /**
  604. * 处理订单金额
  605. * @param $where
  606. * @return array
  607. */
  608. public static function getOrderPrice($where)
  609. {
  610. $where['is_del'] = 0;//删除订单不统计
  611. $model = new self;
  612. $price = [];
  613. $price['pay_price'] = 0;//支付金额
  614. $price['refund_price'] = 0;//退款金额
  615. $price['pay_price_wx'] = 0;//微信支付金额
  616. $price['pay_price_yue'] = 0;//余额支付金额
  617. $price['pay_price_offline'] = 0;//线下支付金额
  618. $price['pay_price_other'] = 0;//其他支付金额
  619. $price['use_integral'] = 0;//用户使用积分
  620. $price['back_integral'] = 0;//退积分总数
  621. $price['deduction_price'] = 0;//抵扣金额
  622. $price['total_num'] = 0; //商品总数
  623. $price['count_sum'] = 0; //商品总数
  624. $price['brokerage'] = 0;
  625. $price['pay_postage'] = 0;
  626. $whereData = ['is_del' => 0];
  627. if ($where['status'] == '') {
  628. $whereData['paid'] = 1;
  629. $whereData['refund_status'] = 0;
  630. }
  631. $ids = self::getOrderWhere($where, $model)->where($whereData)->column('id');
  632. if (count($ids)) {
  633. $price['brokerage'] = UserBill::where(['category' => 'now_money', 'type' => 'brokerage'])->where('link_id', 'in', $ids)->sum('number');
  634. }
  635. $price['refund_price'] = self::getOrderWhere($where, $model)->where(['is_del' => 0, 'paid' => 1, 'refund_status' => 2])->sum('refund_price');
  636. $sumNumber = self::getOrderWhere($where, $model)->where($whereData)->field([
  637. 'sum(total_num) as sum_total_num',
  638. 'count(id) as count_sum',
  639. 'sum(pay_price) as sum_pay_price',
  640. 'sum(pay_postage) as sum_pay_postage',
  641. 'sum(use_integral) as sum_use_integral',
  642. 'sum(back_integral) as sum_back_integral',
  643. 'sum(deduction_price) as sum_deduction_price'
  644. ])->find();
  645. if ($sumNumber) {
  646. $price['count_sum'] = $sumNumber['count_sum'];
  647. $price['total_num'] = $sumNumber['sum_total_num'];
  648. $price['pay_price'] = $sumNumber['sum_pay_price'];
  649. $price['pay_postage'] = $sumNumber['sum_pay_postage'];
  650. $price['use_integral'] = $sumNumber['sum_use_integral'];
  651. $price['back_integral'] = $sumNumber['sum_back_integral'];
  652. $price['deduction_price'] = $sumNumber['sum_deduction_price'];
  653. }
  654. $list = self::getOrderWhere($where, $model)->where($whereData)->group('pay_type')->column('sum(pay_price) as sum_pay_price,pay_type', 'id');
  655. foreach ($list as $v) {
  656. if ($v['pay_type'] == 'weixin') {
  657. $price['pay_price_wx'] = $v['sum_pay_price'];
  658. } elseif ($v['pay_type'] == 'yue') {
  659. $price['pay_price_yue'] = $v['sum_pay_price'];
  660. } elseif ($v['pay_type'] == 'offline') {
  661. $price['pay_price_offline'] = $v['sum_pay_price'];
  662. } else {
  663. $price['pay_price_other'] = $v['sum_pay_price'];
  664. }
  665. }
  666. return $price;
  667. }
  668. public static function systemPagePink($where)
  669. {
  670. $model = new self;
  671. $model = self::getOrderWherePink($where, $model);
  672. $model = $model->order('id desc');
  673. if ($where['export'] == 1) {
  674. $list = $model->select()->toArray();
  675. $export = [];
  676. foreach ($list as $index => $item) {
  677. if ($item['pay_type'] == 'weixin') {
  678. $payType = '微信支付';
  679. } elseif ($item['pay_type'] == 'yue') {
  680. $payType = '余额支付';
  681. } elseif ($item['pay_type'] == 'offline') {
  682. $payType = '线下支付';
  683. } else {
  684. $payType = '其他支付';
  685. }
  686. $_info = StoreOrderCartInfo::where('oid', $item['id'])->column('cart_info', 'oid');
  687. $goodsName = [];
  688. foreach ($_info as $k => $v) {
  689. $v = json_decode($v, true);
  690. $goodsName[] = implode(
  691. [$v['productInfo']['store_name'],
  692. isset($v['productInfo']['attrInfo']) ? '(' . $v['productInfo']['attrInfo']['suk'] . ')' : '',
  693. "[{$v['cart_num']} * {$v['truePrice']}]"
  694. ], ' ');
  695. }
  696. $item['cartInfo'] = $_info;
  697. $export[] = [
  698. $item['order_id'], $payType,
  699. $item['total_num'], $item['total_price'], $item['total_postage'], $item['pay_price'], $item['refund_price'],
  700. $item['mark'], $item['remark'],
  701. [$item['real_name'], $item['user_phone'], $item['user_address']],
  702. $goodsName,
  703. [$item['paid'] == 1 ? '已支付' : '未支付', '支付时间: ' . ($item['pay_time'] > 0 ? date('Y/md H:i', $item['pay_time']) : '暂无')]
  704. ];
  705. $list[$index] = $item;
  706. }
  707. ExportService::exportCsv($export, '订单导出' . time(), ['订单号', '支付方式', '商品总数', '商品总价', '邮费', '支付金额', '退款金额', '用户备注', '管理员备注', '收货人信息', '商品信息', '支付状态']);
  708. }
  709. return self::page($model, function ($item) {
  710. $item['nickname'] = WechatUser::where('uid', $item['uid'])->value('nickname');
  711. $_info = StoreOrderCartInfo::where('oid', $item['id'])->field('cart_info')->select();
  712. foreach ($_info as $k => $v) {
  713. $_info[$k]['cart_info'] = json_decode($v['cart_info'], true);
  714. }
  715. $item['_info'] = $_info;
  716. }, $where);
  717. }
  718. /**
  719. * 处理where条件
  720. * @param $where
  721. * @param $model
  722. * @return mixed
  723. */
  724. public static function getOrderWherePink($where, $model)
  725. {
  726. $model = $model->where('combination_id', '>', 0);
  727. if ($where['status'] != '') $model = $model::statusByWhere($where['status']);
  728. // if($where['is_del'] != '' && $where['is_del'] != -1) $model = $model->where('is_del',$where['is_del']);
  729. if ($where['real_name'] != '') {
  730. $model = $model->where('order_id|real_name|user_phone', 'LIKE', "%$where[real_name]%");
  731. }
  732. if ($where['data'] !== '') {
  733. $model = self::getModelTime($where, $model, 'add_time');
  734. }
  735. return $model;
  736. }
  737. /**
  738. * 处理订单金额
  739. * @param $where
  740. * @return array
  741. */
  742. public static function getOrderPricePink($where)
  743. {
  744. $model = new self;
  745. $price = [];
  746. $price['pay_price'] = 0;//支付金额
  747. $price['refund_price'] = 0;//退款金额
  748. $price['pay_price_wx'] = 0;//微信支付金额
  749. $price['pay_price_yue'] = 0;//余额支付金额
  750. $price['pay_price_offline'] = 0;//线下支付金额
  751. $price['pay_price_other'] = 0;//其他支付金额
  752. $price['use_integral'] = 0;//用户使用积分
  753. $price['back_integral'] = 0;//退积分总数
  754. $price['deduction_price'] = 0;//抵扣金额
  755. $price['total_num'] = 0; //商品总数
  756. $model = self::getOrderWherePink($where, $model);
  757. $list = $model->select()->toArray();
  758. foreach ($list as $v) {
  759. $price['total_num'] = bcadd($price['total_num'], $v['total_num'], 0);
  760. $price['pay_price'] = bcadd($price['pay_price'], $v['pay_price'], 2);
  761. $price['refund_price'] = bcadd($price['refund_price'], $v['refund_price'], 2);
  762. $price['use_integral'] = bcadd($price['use_integral'], $v['use_integral'], 2);
  763. $price['back_integral'] = bcadd($price['back_integral'], $v['back_integral'], 2);
  764. $price['deduction_price'] = bcadd($price['deduction_price'], $v['deduction_price'], 2);
  765. if ($v['pay_type'] == 'weixin') {
  766. $price['pay_price_wx'] = bcadd($price['pay_price_wx'], $v['pay_price'], 2);
  767. } elseif ($v['pay_type'] == 'yue') {
  768. $price['pay_price_yue'] = bcadd($price['pay_price_yue'], $v['pay_price'], 2);
  769. } elseif ($v['pay_type'] == 'offline') {
  770. $price['pay_price_offline'] = bcadd($price['pay_price_offline'], $v['pay_price'], 2);
  771. } else {
  772. $price['pay_price_other'] = bcadd($price['pay_price_other'], $v['pay_price'], 2);
  773. }
  774. }
  775. return $price;
  776. }
  777. /**
  778. * 获取昨天的订单 首页在使用
  779. * @param int $preDay
  780. * @param int $day
  781. * @return $this|StoreOrder
  782. */
  783. public static function isMainYesterdayCount($preDay = 0, $day = 0)
  784. {
  785. $model = new self();
  786. $model = $model->where('add_time', '>', $preDay);
  787. $model = $model->where('add_time', '<', $day);
  788. return $model;
  789. }
  790. /**
  791. * 获取用户购买次数
  792. * @param int $uid
  793. * @return int|string
  794. */
  795. public static function getUserCountPay($uid = 0)
  796. {
  797. if (!$uid) return 0;
  798. return self::where('uid', $uid)->where('paid', 1)->count();
  799. }
  800. /**
  801. * 获取单个用户购买列表
  802. * @param array $where
  803. * @return array
  804. */
  805. public static function getOneorderList($where)
  806. {
  807. return self::where('uid', $where['uid'])
  808. ->order('add_time desc')
  809. ->page((int)$where['page'], (int)$where['limit'])
  810. ->field(['order_id,real_name,total_num,total_price,pay_price,FROM_UNIXTIME(pay_time,"%Y-%m-%d") as pay_time,paid,pay_type,pink_id,seckill_id,bargain_id'
  811. ])->select()
  812. ->toArray();
  813. }
  814. /**
  815. * 设置订单统计图搜索
  816. * @param array $where 条件
  817. * @param null $status
  818. * @param null $time
  819. * @return array
  820. */
  821. public static function setEchatWhere($where, $status = null, $time = null)
  822. {
  823. $model = self::statusByWhere($where['status'])->where('is_system_del', 0);
  824. if ($status !== null) $where['type'] = $status;
  825. if ($time === true) $where['data'] = '';
  826. switch ($where['type']) {
  827. case 1:
  828. //普通商品
  829. $model = $model->where('combination_id', 0)->where('seckill_id', 0)->where('bargain_id', 0);
  830. break;
  831. case 2:
  832. //拼团商品
  833. $model = $model->where('combination_id', ">", 0)->where('pink_id', ">", 0);
  834. break;
  835. case 3:
  836. //秒杀商品
  837. $model = $model->where('seckill_id', ">", 0);
  838. break;
  839. case 4:
  840. //砍价商品
  841. $model = $model->where('bargain_id', '>', 0);
  842. break;
  843. }
  844. return self::getModelTime($where, $model);
  845. }
  846. /*
  847. * 获取订单数据统计图
  848. * $where array
  849. * $limit int
  850. * return array
  851. */
  852. public static function getEchartsOrder($where, $limit = 20)
  853. {
  854. $orderlist = self::setEchatWhere($where)->field(
  855. 'FROM_UNIXTIME(add_time,"%Y-%m-%d") as _add_time,sum(total_num) total_num,count(*) count,sum(total_price) total_price,sum(refund_price) refund_price,group_concat(cart_id SEPARATOR "|") cart_ids'
  856. )->group('_add_time')->order('_add_time asc')->select();
  857. count($orderlist) && $orderlist = $orderlist->toArray();
  858. $legend = ['商品数量', '订单数量', '订单金额', '退款金额'];
  859. $seriesdata = [
  860. [
  861. 'name' => $legend[0],
  862. 'type' => 'line',
  863. 'data' => [],
  864. ],
  865. [
  866. 'name' => $legend[1],
  867. 'type' => 'line',
  868. 'data' => []
  869. ],
  870. [
  871. 'name' => $legend[2],
  872. 'type' => 'line',
  873. 'data' => []
  874. ],
  875. [
  876. 'name' => $legend[3],
  877. 'type' => 'line',
  878. 'data' => []
  879. ]
  880. ];
  881. $xdata = [];
  882. $zoom = '';
  883. foreach ($orderlist as $item) {
  884. $xdata[] = $item['_add_time'];
  885. $seriesdata[0]['data'][] = $item['total_num'];
  886. $seriesdata[1]['data'][] = $item['count'];
  887. $seriesdata[2]['data'][] = $item['total_price'];
  888. $seriesdata[3]['data'][] = $item['refund_price'];
  889. }
  890. count($xdata) > $limit && $zoom = $xdata[$limit - 5];
  891. $badge = self::getOrderBadge($where);
  892. $bingpaytype = self::setEchatWhere($where)->group('pay_type')->field('count(*) as count,pay_type')->select();
  893. count($bingpaytype) && $bingpaytype = $bingpaytype->toArray();
  894. $bing_xdata = ['微信支付', '余额支付', '其他支付'];
  895. $color = ['#ffcccc', '#99cc00', '#fd99cc', '#669966'];
  896. $bing_data = [];
  897. foreach ($bingpaytype as $key => $item) {
  898. if ($item['pay_type'] == 'weixin') {
  899. $value['name'] = $bing_xdata[0];
  900. } else if ($item['pay_type'] == 'yue') {
  901. $value['name'] = $bing_xdata[1];
  902. } else {
  903. $value['name'] = $bing_xdata[2];
  904. }
  905. $value['value'] = $item['count'];
  906. $value['itemStyle']['color'] = isset($color[$key]) ? $color[$key] : $color[0];
  907. $bing_data[] = $value;
  908. }
  909. return compact('zoom', 'xdata', 'seriesdata', 'badge', 'legend', 'bing_data', 'bing_xdata');
  910. }
  911. public static function getOrderBadge($where)
  912. {
  913. return [
  914. [
  915. 'name' => '拼团订单数量',
  916. 'field' => '个',
  917. 'count' => self::setEchatWhere($where, 2)->count(),
  918. 'content' => '拼团总订单数量',
  919. 'background_color' => 'layui-bg-cyan',
  920. 'sum' => self::setEchatWhere($where, 2, true)->count(),
  921. 'class' => 'fa fa-line-chart',
  922. 'col' => 2
  923. ],
  924. [
  925. 'name' => '砍价订单数量',
  926. 'field' => '个',
  927. 'count' => self::setEchatWhere($where, 4)->count(),
  928. 'content' => '砍价总订单数量',
  929. 'background_color' => 'layui-bg-cyan',
  930. 'sum' => self::setEchatWhere($where, 4, true)->count(),
  931. 'class' => 'fa fa-line-chart',
  932. 'col' => 2
  933. ],
  934. [
  935. 'name' => '秒杀订单数量',
  936. 'field' => '个',
  937. 'count' => self::setEchatWhere($where, 3)->count(),
  938. 'content' => '秒杀总订单数量',
  939. 'background_color' => 'layui-bg-cyan',
  940. 'sum' => self::setEchatWhere($where, 3, true)->count(),
  941. 'class' => 'fa fa-line-chart',
  942. 'col' => 2
  943. ],
  944. [
  945. 'name' => '普通订单数量',
  946. 'field' => '个',
  947. 'count' => self::setEchatWhere($where, 1)->count(),
  948. 'content' => '普通总订单数量',
  949. 'background_color' => 'layui-bg-cyan',
  950. 'sum' => self::setEchatWhere($where, 1, true)->count(),
  951. 'class' => 'fa fa-line-chart',
  952. 'col' => 2,
  953. ],
  954. [
  955. 'name' => '使用优惠卷金额',
  956. 'field' => '元',
  957. 'count' => self::setEchatWhere($where)->sum('coupon_price'),
  958. 'content' => '普通总订单数量',
  959. 'background_color' => 'layui-bg-cyan',
  960. 'sum' => self::setEchatWhere($where, null, true)->sum('coupon_price'),
  961. 'class' => 'fa fa-line-chart',
  962. 'col' => 2
  963. ],
  964. [
  965. 'name' => '积分消耗数',
  966. 'field' => '个',
  967. 'count' => self::setEchatWhere($where)->sum('use_integral'),
  968. 'content' => '积分消耗总数',
  969. 'background_color' => 'layui-bg-cyan',
  970. 'sum' => self::setEchatWhere($where, null, true)->sum('use_integral'),
  971. 'class' => 'fa fa-line-chart',
  972. 'col' => 2
  973. ],
  974. [
  975. 'name' => '积分抵扣金额',
  976. 'field' => '个',
  977. 'count' => self::setEchatWhere($where)->sum('deduction_price'),
  978. 'content' => '积分抵扣总金额',
  979. 'background_color' => 'layui-bg-cyan',
  980. 'sum' => self::setEchatWhere($where, null, true)->sum('deduction_price'),
  981. 'class' => 'fa fa-money',
  982. 'col' => 2
  983. ],
  984. [
  985. 'name' => '在线支付金额',
  986. 'field' => '元',
  987. 'count' => self::setEchatWhere($where)->where(['paid' => 1, 'refund_status' => 0])->where('pay_type', 'weixin')->sum('pay_price'),
  988. 'content' => '在线支付总金额',
  989. 'background_color' => 'layui-bg-cyan',
  990. 'sum' => self::setEchatWhere($where, null, true)->where(['paid' => 1, 'refund_status' => 0])->where('pay_type', 'weixin')->sum('pay_price'),
  991. 'class' => 'fa fa-weixin',
  992. 'col' => 2
  993. ],
  994. [
  995. 'name' => '余额支付金额',
  996. 'field' => '元',
  997. 'count' => self::setEchatWhere($where)->where('pay_type', 'yue')->where(['paid' => 1, 'refund_status' => 0])->sum('pay_price'),
  998. 'content' => '余额支付总金额',
  999. 'background_color' => 'layui-bg-cyan',
  1000. 'sum' => self::setEchatWhere($where, null, true)->where(['paid' => 1, 'refund_status' => 0])->where('pay_type', 'yue')->sum('pay_price'),
  1001. 'class' => 'fa fa-balance-scale',
  1002. 'col' => 2
  1003. ],
  1004. [
  1005. 'name' => '赚取积分',
  1006. 'field' => '分',
  1007. 'count' => self::setEchatWhere($where)->sum('gain_integral'),
  1008. 'content' => '赚取总积分',
  1009. 'background_color' => 'layui-bg-cyan',
  1010. 'sum' => self::setEchatWhere($where, null, true)->sum('gain_integral'),
  1011. 'class' => 'fa fa-gg-circle',
  1012. 'col' => 2
  1013. ],
  1014. [
  1015. 'name' => '交易额',
  1016. 'field' => '元',
  1017. 'count' => self::setEchatWhere($where)->where(['paid' => 1, 'refund_status' => 0])->sum('pay_price'),
  1018. 'content' => '总交易额',
  1019. 'background_color' => 'layui-bg-cyan',
  1020. 'sum' => self::setEchatWhere($where, null, true)->where(['paid' => 1, 'refund_status' => 0])->sum('pay_price'),
  1021. 'class' => 'fa fa-jpy',
  1022. 'col' => 2
  1023. ],
  1024. [
  1025. 'name' => '订单商品数量',
  1026. 'field' => '元',
  1027. 'count' => self::setEchatWhere($where)->sum('total_num'),
  1028. 'content' => '订单商品总数量',
  1029. 'background_color' => 'layui-bg-cyan',
  1030. 'sum' => self::setEchatWhere($where, null, true)->sum('total_num'),
  1031. 'class' => 'fa fa-cube',
  1032. 'col' => 2
  1033. ]
  1034. ];
  1035. }
  1036. /**
  1037. * 微信 订单发货
  1038. * @param $oid
  1039. * @param array $postageData
  1040. * @throws \think\db\exception\DataNotFoundException
  1041. * @throws \think\db\exception\ModelNotFoundException
  1042. * @throws \think\exception\DbException
  1043. */
  1044. public static function orderPostageAfter($oid, $postageData = [])
  1045. {
  1046. $order = self::where('id', $oid)->find();
  1047. $url = Url::buildUrl('/order/detail/' . $order['order_id'])->suffix('')->domain(true)->build();
  1048. $group = [
  1049. 'first' => '亲,您的订单已发货,请注意查收',
  1050. 'remark' => '点击查看订单详情'
  1051. ];
  1052. if ($postageData['delivery_type'] == 'send') {//送货
  1053. $goodsName = StoreOrderCartInfo::getProductNameList($order['id']);
  1054. if ($order['is_channel'] == 1) {
  1055. //小程序送货模版消息
  1056. RoutineTemplate::sendOrderPostage($order);
  1057. } else {//公众号
  1058. $openid = WechatUser::where('uid', $order['uid'])->value('openid');
  1059. $group = array_merge($group, [
  1060. 'keyword1' => $goodsName,
  1061. 'keyword2' => $order['pay_type'] == 'offline' ? '线下支付' : date('Y/m/d H:i', $order['pay_time']),
  1062. 'keyword3' => $order['user_address'],
  1063. 'keyword4' => $postageData['delivery_name'],
  1064. 'keyword5' => $postageData['delivery_id']
  1065. ]);
  1066. WechatTemplateService::sendTemplate($openid, WechatTemplateService::ORDER_DELIVER_SUCCESS, $group, $url);
  1067. }
  1068. } else if ($postageData['delivery_type'] == 'express') {//发货
  1069. if ($order['is_channel'] == 1) {
  1070. //小程序发货模版消息
  1071. RoutineTemplate::sendOrderPostage($order, 1);
  1072. } else {//公众号
  1073. try {
  1074. $openid = WechatUser::where('uid', $order['uid'])->value('openid');
  1075. $content = "亲,您的订单{$order['order_id']}已发货,请注意查收!\n快递公司:{$order['delivery_name']}\n快递单号:{$order['delivery_id']}\n";
  1076. $content .= '<a href="https://m.kuaidi100.com/result.jsp?nu=' . $order['delivery_id'] . '">点击查看物流信息</a>';
  1077. WechatService::staffService()->message($content)->to($openid)->send();
  1078. }catch (\Exception $e){
  1079. }
  1080. // $group = array_merge($group, [
  1081. // 'keyword1' => $order['order_id'],
  1082. // 'keyword2' => $postageData['delivery_name'],
  1083. // 'keyword3' => $postageData['delivery_id']
  1084. // ]);
  1085. // WechatTemplateService::sendTemplate($openid, WechatTemplateService::ORDER_POSTAGE_SUCCESS, $group, $url);
  1086. }
  1087. }
  1088. }
  1089. /** 收货后发送模版消息
  1090. * @param $order
  1091. */
  1092. public static function orderTakeAfter($order)
  1093. {
  1094. $title = '';
  1095. $cartInfo = StoreOrderCartInfo::where('oid', $order['id'])->column('cart_info', 'oid');
  1096. if (count($cartInfo)) {
  1097. foreach ($cartInfo as $key => &$cart) {
  1098. $cart = json_decode($cart, true);
  1099. $title .= $cart['productInfo']['store_name'] . ',';
  1100. }
  1101. }
  1102. if (strlen(trim($title)))
  1103. $title = substr($title, 0, bcsub(strlen($title), 1, 0));
  1104. else {
  1105. $cartInfo = StoreCart::alias('a')->where('a.id', 'in', implode(',', json_decode($order['cart_id'], true)))->find();
  1106. $title = StoreProduct::where('id', $cartInfo['product_id'])->value('store_name');
  1107. }
  1108. if ($order['is_channel'] == 1) {//小程序
  1109. RoutineTemplate::sendOrderTakeOver($order, $title);
  1110. } else {
  1111. $openid = WechatUser::where('uid', $order['uid'])->value('openid');
  1112. WechatTemplateService::sendTemplate($openid, WechatTemplateService::ORDER_TAKE_SUCCESS, [
  1113. 'first' => '亲,您的订单已收货',
  1114. 'keyword1' => $order['order_id'],
  1115. 'keyword2' => '已收货',
  1116. 'keyword3' => date('Y-m-d H:i:s', time()),
  1117. 'keyword4' => $title,
  1118. 'remark' => '感谢您的光临!'
  1119. ]);
  1120. }
  1121. }
  1122. /**
  1123. * 不退款发送模板消息
  1124. * @param int $id 订单id
  1125. * @param array $data 退款详情
  1126. * */
  1127. public static function refundNoPrieTemplate($id, $data)
  1128. {
  1129. $order = self::get($id);
  1130. if ($order) return false;
  1131. //小程序模板消息
  1132. $cartInfo = StoreOrderCartInfo::where('oid', $order['id'])->column('product_id', 'oid') ?: [];
  1133. $title = '';
  1134. foreach ($cartInfo as $k => $productId) {
  1135. $store_name = StoreProduct::where('id', $productId)->value('store_name');
  1136. $title .= $store_name . ',';
  1137. }
  1138. if ($order->is_channel == 1) {
  1139. RoutineTemplate::sendOrderRefundFail($order, $title);
  1140. } else {
  1141. WechatTemplateService::sendTemplate(WechatUser::where('uid', $order->uid)->value('openid'), WechatTemplateService::ORDER_REFUND_STATUS, [
  1142. 'first' => '很抱歉您的订单退款失败,失败原因:' . $data,
  1143. 'keyword1' => $order->order_id,
  1144. 'keyword2' => $order->pay_price,
  1145. 'keyword3' => date('Y-m-d H:i:s', time()),
  1146. 'remark' => '给您带来的不便,请谅解!'
  1147. ], Url::buildUrl('/order/detail/' . $order['order_id'])->suffix('')->domain(true)->build());
  1148. }
  1149. }
  1150. /**
  1151. * 获取订单总数
  1152. * @param int $uid
  1153. * @return int|string
  1154. */
  1155. public static function getOrderCount($uid = 0)
  1156. {
  1157. if (!$uid) return 0;
  1158. return self::where('uid', $uid)->where('paid', 1)->where('refund_status', 0)->where('status', 2)->count();
  1159. }
  1160. /**
  1161. * 获取已支付的订单
  1162. * @param int $is_promoter
  1163. * @return int|string
  1164. */
  1165. public static function getOrderPayCount($is_promoter = 0)
  1166. {
  1167. return self::where('o.paid', 1)->alias('o')->join('User u', 'u.uid=o.uid')->where('u.is_promoter', $is_promoter)->count();
  1168. }
  1169. /**
  1170. * 获取最后一个月已支付的订单
  1171. * @param int $is_promoter
  1172. * @return int|string
  1173. */
  1174. public static function getOrderPayMonthCount($is_promoter = 0)
  1175. {
  1176. 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();
  1177. }
  1178. /** 订单收货处理积分
  1179. * @param $order
  1180. * @return bool
  1181. */
  1182. public static function gainUserIntegral($order, bool $open = true)
  1183. {
  1184. if ($order['gain_integral'] > 0) {
  1185. $userInfo = User::get($order['uid']);
  1186. $open && BaseModel::beginTrans();
  1187. $integral = bcadd($userInfo['integral'], $order['gain_integral'], 2);
  1188. $res1 = false != User::where('uid', $userInfo['uid'])->update(['integral' => $integral]);
  1189. $res2 = false != UserBill::income('购买商品赠送积分', $order['uid'], 'integral', 'gain', $order['gain_integral'], $order['id'], bcadd($userInfo['integral'], $order['gain_integral'], 2), '购买商品赠送' . floatval($order['gain_integral']) . '积分');
  1190. $res = $res1 && $res2;
  1191. $open && BaseModel::checkTrans($res);
  1192. RoutineTemplate::sendUserIntegral($order['uid'], $order, $order['gain_integral'], $integral);
  1193. return $res;
  1194. }
  1195. return true;
  1196. }
  1197. public static function integralBack($id)
  1198. {
  1199. $order = self::get($id)->toArray();
  1200. if (!(float)bcsub($order['use_integral'], 0, 2) && !$order['back_integral']) return true;
  1201. if ($order['back_integral'] && !(int)$order['use_integral']) return true;
  1202. BaseModel::beginTrans();
  1203. $data['back_integral'] = bcsub($order['use_integral'], $order['use_integral'], 0);
  1204. if (!$data['back_integral']) return true;
  1205. $data['use_integral'] = 0;
  1206. $data['deduction_price'] = 0.00;
  1207. $data['pay_price'] = 0.00;
  1208. $data['coupon_id'] = 0.00;
  1209. $data['coupon_price'] = 0.00;
  1210. $res4 = true;
  1211. $integral = User::where('uid', $order['uid'])->value('integral');
  1212. $res1 = User::bcInc($order['uid'], 'integral', $data['back_integral'], 'uid');
  1213. $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']) . '积分到用户积分');
  1214. $res3 = self::edit($data, $id);
  1215. if ($order['coupon_id']) $res4 = StoreCouponUser::recoverCoupon($order['coupon_id']);
  1216. StoreOrderStatus::setStatus($id, 'integral_back', '商品退积分:' . $data['back_integral']);
  1217. $res = $res1 && $res2 && $res3 && $res4;
  1218. BaseModel::checkTrans($res);
  1219. return $res;
  1220. }
  1221. /**
  1222. * 订单数量 支付方式
  1223. * @return array
  1224. */
  1225. public static function payTypeCount()
  1226. {
  1227. $where['status'] = 8;
  1228. $where['is_del'] = 0;
  1229. $where['real_name'] = '';
  1230. $where['data'] = '';
  1231. $where['type'] = '';
  1232. $where['order'] = '';
  1233. $where['pay_type'] = 1;
  1234. $weixin = self::getOrderWhere($where, new self)->count();
  1235. $where['pay_type'] = 2;
  1236. $yue = self::getOrderWhere($where, new self)->count();
  1237. $where['pay_type'] = 3;
  1238. $offline = self::getOrderWhere($where, new self)->count();
  1239. return compact('weixin', 'yue', 'offline');
  1240. }
  1241. public static function list($where)
  1242. {
  1243. $model = self::alias('a')
  1244. ->field('*')
  1245. ->order('id DESC');
  1246. if ($where['uid']) $model->where('uid', $where['uid']);
  1247. if ($where['spread_uid']){
  1248. $uids = User::where('spread_uid', $where['spread_uid'])->column('uid');
  1249. $model->where('uid', 'in', $uids);
  1250. }
  1251. $data['count'] = $model->count();
  1252. if ($where['page'] && $where['limit']){
  1253. $model->page($where['page'], $where['limit']);
  1254. }else{
  1255. $model->page(20, 1);
  1256. }
  1257. $list = $model->select();
  1258. $list = count($list) ? $list->toArray() : [];
  1259. foreach ($list as &$item)
  1260. {
  1261. $user = User::where('uid', $item['uid'])->find();
  1262. if ($user['spread_uid']){
  1263. $spread = User::where('uid', $user['spread_uid'])->find();
  1264. }else{
  1265. $spread['nickname'] = '没有推广人';
  1266. }
  1267. $item['nickname'] = $user['nickname'].'/'.$user['uid'];
  1268. $item['spread_name'] = $spread['nickname'].'/'.$user['spread_uid'];
  1269. }
  1270. $data['data'] = $list;
  1271. return $data;
  1272. }
  1273. =======
  1274. <?php
  1275. /**
  1276. *
  1277. * @author: xaboy<365615158@qq.com>
  1278. * @day: 2017/11/11
  1279. */
  1280. namespace app\admin\model\order;
  1281. use crmeb\basic\BaseModel;
  1282. use crmeb\traits\ModelTrait;
  1283. use think\facade\Route as Url;
  1284. use app\models\store\StoreCart;
  1285. use app\admin\model\wechat\WechatUser;
  1286. use app\admin\model\store\StoreProduct;
  1287. use app\models\routine\RoutineTemplate;
  1288. use app\admin\model\user\{User, UserBill};
  1289. use app\admin\model\ump\{StoreCouponUser, StorePink};
  1290. use crmeb\services\{PHPExcelService, WechatService, WechatTemplateService};
  1291. /**
  1292. * 订单管理Model
  1293. * Class StoreOrder
  1294. * @package app\admin\model\store
  1295. */
  1296. class StoreOrder extends BaseModel
  1297. {
  1298. /**
  1299. * 数据表主键
  1300. * @var string
  1301. */
  1302. protected $pk = 'id';
  1303. /**
  1304. * 模型名称
  1305. * @var string
  1306. */
  1307. protected $name = 'store_order';
  1308. use ModelTrait;
  1309. public static function orderCount()
  1310. {
  1311. $data['ys'] = self::statusByWhere(9, new self())->where(['is_system_del' => 0])->count();
  1312. $data['wz'] = self::statusByWhere(0, new self())->where(['is_system_del' => 0])->count();
  1313. $data['wf'] = self::statusByWhere(1, new self())->where(['is_system_del' => 0, 'shipping_type' => 1])->count();
  1314. $data['ds'] = self::statusByWhere(2, new self())->where(['is_system_del' => 0, 'shipping_type' => 1])->count();
  1315. $data['dp'] = self::statusByWhere(3, new self())->where(['is_system_del' => 0])->count();
  1316. $data['jy'] = self::statusByWhere(4, new self())->where(['is_system_del' => 0])->count();
  1317. $data['tk'] = self::statusByWhere(-1, new self())->where(['is_system_del' => 0])->count();
  1318. $data['yt'] = self::statusByWhere(-2, new self())->where(['is_system_del' => 0])->count();
  1319. $data['del'] = self::statusByWhere(-4, new self())->where(['is_system_del' => 0])->count();
  1320. $data['write_off'] = self::statusByWhere(5, new self())->where(['is_system_del' => 0])->count();
  1321. $data['general'] = self::where(['pink_id' => 0, 'combination_id' => 0, 'seckill_id' => 0, 'bargain_id' => 0, 'is_system_del' => 0])->count();
  1322. $data['pink'] = self::where('pink_id|combination_id', '>', 0)->where('is_system_del', 0)->count();
  1323. $data['seckill'] = self::where('seckill_id', '>', 0)->where('is_system_del', 0)->count();
  1324. $data['bargain'] = self::where('bargain_id', '>', 0)->where('is_system_del', 0)->count();
  1325. return $data;
  1326. }
  1327. public static function OrderList($where)
  1328. {
  1329. $model = self::getOrderWhere($where, self::alias('a')
  1330. ->join('user r', 'r.uid=a.uid', 'LEFT'), 'a.', 'r')
  1331. ->field('a.*,r.nickname,r.phone,r.spread_uid');
  1332. if ($where['order'] != '') {
  1333. $model = $model->order(self::setOrder($where['order']));
  1334. } else {
  1335. $model = $model->order('a.id desc');
  1336. }
  1337. if (isset($where['excel']) && $where['excel'] == 1) {
  1338. $data = ($data = $model->select()) && count($data) ? $data->toArray() : [];
  1339. } else {
  1340. $data = ($data = $model->page((int)$where['page'], (int)$where['limit'])->select()) && count($data) ? $data->toArray() : [];
  1341. }
  1342. foreach ($data as &$item) {
  1343. $_info = StoreOrderCartInfo::where('oid', $item['id'])->field('cart_info')->select();
  1344. $_info = count($_info) ? $_info->toArray() : [];
  1345. foreach ($_info as $k => $v) {
  1346. $cart_info = json_decode($v['cart_info'], true);
  1347. if (!isset($cart_info['productInfo'])) $cart_info['productInfo'] = [];
  1348. $_info[$k]['cart_info'] = $cart_info;
  1349. unset($cart_info);
  1350. }
  1351. $item['_info'] = $_info;
  1352. $item['spread_nickname'] = User::where('uid', $item['spread_uid'])->value('nickname');
  1353. $item['add_time'] = $item['add_time'] ? date('Y-m-d H:i:s', $item['add_time']) : '';
  1354. $item['back_integral'] = $item['back_integral'] ?: 0;
  1355. if ($item['pink_id'] || $item['combination_id']) {
  1356. $pinkStatus = StorePink::where('order_id_key', $item['id'])->value('status');
  1357. switch ($pinkStatus) {
  1358. case 1:
  1359. $item['pink_name'] = '[拼团订单]正在进行中';
  1360. $item['color'] = '#f00';
  1361. break;
  1362. case 2:
  1363. $item['pink_name'] = '[拼团订单]已完成';
  1364. $item['color'] = '#00f';
  1365. break;
  1366. case 3:
  1367. $item['pink_name'] = '[拼团订单]未完成';
  1368. $item['color'] = '#f0f';
  1369. break;
  1370. default:
  1371. $item['pink_name'] = '[拼团订单]历史订单';
  1372. $item['color'] = '#457856';
  1373. break;
  1374. }
  1375. } elseif ($item['seckill_id']) {
  1376. $item['pink_name'] = '[秒杀订单]';
  1377. $item['color'] = '#32c5e9';
  1378. } elseif ($item['bargain_id']) {
  1379. $item['pink_name'] = '[砍价订单]';
  1380. $item['color'] = '#12c5e9';
  1381. } else {
  1382. if ($item['shipping_type'] == 1) {
  1383. $item['pink_name'] = '[普通订单]';
  1384. $item['color'] = '#895612';
  1385. } else if ($item['shipping_type'] == 2) {
  1386. $item['pink_name'] = '[核销订单]';
  1387. $item['color'] = '#8956E8';
  1388. }
  1389. }
  1390. if ($item['paid'] == 1) {
  1391. switch ($item['pay_type']) {
  1392. case 'weixin':
  1393. $item['pay_type_name'] = '微信支付';
  1394. break;
  1395. case 'yue':
  1396. $item['pay_type_name'] = '余额支付';
  1397. break;
  1398. case 'ali':
  1399. $item['pay_type_name'] = '支付宝支付';
  1400. break;
  1401. case 'offline':
  1402. $item['pay_type_name'] = '线下支付';
  1403. break;
  1404. default:
  1405. $item['pay_type_name'] = '其他支付';
  1406. break;
  1407. }
  1408. } else {
  1409. switch ($item['pay_type']) {
  1410. default:
  1411. $item['pay_type_name'] = '未支付';
  1412. break;
  1413. case 'offline':
  1414. $item['pay_type_name'] = '线下支付';
  1415. $item['pay_type_info'] = 1;
  1416. break;
  1417. }
  1418. }
  1419. if ($item['paid'] == 0 && $item['status'] == 0) {
  1420. $item['status_name'] = '未支付';
  1421. } else if ($item['paid'] == 1 && $item['status'] == 0 && $item['shipping_type'] == 1 && $item['refund_status'] == 0) {
  1422. $item['status_name'] = '未发货';
  1423. } else if ($item['paid'] == 1 && $item['status'] == 0 && $item['shipping_type'] == 2 && $item['refund_status'] == 0) {
  1424. $item['status_name'] = '未核销';
  1425. } else if ($item['paid'] == 1 && $item['status'] == 1 && $item['shipping_type'] == 1 && $item['refund_status'] == 0) {
  1426. $item['status_name'] = '待收货';
  1427. } else if ($item['paid'] == 1 && $item['status'] == 1 && $item['shipping_type'] == 2 && $item['refund_status'] == 0) {
  1428. $item['status_name'] = '未核销';
  1429. } else if ($item['paid'] == 1 && $item['status'] == 2 && $item['refund_status'] == 0) {
  1430. $item['status_name'] = '待评价';
  1431. } else if ($item['paid'] == 1 && $item['status'] == 3 && $item['refund_status'] == 0) {
  1432. $item['status_name'] = '已完成';
  1433. } else if ($item['paid'] == 1 && $item['refund_status'] == 1) {
  1434. $refundReasonTime = date('Y-m-d H:i', $item['refund_reason_time']);
  1435. $refundReasonWapImg = json_decode($item['refund_reason_wap_img'], true);
  1436. $refundReasonWapImg = $refundReasonWapImg ? $refundReasonWapImg : [];
  1437. $img = '';
  1438. if (count($refundReasonWapImg)) {
  1439. foreach ($refundReasonWapImg as $itemImg) {
  1440. if (strlen(trim($itemImg)))
  1441. $img .= '<img style="height:50px;" src="' . $itemImg . '" />';
  1442. }
  1443. }
  1444. if (!strlen(trim($img))) $img = '无';
  1445. if (isset($where['excel']) && $where['excel'] == 1) {
  1446. $refundImageStr = implode(',', $refundReasonWapImg);
  1447. $item['status_name'] = <<<TEXT
  1448. 退款原因:{$item['refund_reason_wap']}
  1449. 备注说明:{$item['refund_reason_wap_explain']}
  1450. 退款时间:{$refundReasonTime}
  1451. 凭证连接:{$refundImageStr}
  1452. TEXT;
  1453. unset($refundImageStr);
  1454. } else {
  1455. $item['status_name'] = <<<HTML
  1456. <b style="color:#f124c7">申请退款</b><br/>
  1457. <span>退款原因:{$item['refund_reason_wap']}</span><br/>
  1458. <span>备注说明:{$item['refund_reason_wap_explain']}</span><br/>
  1459. <span>退款时间:{$refundReasonTime}</span><br/>
  1460. <span>退款凭证:{$img}</span>
  1461. HTML;
  1462. }
  1463. } else if ($item['paid'] == 1 && $item['refund_status'] == 2) {
  1464. $item['status_name'] = '已退款';
  1465. }
  1466. if ($item['paid'] == 1 && $item['status'] == 0 && $item['shipping_type'] == 2) {
  1467. $item['_status'] = 0;
  1468. } else if ($item['paid'] == 0 && $item['status'] == 0 && $item['refund_status'] == 0) {
  1469. $item['_status'] = 1;
  1470. } else if ($item['paid'] == 1 && $item['status'] == 0 && $item['refund_status'] == 0) {
  1471. $item['_status'] = 2;
  1472. } else if ($item['paid'] == 1 && $item['refund_status'] == 1) {
  1473. $item['_status'] = 3;
  1474. } else if ($item['paid'] == 1 && $item['status'] == 1 && $item['refund_status'] == 0) {
  1475. $item['_status'] = 4;
  1476. } else if ($item['paid'] == 1 && $item['status'] == 2 && $item['refund_status'] == 0) {
  1477. $item['_status'] = 5;
  1478. } else if ($item['paid'] == 1 && $item['status'] == 3 && $item['refund_status'] == 0) {
  1479. $item['_status'] = 6;
  1480. } else if ($item['paid'] == 1 && $item['refund_status'] == 2) {
  1481. $item['_status'] = 7;
  1482. }
  1483. }
  1484. if (isset($where['excel']) && $where['excel'] == 1) {
  1485. self::SaveExcel($data);
  1486. }
  1487. $count = self::getOrderWhere($where, self::alias('a')->join('user r', 'r.uid=a.uid', 'LEFT'), 'a.', 'r')->count();
  1488. return compact('count', 'data');
  1489. }
  1490. /*
  1491. * 保存并下载excel
  1492. * $list array
  1493. * return
  1494. */
  1495. public static function SaveExcel($list)
  1496. {
  1497. $export = [];
  1498. foreach ($list as $index => $item) {
  1499. $_info = StoreOrderCartInfo::where('oid', $item['id'])->column('cart_info');
  1500. $goodsName = [];
  1501. foreach ($_info as $k => $v) {
  1502. $v = json_decode($v, true);
  1503. $suk = '';
  1504. if (isset($v['productInfo']['attrInfo'])) {
  1505. if (isset($v['productInfo']['attrInfo']['suk'])) {
  1506. $suk = '(' . $v['productInfo']['attrInfo']['suk'] . ')';
  1507. }
  1508. }
  1509. $goodsName[] = implode(
  1510. [$v['productInfo']['store_name'],
  1511. $suk,
  1512. "[{$v['cart_num']} * {$v['truePrice']}]"
  1513. ], ' ');
  1514. }
  1515. $item['cartInfo'] = $_info;
  1516. $sex = WechatUser::where('uid', $item['uid'])->value('sex');
  1517. if ($sex == 1) $sex_name = '男';
  1518. else if ($sex == 2) $sex_name = '女';
  1519. else $sex_name = '未知';
  1520. $export[] = [
  1521. $item['order_id'],
  1522. $sex_name,
  1523. $item['phone'],
  1524. $item['real_name'],
  1525. $item['user_phone'],
  1526. $item['user_address'],
  1527. $goodsName,
  1528. $item['total_price'],
  1529. $item['pay_price'],
  1530. $item['pay_postage'],
  1531. $item['coupon_price'],
  1532. $item['pay_type_name'],
  1533. $item['pay_time'] > 0 ? date('Y/m-d H:i', $item['pay_time']) : '暂无',
  1534. $item['status_name'],
  1535. $item['add_time'],
  1536. $item['mark']
  1537. ];
  1538. }
  1539. PHPExcelService::setExcelHeader(['订单号', '性别', '电话', '收货人姓名', '收货人电话', '收货地址', '商品信息',
  1540. '总价格', '实际支付', '邮费', '优惠金额', '支付状态', '支付时间', '订单状态', '下单时间', '用户备注'])
  1541. ->setExcelTile('订单导出' . date('YmdHis', time()), '订单信息' . time(), ' 生成时间:' . date('Y-m-d H:i:s', time()))
  1542. ->setExcelContent($export)
  1543. ->ExcelSave();
  1544. }
  1545. /**
  1546. * @param $where
  1547. * @return array
  1548. */
  1549. public static function systemPage($where, $userid = false)
  1550. {
  1551. $model = self::getOrderWhere($where, self::alias('a')
  1552. ->join('user r', 'r.uid=a.uid', 'LEFT'), 'a.', 'r')
  1553. ->field('a.*,r.nickname');
  1554. if ($where['order']) {
  1555. $model = $model->order('a.' . $where['order']);
  1556. } else {
  1557. $model = $model->order('a.id desc');
  1558. }
  1559. if ($where['export'] == 1) {
  1560. $list = $model->select()->toArray();
  1561. $export = [];
  1562. foreach ($list as $index => $item) {
  1563. if ($item['pay_type'] == 'weixin') {
  1564. $payType = '微信支付';
  1565. } elseif ($item['pay_type'] == 'yue') {
  1566. $payType = '余额支付';
  1567. } elseif ($item['pay_type'] == 'offline') {
  1568. $payType = '线下支付';
  1569. } else {
  1570. $payType = '其他支付';
  1571. }
  1572. $_info = StoreOrderCartInfo::where('oid', $item['id'])->column('cart_info', 'oid');
  1573. $goodsName = [];
  1574. foreach ($_info as $k => $v) {
  1575. $v = json_decode($v, true);
  1576. $goodsName[] = implode(
  1577. [$v['productInfo']['store_name'],
  1578. isset($v['productInfo']['attrInfo']) ? '(' . $v['productInfo']['attrInfo']['suk'] . ')' : '',
  1579. "[{$v['cart_num']} * {$v['truePrice']}]"
  1580. ], ' ');
  1581. }
  1582. $item['cartInfo'] = $_info;
  1583. $export[] = [
  1584. $item['order_id'], $payType,
  1585. $item['total_num'], $item['total_price'], $item['total_postage'], $item['pay_price'], $item['refund_price'],
  1586. $item['mark'], $item['remark'],
  1587. [$item['real_name'], $item['user_phone'], $item['user_address']],
  1588. $goodsName,
  1589. [$item['paid'] == 1 ? '已支付' : '未支付', '支付时间: ' . ($item['pay_time'] > 0 ? date('Y/md H:i', $item['pay_time']) : '暂无')]
  1590. ];
  1591. $list[$index] = $item;
  1592. }
  1593. PHPExcelService::setExcelHeader(['订单号', '支付方式', '商品总数', '商品总价', '邮费', '支付金额', '退款金额', '用户备注', '管理员备注', '收货人信息', '商品信息', '支付状态'])
  1594. ->setExcelTile('订单导出', '订单信息' . time(), ' 生成时间:' . date('Y-m-d H:i:s', time()))
  1595. ->setExcelContent($export)
  1596. ->ExcelSave();
  1597. }
  1598. return self::page($model, function ($item) {
  1599. $_info = StoreOrderCartInfo::where('oid', $item['id'])->field('cart_info')->select();
  1600. foreach ($_info as $k => $v) {
  1601. $_info[$k]['cart_info'] = json_decode($v['cart_info'], true);
  1602. }
  1603. $item['_info'] = $_info;
  1604. if ($item['pink_id'] && $item['combination_id']) {
  1605. $pinkStatus = StorePink::where('order_id_key', $item['id'])->value('status');
  1606. switch ($pinkStatus) {
  1607. case 1:
  1608. $item['pink_name'] = '[拼团订单]正在进行中';
  1609. $item['color'] = '#f00';
  1610. break;
  1611. case 2:
  1612. $item['pink_name'] = '[拼团订单]已完成';
  1613. $item['color'] = '#00f';
  1614. break;
  1615. case 3:
  1616. $item['pink_name'] = '[拼团订单]未完成';
  1617. $item['color'] = '#f0f';
  1618. break;
  1619. default:
  1620. $item['pink_name'] = '[拼团订单]历史订单';
  1621. $item['color'] = '#457856';
  1622. break;
  1623. }
  1624. } else {
  1625. if ($item['seckill_id']) {
  1626. $item['pink_name'] = '[秒杀订单]';
  1627. $item['color'] = '#32c5e9';
  1628. } elseif ($item['bargain_id']) {
  1629. $item['pink_name'] = '[砍价订单]';
  1630. $item['color'] = '#12c5e9';
  1631. } else {
  1632. $item['pink_name'] = '[普通订单]';
  1633. $item['color'] = '#895612';
  1634. }
  1635. }
  1636. }, $where);
  1637. }
  1638. public static function statusByWhere($status, $model = null, $alert = '')
  1639. {
  1640. if ($model == null) $model = new self;
  1641. if ('' === $status)
  1642. return $model;
  1643. else if ($status == 8)
  1644. return $model;
  1645. else if ($status == 0)//未支付
  1646. return $model->where($alert . 'paid', 0)->where($alert . 'status', 0)->where($alert . 'refund_status', 0)->where($alert . 'is_del', 0);
  1647. else if ($status == 1)//已支付 未发货
  1648. 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);
  1649. else if ($status == 2)//已支付 待收货
  1650. 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);
  1651. else if ($status == 5)//已支付 待核销
  1652. 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);
  1653. else if ($status == 3)// 已支付 已收货 待评价
  1654. return $model->where($alert . 'paid', 1)->where($alert . 'status', 2)->where($alert . 'refund_status', 0)->where($alert . 'is_del', 0);
  1655. else if ($status == 4)// 交易完成
  1656. return $model->where($alert . 'paid', 1)->where($alert . 'status', 3)->where($alert . 'refund_status', 0)->where($alert . 'is_del', 0);
  1657. else if ($status == -1)//退款中
  1658. return $model->where($alert . 'paid', 1)->where($alert . 'refund_status', 1)->where($alert . 'is_del', 0);
  1659. else if ($status == -2)//已退款
  1660. return $model->where($alert . 'paid', 1)->where($alert . 'refund_status', 2)->where($alert . 'is_del', 0);
  1661. else if ($status == -3)//退款
  1662. return $model->where($alert . 'paid', 1)->where($alert . 'refund_status', 'in', '1,2')->where($alert . 'is_del', 0);
  1663. else if ($status == -4)//已删除
  1664. return $model->where($alert . 'is_del', 1);
  1665. else if ($status == 9)//已卖出
  1666. return $model->where($alert . 'paid', 1)->where($alert . 'refund_status', 0)->where($alert . 'is_del', 0);
  1667. else
  1668. return $model;
  1669. }
  1670. public static function timeQuantumWhere($startTime = null, $endTime = null, $model = null)
  1671. {
  1672. if ($model === null) $model = new self;
  1673. if ($startTime != null && $endTime != null)
  1674. $model = $model->where('add_time', '>', strtotime($startTime))->where('add_time', '<', strtotime($endTime));
  1675. return $model;
  1676. }
  1677. public static function changeOrderId($orderId)
  1678. {
  1679. $ymd = substr($orderId, 2, 8);
  1680. $key = substr($orderId, 16);
  1681. return 'wx' . $ymd . date('His') . $key;
  1682. }
  1683. /**
  1684. * 线下付款
  1685. * @param $id
  1686. * @return $this
  1687. */
  1688. public static function updateOffline($id)
  1689. {
  1690. $count = self::where('id', $id)->count();
  1691. if (!$count) return self::setErrorInfo('订单不存在');
  1692. $count = self::where('id', $id)->where('paid', 0)->count();
  1693. if (!$count) return self::setErrorInfo('订单已支付');
  1694. $res = self::where('id', $id)->update(['paid' => 1, 'pay_time' => time()]);
  1695. return $res;
  1696. }
  1697. /**
  1698. * TODO 公众号退款发送模板消息
  1699. * @param $oid
  1700. * $oid 订单id key
  1701. */
  1702. public static function refundTemplate($data, $oid)
  1703. {
  1704. $order = self::where('id', $oid)->find();
  1705. WechatTemplateService::sendTemplate(WechatUser::where('uid', $order['uid'])->value('openid'), WechatTemplateService::ORDER_REFUND_STATUS, [
  1706. 'first' => '亲,您购买的商品已退款,本次退款' . $data['refund_price'] . '金额',
  1707. 'keyword1' => $order['order_id'],
  1708. 'keyword2' => $order['pay_price'],
  1709. 'keyword3' => date('Y-m-d H:i:s', $order['add_time']),
  1710. 'remark' => '点击查看订单详情'
  1711. ], Url::buildUrl('/order/detail/' . $order['order_id'])->suffix('')->domain(true)->build());
  1712. }
  1713. /**
  1714. * TODO 小程序余额退款模板消息
  1715. * @param $oid
  1716. * @return bool|mixed
  1717. * @throws \think\db\exception\DataNotFoundException
  1718. * @throws \think\db\exception\ModelNotFoundException
  1719. * @throws \think\exception\DbException
  1720. */
  1721. public static function refundRoutineTemplate($oid)
  1722. {
  1723. $order = self::where('id', $oid)->find();
  1724. return RoutineTemplate::sendOrderRefundSuccess($order);
  1725. }
  1726. /**
  1727. * 处理where条件
  1728. * @param $where
  1729. * @param $model
  1730. * @return mixed
  1731. */
  1732. public static function getOrderWhere($where, $model, $aler = '', $join = '')
  1733. {
  1734. // $model = $model->where('combination_id',0);
  1735. $model = $model->where('is_system_del', 0);
  1736. if (isset($where['status']) && $where['status'] != '') {
  1737. $model = self::statusByWhere($where['status'], $model, $aler);
  1738. }
  1739. if (isset($where['is_del']) && $where['is_del'] != '' && $where['is_del'] != -1) $model = $model->where($aler . 'is_del', $where['is_del']);
  1740. if (isset($where['combination_id'])) {
  1741. if ($where['combination_id'] == '普通订单') {
  1742. $model = $model->where($aler . 'combination_id', 0)->where($aler . 'seckill_id', 0)->where($aler . 'bargain_id', 0);
  1743. }
  1744. if ($where['combination_id'] == '拼团订单') {
  1745. $model = $model->where($aler . 'combination_id', ">", 0)->where($aler . 'pink_id', ">", 0);
  1746. }
  1747. if ($where['combination_id'] == '秒杀订单') {
  1748. $model = $model->where($aler . 'seckill_id', ">", 0);
  1749. }
  1750. if ($where['combination_id'] == '砍价订单') {
  1751. $model = $model->where($aler . 'bargain_id', ">", 0);
  1752. }
  1753. }
  1754. if (isset($where['pay_type'])) {
  1755. switch ($where['pay_type']) {
  1756. case 1:
  1757. $model = $model->where($aler . 'pay_type', 'weixin');
  1758. break;
  1759. case 2:
  1760. $model = $model->where($aler . 'pay_type', 'yue');
  1761. break;
  1762. case 3:
  1763. $model = $model->where($aler . 'pay_type', 'offline');
  1764. break;
  1765. }
  1766. }
  1767. if (isset($where['type'])) {
  1768. switch ($where['type']) {
  1769. case 1:
  1770. $model = $model->where($aler . 'combination_id', 0)->where($aler . 'seckill_id', 0)->where($aler . 'bargain_id', 0);
  1771. break;
  1772. case 2:
  1773. // $model = $model->where($aler.'combination_id',">",0)->where($aler.'pink_id',">",0);
  1774. $model = $model->where($aler . 'combination_id', ">", 0);
  1775. break;
  1776. case 3:
  1777. $model = $model->where($aler . 'seckill_id', ">", 0);
  1778. break;
  1779. case 4:
  1780. $model = $model->where($aler . 'bargain_id', ">", 0);
  1781. break;
  1782. }
  1783. }
  1784. if (isset($where['real_name']) && $where['real_name'] != '') {
  1785. $model = $model->where($aler . 'order_id|' . $aler . 'real_name|' . $aler . 'user_phone' . ($join ? '|' . $join . '.nickname|' . $join . '.uid|' . $join . '.phone' : ''), 'LIKE', "%$where[real_name]%");
  1786. }
  1787. if (isset($where['data']) && $where['data'] !== '') {
  1788. $model = self::getModelTime($where, $model, $aler . 'add_time');
  1789. }
  1790. return $model;
  1791. }
  1792. public static function getBadge($where)
  1793. {
  1794. $price = self::getOrderPrice($where);
  1795. return [
  1796. [
  1797. 'name' => '订单数量',
  1798. 'field' => '件',
  1799. 'count' => $price['count_sum'],
  1800. 'background_color' => 'layui-bg-blue',
  1801. 'col' => 2
  1802. ],
  1803. [
  1804. 'name' => '售出商品',
  1805. 'field' => '件',
  1806. 'count' => $price['total_num'],
  1807. 'background_color' => 'layui-bg-blue',
  1808. 'col' => 2
  1809. ],
  1810. [
  1811. 'name' => '订单金额',
  1812. 'field' => '元',
  1813. 'count' => $price['pay_price'],
  1814. 'background_color' => 'layui-bg-blue',
  1815. 'col' => 2
  1816. ],
  1817. [
  1818. 'name' => '退款金额',
  1819. 'field' => '元',
  1820. 'count' => $price['refund_price'],
  1821. 'background_color' => 'layui-bg-blue',
  1822. 'col' => 2
  1823. ],
  1824. [
  1825. 'name' => '微信支付金额',
  1826. 'field' => '元',
  1827. 'count' => $price['pay_price_wx'],
  1828. 'background_color' => 'layui-bg-blue',
  1829. 'col' => 2
  1830. ],
  1831. [
  1832. 'name' => '余额支付金额',
  1833. 'field' => '元',
  1834. 'count' => $price['pay_price_yue'],
  1835. 'background_color' => 'layui-bg-blue',
  1836. 'col' => 2
  1837. ],
  1838. [
  1839. 'name' => '运费金额',
  1840. 'field' => '元',
  1841. 'count' => $price['pay_postage'],
  1842. 'background_color' => 'layui-bg-blue',
  1843. 'col' => 2
  1844. ],
  1845. [
  1846. 'name' => '分佣金额',
  1847. 'field' => '元',
  1848. 'count' => $price['brokerage'],
  1849. 'background_color' => 'layui-bg-blue',
  1850. 'col' => 2
  1851. ],
  1852. [
  1853. 'name' => '线下支付金额',
  1854. 'field' => '元',
  1855. 'count' => $price['pay_price_offline'],
  1856. 'background_color' => 'layui-bg-blue',
  1857. 'col' => 2
  1858. ],
  1859. [
  1860. 'name' => '积分抵扣',
  1861. 'field' => '分',
  1862. 'count' => $price['use_integral'] . '(抵扣金额:¥' . $price['deduction_price'] . ')',
  1863. 'background_color' => 'layui-bg-blue',
  1864. 'col' => 2
  1865. ],
  1866. [
  1867. 'name' => '退回积分',
  1868. 'field' => '元',
  1869. 'count' => $price['back_integral'],
  1870. 'background_color' => 'layui-bg-blue',
  1871. 'col' => 2
  1872. ]
  1873. ];
  1874. }
  1875. /**
  1876. * 处理订单金额
  1877. * @param $where
  1878. * @return array
  1879. */
  1880. public static function getOrderPrice($where)
  1881. {
  1882. $where['is_del'] = 0;//删除订单不统计
  1883. $model = new self;
  1884. $price = [];
  1885. $price['pay_price'] = 0;//支付金额
  1886. $price['refund_price'] = 0;//退款金额
  1887. $price['pay_price_wx'] = 0;//微信支付金额
  1888. $price['pay_price_yue'] = 0;//余额支付金额
  1889. $price['pay_price_offline'] = 0;//线下支付金额
  1890. $price['pay_price_other'] = 0;//其他支付金额
  1891. $price['use_integral'] = 0;//用户使用积分
  1892. $price['back_integral'] = 0;//退积分总数
  1893. $price['deduction_price'] = 0;//抵扣金额
  1894. $price['total_num'] = 0; //商品总数
  1895. $price['count_sum'] = 0; //商品总数
  1896. $price['brokerage'] = 0;
  1897. $price['pay_postage'] = 0;
  1898. $whereData = ['is_del' => 0];
  1899. if ($where['status'] == '') {
  1900. $whereData['paid'] = 1;
  1901. $whereData['refund_status'] = 0;
  1902. }
  1903. $ids = self::getOrderWhere($where, $model)->where($whereData)->column('id');
  1904. if (count($ids)) {
  1905. $price['brokerage'] = UserBill::where(['category' => 'now_money', 'type' => 'brokerage'])->where('link_id', 'in', $ids)->sum('number');
  1906. }
  1907. $price['refund_price'] = self::getOrderWhere($where, $model)->where(['is_del' => 0, 'paid' => 1, 'refund_status' => 2])->sum('refund_price');
  1908. $sumNumber = self::getOrderWhere($where, $model)->where($whereData)->field([
  1909. 'sum(total_num) as sum_total_num',
  1910. 'count(id) as count_sum',
  1911. 'sum(pay_price) as sum_pay_price',
  1912. 'sum(pay_postage) as sum_pay_postage',
  1913. 'sum(use_integral) as sum_use_integral',
  1914. 'sum(back_integral) as sum_back_integral',
  1915. 'sum(deduction_price) as sum_deduction_price'
  1916. ])->find();
  1917. if ($sumNumber) {
  1918. $price['count_sum'] = $sumNumber['count_sum'];
  1919. $price['total_num'] = $sumNumber['sum_total_num'];
  1920. $price['pay_price'] = $sumNumber['sum_pay_price'];
  1921. $price['pay_postage'] = $sumNumber['sum_pay_postage'];
  1922. $price['use_integral'] = $sumNumber['sum_use_integral'];
  1923. $price['back_integral'] = $sumNumber['sum_back_integral'];
  1924. $price['deduction_price'] = $sumNumber['sum_deduction_price'];
  1925. }
  1926. $list = self::getOrderWhere($where, $model)->where($whereData)->group('pay_type')->column('sum(pay_price) as sum_pay_price,pay_type', 'id');
  1927. foreach ($list as $v) {
  1928. if ($v['pay_type'] == 'weixin') {
  1929. $price['pay_price_wx'] = $v['sum_pay_price'];
  1930. } elseif ($v['pay_type'] == 'yue') {
  1931. $price['pay_price_yue'] = $v['sum_pay_price'];
  1932. } elseif ($v['pay_type'] == 'offline') {
  1933. $price['pay_price_offline'] = $v['sum_pay_price'];
  1934. } else {
  1935. $price['pay_price_other'] = $v['sum_pay_price'];
  1936. }
  1937. }
  1938. return $price;
  1939. }
  1940. public static function systemPagePink($where)
  1941. {
  1942. $model = new self;
  1943. $model = self::getOrderWherePink($where, $model);
  1944. $model = $model->order('id desc');
  1945. if ($where['export'] == 1) {
  1946. $list = $model->select()->toArray();
  1947. $export = [];
  1948. foreach ($list as $index => $item) {
  1949. if ($item['pay_type'] == 'weixin') {
  1950. $payType = '微信支付';
  1951. } elseif ($item['pay_type'] == 'yue') {
  1952. $payType = '余额支付';
  1953. } elseif ($item['pay_type'] == 'offline') {
  1954. $payType = '线下支付';
  1955. } else {
  1956. $payType = '其他支付';
  1957. }
  1958. $_info = StoreOrderCartInfo::where('oid', $item['id'])->column('cart_info', 'oid');
  1959. $goodsName = [];
  1960. foreach ($_info as $k => $v) {
  1961. $v = json_decode($v, true);
  1962. $goodsName[] = implode(
  1963. [$v['productInfo']['store_name'],
  1964. isset($v['productInfo']['attrInfo']) ? '(' . $v['productInfo']['attrInfo']['suk'] . ')' : '',
  1965. "[{$v['cart_num']} * {$v['truePrice']}]"
  1966. ], ' ');
  1967. }
  1968. $item['cartInfo'] = $_info;
  1969. $export[] = [
  1970. $item['order_id'], $payType,
  1971. $item['total_num'], $item['total_price'], $item['total_postage'], $item['pay_price'], $item['refund_price'],
  1972. $item['mark'], $item['remark'],
  1973. [$item['real_name'], $item['user_phone'], $item['user_address']],
  1974. $goodsName,
  1975. [$item['paid'] == 1 ? '已支付' : '未支付', '支付时间: ' . ($item['pay_time'] > 0 ? date('Y/md H:i', $item['pay_time']) : '暂无')]
  1976. ];
  1977. $list[$index] = $item;
  1978. }
  1979. ExportService::exportCsv($export, '订单导出' . time(), ['订单号', '支付方式', '商品总数', '商品总价', '邮费', '支付金额', '退款金额', '用户备注', '管理员备注', '收货人信息', '商品信息', '支付状态']);
  1980. }
  1981. return self::page($model, function ($item) {
  1982. $item['nickname'] = WechatUser::where('uid', $item['uid'])->value('nickname');
  1983. $_info = StoreOrderCartInfo::where('oid', $item['id'])->field('cart_info')->select();
  1984. foreach ($_info as $k => $v) {
  1985. $_info[$k]['cart_info'] = json_decode($v['cart_info'], true);
  1986. }
  1987. $item['_info'] = $_info;
  1988. }, $where);
  1989. }
  1990. /**
  1991. * 处理where条件
  1992. * @param $where
  1993. * @param $model
  1994. * @return mixed
  1995. */
  1996. public static function getOrderWherePink($where, $model)
  1997. {
  1998. $model = $model->where('combination_id', '>', 0);
  1999. if ($where['status'] != '') $model = $model::statusByWhere($where['status']);
  2000. // if($where['is_del'] != '' && $where['is_del'] != -1) $model = $model->where('is_del',$where['is_del']);
  2001. if ($where['real_name'] != '') {
  2002. $model = $model->where('order_id|real_name|user_phone', 'LIKE', "%$where[real_name]%");
  2003. }
  2004. if ($where['data'] !== '') {
  2005. $model = self::getModelTime($where, $model, 'add_time');
  2006. }
  2007. return $model;
  2008. }
  2009. /**
  2010. * 处理订单金额
  2011. * @param $where
  2012. * @return array
  2013. */
  2014. public static function getOrderPricePink($where)
  2015. {
  2016. $model = new self;
  2017. $price = [];
  2018. $price['pay_price'] = 0;//支付金额
  2019. $price['refund_price'] = 0;//退款金额
  2020. $price['pay_price_wx'] = 0;//微信支付金额
  2021. $price['pay_price_yue'] = 0;//余额支付金额
  2022. $price['pay_price_offline'] = 0;//线下支付金额
  2023. $price['pay_price_other'] = 0;//其他支付金额
  2024. $price['use_integral'] = 0;//用户使用积分
  2025. $price['back_integral'] = 0;//退积分总数
  2026. $price['deduction_price'] = 0;//抵扣金额
  2027. $price['total_num'] = 0; //商品总数
  2028. $model = self::getOrderWherePink($where, $model);
  2029. $list = $model->select()->toArray();
  2030. foreach ($list as $v) {
  2031. $price['total_num'] = bcadd($price['total_num'], $v['total_num'], 0);
  2032. $price['pay_price'] = bcadd($price['pay_price'], $v['pay_price'], 2);
  2033. $price['refund_price'] = bcadd($price['refund_price'], $v['refund_price'], 2);
  2034. $price['use_integral'] = bcadd($price['use_integral'], $v['use_integral'], 2);
  2035. $price['back_integral'] = bcadd($price['back_integral'], $v['back_integral'], 2);
  2036. $price['deduction_price'] = bcadd($price['deduction_price'], $v['deduction_price'], 2);
  2037. if ($v['pay_type'] == 'weixin') {
  2038. $price['pay_price_wx'] = bcadd($price['pay_price_wx'], $v['pay_price'], 2);
  2039. } elseif ($v['pay_type'] == 'yue') {
  2040. $price['pay_price_yue'] = bcadd($price['pay_price_yue'], $v['pay_price'], 2);
  2041. } elseif ($v['pay_type'] == 'offline') {
  2042. $price['pay_price_offline'] = bcadd($price['pay_price_offline'], $v['pay_price'], 2);
  2043. } else {
  2044. $price['pay_price_other'] = bcadd($price['pay_price_other'], $v['pay_price'], 2);
  2045. }
  2046. }
  2047. return $price;
  2048. }
  2049. /**
  2050. * 获取昨天的订单 首页在使用
  2051. * @param int $preDay
  2052. * @param int $day
  2053. * @return $this|StoreOrder
  2054. */
  2055. public static function isMainYesterdayCount($preDay = 0, $day = 0)
  2056. {
  2057. $model = new self();
  2058. $model = $model->where('add_time', '>', $preDay);
  2059. $model = $model->where('add_time', '<', $day);
  2060. return $model;
  2061. }
  2062. /**
  2063. * 获取用户购买次数
  2064. * @param int $uid
  2065. * @return int|string
  2066. */
  2067. public static function getUserCountPay($uid = 0)
  2068. {
  2069. if (!$uid) return 0;
  2070. return self::where('uid', $uid)->where('paid', 1)->count();
  2071. }
  2072. /**
  2073. * 获取单个用户购买列表
  2074. * @param array $where
  2075. * @return array
  2076. */
  2077. public static function getOneorderList($where)
  2078. {
  2079. return self::where('uid', $where['uid'])
  2080. ->order('add_time desc')
  2081. ->page((int)$where['page'], (int)$where['limit'])
  2082. ->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'
  2083. ])->select()
  2084. ->toArray();
  2085. }
  2086. /**
  2087. * 设置订单统计图搜索
  2088. * @param array $where 条件
  2089. * @param null $status
  2090. * @param null $time
  2091. * @return array
  2092. */
  2093. public static function setEchatWhere($where, $status = null, $time = null)
  2094. {
  2095. $model = self::statusByWhere($where['status'])->where('is_system_del', 0);
  2096. if ($status !== null) $where['type'] = $status;
  2097. if ($time === true) $where['data'] = '';
  2098. switch ($where['type']) {
  2099. case 1:
  2100. //普通商品
  2101. $model = $model->where('combination_id', 0)->where('seckill_id', 0)->where('bargain_id', 0);
  2102. break;
  2103. case 2:
  2104. //拼团商品
  2105. $model = $model->where('combination_id', ">", 0)->where('pink_id', ">", 0);
  2106. break;
  2107. case 3:
  2108. //秒杀商品
  2109. $model = $model->where('seckill_id', ">", 0);
  2110. break;
  2111. case 4:
  2112. //砍价商品
  2113. $model = $model->where('bargain_id', '>', 0);
  2114. break;
  2115. }
  2116. return self::getModelTime($where, $model);
  2117. }
  2118. /*
  2119. * 获取订单数据统计图
  2120. * $where array
  2121. * $limit int
  2122. * return array
  2123. */
  2124. public static function getEchartsOrder($where, $limit = 20)
  2125. {
  2126. $orderlist = self::setEchatWhere($where)->field(
  2127. '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'
  2128. )->group('_add_time')->order('_add_time asc')->select();
  2129. count($orderlist) && $orderlist = $orderlist->toArray();
  2130. $legend = ['商品数量', '订单数量', '订单金额', '退款金额'];
  2131. $seriesdata = [
  2132. [
  2133. 'name' => $legend[0],
  2134. 'type' => 'line',
  2135. 'data' => [],
  2136. ],
  2137. [
  2138. 'name' => $legend[1],
  2139. 'type' => 'line',
  2140. 'data' => []
  2141. ],
  2142. [
  2143. 'name' => $legend[2],
  2144. 'type' => 'line',
  2145. 'data' => []
  2146. ],
  2147. [
  2148. 'name' => $legend[3],
  2149. 'type' => 'line',
  2150. 'data' => []
  2151. ]
  2152. ];
  2153. $xdata = [];
  2154. $zoom = '';
  2155. foreach ($orderlist as $item) {
  2156. $xdata[] = $item['_add_time'];
  2157. $seriesdata[0]['data'][] = $item['total_num'];
  2158. $seriesdata[1]['data'][] = $item['count'];
  2159. $seriesdata[2]['data'][] = $item['total_price'];
  2160. $seriesdata[3]['data'][] = $item['refund_price'];
  2161. }
  2162. count($xdata) > $limit && $zoom = $xdata[$limit - 5];
  2163. $badge = self::getOrderBadge($where);
  2164. $bingpaytype = self::setEchatWhere($where)->group('pay_type')->field('count(*) as count,pay_type')->select();
  2165. count($bingpaytype) && $bingpaytype = $bingpaytype->toArray();
  2166. $bing_xdata = ['微信支付', '余额支付', '其他支付'];
  2167. $color = ['#ffcccc', '#99cc00', '#fd99cc', '#669966'];
  2168. $bing_data = [];
  2169. foreach ($bingpaytype as $key => $item) {
  2170. if ($item['pay_type'] == 'weixin') {
  2171. $value['name'] = $bing_xdata[0];
  2172. } else if ($item['pay_type'] == 'yue') {
  2173. $value['name'] = $bing_xdata[1];
  2174. } else {
  2175. $value['name'] = $bing_xdata[2];
  2176. }
  2177. $value['value'] = $item['count'];
  2178. $value['itemStyle']['color'] = isset($color[$key]) ? $color[$key] : $color[0];
  2179. $bing_data[] = $value;
  2180. }
  2181. return compact('zoom', 'xdata', 'seriesdata', 'badge', 'legend', 'bing_data', 'bing_xdata');
  2182. }
  2183. public static function getOrderBadge($where)
  2184. {
  2185. return [
  2186. [
  2187. 'name' => '拼团订单数量',
  2188. 'field' => '个',
  2189. 'count' => self::setEchatWhere($where, 2)->count(),
  2190. 'content' => '拼团总订单数量',
  2191. 'background_color' => 'layui-bg-cyan',
  2192. 'sum' => self::setEchatWhere($where, 2, true)->count(),
  2193. 'class' => 'fa fa-line-chart',
  2194. 'col' => 2
  2195. ],
  2196. [
  2197. 'name' => '砍价订单数量',
  2198. 'field' => '个',
  2199. 'count' => self::setEchatWhere($where, 4)->count(),
  2200. 'content' => '砍价总订单数量',
  2201. 'background_color' => 'layui-bg-cyan',
  2202. 'sum' => self::setEchatWhere($where, 4, true)->count(),
  2203. 'class' => 'fa fa-line-chart',
  2204. 'col' => 2
  2205. ],
  2206. [
  2207. 'name' => '秒杀订单数量',
  2208. 'field' => '个',
  2209. 'count' => self::setEchatWhere($where, 3)->count(),
  2210. 'content' => '秒杀总订单数量',
  2211. 'background_color' => 'layui-bg-cyan',
  2212. 'sum' => self::setEchatWhere($where, 3, true)->count(),
  2213. 'class' => 'fa fa-line-chart',
  2214. 'col' => 2
  2215. ],
  2216. [
  2217. 'name' => '普通订单数量',
  2218. 'field' => '个',
  2219. 'count' => self::setEchatWhere($where, 1)->count(),
  2220. 'content' => '普通总订单数量',
  2221. 'background_color' => 'layui-bg-cyan',
  2222. 'sum' => self::setEchatWhere($where, 1, true)->count(),
  2223. 'class' => 'fa fa-line-chart',
  2224. 'col' => 2,
  2225. ],
  2226. [
  2227. 'name' => '使用优惠卷金额',
  2228. 'field' => '元',
  2229. 'count' => self::setEchatWhere($where)->sum('coupon_price'),
  2230. 'content' => '普通总订单数量',
  2231. 'background_color' => 'layui-bg-cyan',
  2232. 'sum' => self::setEchatWhere($where, null, true)->sum('coupon_price'),
  2233. 'class' => 'fa fa-line-chart',
  2234. 'col' => 2
  2235. ],
  2236. [
  2237. 'name' => '积分消耗数',
  2238. 'field' => '个',
  2239. 'count' => self::setEchatWhere($where)->sum('use_integral'),
  2240. 'content' => '积分消耗总数',
  2241. 'background_color' => 'layui-bg-cyan',
  2242. 'sum' => self::setEchatWhere($where, null, true)->sum('use_integral'),
  2243. 'class' => 'fa fa-line-chart',
  2244. 'col' => 2
  2245. ],
  2246. [
  2247. 'name' => '积分抵扣金额',
  2248. 'field' => '个',
  2249. 'count' => self::setEchatWhere($where)->sum('deduction_price'),
  2250. 'content' => '积分抵扣总金额',
  2251. 'background_color' => 'layui-bg-cyan',
  2252. 'sum' => self::setEchatWhere($where, null, true)->sum('deduction_price'),
  2253. 'class' => 'fa fa-money',
  2254. 'col' => 2
  2255. ],
  2256. [
  2257. 'name' => '在线支付金额',
  2258. 'field' => '元',
  2259. 'count' => self::setEchatWhere($where)->where(['paid' => 1, 'refund_status' => 0])->where('pay_type', 'weixin')->sum('pay_price'),
  2260. 'content' => '在线支付总金额',
  2261. 'background_color' => 'layui-bg-cyan',
  2262. 'sum' => self::setEchatWhere($where, null, true)->where(['paid' => 1, 'refund_status' => 0])->where('pay_type', 'weixin')->sum('pay_price'),
  2263. 'class' => 'fa fa-weixin',
  2264. 'col' => 2
  2265. ],
  2266. [
  2267. 'name' => '余额支付金额',
  2268. 'field' => '元',
  2269. 'count' => self::setEchatWhere($where)->where('pay_type', 'yue')->where(['paid' => 1, 'refund_status' => 0])->sum('pay_price'),
  2270. 'content' => '余额支付总金额',
  2271. 'background_color' => 'layui-bg-cyan',
  2272. 'sum' => self::setEchatWhere($where, null, true)->where(['paid' => 1, 'refund_status' => 0])->where('pay_type', 'yue')->sum('pay_price'),
  2273. 'class' => 'fa fa-balance-scale',
  2274. 'col' => 2
  2275. ],
  2276. [
  2277. 'name' => '赚取积分',
  2278. 'field' => '分',
  2279. 'count' => self::setEchatWhere($where)->sum('gain_integral'),
  2280. 'content' => '赚取总积分',
  2281. 'background_color' => 'layui-bg-cyan',
  2282. 'sum' => self::setEchatWhere($where, null, true)->sum('gain_integral'),
  2283. 'class' => 'fa fa-gg-circle',
  2284. 'col' => 2
  2285. ],
  2286. [
  2287. 'name' => '交易额',
  2288. 'field' => '元',
  2289. 'count' => self::setEchatWhere($where)->where(['paid' => 1, 'refund_status' => 0])->sum('pay_price'),
  2290. 'content' => '总交易额',
  2291. 'background_color' => 'layui-bg-cyan',
  2292. 'sum' => self::setEchatWhere($where, null, true)->where(['paid' => 1, 'refund_status' => 0])->sum('pay_price'),
  2293. 'class' => 'fa fa-jpy',
  2294. 'col' => 2
  2295. ],
  2296. [
  2297. 'name' => '订单商品数量',
  2298. 'field' => '元',
  2299. 'count' => self::setEchatWhere($where)->sum('total_num'),
  2300. 'content' => '订单商品总数量',
  2301. 'background_color' => 'layui-bg-cyan',
  2302. 'sum' => self::setEchatWhere($where, null, true)->sum('total_num'),
  2303. 'class' => 'fa fa-cube',
  2304. 'col' => 2
  2305. ]
  2306. ];
  2307. }
  2308. /**
  2309. * 微信 订单发货
  2310. * @param $oid
  2311. * @param array $postageData
  2312. * @throws \think\db\exception\DataNotFoundException
  2313. * @throws \think\db\exception\ModelNotFoundException
  2314. * @throws \think\exception\DbException
  2315. */
  2316. public static function orderPostageAfter($oid, $postageData = [])
  2317. {
  2318. $order = self::where('id', $oid)->find();
  2319. $url = Url::buildUrl('/order/detail/' . $order['order_id'])->suffix('')->domain(true)->build();
  2320. $group = [
  2321. 'first' => '亲,您的订单已发货,请注意查收',
  2322. 'remark' => '点击查看订单详情'
  2323. ];
  2324. if ($postageData['delivery_type'] == 'send') {//送货
  2325. $goodsName = StoreOrderCartInfo::getProductNameList($order['id']);
  2326. if ($order['is_channel'] == 1) {
  2327. //小程序送货模版消息
  2328. RoutineTemplate::sendOrderPostage($order);
  2329. } else {//公众号
  2330. $openid = WechatUser::where('uid', $order['uid'])->value('openid');
  2331. $group = array_merge($group, [
  2332. 'keyword1' => $goodsName,
  2333. 'keyword2' => $order['pay_type'] == 'offline' ? '线下支付' : date('Y/m/d H:i', $order['pay_time']),
  2334. 'keyword3' => $order['user_address'],
  2335. 'keyword4' => $postageData['delivery_name'],
  2336. 'keyword5' => $postageData['delivery_id']
  2337. ]);
  2338. WechatTemplateService::sendTemplate($openid, WechatTemplateService::ORDER_DELIVER_SUCCESS, $group, $url);
  2339. }
  2340. } else if ($postageData['delivery_type'] == 'express') {//发货
  2341. if ($order['is_channel'] == 1) {
  2342. //小程序发货模版消息
  2343. RoutineTemplate::sendOrderPostage($order, 1);
  2344. } else {//公众号
  2345. try {
  2346. $openid = WechatUser::where('uid', $order['uid'])->value('openid');
  2347. $content = "亲,您的订单{$order['order_id']}已发货,请注意查收!\n快递公司:{$order['delivery_name']}\n快递单号:{$order['delivery_id']}\n";
  2348. $content .= '<a href="https://m.kuaidi100.com/result.jsp?nu=' . $order['delivery_id'] . '">点击查看物流信息</a>';
  2349. WechatService::staffService()->message($content)->to($openid)->send();
  2350. }catch (\Exception $e){
  2351. }
  2352. // $group = array_merge($group, [
  2353. // 'keyword1' => $order['order_id'],
  2354. // 'keyword2' => $postageData['delivery_name'],
  2355. // 'keyword3' => $postageData['delivery_id']
  2356. // ]);
  2357. // WechatTemplateService::sendTemplate($openid, WechatTemplateService::ORDER_POSTAGE_SUCCESS, $group, $url);
  2358. }
  2359. }
  2360. }
  2361. /** 收货后发送模版消息
  2362. * @param $order
  2363. */
  2364. public static function orderTakeAfter($order)
  2365. {
  2366. $title = '';
  2367. $cartInfo = StoreOrderCartInfo::where('oid', $order['id'])->column('cart_info', 'oid');
  2368. if (count($cartInfo)) {
  2369. foreach ($cartInfo as $key => &$cart) {
  2370. $cart = json_decode($cart, true);
  2371. $title .= $cart['productInfo']['store_name'] . ',';
  2372. }
  2373. }
  2374. if (strlen(trim($title)))
  2375. $title = substr($title, 0, bcsub(strlen($title), 1, 0));
  2376. else {
  2377. $cartInfo = StoreCart::alias('a')->where('a.id', 'in', implode(',', json_decode($order['cart_id'], true)))->find();
  2378. $title = StoreProduct::where('id', $cartInfo['product_id'])->value('store_name');
  2379. }
  2380. if ($order['is_channel'] == 1) {//小程序
  2381. RoutineTemplate::sendOrderTakeOver($order, $title);
  2382. } else {
  2383. $openid = WechatUser::where('uid', $order['uid'])->value('openid');
  2384. WechatTemplateService::sendTemplate($openid, WechatTemplateService::ORDER_TAKE_SUCCESS, [
  2385. 'first' => '亲,您的订单已收货',
  2386. 'keyword1' => $order['order_id'],
  2387. 'keyword2' => '已收货',
  2388. 'keyword3' => date('Y-m-d H:i:s', time()),
  2389. 'keyword4' => $title,
  2390. 'remark' => '感谢您的光临!'
  2391. ]);
  2392. }
  2393. }
  2394. /**
  2395. * 不退款发送模板消息
  2396. * @param int $id 订单id
  2397. * @param array $data 退款详情
  2398. * */
  2399. public static function refundNoPrieTemplate($id, $data)
  2400. {
  2401. $order = self::get($id);
  2402. if ($order) return false;
  2403. //小程序模板消息
  2404. $cartInfo = StoreOrderCartInfo::where('oid', $order['id'])->column('product_id', 'oid') ?: [];
  2405. $title = '';
  2406. foreach ($cartInfo as $k => $productId) {
  2407. $store_name = StoreProduct::where('id', $productId)->value('store_name');
  2408. $title .= $store_name . ',';
  2409. }
  2410. if ($order->is_channel == 1) {
  2411. RoutineTemplate::sendOrderRefundFail($order, $title);
  2412. } else {
  2413. WechatTemplateService::sendTemplate(WechatUser::where('uid', $order->uid)->value('openid'), WechatTemplateService::ORDER_REFUND_STATUS, [
  2414. 'first' => '很抱歉您的订单退款失败,失败原因:' . $data,
  2415. 'keyword1' => $order->order_id,
  2416. 'keyword2' => $order->pay_price,
  2417. 'keyword3' => date('Y-m-d H:i:s', time()),
  2418. 'remark' => '给您带来的不便,请谅解!'
  2419. ], Url::buildUrl('/order/detail/' . $order['order_id'])->suffix('')->domain(true)->build());
  2420. }
  2421. }
  2422. /**
  2423. * 获取订单总数
  2424. * @param int $uid
  2425. * @return int|string
  2426. */
  2427. public static function getOrderCount($uid = 0)
  2428. {
  2429. if (!$uid) return 0;
  2430. return self::where('uid', $uid)->where('paid', 1)->where('refund_status', 0)->where('status', 2)->count();
  2431. }
  2432. /**
  2433. * 获取已支付的订单
  2434. * @param int $is_promoter
  2435. * @return int|string
  2436. */
  2437. public static function getOrderPayCount($is_promoter = 0)
  2438. {
  2439. return self::where('o.paid', 1)->alias('o')->join('User u', 'u.uid=o.uid')->where('u.is_promoter', $is_promoter)->count();
  2440. }
  2441. /**
  2442. * 获取最后一个月已支付的订单
  2443. * @param int $is_promoter
  2444. * @return int|string
  2445. */
  2446. public static function getOrderPayMonthCount($is_promoter = 0)
  2447. {
  2448. 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();
  2449. }
  2450. /** 订单收货处理积分
  2451. * @param $order
  2452. * @return bool
  2453. */
  2454. public static function gainUserIntegral($order, bool $open = true)
  2455. {
  2456. if ($order['gain_integral'] > 0) {
  2457. $userInfo = User::get($order['uid']);
  2458. $open && BaseModel::beginTrans();
  2459. $integral = bcadd($userInfo['integral'], $order['gain_integral'], 2);
  2460. $res1 = false != User::where('uid', $userInfo['uid'])->update(['integral' => $integral]);
  2461. $res2 = false != UserBill::income('购买商品赠送积分', $order['uid'], 'integral', 'gain', $order['gain_integral'], $order['id'], bcadd($userInfo['integral'], $order['gain_integral'], 2), '购买商品赠送' . floatval($order['gain_integral']) . '积分');
  2462. $res = $res1 && $res2;
  2463. $open && BaseModel::checkTrans($res);
  2464. RoutineTemplate::sendUserIntegral($order['uid'], $order, $order['gain_integral'], $integral);
  2465. return $res;
  2466. }
  2467. return true;
  2468. }
  2469. public static function integralBack($id)
  2470. {
  2471. $order = self::get($id)->toArray();
  2472. if (!(float)bcsub($order['use_integral'], 0, 2) && !$order['back_integral']) return true;
  2473. if ($order['back_integral'] && !(int)$order['use_integral']) return true;
  2474. BaseModel::beginTrans();
  2475. $data['back_integral'] = bcsub($order['use_integral'], $order['use_integral'], 0);
  2476. if (!$data['back_integral']) return true;
  2477. $data['use_integral'] = 0;
  2478. $data['deduction_price'] = 0.00;
  2479. $data['pay_price'] = 0.00;
  2480. $data['coupon_id'] = 0.00;
  2481. $data['coupon_price'] = 0.00;
  2482. $res4 = true;
  2483. $integral = User::where('uid', $order['uid'])->value('integral');
  2484. $res1 = User::bcInc($order['uid'], 'integral', $data['back_integral'], 'uid');
  2485. $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']) . '积分到用户积分');
  2486. $res3 = self::edit($data, $id);
  2487. if ($order['coupon_id']) $res4 = StoreCouponUser::recoverCoupon($order['coupon_id']);
  2488. StoreOrderStatus::setStatus($id, 'integral_back', '商品退积分:' . $data['back_integral']);
  2489. $res = $res1 && $res2 && $res3 && $res4;
  2490. BaseModel::checkTrans($res);
  2491. return $res;
  2492. }
  2493. /**
  2494. * 订单数量 支付方式
  2495. * @return array
  2496. */
  2497. public static function payTypeCount()
  2498. {
  2499. $where['status'] = 8;
  2500. $where['is_del'] = 0;
  2501. $where['real_name'] = '';
  2502. $where['data'] = '';
  2503. $where['type'] = '';
  2504. $where['order'] = '';
  2505. $where['pay_type'] = 1;
  2506. $weixin = self::getOrderWhere($where, new self)->count();
  2507. $where['pay_type'] = 2;
  2508. $yue = self::getOrderWhere($where, new self)->count();
  2509. $where['pay_type'] = 3;
  2510. $offline = self::getOrderWhere($where, new self)->count();
  2511. return compact('weixin', 'yue', 'offline');
  2512. }
  2513. public static function list($where)
  2514. {
  2515. $model = self::alias('a')
  2516. ->field('*')
  2517. ->order('id DESC');
  2518. if ($where['uid']) $model->where('uid', $where['uid']);
  2519. if ($where['spread_uid']){
  2520. $uids = User::where('spread_uid', $where['spread_uid'])->column('uid');
  2521. $model->where('uid', 'in', $uids);
  2522. }
  2523. $data['count'] = $model->count();
  2524. if ($where['page'] && $where['limit']){
  2525. $model->page($where['page'], $where['limit']);
  2526. }else{
  2527. $model->page(20, 1);
  2528. }
  2529. $list = $model->select();
  2530. $list = count($list) ? $list->toArray() : [];
  2531. foreach ($list as &$item)
  2532. {
  2533. $user = User::where('uid', $item['uid'])->find();
  2534. if ($user['spread_uid']){
  2535. $spread = User::where('uid', $user['spread_uid'])->find();
  2536. }else{
  2537. $spread['nickname'] = '没有推广人';
  2538. }
  2539. $item['nickname'] = $user['nickname'].'/'.$user['uid'];
  2540. $item['spread_name'] = $spread['nickname'].'/'.$user['spread_uid'];
  2541. }
  2542. $data['data'] = $list;
  2543. return $data;
  2544. }
  2545. >>>>>>> 386b37d33e5ba817cba00df29efaefbd692e4dd1
  2546. }