StoreOrderServices.php 123 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2023 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace app\services\order;
  12. use app\dao\order\StoreOrderDao;
  13. use app\jobs\AutoCommentJob;
  14. use app\services\activity\combination\StorePinkServices;
  15. use app\services\activity\seckill\StoreSeckillServices;
  16. use app\services\BaseServices;
  17. use app\services\other\PosterServices;
  18. use app\services\pay\OrderPayServices;
  19. use app\services\pay\PayServices;
  20. use app\services\product\product\StoreProductLogServices;
  21. use app\services\serve\ServeServices;
  22. use app\services\system\attachment\SystemAttachmentServices;
  23. use app\services\system\store\SystemStoreServices;
  24. use app\services\user\UserInvoiceServices;
  25. use app\services\user\UserRechargeServices;
  26. use app\services\user\UserServices;
  27. use app\services\product\product\StoreProductReplyServices;
  28. use app\services\user\UserAddressServices;
  29. use app\services\user\UserBillServices;
  30. use app\services\user\UserLevelServices;
  31. use app\services\wechat\WechatUserServices;
  32. use crmeb\exceptions\AdminException;
  33. use crmeb\exceptions\ApiException;
  34. use crmeb\exceptions\PayException;
  35. use crmeb\services\CacheService;
  36. use crmeb\services\easywechat\orderShipping\MiniOrderService;
  37. use crmeb\services\FormBuilder as Form;
  38. use crmeb\services\printer\Printer;
  39. use crmeb\services\SystemConfigService;
  40. use crmeb\utils\Arr;
  41. use think\exception\ValidateException;
  42. use think\facade\Log;
  43. /**
  44. * Class StoreOrderServices
  45. * @package app\services\order
  46. * @method getOrderIdsCount(array $ids) 获取订单id下没有删除的订单数量
  47. * @method StoreOrderDao getUserOrderDetail(string $key, int $uid, array $with) 获取订单详情
  48. * @method chartTimePrice($start, $stop) 获取当前时间到指定时间的支付金额 管理员
  49. * @method chartTimeNumber($start, $stop) 获取当前时间到指定时间的支付订单数 管理员
  50. * @method together(array $where, string $field, string $together = 'sum') 聚合查询
  51. * @method getBuyCount($uid, $type, $typeId) 获取用户已购买此活动商品的个数
  52. * @method getDistinctCount(array $where, $field, ?bool $search = true)
  53. * @method getTrendData($time, $type, $timeType, $str) 用户趋势
  54. * @method getRegion($time, $channelType) 地域统计
  55. * @method getProductTrend($time, $timeType, $field, $str) 商品趋势
  56. * @method getList(array $where, array $field, int $page = 0, int $limit = 0, array $with = [])
  57. */
  58. class StoreOrderServices extends BaseServices
  59. {
  60. /**
  61. * 发货类型
  62. * @var string[]
  63. */
  64. public $deliveryType = [
  65. 'send' => '商家配送',
  66. 'express' => '快递配送',
  67. 'fictitious' => '虚拟发货',
  68. 'delivery_part_split' => '拆分部分发货',
  69. 'delivery_split' => '拆分发货完成'
  70. ];
  71. /**
  72. * StoreOrderProductServices constructor.
  73. * @param StoreOrderDao $dao
  74. */
  75. public function __construct(StoreOrderDao $dao)
  76. {
  77. $this->dao = $dao;
  78. }
  79. /**
  80. * 获取列表
  81. * @param array $where
  82. * @param array $field
  83. * @param array $with
  84. * @return array
  85. * @throws \think\db\exception\DataNotFoundException
  86. * @throws \think\db\exception\DbException
  87. * @throws \think\db\exception\ModelNotFoundException
  88. */
  89. public function getOrderList(array $where, array $field = ['*'], array $with = [])
  90. {
  91. [$page, $limit] = $this->getPageValue();
  92. $data = $this->dao->getOrderList($where, $field, $page, $limit, $with);
  93. $count = $this->dao->count($where, false);
  94. $data = $this->tidyOrderList($data);
  95. foreach ($data as &$item) {
  96. $refund_num = array_sum(array_column($item['refund'], 'refund_num'));
  97. $cart_num = 0;
  98. foreach ($item['_info'] as $items) {
  99. $cart_num += $items['cart_info']['cart_num'];
  100. }
  101. $item['is_all_refund'] = $refund_num == $cart_num ? true : false;
  102. }
  103. return compact('data', 'count');
  104. }
  105. /**
  106. * 前端订单列表
  107. * @param array $where
  108. * @param array|string[] $field
  109. * @param array $with
  110. * @return array
  111. * @throws \think\db\exception\DataNotFoundException
  112. * @throws \think\db\exception\DbException
  113. * @throws \think\db\exception\ModelNotFoundException
  114. */
  115. public function getOrderApiList(array $where, array $field = ['*'], array $with = [])
  116. {
  117. [$page, $limit] = $this->getPageValue();
  118. $data = $this->dao->getOrderList($where, $field, $page, $limit, $with);
  119. foreach ($data as &$item) {
  120. $item = $this->tidyOrder($item, true);
  121. foreach ($item['cartInfo'] ?: [] as $key => $product) {
  122. if ($item['_status']['_type'] == 3) {
  123. $item['cartInfo'][$key]['add_time'] = isset($product['add_time']) ? date('Y-m-d H:i', (int)$product['add_time']) : '时间错误';
  124. }
  125. $item['cartInfo'][$key]['productInfo']['price'] = $product['truePrice'] ?? 0;
  126. }
  127. if (count($item['refund'])) {
  128. $refund_num = array_sum(array_column($item['refund'], 'refund_num'));
  129. $cart_num = array_sum(array_column($item['cartInfo'], 'cart_num'));
  130. $item['is_all_refund'] = $refund_num == $cart_num ? true : false;
  131. } else {
  132. $item['is_all_refund'] = false;
  133. }
  134. }
  135. return $data;
  136. }
  137. /**
  138. * 获取订单数量
  139. * @param int $uid
  140. * @return array
  141. * @throws \ReflectionException
  142. */
  143. public function getOrderData(int $uid = 0)
  144. {
  145. $data['order_count'] = (string)$this->dao->count(['uid' => $uid, 'refund_status' => [0, 3], 'pid' => 0, 'is_del' => 0, 'is_system_del' => 0]);
  146. $data['sum_price'] = (string)$this->dao->sum([
  147. ['uid', '=', $uid],
  148. ['paid', '=', 1],
  149. ['refund_status', '=', 0],
  150. ['pid', '>=', 0]
  151. ], 'pay_price', false);
  152. $countWhere = ['is_del' => 0, 'is_system_del' => 0];
  153. if ($uid) {
  154. $countWhere['uid'] = $uid;
  155. }
  156. $data['unpaid_count'] = (string)$this->dao->count(['status' => 0] + $countWhere);
  157. $data['unshipped_count'] = (string)$this->dao->count(['status' => 1] + $countWhere + ['pid' => 0]);
  158. $data['received_count'] = (string)$this->dao->count(['status' => 2] + $countWhere + ['pid' => 0]);
  159. $data['evaluated_count'] = (string)$this->dao->count(['status' => 3] + $countWhere + ['pid' => 0]);
  160. $data['complete_count'] = (string)$this->dao->count(['status' => 4] + $countWhere + ['pid' => 0]);
  161. /** @var StoreOrderRefundServices $storeOrderRefundServices */
  162. $storeOrderRefundServices = app()->make(StoreOrderRefundServices::class);
  163. $refund_where = ['is_cancel' => 0];
  164. if ($uid) $refund_where['uid'] = $uid;
  165. $data['refunding_count'] = (string)$storeOrderRefundServices->count($refund_where + ['refund_type' => [1, 2, 4, 5]]);
  166. $data['no_refund_count'] = (string)$storeOrderRefundServices->count($refund_where + ['refund_type' => 3]);
  167. $data['refunded_count'] = (string)$storeOrderRefundServices->count($refund_where + ['refund_type' => 6]);
  168. $data['refund_count'] = bcadd(bcadd($data['refunding_count'], $data['refunded_count'], 0), $data['no_refund_count'], 0);
  169. $data['yue_pay_status'] = (int)sys_config('balance_func_status') && (int)sys_config('yue_pay_status') == 1 ? (int)1 : (int)2;//余额支付 1 开启 2 关闭
  170. $data['pc_order_count'] = $data['order_count'] + $data['refunding_count'] + $data['refunded_count'];
  171. $data['pay_weixin_open'] = sys_config('pay_weixin_open', '0') != '0';//微信支付 1 开启 0 关闭
  172. $data['ali_pay_status'] = sys_config('ali_pay_status', '0') != '0';//支付包支付 1 开启 0 关闭
  173. $data['friend_pay_status'] = (int)sys_config('friend_pay_status') ?? 0;//好友代付 1 开启 0 关闭
  174. return $data;
  175. }
  176. /**
  177. * 订单详情数据格式化
  178. * @param $order
  179. * @param bool $detail 是否需要订单商品详情
  180. * @param bool $isPic 是否需要订单状态图片
  181. * @return mixed
  182. */
  183. public function tidyOrder($order, bool $detail = false, $isPic = false)
  184. {
  185. if ($detail == true && isset($order['id'])) {
  186. /** @var StoreOrderCartInfoServices $cartServices */
  187. $cartServices = app()->make(StoreOrderCartInfoServices::class);
  188. $cartInfos = $cartServices->getCartColunm(['oid' => $order['id']], 'cart_num,surplus_num,cart_info,refund_num', 'unique');
  189. $info = [];
  190. /** @var StoreProductReplyServices $replyServices */
  191. $replyServices = app()->make(StoreProductReplyServices::class);
  192. foreach ($cartInfos as $k => $cartInfo) {
  193. $cart = json_decode($cartInfo['cart_info'], true);
  194. $cart['cart_num'] = $cartInfo['cart_num'];
  195. $cart['surplus_num'] = $cartInfo['surplus_num'];
  196. $cart['refund_num'] = $cartInfo['refund_num'];
  197. $cart['surplus_refund_num'] = $cartInfo['surplus_num'] - $cartInfo['refund_num'];
  198. $cart['unique'] = $k;
  199. //新增是否评价字段
  200. $cart['is_reply'] = $replyServices->count(['unique' => $k]);
  201. if (isset($cart['productInfo']['attrInfo'])) {
  202. $cart['productInfo']['attrInfo'] = get_thumb_water($cart['productInfo']['attrInfo']);
  203. }
  204. $cart['productInfo'] = get_thumb_water($cart['productInfo']);
  205. //一种商品买多件 计算总优惠
  206. $cart['vip_sum_truePrice'] = bcmul($cart['vip_truePrice'], $cart['cart_num'] ? $cart['cart_num'] : 1, 2);
  207. $cart['is_valid'] = 1;
  208. array_push($info, $cart);
  209. unset($cart);
  210. }
  211. $order['cartInfo'] = $info;
  212. }
  213. /** @var StoreOrderStatusServices $statusServices */
  214. $statusServices = app()->make(StoreOrderStatusServices::class);
  215. $status = [];
  216. if (!$order['paid'] && $order['pay_type'] == 'offline' && !$order['status'] >= 2) {
  217. $status['_type'] = 9;
  218. $status['_title'] = '线下付款,未支付';
  219. $status['_msg'] = '等待商家处理,请耐心等待';
  220. $status['_class'] = 'nobuy';
  221. } else if (!$order['paid']) {
  222. $status['_type'] = 0;
  223. $status['_title'] = '未支付';
  224. //系统预设取消订单时间段
  225. $keyValue = ['order_cancel_time', 'order_activity_time', 'order_bargain_time', 'order_seckill_time', 'order_pink_time'];
  226. //获取配置
  227. $systemValue = SystemConfigService::more($keyValue);
  228. //格式化数据
  229. $systemValue = Arr::setValeTime($keyValue, is_array($systemValue) ? $systemValue : []);
  230. if ($order['pink_id'] || $order['combination_id']) {
  231. $order_pink_time = $systemValue['order_pink_time'] ?: $systemValue['order_activity_time'];
  232. $time = $order['add_time'] + $order_pink_time * 3600;
  233. $status['_msg'] = '请在' . date('m-d H:i:s', $time) . '前完成支付!';
  234. } else if ($order['seckill_id']) {
  235. $order_seckill_time = $systemValue['order_seckill_time'] ?: $systemValue['order_activity_time'];
  236. $time = $order['add_time'] + $order_seckill_time * 3600;
  237. $status['_msg'] = '请在' . date('m-d H:i:s', $time) . '前完成支付!';
  238. } else if ($order['bargain_id']) {
  239. $order_bargain_time = $systemValue['order_bargain_time'] ?: $systemValue['order_activity_time'];
  240. $time = $order['add_time'] + $order_bargain_time * 3600;
  241. $status['_msg'] = '请在' . date('m-d H:i:s', $time) . '前完成支付!';
  242. } else {
  243. $time = $order['add_time'] + $systemValue['order_cancel_time'] * 3600;
  244. $status['_msg'] = '请在' . date('m-d H:i:s', (int)$time) . '前完成支付!';
  245. }
  246. $status['_class'] = 'nobuy';
  247. } else if ($order['status'] == 4) {
  248. if ($order['delivery_type'] == 'send') {//TODO 送货
  249. $status['_type'] = 1;
  250. $status['_title'] = '待收货';
  251. $status['_msg'] = date('m月d日H时i分', $statusServices->value(['oid' => $order['id'], 'change_type' => 'delivery'], 'change_time')) . '服务商已送货';
  252. $status['_class'] = 'state-ysh';
  253. } elseif ($order['delivery_type'] == 'express') {//TODO 发货
  254. $status['_type'] = 1;
  255. $status['_title'] = '待收货';
  256. $status['_msg'] = date('m月d日H时i分', $statusServices->value(['oid' => $order['id'], 'change_type' => 'delivery_goods'], 'change_time')) . '服务商已发货';
  257. $status['_class'] = 'state-ysh';
  258. } elseif ($order['delivery_type'] == 'split') {//拆分发货
  259. $status['_type'] = 1;
  260. $status['_title'] = '待收货';
  261. $status['_msg'] = date('m月d日H时i分', $statusServices->value(['oid' => $order['id'], 'change_type' => 'delivery_part_split'], 'change_time')) . '服务商已拆分多个包裹发货';
  262. $status['_class'] = 'state-ysh';
  263. } else {
  264. $status['_type'] = 1;
  265. $status['_title'] = '待收货';
  266. $status['_msg'] = date('m月d日H时i分', $statusServices->value(['oid' => $order['id'], 'change_type' => 'delivery_fictitious'], 'change_time')) . '服务商已虚拟发货';
  267. $status['_class'] = 'state-ysh';
  268. }
  269. } else if ($order['refund_status'] == 1) {
  270. if (in_array($order['refund_type'], [0, 1, 2])) {
  271. $status['_type'] = -1;
  272. $status['_title'] = '申请退款中';
  273. $status['_msg'] = '商家审核中,请耐心等待';
  274. $status['_class'] = 'state-sqtk';
  275. } elseif ($order['refund_type'] == 4) {
  276. $status['_type'] = -1;
  277. $status['_title'] = '申请退款中';
  278. $status['_msg'] = '商家同意退款,请填写退货订单号';
  279. $status['_class'] = 'state-sqtk';
  280. $status['refund_name'] = sys_config('refund_name', '');
  281. $status['refund_phone'] = sys_config('refund_phone', '');
  282. $status['refund_address'] = sys_config('refund_address', '');
  283. } elseif ($order['refund_type'] == 5) {
  284. $status['_type'] = -1;
  285. $status['_title'] = '申请退款中';
  286. $status['_msg'] = '等待商家收货';
  287. $status['_class'] = 'state-sqtk';
  288. $status['refund_name'] = sys_config('refund_name', '');
  289. $status['refund_phone'] = sys_config('refund_phone', '');
  290. $status['refund_address'] = sys_config('refund_address', '');
  291. }
  292. } else if ($order['refund_status'] == 2 || $order['refund_type'] == 6) {
  293. $status['_type'] = -2;
  294. $status['_title'] = '已退款';
  295. $status['_msg'] = '已为您退款,感谢您的支持';
  296. $status['_class'] = 'state-sqtk';
  297. } else if ($order['refund_status'] == 3) {
  298. $status['_type'] = -1;
  299. $status['_title'] = '部分退款(子订单)';
  300. $status['_msg'] = '拆分发货,部分退款';
  301. $status['_class'] = 'state-sqtk';
  302. } else if ($order['refund_status'] == 4) {
  303. $status['_type'] = -1;
  304. $status['_title'] = '子订单已全部申请退款中';
  305. $status['_msg'] = '拆分发货,全部退款';
  306. $status['_class'] = 'state-sqtk';
  307. } else if (!$order['status']) {
  308. if ($order['pink_id']) {
  309. /** @var StorePinkServices $pinkServices */
  310. $pinkServices = app()->make(StorePinkServices::class);
  311. if ($pinkServices->getCount(['id' => $order['pink_id'], 'status' => 1])) {
  312. $status['_type'] = 1;
  313. $status['_title'] = '拼团中';
  314. $status['_msg'] = '等待其他人参加拼团';
  315. $status['_class'] = 'state-nfh';
  316. } else {
  317. $status['_type'] = 1;
  318. $status['_title'] = '未发货';
  319. $status['_msg'] = '商家未发货,请耐心等待';
  320. $status['_class'] = 'state-nfh';
  321. }
  322. } else {
  323. if ($order['shipping_type'] === 1) {
  324. $status['_type'] = 1;
  325. $status['_title'] = '未发货';
  326. if ($order['advance_id']) {
  327. $status['_msg'] = date('Y-m-d', $order['cartInfo'][0]['productInfo']['presale_end_time']) . '预售结束后' . $order['cartInfo'][0]['productInfo']['presale_day'] . '天内发货,请耐心等待';
  328. } else {
  329. $status['_msg'] = '商家未发货,请耐心等待';
  330. }
  331. $status['_class'] = 'state-nfh';
  332. } else {
  333. $status['_type'] = 1;
  334. $status['_title'] = '待核销';
  335. $status['_msg'] = '待核销,请到核销点进行核销';
  336. $status['_class'] = 'state-nfh';
  337. }
  338. }
  339. } else if ($order['status'] == 1) {
  340. if ($order['delivery_type'] == 'send') {//TODO 送货
  341. $status['_type'] = 2;
  342. $status['_title'] = '待收货';
  343. $status['_msg'] = date('m月d日H时i分', $statusServices->value(['oid' => $order['id'], 'change_type' => 'delivery'], 'change_time')) . '服务商已送货';
  344. $status['_class'] = 'state-ysh';
  345. } elseif ($order['delivery_type'] == 'express') {//TODO 发货
  346. $status['_type'] = 2;
  347. $status['_title'] = '待收货';
  348. $status['_msg'] = date('m月d日H时i分', $statusServices->value(['oid' => $order['id'], 'change_type' => 'delivery_goods'], 'change_time')) . '服务商已发货';
  349. $status['_class'] = 'state-ysh';
  350. } elseif ($order['delivery_type'] == 'split') {//拆分发货
  351. $status['_type'] = 2;
  352. $status['_title'] = '待收货';
  353. $status['_msg'] = date('m月d日H时i分', $statusServices->value(['oid' => $order['id'], 'change_type' => 'delivery_split'], 'change_time')) . '服务商已拆分多个包裹发货';
  354. $status['_class'] = 'state-ysh';
  355. } else {
  356. $status['_type'] = 2;
  357. $status['_title'] = '待收货';
  358. $status['_msg'] = date('m月d日H时i分', $statusServices->value(['oid' => $order['id'], 'change_type' => 'delivery_fictitious'], 'change_time')) . '服务商已虚拟发货';
  359. $status['_class'] = 'state-ysh';
  360. }
  361. } else if ($order['status'] == 2) {
  362. $status['_type'] = 3;
  363. $status['_title'] = '待评价';
  364. $status['_msg'] = '已收货,快去评价一下吧';
  365. $status['_class'] = 'state-ypj';
  366. } else if ($order['status'] == 3) {
  367. $status['_type'] = 4;
  368. $status['_title'] = '交易完成';
  369. $status['_msg'] = '交易完成,感谢您的支持';
  370. $status['_class'] = 'state-ytk';
  371. }
  372. if (isset($order['pay_type']))
  373. $status['_payType'] = $status['_type'] == 0 ? '' : PayServices::PAY_TYPE[$order['pay_type']] ?? '其他方式';
  374. if (isset($order['delivery_type']))
  375. $status['_deliveryType'] = $this->deliveryType[$order['delivery_type']] ?? '其他方式';
  376. $order['_status'] = $status;
  377. $order['_pay_time'] = isset($order['pay_time']) && $order['pay_time'] != null ? date('Y-m-d H:i:s', $order['pay_time']) : '';
  378. $order['_add_time'] = isset($order['add_time']) ? (strstr((string)$order['add_time'], '-') === false ? date('Y-m-d H:i:s', $order['add_time']) : $order['add_time']) : '';
  379. //系统预设取消订单时间段
  380. $keyValue = ['order_cancel_time', 'order_activity_time', 'order_bargain_time', 'order_seckill_time', 'order_pink_time'];
  381. //获取配置
  382. $systemValue = SystemConfigService::more($keyValue);
  383. //格式化数据
  384. $systemValue = Arr::setValeTime($keyValue, is_array($systemValue) ? $systemValue : []);
  385. if ($order['seckill_id']) {
  386. $secs = $systemValue['order_seckill_time'] ? $systemValue['order_seckill_time'] : $systemValue['order_activity_time'];
  387. } elseif ($order['bargain_id']) {
  388. $secs = $systemValue['order_bargain_time'] ? $systemValue['order_bargain_time'] : $systemValue['order_activity_time'];
  389. } elseif ($order['combination_id']) {
  390. $secs = $systemValue['order_pink_time'] ? $systemValue['order_pink_time'] : $systemValue['order_activity_time'];
  391. } else {
  392. $secs = $systemValue['order_cancel_time'];
  393. }
  394. $order['stop_time'] = $secs * 3600 + $order['add_time'];
  395. $order['status_pic'] = '';
  396. //获取商品状态图片
  397. if ($isPic) {
  398. $order_details_images = sys_data('order_details_images') ?: [];
  399. foreach ($order_details_images as $image) {
  400. if (isset($image['order_status']) && $image['order_status'] == $order['_status']['_type']) {
  401. $order['status_pic'] = $image['pic'];
  402. break;
  403. }
  404. }
  405. }
  406. if ($order['seckill_id'] || $order['bargain_id'] || $order['combination_id'] || $order['advance_id']) {
  407. if ($order['seckill_id']) $order['type'] = 1;
  408. if ($order['bargain_id']) $order['type'] = 2;
  409. if ($order['combination_id']) $order['type'] = 3;
  410. if ($order['advance_id']) $order['type'] = 4;
  411. }
  412. $order['offlinePayStatus'] = (int)sys_config('offline_pay_status') ?? (int)2;
  413. $log = $statusServices->getColumn(['oid' => $order['id']], 'change_time', 'change_type');
  414. if (isset($log['delivery'])) {
  415. $delivery = date('Y-m-d', $log['delivery']);
  416. } elseif (isset($log['delivery_goods'])) {
  417. $delivery = date('Y-m-d', $log['delivery_goods']);
  418. } elseif (isset($log['delivery_fictitious'])) {
  419. $delivery = date('Y-m-d', $log['delivery_fictitious']);
  420. } else {
  421. $delivery = '';
  422. }
  423. $order['order_log'] = [
  424. 'create' => isset($log['cache_key_create_order']) ? date('Y-m-d', $log['cache_key_create_order']) : '',
  425. 'pay' => isset($log['pay_success']) ? date('Y-m-d', $log['pay_success']) : '',
  426. 'delivery' => $delivery,
  427. 'take' => isset($log['take_delivery']) ? date('Y-m-d', $log['take_delivery']) : '',
  428. 'complete' => isset($log['check_order_over']) ? date('Y-m-d', $log['check_order_over']) : '',
  429. ];
  430. return $order;
  431. }
  432. /**
  433. * 数据转换
  434. * @param array $data
  435. * @return array
  436. */
  437. public function tidyOrderList(array $data)
  438. {
  439. /** @var StoreOrderCartInfoServices $services */
  440. $services = app()->make(StoreOrderCartInfoServices::class);
  441. foreach ($data as &$item) {
  442. $item['_info'] = $services->getOrderCartInfo((int)$item['id']);
  443. $item['add_time'] = date('Y-m-d H:i:s', $item['add_time']);
  444. $item['_refund_time'] = isset($item['refund_reason_time']) && $item['refund_reason_time'] ? date('Y-m-d H:i:s', $item['refund_reason_time']) : '';
  445. $item['_pay_time'] = isset($item['pay_time']) && $item['pay_time'] ? date('Y-m-d H:i:s', $item['pay_time']) : '';
  446. if (($item['pink_id'] || $item['combination_id']) && isset($item['pinkStatus'])) {
  447. switch ($item['pinkStatus']) {
  448. case 1:
  449. $item['pink_name'] = '[拼团订单]正在进行中';
  450. $item['color'] = '#f00';
  451. break;
  452. case 2:
  453. $item['pink_name'] = '[拼团订单]已完成';
  454. $item['color'] = '#00f';
  455. break;
  456. case 3:
  457. $item['pink_name'] = '[拼团订单]未完成';
  458. $item['color'] = '#f0f';
  459. break;
  460. default:
  461. $item['pink_name'] = '[拼团订单]历史订单';
  462. $item['color'] = '#457856';
  463. break;
  464. }
  465. } elseif ($item['seckill_id']) {
  466. $item['pink_name'] = '[秒杀订单]';
  467. $item['color'] = '#32c5e9';
  468. } elseif ($item['bargain_id']) {
  469. $item['pink_name'] = '[砍价订单]';
  470. $item['color'] = '#12c5e9';
  471. } elseif ($item['advance_id']) {
  472. $item['pink_name'] = '[预售订单]';
  473. $item['color'] = '#12c5e9';
  474. } else {
  475. if ($item['shipping_type'] == 1) {
  476. $item['pink_name'] = '[普通订单]';
  477. $item['color'] = '#895612';
  478. } else if ($item['shipping_type'] == 2) {
  479. $item['pink_name'] = '[核销订单]';
  480. $item['color'] = '#8956E8';
  481. }
  482. }
  483. if ($item['paid'] == 1) {
  484. switch ($item['pay_type']) {
  485. case PayServices::WEIXIN_PAY:
  486. $item['pay_type_name'] = '微信支付';
  487. break;
  488. case PayServices::YUE_PAY:
  489. $item['pay_type_name'] = '余额支付';
  490. break;
  491. case PayServices::OFFLINE_PAY:
  492. $item['pay_type_name'] = '线下支付';
  493. break;
  494. case PayServices::ALIAPY_PAY:
  495. $item['pay_type_name'] = '支付宝支付';
  496. break;
  497. case PayServices::ALLIN_PAY:
  498. $item['pay_type_name'] = '通联支付';
  499. break;
  500. default:
  501. $item['pay_type_name'] = '其他支付';
  502. break;
  503. }
  504. } else {
  505. switch ($item['pay_type']) {
  506. case 'offline':
  507. $item['pay_type_name'] = '线下支付';
  508. $item['pay_type_info'] = 1;
  509. break;
  510. default:
  511. $item['pay_type_name'] = '';
  512. break;
  513. }
  514. }
  515. $status_name = ['status_name' => '', 'pics' => []];
  516. if ($item['paid'] == 0 && $item['status'] == 0) {
  517. $status_name['status_name'] = '未支付';
  518. } else if ($item['paid'] == 1 && $item['status'] == 0 && $item['shipping_type'] == 1 && $item['refund_status'] == 0) {
  519. $status_name['status_name'] = '未发货';
  520. } else if ($item['paid'] == 1 && $item['status'] == 4 && $item['shipping_type'] == 1 && $item['refund_status'] == 0) {
  521. $status_name['status_name'] = '部分发货';
  522. } else if ($item['paid'] == 1 && $item['status'] == 0 && $item['shipping_type'] == 2 && $item['refund_status'] == 0) {
  523. $status_name['status_name'] = '未核销';
  524. } else if ($item['paid'] == 1 && $item['status'] == 1 && $item['shipping_type'] == 1 && $item['refund_status'] == 0) {
  525. $status_name['status_name'] = '待收货';
  526. } else if ($item['paid'] == 1 && $item['status'] == 1 && $item['shipping_type'] == 2 && $item['refund_status'] == 0) {
  527. $status_name['status_name'] = '未核销';
  528. } else if ($item['paid'] == 1 && $item['status'] == 2 && $item['refund_status'] == 0) {
  529. $status_name['status_name'] = '待评价';
  530. } else if ($item['paid'] == 1 && $item['status'] == 3 && $item['refund_status'] == 0) {
  531. $status_name['status_name'] = '已完成';
  532. } else if ($item['paid'] == 1 && $item['refund_status'] == 1) {
  533. $refundReasonTime = date('Y-m-d H:i', $item['refund_reason_time']);
  534. $refundReasonWapImg = json_decode($item['refund_reason_wap_img'], true);
  535. $refundReasonWapImg = $refundReasonWapImg ?: [];
  536. $img = [];
  537. if (count($refundReasonWapImg)) {
  538. foreach ($refundReasonWapImg as $itemImg) {
  539. if (strlen(trim($itemImg)))
  540. $img[] = $itemImg;
  541. }
  542. }
  543. $status_name['status_name'] = '退款中';
  544. $status_name['pics'] = $img;
  545. } else if ($item['paid'] == 1 && $item['refund_status'] == 2) {
  546. $status_name['status_name'] = '已退款';
  547. } else if ($item['paid'] == 1 && $item['refund_status'] == 3) {
  548. $status_name['status_name'] = <<<HTML
  549. <b style="color:#f124c7">部分退款</b><br/>
  550. HTML;
  551. } else if ($item['paid'] == 1 && $item['refund_status'] == 4) {
  552. $status_name['status_name'] = <<<HTML
  553. <b style="color:#f124c7">退款中</b><br/>
  554. HTML;
  555. }
  556. $item['status_name'] = $status_name;
  557. if ($item['paid'] == 0 && $item['status'] == 0 && $item['refund_status'] == 0) {
  558. $item['_status'] = 1;//未支付
  559. } else if ($item['paid'] == 1 && $item['status'] == 0 && $item['refund_status'] == 0) {
  560. $item['_status'] = 2;//已支付 未发货
  561. } else if ($item['paid'] == 1 && $item['status'] == 4 && $item['refund_status'] == 0) {
  562. $item['_status'] = 8;//已支付 部分发货
  563. } else if ($item['paid'] == 1 && $item['refund_status'] == 1) {
  564. $item['_status'] = 3;//已支付 申请退款中
  565. } else if ($item['paid'] == 1 && $item['status'] == 1 && $item['refund_status'] == 0) {
  566. $item['_status'] = 4;//已支付 待收货
  567. } else if ($item['paid'] == 1 && $item['status'] == 2 && $item['refund_status'] == 0) {
  568. $item['_status'] = 5;//已支付 待评价
  569. } else if ($item['paid'] == 1 && $item['status'] == 3 && $item['refund_status'] == 0) {
  570. $item['_status'] = 6;//已支付 已完成
  571. } else if ($item['paid'] == 1 && $item['refund_status'] == 2) {
  572. $item['_status'] = 7;//已支付 已退款
  573. } else if ($item['paid'] == 1 && $item['refund_status'] == 3 && $item['status'] == 4) {
  574. $item['_status'] = 9;//拆单发货 部分申请退款
  575. } else if ($item['paid'] == 1 && $item['refund_status'] == 4) {
  576. $item['_status'] = 10;//拆单发货 已全部申请退款
  577. } else if ($item['paid'] == 1 && $item['refund_status'] == 3 && $item['status'] == 0) {
  578. $item['_status'] = 11;//拆单退款 未发货
  579. }
  580. if ($item['clerk_id'] == 0 && !isset($item['clerk_name'])) {
  581. $item['clerk_name'] = '总平台';
  582. }
  583. //根据核销员更改store_name
  584. if ($item['clerk_id'] && isset($item['staff_store_id']) && $item['staff_store_id']) {
  585. /** @var SystemStoreServices $store */
  586. $store = app()->make(SystemStoreServices::class);
  587. $storeOne = $store->value(['id' => $item['staff_store_id']], 'name');
  588. if ($storeOne) $item['store_name'] = $storeOne;
  589. }
  590. }
  591. return $data;
  592. }
  593. /**
  594. * 处理订单金额
  595. * @param $where
  596. * @return array
  597. */
  598. public function getOrderPrice($where)
  599. {
  600. if (isset($where['refund_type']) && $where['refund_type']) unset($where['refund_type']);
  601. $where['is_del'] = 0;//删除订单不统计
  602. $price['today_pay_price'] = 0;//今日支付金额
  603. $price['pay_price'] = 0;//支付金额
  604. $price['refund_price'] = 0;//退款金额
  605. $price['pay_price_wx'] = 0;//微信支付金额
  606. $price['pay_price_yue'] = 0;//余额支付金额
  607. $price['pay_price_offline'] = 0;//线下支付金额
  608. $price['pay_price_other'] = 0;//其他支付金额
  609. $price['use_integral'] = 0;//用户使用积分
  610. $price['back_integral'] = 0;//退积分总数
  611. $price['deduction_price'] = 0;//抵扣金额
  612. $price['total_num'] = 0; //商品总数
  613. $price['today_count_sum'] = 0; //今日订单总数
  614. $price['count_sum'] = 0; //订单总数
  615. $price['brokerage'] = 0;
  616. $price['pay_postage'] = 0;
  617. $whereData = ['is_del' => 0];
  618. if ($where['status'] == '' && $where['pay_type'] != 3) {
  619. $whereData['paid'] = 1;
  620. }
  621. $ids = $this->dao->column($where + $whereData, 'id');
  622. if (count($ids)) {
  623. /** @var UserBillServices $services */
  624. $services = app()->make(UserBillServices::class);
  625. $price['brokerage'] = $services->getBrokerageNumSum($ids);
  626. }
  627. $price['refund_price'] = $this->dao->together($where + ['is_del' => 0, 'paid' => 1, 'refund_status' => 2], 'refund_price');
  628. $sumNumber = $this->dao->search($where + $whereData)->field([
  629. 'sum(total_num) as sum_total_num',
  630. 'count(id) as count_sum',
  631. 'sum(pay_price) as sum_pay_price',
  632. 'sum(pay_postage) as sum_pay_postage',
  633. 'sum(use_integral) as sum_use_integral',
  634. 'sum(back_integral) as sum_back_integral',
  635. 'sum(deduction_price) as sum_deduction_price'
  636. ])->find();
  637. if ($sumNumber) {
  638. $price['count_sum'] = $sumNumber['count_sum'];
  639. $price['total_num'] = $sumNumber['sum_total_num'];
  640. $price['pay_price'] = $sumNumber['sum_pay_price'];
  641. $price['pay_postage'] = $sumNumber['sum_pay_postage'];
  642. $price['use_integral'] = $sumNumber['sum_use_integral'];
  643. $price['back_integral'] = $sumNumber['sum_back_integral'];
  644. $price['deduction_price'] = $sumNumber['sum_deduction_price'];
  645. }
  646. $list = $this->dao->column($where + $whereData, 'sum(pay_price) as sum_pay_price,pay_type', 'id', 'pay_type');
  647. foreach ($list as $v) {
  648. if ($v['pay_type'] == 'weixin') {
  649. $price['pay_price_wx'] = $v['sum_pay_price'];
  650. } elseif ($v['pay_type'] == 'yue') {
  651. $price['pay_price_yue'] = $v['sum_pay_price'];
  652. } elseif ($v['pay_type'] == 'offline') {
  653. $price['pay_price_offline'] = $v['sum_pay_price'];
  654. } else {
  655. $price['pay_price_other'] = $v['sum_pay_price'];
  656. }
  657. }
  658. $where['time'] = 'today';
  659. $sumNumber = $this->dao->search($where + $whereData)->field([
  660. 'count(id) as today_count_sum',
  661. 'sum(pay_price) as today_pay_price',
  662. ])->find();
  663. if ($sumNumber) {
  664. $price['today_count_sum'] = $sumNumber['today_count_sum'];
  665. $price['today_pay_price'] = $where['status'] !== 0 ? $sumNumber['today_pay_price'] : 0;
  666. }
  667. return $price;
  668. }
  669. /**
  670. * 获取订单列表页面统计数据
  671. * @param $where
  672. * @return array
  673. */
  674. public function getBadge($where)
  675. {
  676. $price = $this->getOrderPrice($where);
  677. return [
  678. [
  679. 'name' => '订单数量',
  680. 'field' => '件',
  681. 'count' => $price['count_sum'],
  682. 'className' => 'md-basket',
  683. 'col' => 6
  684. ],
  685. [
  686. 'name' => '订单金额',
  687. 'field' => '元',
  688. 'count' => $price['pay_price'],
  689. 'className' => 'md-pricetags',
  690. 'col' => 6
  691. ],
  692. [
  693. 'name' => '今日订单数量',
  694. 'field' => '件',
  695. 'count' => $price['today_count_sum'],
  696. 'className' => 'ios-chatbubbles',
  697. 'col' => 6
  698. ],
  699. [
  700. 'name' => '今日支付金额',
  701. 'field' => '元',
  702. 'count' => $price['today_pay_price'],
  703. 'className' => 'ios-cash',
  704. 'col' => 6
  705. ],
  706. ];
  707. }
  708. /**
  709. *
  710. * @param array $where
  711. * @return mixed
  712. */
  713. public function orderCount(array $where)
  714. {
  715. $where_one = ['time' => $where['time'], 'is_system_del' => 0, 'pid' => 0, 'status' => 1, 'shipping_type' => 1];
  716. $data['all'] = (string)$this->dao->count($where_one);
  717. $data['general'] = (string)$this->dao->count($where_one + ['type' => 1]);
  718. $data['pink'] = (string)$this->dao->count($where_one + ['type' => 2]);
  719. $data['seckill'] = (string)$this->dao->count($where_one + ['type' => 3]);
  720. $data['bargain'] = (string)$this->dao->count($where_one + ['type' => 4]);
  721. $data['advance'] = (string)$this->dao->count($where_one + ['type' => 5]);
  722. return $data;
  723. }
  724. /**
  725. * 创建修改订单表单
  726. * @param int $id
  727. * @return array
  728. * @throws \FormBuilder\Exception\FormBuilderException
  729. */
  730. public function updateForm(int $id)
  731. {
  732. $product = $this->dao->get($id);
  733. if (!$product) {
  734. throw new AdminException(100026);
  735. }
  736. $f = [];
  737. $f[] = Form::input('order_id', '订单编号', $product->getData('order_id'))->disabled(true);
  738. $f[] = Form::number('total_price', '商品总价', (float)$product->getData('total_price'))->min(0)->disabled(true);
  739. $f[] = Form::number('pay_postage', '支付邮费', (float)$product->getData('pay_postage') ?: 0)->disabled(true);
  740. $f[] = Form::number('pay_price', '实际支付金额', (float)$product->getData('pay_price'))->min(0);
  741. $f[] = Form::number('gain_integral', '赠送积分', (float)$product->getData('gain_integral') ?: 0);
  742. return create_form('修改订单', $f, $this->url('/order/update/' . $id), 'PUT');
  743. }
  744. /**
  745. * 修改订单
  746. * @param int $id
  747. * @param array $data
  748. * @return mixed
  749. * @throws \Exception
  750. */
  751. public function updateOrder(int $id, array $data)
  752. {
  753. $order = $this->dao->getOne(['id' => $id, 'is_del' => 0]);
  754. if (!$order) {
  755. throw new AdminException(400118);
  756. }
  757. /** @var StoreOrderCreateServices $createServices */
  758. $createServices = app()->make(StoreOrderCreateServices::class);
  759. $data['order_id'] = $createServices->getNewOrderId('cp');
  760. if (sys_config('user_brokerage_type') == 1) {
  761. $percent = bcdiv((string)$data['pay_price'], (string)$order['pay_price'], 6);
  762. if ($order['one_brokerage'] > 0) {
  763. $data['one_brokerage'] = bcmul((string)$order['one_brokerage'], $percent, 2);
  764. }
  765. if ($order['two_brokerage'] > 0) {
  766. $data['two_brokerage'] = bcmul((string)$order['two_brokerage'], $percent, 2);
  767. }
  768. }
  769. /** @var StoreOrderStatusServices $services */
  770. $services = app()->make(StoreOrderStatusServices::class);
  771. return $this->transaction(function () use ($id, $data, $services) {
  772. $res = $this->dao->update($id, $data);
  773. $res = $res && $services->save([
  774. 'oid' => $id,
  775. 'change_type' => 'order_edit',
  776. 'change_time' => time(),
  777. 'change_message' => '修改商品总价为:' . $data['total_price'] . ' 实际支付金额' . $data['pay_price']
  778. ]);
  779. if ($res) {
  780. $order = $this->dao->getOne(['id' => $id, 'is_del' => 0]);
  781. //改价短信提醒
  782. event('NoticeListener', [['order' => $order, 'pay_price' => $data['pay_price']], 'price_revision']);
  783. return $data['order_id'];
  784. } else {
  785. throw new AdminException(100007);
  786. }
  787. });
  788. }
  789. /**
  790. * 订单图表
  791. * @param $cycle
  792. * @return array
  793. */
  794. public function orderCharts($cycle)
  795. {
  796. $datalist = [];
  797. switch ($cycle) {
  798. case 'thirtyday':
  799. $datebefor = date('Y-m-d', strtotime('-30 day'));
  800. $dateafter = date('Y-m-d 23:59:59');
  801. //上期
  802. $pre_datebefor = date('Y-m-d', strtotime('-60 day'));
  803. $pre_dateafter = date('Y-m-d', strtotime('-30 day'));
  804. for ($i = -29; $i <= 0; $i++) {
  805. $datalist[date('m-d', strtotime($i . ' day'))] = date('m-d', strtotime($i . ' day'));
  806. }
  807. $order_list = $this->dao->orderAddTimeList($datebefor, $dateafter, '30');
  808. if (empty($order_list)) return ['yAxis' => [], 'legend' => [], 'xAxis' => [], 'serise' => [], 'pre_cycle' => [], 'cycle' => []];
  809. foreach ($order_list as $k => &$v) {
  810. $order_list[$v['day']] = $v;
  811. }
  812. $cycle_list = [];
  813. foreach ($datalist as $dk => $dd) {
  814. if (!empty($order_list[$dd])) {
  815. $cycle_list[$dd] = $order_list[$dd];
  816. } else {
  817. $cycle_list[$dd] = ['count' => 0, 'day' => $dd, 'price' => ''];
  818. }
  819. }
  820. $chartdata = [];
  821. $data = [];//临时
  822. $chartdata['yAxis']['maxnum'] = 0;//最大值数量
  823. $chartdata['yAxis']['maxprice'] = 0;//最大值金额
  824. foreach ($cycle_list as $k => $v) {
  825. $data['day'][] = $v['day'];
  826. $data['count'][] = $v['count'];
  827. $data['price'][] = round($v['price'], 2);
  828. if ($chartdata['yAxis']['maxnum'] < $v['count'])
  829. $chartdata['yAxis']['maxnum'] = $v['count'];//日最大订单数
  830. if ($chartdata['yAxis']['maxprice'] < $v['price'])
  831. $chartdata['yAxis']['maxprice'] = $v['price'];//日最大金额
  832. }
  833. $chartdata['legend'] = ['订单金额', '订单数'];//分类
  834. $chartdata['xAxis'] = $data['day'];//X轴值
  835. $series1 = ['normal' => ['color' => [
  836. 'x' => 0, 'y' => 0, 'x2' => 0, 'y2' => 1,
  837. 'colorStops' => [
  838. [
  839. 'offset' => 0,
  840. 'color' => '#69cdff'
  841. ],
  842. [
  843. 'offset' => 0.5,
  844. 'color' => '#3eb3f7'
  845. ],
  846. [
  847. 'offset' => 1,
  848. 'color' => '#1495eb'
  849. ]
  850. ]
  851. ]]
  852. ];
  853. $series2 = ['normal' => ['color' => [
  854. 'x' => 0, 'y' => 0, 'x2' => 0, 'y2' => 1,
  855. 'colorStops' => [
  856. [
  857. 'offset' => 0,
  858. 'color' => '#6fdeab'
  859. ],
  860. [
  861. 'offset' => 0.5,
  862. 'color' => '#44d693'
  863. ],
  864. [
  865. 'offset' => 1,
  866. 'color' => '#2cc981'
  867. ]
  868. ]
  869. ]]
  870. ];
  871. $chartdata['series'][] = ['name' => $chartdata['legend'][0], 'type' => 'bar', 'itemStyle' => $series1, 'data' => $data['price']];//分类1值
  872. $chartdata['series'][] = ['name' => $chartdata['legend'][1], 'type' => 'line', 'itemStyle' => $series2, 'data' => $data['count'], 'yAxisIndex' => 1];//分类2值
  873. //统计总数上期
  874. $pre_total = $this->dao->preTotalFind($pre_datebefor, $pre_dateafter);
  875. if ($pre_total) {
  876. $chartdata['pre_cycle']['count'] = [
  877. 'data' => $pre_total['count'] ?: 0
  878. ];
  879. $chartdata['pre_cycle']['price'] = [
  880. 'data' => $pre_total['price'] ?: 0
  881. ];
  882. }
  883. //统计总数
  884. $total = $this->dao->preTotalFind($datebefor, $dateafter);
  885. if ($total) {
  886. $cha_count = intval($pre_total['count']) - intval($total['count']);
  887. $pre_total['count'] = $pre_total['count'] == 0 ? 1 : $pre_total['count'];
  888. $chartdata['cycle']['count'] = [
  889. 'data' => $total['count'] ?: 0,
  890. 'percent' => round((abs($cha_count) / intval($pre_total['count']) * 100), 2),
  891. 'is_plus' => $cha_count > 0 ? -1 : ($cha_count == 0 ? 0 : 1)
  892. ];
  893. $cha_price = round($pre_total['price'], 2) - round($total['price'], 2);
  894. $pre_total['price'] = $pre_total['price'] == 0 ? 1 : $pre_total['price'];
  895. $chartdata['cycle']['price'] = [
  896. 'data' => $total['price'] ?: 0,
  897. 'percent' => round(abs($cha_price) / $pre_total['price'] * 100, 2),
  898. 'is_plus' => $cha_price > 0 ? -1 : ($cha_price == 0 ? 0 : 1)
  899. ];
  900. }
  901. return $chartdata;
  902. case 'week':
  903. $weekarray = array(['周日'], ['周一'], ['周二'], ['周三'], ['周四'], ['周五'], ['周六']);
  904. $datebefor = date('Y-m-d', strtotime('-1 week Monday'));
  905. $dateafter = date('Y-m-d', strtotime('-1 week Sunday'));
  906. // $order_list = $this->dao->orderAddTimeList($datebefor, $dateafter, 'week');
  907. //数据查询重新处理
  908. $new_order_list = [];
  909. // foreach ($order_list as $k => $v) {
  910. // $new_order_list[$v['day']] = $v;
  911. // }
  912. $now_datebefor = date('Y-m-d', (time() - ((date('w') == 0 ? 7 : date('w')) - 1) * 24 * 3600));
  913. $now_dateafter = date('Y-m-d', strtotime("+1 day"));
  914. $now_order_list = $this->dao->nowOrderList($now_datebefor, $now_dateafter, 'week');
  915. //数据查询重新处理 key 变为当前值
  916. $new_now_order_list = [];
  917. foreach ($now_order_list as $k => $v) {
  918. $new_now_order_list[$v['day']] = $v;
  919. }
  920. foreach ($weekarray as $dk => $dd) {
  921. if (!empty($new_order_list[$dk])) {
  922. $weekarray[$dk]['pre'] = $new_order_list[$dk];
  923. } else {
  924. $weekarray[$dk]['pre'] = ['count' => 0, 'day' => $weekarray[$dk][0], 'price' => '0'];
  925. }
  926. if (!empty($new_now_order_list[$dk])) {
  927. $weekarray[$dk]['now'] = $new_now_order_list[$dk];
  928. } else {
  929. $weekarray[$dk]['now'] = ['count' => 0, 'day' => $weekarray[$dk][0], 'price' => '0'];
  930. }
  931. }
  932. $chartdata = [];
  933. $data = [];//临时
  934. $chartdata['yAxis']['maxnum'] = 0;//最大值数量
  935. $chartdata['yAxis']['maxprice'] = 0;//最大值金额
  936. foreach ($weekarray as $k => $v) {
  937. $data['day'][] = $v[0];
  938. $data['pre']['count'][] = $v['pre']['count'];
  939. $data['pre']['price'][] = round($v['pre']['price'], 2);
  940. $data['now']['count'][] = $v['now']['count'];
  941. $data['now']['price'][] = round($v['now']['price'], 2);
  942. if ($chartdata['yAxis']['maxnum'] < $v['pre']['count'] || $chartdata['yAxis']['maxnum'] < $v['now']['count']) {
  943. $chartdata['yAxis']['maxnum'] = $v['pre']['count'] > $v['now']['count'] ? $v['pre']['count'] : $v['now']['count'];//日最大订单数
  944. }
  945. if ($chartdata['yAxis']['maxprice'] < $v['pre']['price'] || $chartdata['yAxis']['maxprice'] < $v['now']['price']) {
  946. $chartdata['yAxis']['maxprice'] = $v['pre']['price'] > $v['now']['price'] ? $v['pre']['price'] : $v['now']['price'];//日最大金额
  947. }
  948. }
  949. $chartdata['legend'] = ['上周金额', '本周金额', '上周订单数', '本周订单数'];//分类
  950. $chartdata['xAxis'] = $data['day'];//X轴值
  951. $series1 = ['normal' => ['color' => [
  952. 'x' => 0, 'y' => 0, 'x2' => 0, 'y2' => 1,
  953. 'colorStops' => [
  954. [
  955. 'offset' => 0,
  956. 'color' => '#69cdff'
  957. ],
  958. [
  959. 'offset' => 0.5,
  960. 'color' => '#3eb3f7'
  961. ],
  962. [
  963. 'offset' => 1,
  964. 'color' => '#1495eb'
  965. ]
  966. ]
  967. ]]
  968. ];
  969. $series2 = ['normal' => ['color' => [
  970. 'x' => 0, 'y' => 0, 'x2' => 0, 'y2' => 1,
  971. 'colorStops' => [
  972. [
  973. 'offset' => 0,
  974. 'color' => '#6fdeab'
  975. ],
  976. [
  977. 'offset' => 0.5,
  978. 'color' => '#44d693'
  979. ],
  980. [
  981. 'offset' => 1,
  982. 'color' => '#2cc981'
  983. ]
  984. ]
  985. ]]
  986. ];
  987. $series3 = ['normal' => ['color' => [
  988. 'x' => 0, 'y' => 0, 'x2' => 0, 'y2' => 1,
  989. 'colorStops' => [
  990. [
  991. 'offset' => 0,
  992. 'color' => '#69cdff'
  993. ],
  994. [
  995. 'offset' => 0.5,
  996. 'color' => '#3eb3f7'
  997. ],
  998. [
  999. 'offset' => 1,
  1000. 'color' => '#1495eb'
  1001. ]
  1002. ]
  1003. ]]
  1004. ];
  1005. $series4 = ['normal' => ['color' => [
  1006. 'x' => 0, 'y' => 0, 'x2' => 0, 'y2' => 1,
  1007. 'colorStops' => [
  1008. [
  1009. 'offset' => 0,
  1010. 'color' => '#6fdeab'
  1011. ],
  1012. [
  1013. 'offset' => 0.5,
  1014. 'color' => '#44d693'
  1015. ],
  1016. [
  1017. 'offset' => 1,
  1018. 'color' => '#2cc981'
  1019. ]
  1020. ]
  1021. ]]
  1022. ];
  1023. $chartdata['series'][] = ['name' => $chartdata['legend'][0], 'type' => 'bar', 'itemStyle' => $series1, 'data' => $data['pre']['price']];//分类1值
  1024. $chartdata['series'][] = ['name' => $chartdata['legend'][1], 'type' => 'bar', 'itemStyle' => $series2, 'data' => $data['now']['price']];//分类1值
  1025. $chartdata['series'][] = ['name' => $chartdata['legend'][2], 'type' => 'line', 'itemStyle' => $series3, 'data' => $data['pre']['count'], 'yAxisIndex' => 1];//分类2值
  1026. $chartdata['series'][] = ['name' => $chartdata['legend'][3], 'type' => 'line', 'itemStyle' => $series4, 'data' => $data['now']['count'], 'yAxisIndex' => 1];//分类2值
  1027. //统计总数上期
  1028. $pre_total = $this->dao->preTotalFind($datebefor, $dateafter);
  1029. if ($pre_total) {
  1030. $chartdata['pre_cycle']['count'] = [
  1031. 'data' => $pre_total['count'] ?: 0
  1032. ];
  1033. $chartdata['pre_cycle']['price'] = [
  1034. 'data' => $pre_total['price'] ?: 0
  1035. ];
  1036. }
  1037. //统计总数
  1038. $total = $this->dao->preTotalFind($now_datebefor, $now_dateafter);
  1039. if ($total) {
  1040. $cha_count = intval($pre_total['count']) - intval($total['count']);
  1041. $pre_total['count'] = $pre_total['count'] == 0 ? 1 : $pre_total['count'];
  1042. $chartdata['cycle']['count'] = [
  1043. 'data' => $total['count'] ?: 0,
  1044. 'percent' => round((abs($cha_count) / intval($pre_total['count']) * 100), 2),
  1045. 'is_plus' => $cha_count > 0 ? -1 : ($cha_count == 0 ? 0 : 1)
  1046. ];
  1047. $cha_price = round($pre_total['price'], 2) - round($total['price'], 2);
  1048. $pre_total['price'] = $pre_total['price'] == 0 ? 1 : $pre_total['price'];
  1049. $chartdata['cycle']['price'] = [
  1050. 'data' => $total['price'] ?: 0,
  1051. 'percent' => round(abs($cha_price) / $pre_total['price'] * 100, 2),
  1052. 'is_plus' => $cha_price > 0 ? -1 : ($cha_price == 0 ? 0 : 1)
  1053. ];
  1054. }
  1055. return $chartdata;
  1056. case 'month':
  1057. $weekarray = array('01' => ['1'], '02' => ['2'], '03' => ['3'], '04' => ['4'], '05' => ['5'], '06' => ['6'], '07' => ['7'], '08' => ['8'], '09' => ['9'], '10' => ['10'], '11' => ['11'], '12' => ['12'], '13' => ['13'], '14' => ['14'], '15' => ['15'], '16' => ['16'], '17' => ['17'], '18' => ['18'], '19' => ['19'], '20' => ['20'], '21' => ['21'], '22' => ['22'], '23' => ['23'], '24' => ['24'], '25' => ['25'], '26' => ['26'], '27' => ['27'], '28' => ['28'], '29' => ['29'], '30' => ['30'], '31' => ['31']);
  1058. $datebefor = date('Y-m-01', strtotime('-1 month'));
  1059. $dateafter = date('Y-m-d', strtotime(date('Y-m-01')));
  1060. $order_list = $this->dao->orderAddTimeList($datebefor, $dateafter, "month");
  1061. //数据查询重新处理
  1062. $new_order_list = [];
  1063. foreach ($order_list as $k => $v) {
  1064. $new_order_list[$v['day']] = $v;
  1065. }
  1066. $now_datebefor = date('Y-m-01');
  1067. $now_dateafter = date('Y-m-d', strtotime("+1 day"));
  1068. $now_order_list = $this->dao->nowOrderList($now_datebefor, $now_dateafter, "month");
  1069. //数据查询重新处理 key 变为当前值
  1070. $new_now_order_list = [];
  1071. foreach ($now_order_list as $k => $v) {
  1072. $new_now_order_list[$v['day']] = $v;
  1073. }
  1074. foreach ($weekarray as $dk => $dd) {
  1075. if (!empty($new_order_list[$dk])) {
  1076. $weekarray[$dk]['pre'] = $new_order_list[$dk];
  1077. } else {
  1078. $weekarray[$dk]['pre'] = ['count' => 0, 'day' => $weekarray[$dk][0], 'price' => '0'];
  1079. }
  1080. if (!empty($new_now_order_list[$dk])) {
  1081. $weekarray[$dk]['now'] = $new_now_order_list[$dk];
  1082. } else {
  1083. $weekarray[$dk]['now'] = ['count' => 0, 'day' => $weekarray[$dk][0], 'price' => '0'];
  1084. }
  1085. }
  1086. $chartdata = [];
  1087. $data = [];//临时
  1088. $chartdata['yAxis']['maxnum'] = 0;//最大值数量
  1089. $chartdata['yAxis']['maxprice'] = 0;//最大值金额
  1090. foreach ($weekarray as $k => $v) {
  1091. $data['day'][] = $v[0];
  1092. $data['pre']['count'][] = $v['pre']['count'];
  1093. $data['pre']['price'][] = round($v['pre']['price'], 2);
  1094. $data['now']['count'][] = $v['now']['count'];
  1095. $data['now']['price'][] = round($v['now']['price'], 2);
  1096. if ($chartdata['yAxis']['maxnum'] < $v['pre']['count'] || $chartdata['yAxis']['maxnum'] < $v['now']['count']) {
  1097. $chartdata['yAxis']['maxnum'] = $v['pre']['count'] > $v['now']['count'] ? $v['pre']['count'] : $v['now']['count'];//日最大订单数
  1098. }
  1099. if ($chartdata['yAxis']['maxprice'] < $v['pre']['price'] || $chartdata['yAxis']['maxprice'] < $v['now']['price']) {
  1100. $chartdata['yAxis']['maxprice'] = $v['pre']['price'] > $v['now']['price'] ? $v['pre']['price'] : $v['now']['price'];//日最大金额
  1101. }
  1102. }
  1103. $chartdata['legend'] = ['上月金额', '本月金额', '上月订单数', '本月订单数'];//分类
  1104. $chartdata['xAxis'] = $data['day'];//X轴值
  1105. $series1 = ['normal' => ['color' => [
  1106. 'x' => 0, 'y' => 0, 'x2' => 0, 'y2' => 1,
  1107. 'colorStops' => [
  1108. [
  1109. 'offset' => 0,
  1110. 'color' => '#69cdff'
  1111. ],
  1112. [
  1113. 'offset' => 0.5,
  1114. 'color' => '#3eb3f7'
  1115. ],
  1116. [
  1117. 'offset' => 1,
  1118. 'color' => '#1495eb'
  1119. ]
  1120. ]
  1121. ]]
  1122. ];
  1123. $series2 = ['normal' => ['color' => [
  1124. 'x' => 0, 'y' => 0, 'x2' => 0, 'y2' => 1,
  1125. 'colorStops' => [
  1126. [
  1127. 'offset' => 0,
  1128. 'color' => '#6fdeab'
  1129. ],
  1130. [
  1131. 'offset' => 0.5,
  1132. 'color' => '#44d693'
  1133. ],
  1134. [
  1135. 'offset' => 1,
  1136. 'color' => '#2cc981'
  1137. ]
  1138. ]
  1139. ]]
  1140. ];
  1141. $series3 = ['normal' => ['color' => [
  1142. 'x' => 0, 'y' => 0, 'x2' => 0, 'y2' => 1,
  1143. 'colorStops' => [
  1144. [
  1145. 'offset' => 0,
  1146. 'color' => '#69cdff'
  1147. ],
  1148. [
  1149. 'offset' => 0.5,
  1150. 'color' => '#3eb3f7'
  1151. ],
  1152. [
  1153. 'offset' => 1,
  1154. 'color' => '#1495eb'
  1155. ]
  1156. ]
  1157. ]]
  1158. ];
  1159. $series4 = ['normal' => ['color' => [
  1160. 'x' => 0, 'y' => 0, 'x2' => 0, 'y2' => 1,
  1161. 'colorStops' => [
  1162. [
  1163. 'offset' => 0,
  1164. 'color' => '#6fdeab'
  1165. ],
  1166. [
  1167. 'offset' => 0.5,
  1168. 'color' => '#44d693'
  1169. ],
  1170. [
  1171. 'offset' => 1,
  1172. 'color' => '#2cc981'
  1173. ]
  1174. ]
  1175. ]]
  1176. ];
  1177. $chartdata['series'][] = ['name' => $chartdata['legend'][0], 'type' => 'bar', 'itemStyle' => $series1, 'data' => $data['pre']['price']];//分类1值
  1178. $chartdata['series'][] = ['name' => $chartdata['legend'][1], 'type' => 'bar', 'itemStyle' => $series2, 'data' => $data['now']['price']];//分类1值
  1179. $chartdata['series'][] = ['name' => $chartdata['legend'][2], 'type' => 'line', 'itemStyle' => $series3, 'data' => $data['pre']['count'], 'yAxisIndex' => 1];//分类2值
  1180. $chartdata['series'][] = ['name' => $chartdata['legend'][3], 'type' => 'line', 'itemStyle' => $series4, 'data' => $data['now']['count'], 'yAxisIndex' => 1];//分类2值
  1181. //统计总数上期
  1182. $pre_total = $this->dao->preTotalFind($datebefor, $dateafter);
  1183. if ($pre_total) {
  1184. $chartdata['pre_cycle']['count'] = [
  1185. 'data' => $pre_total['count'] ?: 0
  1186. ];
  1187. $chartdata['pre_cycle']['price'] = [
  1188. 'data' => $pre_total['price'] ?: 0
  1189. ];
  1190. }
  1191. //统计总数
  1192. $total = $this->dao->preTotalFind($now_datebefor, $now_dateafter);
  1193. if ($total) {
  1194. $cha_count = intval($pre_total['count']) - intval($total['count']);
  1195. $pre_total['count'] = $pre_total['count'] == 0 ? 1 : $pre_total['count'];
  1196. $chartdata['cycle']['count'] = [
  1197. 'data' => $total['count'] ?: 0,
  1198. 'percent' => round((abs($cha_count) / intval($pre_total['count']) * 100), 2),
  1199. 'is_plus' => $cha_count > 0 ? -1 : ($cha_count == 0 ? 0 : 1)
  1200. ];
  1201. $cha_price = round($pre_total['price'], 2) - round($total['price'], 2);
  1202. $pre_total['price'] = $pre_total['price'] == 0 ? 1 : $pre_total['price'];
  1203. $chartdata['cycle']['price'] = [
  1204. 'data' => $total['price'] ?: 0,
  1205. 'percent' => round(abs($cha_price) / $pre_total['price'] * 100, 2),
  1206. 'is_plus' => $cha_price > 0 ? -1 : ($cha_price == 0 ? 0 : 1)
  1207. ];
  1208. }
  1209. return $chartdata;
  1210. case 'year':
  1211. $weekarray = array('01' => ['一月'], '02' => ['二月'], '03' => ['三月'], '04' => ['四月'], '05' => ['五月'], '06' => ['六月'], '07' => ['七月'], '08' => ['八月'], '09' => ['九月'], '10' => ['十月'], '11' => ['十一月'], '12' => ['十二月']);
  1212. $datebefor = date('Y-01-01', strtotime('-1 year'));
  1213. $dateafter = date('Y-12-31', strtotime('-1 year'));
  1214. $order_list = $this->dao->orderAddTimeList($datebefor, $dateafter, 'year');
  1215. //数据查询重新处理
  1216. $new_order_list = [];
  1217. foreach ($order_list as $k => $v) {
  1218. $new_order_list[$v['day']] = $v;
  1219. }
  1220. $now_datebefor = date('Y-01-01');
  1221. $now_dateafter = date('Y-12-31 23:59:59');
  1222. $now_order_list = $this->dao->nowOrderList($now_datebefor, $now_dateafter, 'year');
  1223. //数据查询重新处理 key 变为当前值
  1224. $new_now_order_list = [];
  1225. foreach ($now_order_list as $k => $v) {
  1226. $new_now_order_list[$v['day']] = $v;
  1227. }
  1228. foreach ($weekarray as $dk => $dd) {
  1229. if (!empty($new_order_list[$dk])) {
  1230. $weekarray[$dk]['pre'] = $new_order_list[$dk];
  1231. } else {
  1232. $weekarray[$dk]['pre'] = ['count' => 0, 'day' => $weekarray[$dk][0], 'price' => '0'];
  1233. }
  1234. if (!empty($new_now_order_list[$dk])) {
  1235. $weekarray[$dk]['now'] = $new_now_order_list[$dk];
  1236. } else {
  1237. $weekarray[$dk]['now'] = ['count' => 0, 'day' => $weekarray[$dk][0], 'price' => '0'];
  1238. }
  1239. }
  1240. $chartdata = [];
  1241. $data = [];//临时
  1242. $chartdata['yAxis']['maxnum'] = 0;//最大值数量
  1243. $chartdata['yAxis']['maxprice'] = 0;//最大值金额
  1244. foreach ($weekarray as $k => $v) {
  1245. $data['day'][] = $v[0];
  1246. $data['pre']['count'][] = $v['pre']['count'];
  1247. $data['pre']['price'][] = round($v['pre']['price'], 2);
  1248. $data['now']['count'][] = $v['now']['count'];
  1249. $data['now']['price'][] = round($v['now']['price'], 2);
  1250. if ($chartdata['yAxis']['maxnum'] < $v['pre']['count'] || $chartdata['yAxis']['maxnum'] < $v['now']['count']) {
  1251. $chartdata['yAxis']['maxnum'] = $v['pre']['count'] > $v['now']['count'] ? $v['pre']['count'] : $v['now']['count'];//日最大订单数
  1252. }
  1253. if ($chartdata['yAxis']['maxprice'] < $v['pre']['price'] || $chartdata['yAxis']['maxprice'] < $v['now']['price']) {
  1254. $chartdata['yAxis']['maxprice'] = $v['pre']['price'] > $v['now']['price'] ? $v['pre']['price'] : $v['now']['price'];//日最大金额
  1255. }
  1256. }
  1257. $chartdata['legend'] = ['去年金额', '今年金额', '去年订单数', '今年订单数'];//分类
  1258. $chartdata['xAxis'] = $data['day'];//X轴值
  1259. $series1 = ['normal' => ['color' => [
  1260. 'x' => 0, 'y' => 0, 'x2' => 0, 'y2' => 1,
  1261. 'colorStops' => [
  1262. [
  1263. 'offset' => 0,
  1264. 'color' => '#69cdff'
  1265. ],
  1266. [
  1267. 'offset' => 0.5,
  1268. 'color' => '#3eb3f7'
  1269. ],
  1270. [
  1271. 'offset' => 1,
  1272. 'color' => '#1495eb'
  1273. ]
  1274. ]
  1275. ]]
  1276. ];
  1277. $series2 = ['normal' => ['color' => [
  1278. 'x' => 0, 'y' => 0, 'x2' => 0, 'y2' => 1,
  1279. 'colorStops' => [
  1280. [
  1281. 'offset' => 0,
  1282. 'color' => '#6fdeab'
  1283. ],
  1284. [
  1285. 'offset' => 0.5,
  1286. 'color' => '#44d693'
  1287. ],
  1288. [
  1289. 'offset' => 1,
  1290. 'color' => '#2cc981'
  1291. ]
  1292. ]
  1293. ]]
  1294. ];
  1295. $series3 = ['normal' => ['color' => [
  1296. 'x' => 0, 'y' => 0, 'x2' => 0, 'y2' => 1,
  1297. 'colorStops' => [
  1298. [
  1299. 'offset' => 0,
  1300. 'color' => '#69cdff'
  1301. ],
  1302. [
  1303. 'offset' => 0.5,
  1304. 'color' => '#3eb3f7'
  1305. ],
  1306. [
  1307. 'offset' => 1,
  1308. 'color' => '#1495eb'
  1309. ]
  1310. ]
  1311. ]]
  1312. ];
  1313. $series4 = ['normal' => ['color' => [
  1314. 'x' => 0, 'y' => 0, 'x2' => 0, 'y2' => 1,
  1315. 'colorStops' => [
  1316. [
  1317. 'offset' => 0,
  1318. 'color' => '#6fdeab'
  1319. ],
  1320. [
  1321. 'offset' => 0.5,
  1322. 'color' => '#44d693'
  1323. ],
  1324. [
  1325. 'offset' => 1,
  1326. 'color' => '#2cc981'
  1327. ]
  1328. ]
  1329. ]]
  1330. ];
  1331. $chartdata['series'][] = ['name' => $chartdata['legend'][0], 'type' => 'bar', 'itemStyle' => $series1, 'data' => $data['pre']['price']];//分类1值
  1332. $chartdata['series'][] = ['name' => $chartdata['legend'][1], 'type' => 'bar', 'itemStyle' => $series2, 'data' => $data['now']['price']];//分类1值
  1333. $chartdata['series'][] = ['name' => $chartdata['legend'][2], 'type' => 'line', 'itemStyle' => $series3, 'data' => $data['pre']['count'], 'yAxisIndex' => 1];//分类2值
  1334. $chartdata['series'][] = ['name' => $chartdata['legend'][3], 'type' => 'line', 'itemStyle' => $series4, 'data' => $data['now']['count'], 'yAxisIndex' => 1];//分类2值
  1335. //统计总数上期
  1336. $pre_total = $this->dao->preTotalFind($datebefor, $dateafter);
  1337. if ($pre_total) {
  1338. $chartdata['pre_cycle']['count'] = [
  1339. 'data' => $pre_total['count'] ?: 0
  1340. ];
  1341. $chartdata['pre_cycle']['price'] = [
  1342. 'data' => $pre_total['price'] ?: 0
  1343. ];
  1344. }
  1345. //统计总数
  1346. $total = $this->dao->preTotalFind($now_datebefor, $now_dateafter);
  1347. if ($total) {
  1348. $cha_count = intval($pre_total['count']) - intval($total['count']);
  1349. $pre_total['count'] = $pre_total['count'] == 0 ? 1 : $pre_total['count'];
  1350. $chartdata['cycle']['count'] = [
  1351. 'data' => $total['count'] ?: 0,
  1352. 'percent' => round((abs($cha_count) / intval($pre_total['count']) * 100), 2),
  1353. 'is_plus' => $cha_count > 0 ? -1 : ($cha_count == 0 ? 0 : 1)
  1354. ];
  1355. $cha_price = round($pre_total['price'], 2) - round($total['price'], 2);
  1356. $pre_total['price'] = $pre_total['price'] == 0 ? 1 : $pre_total['price'];
  1357. $chartdata['cycle']['price'] = [
  1358. 'data' => $total['price'] ?: 0,
  1359. 'percent' => round(abs($cha_price) / $pre_total['price'] * 100, 2),
  1360. 'is_plus' => $cha_price > 0 ? -1 : ($cha_price == 0 ? 0 : 1)
  1361. ];
  1362. }
  1363. return $chartdata;
  1364. default:
  1365. break;
  1366. }
  1367. }
  1368. /**
  1369. * 获取订单数量
  1370. * @return int
  1371. */
  1372. public function storeOrderCount()
  1373. {
  1374. return $this->dao->storeOrderCount();
  1375. }
  1376. /**
  1377. * 新订单ID
  1378. * @param $status
  1379. * @return array
  1380. */
  1381. public function newOrderId($status)
  1382. {
  1383. return $this->dao->search(['status' => $status, 'is_remind' => 0])->column('order_id', 'id');
  1384. }
  1385. /**
  1386. * 新订单修改
  1387. * @param $newOrderId
  1388. * @return \crmeb\basic\BaseModel
  1389. */
  1390. public function newOrderUpdate($newOrderId)
  1391. {
  1392. return $this->dao->newOrderUpdates($newOrderId);
  1393. }
  1394. /**
  1395. * 增长率
  1396. * @param $left
  1397. * @param $right
  1398. * @return int|string
  1399. */
  1400. public function growth($nowValue, $lastValue)
  1401. {
  1402. if ($lastValue == 0 && $nowValue == 0) return 0;
  1403. if ($lastValue == 0) return bcmul((string)$nowValue, '100', 2);
  1404. if ($nowValue == 0) return bcdiv(bcsub($nowValue, $lastValue, 2), $lastValue, 4) * 100;
  1405. return bcmul(bcdiv((bcsub($nowValue, $lastValue, 2)), $lastValue, 4), 100, 2);
  1406. }
  1407. /**
  1408. * 后台首页顶部统计
  1409. * @return array
  1410. * @author 吴汐
  1411. * @email 442384644@qq.com
  1412. * @date 2023/04/03
  1413. */
  1414. public function homeStatics()
  1415. {
  1416. /** @var UserServices $userService */
  1417. $userService = app()->make(UserServices::class);
  1418. /** @var StoreProductLogServices $productLogServices */
  1419. $productLogServices = app()->make(StoreProductLogServices::class);
  1420. //TODO 销售额
  1421. //今日销售额
  1422. $today_sales = $this->dao->todaySales('today');
  1423. //昨日销售额
  1424. $yesterday_sales = $this->dao->todaySales('yesterday');
  1425. //日同比
  1426. $sales_today_ratio = $this->growth($today_sales, $yesterday_sales);
  1427. //总销售额
  1428. $total_sales = $this->dao->totalSales('month');
  1429. $sales = [
  1430. 'today' => $today_sales,
  1431. 'yesterday' => $yesterday_sales,
  1432. 'today_ratio' => $sales_today_ratio,
  1433. 'total' => $total_sales . '元',
  1434. 'date' => '今日'
  1435. ];
  1436. //TODO:用户访问量
  1437. //今日访问量
  1438. $today_visits = $productLogServices->count(['time' => 'today', 'type' => 'visit']);
  1439. //昨日访问量
  1440. $yesterday_visits = $productLogServices->count(['time' => 'yesterday', 'type' => 'visit']);
  1441. //日同比
  1442. $visits_today_ratio = $this->growth($today_visits, $yesterday_visits);
  1443. //总访问量
  1444. $total_visits = $productLogServices->count(['time' => 'month', 'type' => 'visit']);
  1445. $visits = [
  1446. 'today' => $today_visits,
  1447. 'yesterday' => $yesterday_visits,
  1448. 'today_ratio' => $visits_today_ratio,
  1449. 'total' => $total_visits . 'Pv',
  1450. 'date' => '今日'
  1451. ];
  1452. //TODO 订单量
  1453. //今日订单量
  1454. $today_order = $this->dao->todayOrderVisit('today', 1);
  1455. //昨日订单量
  1456. $yesterday_order = $this->dao->todayOrderVisit('yesterday', 1);
  1457. //订单日同比
  1458. $order_today_ratio = $this->growth($today_order, $yesterday_order);
  1459. //总订单量
  1460. $total_order = $this->dao->count(['time' => 'month', 'paid' => 1, 'refund_status' => 0, 'pid' => 0]);
  1461. $order = [
  1462. 'today' => $today_order,
  1463. 'yesterday' => $yesterday_order,
  1464. 'today_ratio' => $order_today_ratio,
  1465. 'total' => $total_order . '单',
  1466. 'date' => '今日'
  1467. ];
  1468. //TODO 用户
  1469. //今日新增用户
  1470. $today_user = $userService->todayAddVisits('today', 1);
  1471. //昨日新增用户
  1472. $yesterday_user = $userService->todayAddVisits('yesterday', 1);
  1473. //新增用户日同比
  1474. $user_today_ratio = $this->growth($today_user, $yesterday_user);
  1475. //所有用户
  1476. $total_user = $userService->count(['time' => 'month']);
  1477. $user = [
  1478. 'today' => $today_user,
  1479. 'yesterday' => $yesterday_user,
  1480. 'today_ratio' => $user_today_ratio,
  1481. 'total' => $total_user . '人',
  1482. 'date' => '今日'
  1483. ];
  1484. $info = array_values(compact('sales', 'visits', 'order', 'user'));
  1485. $info[0]['title'] = '销售额';
  1486. $info[1]['title'] = '用户访问量';
  1487. $info[2]['title'] = '订单量';
  1488. $info[3]['title'] = '新增用户';
  1489. $info[0]['total_name'] = '本月销售额';
  1490. $info[1]['total_name'] = '本月访问量';
  1491. $info[2]['total_name'] = '本月订单量';
  1492. $info[3]['total_name'] = '本月新增用户';
  1493. return $info;
  1494. }
  1495. /**
  1496. * 订单小票打印
  1497. * @param int $id
  1498. * @param bool $start
  1499. * @return bool|void
  1500. * @throws \think\db\exception\DataNotFoundException
  1501. * @throws \think\db\exception\DbException
  1502. * @throws \think\db\exception\ModelNotFoundException
  1503. * @throws \Exception
  1504. */
  1505. public function orderPrintTicket(int $id)
  1506. {
  1507. $order = $this->get($id);
  1508. if (!$order) {
  1509. throw new AdminException(400118);
  1510. }
  1511. /** @var StoreOrderCartInfoServices $cartServices */
  1512. $cartServices = app()->make(StoreOrderCartInfoServices::class);
  1513. $product = $cartServices->getCartInfoPrintProduct($order['id']);
  1514. if (!$product) {
  1515. throw new AdminException(400463);
  1516. }
  1517. $switch = (bool)sys_config('pay_success_printing_switch');
  1518. if (!$switch) {
  1519. throw new AdminException(400464);
  1520. }
  1521. if (sys_config('print_type', 1) == 1) {
  1522. $name = 'yi_lian_yun';
  1523. $configData = [
  1524. 'clientId' => sys_config('printing_client_id', ''),
  1525. 'apiKey' => sys_config('printing_api_key', ''),
  1526. 'partner' => sys_config('develop_id', ''),
  1527. 'terminal' => sys_config('terminal_number', '')
  1528. ];
  1529. if (!$configData['clientId'] || !$configData['apiKey'] || !$configData['partner'] || !$configData['terminal']) {
  1530. throw new AdminException(400465);
  1531. }
  1532. } else {
  1533. $name = 'fei_e_yun';
  1534. $configData = [
  1535. 'feyUser' => sys_config('fey_user', ''),
  1536. 'feyUkey' => sys_config('fey_ukey', ''),
  1537. 'feySn' => sys_config('fey_sn', '')
  1538. ];
  1539. if (!$configData['feyUser'] || !$configData['feyUkey'] || !$configData['feySn']) {
  1540. throw new AdminException(400465);
  1541. }
  1542. }
  1543. $printer = new Printer($name, $configData);
  1544. $res = $printer->setPrinterContent([
  1545. 'name' => sys_config('site_name'),
  1546. 'url' => sys_config('site_url'),
  1547. 'orderInfo' => is_object($order) ? $order->toArray() : $order,
  1548. 'product' => $product
  1549. ])->startPrinter();
  1550. if (!$res) {
  1551. throw new AdminException($printer->getError());
  1552. }
  1553. return true;
  1554. }
  1555. /**
  1556. * 获取订单确认数据
  1557. * @param array $user
  1558. * @param $cartId
  1559. * @param bool $new
  1560. * @param int $addressId
  1561. * @param int $shipping_type
  1562. * @return array
  1563. * @throws \Psr\SimpleCache\InvalidArgumentException
  1564. * @throws \think\db\exception\DataNotFoundException
  1565. * @throws \think\db\exception\DbException
  1566. * @throws \think\db\exception\ModelNotFoundException
  1567. */
  1568. public function getOrderConfirmData(array $user, $cartId, bool $new, int $addressId, int $shipping_type = 1)
  1569. {
  1570. $addr = [];
  1571. /** @var UserAddressServices $addressServices */
  1572. $addressServices = app()->make(UserAddressServices::class);
  1573. if ($addressId) {
  1574. $addr = $addressServices->getAddress($addressId);
  1575. }
  1576. //没传地址id或地址已删除未找到 ||获取默认地址
  1577. if (!$addr) {
  1578. $addr = $addressServices->getUserDefaultAddress((int)$user['uid']);
  1579. }
  1580. if ($addr) {
  1581. $addr = $addr->toArray();
  1582. } else {
  1583. $addr = [];
  1584. }
  1585. if ($shipping_type == 2) $addr = [];
  1586. /** @var StoreCartServices $cartServices */
  1587. $cartServices = app()->make(StoreCartServices::class);
  1588. $cartGroup = $cartServices->getUserProductCartListV1($user['uid'], $cartId, $new, $addr, $shipping_type);
  1589. $data = [];
  1590. $data['storeFreePostage'] = $storeFreePostage = floatval(sys_config('store_free_postage')) ?: 0;//满额包邮金额
  1591. $validCartInfo = $cartGroup['valid'];
  1592. if (count($validCartInfo)) {
  1593. if (isset($validCartInfo[0]['productInfo']['is_virtual']) && $validCartInfo[0]['productInfo']['is_virtual']) {
  1594. $data['virtual_type'] = 1;
  1595. $data['deduction'] = true;
  1596. } else {
  1597. if ($validCartInfo[0]['productInfo']['virtual_type'] == 3) {
  1598. $data['virtual_type'] = 1;
  1599. $data['deduction'] = true;
  1600. } else {
  1601. $data['virtual_type'] = 0;
  1602. }
  1603. }
  1604. }
  1605. /** @var StoreOrderComputedServices $computedServices */
  1606. $computedServices = app()->make(StoreOrderComputedServices::class);
  1607. $priceGroup = $computedServices->getOrderPriceGroup($storeFreePostage, $validCartInfo, $addr, $user);
  1608. $validCartInfo = $priceGroup['cartInfo'] ?? $validCartInfo;
  1609. $other = [
  1610. 'offlinePostage' => sys_config('offline_postage'),
  1611. 'integralRatio' => sys_config('integral_ratio')
  1612. ];
  1613. $cartIdA = explode(',', $cartId);
  1614. $seckill_id = 0;
  1615. $combination_id = 0;
  1616. $bargain_id = 0;
  1617. $advance_id = 0;
  1618. if (count($cartIdA) == 1) {
  1619. $seckill_id = $cartGroup['deduction']['seckill_id'] ?? 0;
  1620. $combination_id = $cartGroup['deduction']['combination_id'] ?? 0;
  1621. $bargain_id = $cartGroup['deduction']['bargain_id'] ?? 0;
  1622. $advance_id = $cartGroup['deduction']['advance_id'] ?? 0;
  1623. }
  1624. $data['valid_count'] = count($validCartInfo);
  1625. $data['deduction'] = $seckill_id || $combination_id || $bargain_id || $advance_id;
  1626. $data['addressInfo'] = $addr;
  1627. $data['seckill_id'] = $seckill_id;
  1628. $data['combination_id'] = $combination_id;
  1629. $data['bargain_id'] = $bargain_id;
  1630. $data['advance_id'] = $advance_id;
  1631. $data['cartInfo'] = $cartGroup['cartInfo'];
  1632. $data['custom_form'] = json_decode($cartGroup['cartInfo'][0]['productInfo']['custom_form'], true) ?? [];
  1633. $data['priceGroup'] = $priceGroup;
  1634. $data['orderKey'] = $this->cacheOrderInfo($user['uid'], $validCartInfo, $priceGroup, $other);
  1635. $data['offlinePostage'] = $other['offlinePostage'];
  1636. /** @var UserLevelServices $levelServices */
  1637. $levelServices = app()->make(UserLevelServices::class);
  1638. $userLevel = $levelServices->getUerLevelInfoByUid($user['uid']);
  1639. if (isset($user['pwd'])) unset($user['pwd']);
  1640. $user['vip'] = $userLevel !== false;
  1641. if ($user['vip']) {
  1642. $user['vip_id'] = $userLevel['id'] ?? 0;
  1643. $user['discount'] = $userLevel['discount'] ?? 0;
  1644. }
  1645. $data['userInfo'] = $user;
  1646. $data['integralRatio'] = $other['integralRatio'];
  1647. $data['offline_pay_status'] = (int)sys_config('offline_pay_status') ?? (int)2;
  1648. $data['yue_pay_status'] = (int)sys_config('balance_func_status') && (int)sys_config('yue_pay_status') == 1 ? (int)1 : (int)2;//余额支付 1 开启 2 关闭
  1649. $data['pay_weixin_open'] = sys_config('pay_weixin_open', '0') != '0';//微信支付 1 开启 0 关闭
  1650. $data['friend_pay_status'] = (int)sys_config('friend_pay_status') ?? 0;//好友代付 1 开启 0 关闭
  1651. $data['store_self_mention'] = (int)sys_config('store_self_mention') ?? 0;//门店自提是否开启
  1652. /** @var SystemStoreServices $systemStoreServices */
  1653. $systemStoreServices = app()->make(SystemStoreServices::class);
  1654. $store_count = $systemStoreServices->count(['type' => 0]);
  1655. $data['store_self_mention'] = $data['store_self_mention'] && $store_count;
  1656. $data['ali_pay_status'] = sys_config('ali_pay_status', '0') != '0';//支付包支付 1 开启 0 关闭
  1657. $data['system_store'] = [];//门店信息
  1658. /** @var UserInvoiceServices $userInvoice */
  1659. $userInvoice = app()->make(UserInvoiceServices::class);
  1660. $invoice_func = $userInvoice->invoiceFuncStatus();
  1661. $data['invoice_func'] = $invoice_func['invoice_func'];
  1662. $data['special_invoice'] = $invoice_func['special_invoice'];
  1663. /** @var UserBillServices $userBillServices */
  1664. $userBillServices = app()->make(UserBillServices::class);
  1665. $data['usable_integral'] = bcsub((string)$user['integral'], (string)$userBillServices->getBillSum(['uid' => $user['uid'], 'is_frozen' => 1]), 0);
  1666. $data['integral_open'] = sys_config('integral_ratio', 0) > 0;
  1667. return $data;
  1668. }
  1669. /**
  1670. * 缓存订单信息
  1671. * @param $uid
  1672. * @param $cartInfo
  1673. * @param $priceGroup
  1674. * @param array $other
  1675. * @param int $cacheTime
  1676. * @return string
  1677. * @throws \Psr\SimpleCache\InvalidArgumentException
  1678. */
  1679. public function cacheOrderInfo($uid, $cartInfo, $priceGroup, $other = [], $cacheTime = 600)
  1680. {
  1681. $key = $this->getCacheKey();
  1682. CacheService::set('user_order_' . $uid . $key, compact('cartInfo', 'priceGroup', 'other'), $cacheTime);
  1683. return $key;
  1684. }
  1685. /**
  1686. * 使用雪花算法生成订单ID
  1687. * @return string
  1688. * @throws \Exception
  1689. */
  1690. public function getCacheKey(string $prefix = '')
  1691. {
  1692. $snowflake = new \Godruoyi\Snowflake\Snowflake();
  1693. //32位
  1694. if (PHP_INT_SIZE == 4) {
  1695. $id = abs($snowflake->id());
  1696. } else {
  1697. $id = $snowflake->setStartTimeStamp(strtotime('2020-06-05') * 1000)->id();
  1698. }
  1699. return $prefix . $id;
  1700. }
  1701. /**获取用户购买活动产品的次数
  1702. * @param $uid
  1703. * @param $seckill_id
  1704. * @return int
  1705. */
  1706. public function activityProductCount(array $where)
  1707. {
  1708. return $this->dao->count($where);
  1709. }
  1710. /**
  1711. * 获取订单缓存信息
  1712. * @param int $uid
  1713. * @param string $key
  1714. * @return |null
  1715. */
  1716. public function getCacheOrderInfo(int $uid, string $key)
  1717. {
  1718. $cacheName = 'user_order_' . $uid . $key;
  1719. if (!CacheService::has($cacheName)) return null;
  1720. return CacheService::get($cacheName);
  1721. }
  1722. /**
  1723. * 获取拼团的订单id
  1724. * @param int $pid
  1725. * @param int $uid
  1726. * @return mixed
  1727. */
  1728. public function getStoreIdPink(int $pid, int $uid)
  1729. {
  1730. return $this->dao->value(['uid' => $uid, 'pink_id' => $pid, 'is_del' => 0], 'order_id');
  1731. }
  1732. /**
  1733. * 判断当前订单中是否有拼团
  1734. * @param int $pid
  1735. * @param int $uid
  1736. * @return int
  1737. */
  1738. public function getIsOrderPink($pid = 0, $uid = 0)
  1739. {
  1740. return $this->dao->count(['uid' => $uid, 'pink_id' => $pid, 'refund_status' => 0, 'is_del' => 0]);
  1741. }
  1742. /**
  1743. * 判断支付方式是否开启
  1744. * @param $payType
  1745. * @return bool
  1746. */
  1747. public function checkPaytype(string $payType)
  1748. {
  1749. $res = false;
  1750. switch ($payType) {
  1751. case PayServices::WEIXIN_PAY:
  1752. $res = sys_config('pay_weixin_open', '0') != '0';
  1753. break;
  1754. case PayServices::YUE_PAY:
  1755. $res = sys_config('balance_func_status') && sys_config('yue_pay_status') == 1;
  1756. break;
  1757. case 'offline':
  1758. $res = sys_config('offline_pay_status') == 1;
  1759. break;
  1760. case PayServices::ALIAPY_PAY:
  1761. $res = sys_config('ali_pay_status', '0') != '0';
  1762. break;
  1763. case PayServices::FRIEND:
  1764. $res = sys_config('friend_pay_status', 1) == 1;
  1765. break;
  1766. case PayServices::ALLIN_PAY:
  1767. $res = sys_config('allin_pay_status') == 1;
  1768. }
  1769. return $res;
  1770. }
  1771. /**
  1772. * 修改支付方式为线下支付
  1773. * @param string $orderId
  1774. * @return bool|\crmeb\basic\BaseModel
  1775. */
  1776. public function setOrderTypePayOffline(string $orderId)
  1777. {
  1778. if (($count = strpos($orderId, '_')) !== false) {
  1779. $orderId = substr($orderId, $count + 1);
  1780. }
  1781. if (sys_config('offline_postage', 0) == 1) {
  1782. $orderInfo = $this->dao->get(['order_id' => $orderId]);
  1783. $cartInfoService = app()->make(StoreOrderCartInfoServices::class);
  1784. $cartInfo = $cartInfoService->getColumn(['oid' => $orderInfo['id']], 'cart_info', 'id');
  1785. foreach ($cartInfo as $key => &$item) {
  1786. $item_arr = json_decode($item, true);
  1787. $item_arr['postage_price'] = $item_arr['origin_postage_price'] = 0;
  1788. $cartInfoService->update(['id' => $key], ['cart_info' => json_encode($item_arr)]);
  1789. }
  1790. return $this->dao->update($orderId, [
  1791. 'pay_type' => 'offline',
  1792. 'pay_price' => bcsub((string)$orderInfo['pay_price'], (string)$orderInfo['pay_postage'], 2),
  1793. 'pay_postage' => 0
  1794. ], 'order_id');
  1795. }
  1796. return $this->dao->update($orderId, ['pay_type' => 'offline'], 'order_id');
  1797. }
  1798. /**
  1799. * 删除订单
  1800. * @param string $uni
  1801. * @param int $uid
  1802. * @return bool
  1803. * @throws \Psr\SimpleCache\InvalidArgumentException
  1804. */
  1805. public function removeOrder(string $uni, int $uid)
  1806. {
  1807. $order = $this->getUserOrderDetail($uni, $uid);
  1808. if (!$order) {
  1809. throw new ApiException(410173);
  1810. }
  1811. $order = $this->tidyOrder($order);
  1812. if ($order['_status']['_type'] != 0 && $order['_status']['_type'] != -2 && $order['_status']['_type'] != 4)
  1813. throw new ApiException(410256);
  1814. $order->is_del = 1;
  1815. /** @var StoreOrderStatusServices $statusService */
  1816. $statusService = app()->make(StoreOrderStatusServices::class);
  1817. $res = $statusService->save([
  1818. 'oid' => $order['id'],
  1819. 'change_type' => 'remove_order',
  1820. 'change_message' => '删除订单',
  1821. 'change_time' => time()
  1822. ]);
  1823. if ($order->save() && $res) {
  1824. return true;
  1825. } else
  1826. throw new ApiException(100020);
  1827. }
  1828. /**
  1829. * 取消订单
  1830. * @param $order_id
  1831. * @param $uid
  1832. * @return bool|void
  1833. * @throws \think\db\exception\DataNotFoundException
  1834. * @throws \think\db\exception\DbException
  1835. * @throws \think\db\exception\ModelNotFoundException
  1836. */
  1837. public function cancelOrder($order_id, int $uid)
  1838. {
  1839. $order = $this->dao->getOne(['order_id' => $order_id, 'uid' => $uid, 'is_del' => 0]);
  1840. if (!$order) {
  1841. throw new ApiException(410173);
  1842. }
  1843. if ($order->paid) {
  1844. throw new ApiException(410257);
  1845. }
  1846. /** @var StoreOrderCartInfoServices $cartServices */
  1847. $cartServices = app()->make(StoreOrderCartInfoServices::class);
  1848. $cartInfo = $cartServices->getOrderCartInfo($order['id']);
  1849. /** @var StoreOrderRefundServices $refundServices */
  1850. $refundServices = app()->make(StoreOrderRefundServices::class);
  1851. $this->transaction(function () use ($refundServices, $order) {
  1852. $res = $refundServices->integralAndCouponBack($order, 'cancel') && $refundServices->regressionStock($order);
  1853. $order->is_del = 1;
  1854. if (!($res && $order->save())) {
  1855. throw new ApiException(100020);
  1856. }
  1857. });
  1858. return true;
  1859. }
  1860. /**
  1861. * 判断订单完成
  1862. * @param StoreProductReplyServices $replyServices
  1863. * @param array $uniqueList
  1864. * @param $oid
  1865. * @return mixed
  1866. */
  1867. public function checkOrderOver($replyServices, array $uniqueList, $oid)
  1868. {
  1869. //订单商品全部评价完成
  1870. $replyServices->count(['unique' => $uniqueList, 'oid' => $oid]);
  1871. if ($replyServices->count(['unique' => $uniqueList, 'oid' => $oid]) == count($uniqueList)) {
  1872. $res = $this->dao->update($oid, ['status' => '3']);
  1873. if (!$res) throw new ApiException(100007);
  1874. /** @var StoreOrderStatusServices $statusService */
  1875. $statusService = app()->make(StoreOrderStatusServices::class);
  1876. $statusService->save([
  1877. 'oid' => $oid,
  1878. 'change_type' => 'check_order_over',
  1879. 'change_message' => '用户评价',
  1880. 'change_time' => time()
  1881. ]);
  1882. $order = $this->dao->get((int)$oid, ['id,pid,status']);
  1883. if ($order && $order['pid'] > 0) {
  1884. $p_order = $this->dao->get((int)$order['pid'], ['id,pid,status']);
  1885. //主订单全部收货 且子订单没有待评价 有已完成
  1886. if ($p_order['status'] == 2 && !$this->dao->count(['pid' => $order['pid'], 'status' => 3]) && $this->dao->count(['pid' => $order['pid'], 'status' => 4])) {
  1887. $this->dao->update($p_order['id'], ['status' => 3]);
  1888. $statusService->save([
  1889. 'oid' => $p_order['id'],
  1890. 'change_type' => 'check_order_over',
  1891. 'change_message' => '用户评价',
  1892. 'change_time' => time()
  1893. ]);
  1894. }
  1895. }
  1896. }
  1897. }
  1898. /**
  1899. * 某个用户订单
  1900. * @param int $uid
  1901. * @param UserServices $userServices
  1902. * @return array
  1903. * @throws \think\db\exception\DataNotFoundException
  1904. * @throws \think\db\exception\DbException
  1905. * @throws \think\db\exception\ModelNotFoundException
  1906. */
  1907. public function getUserOrderList(int $uid)
  1908. {
  1909. /** @var UserServices $userServices */
  1910. $userServices = app()->make(UserServices::class);
  1911. $user = $userServices->getUserInfo($uid, 'uid');
  1912. if (!$user) {
  1913. throw new AdminException(100026);
  1914. }
  1915. [$page, $limit] = $this->getPageValue();
  1916. $where = ['uid' => $uid, 'paid' => 1, 'refund_status' => 0, 'pid' => 0];
  1917. $list = $this->dao->getStairOrderList($where, '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', $page, $limit);
  1918. $count = $this->dao->count($where);
  1919. return compact('list', 'count');
  1920. }
  1921. /**
  1922. * 获取推广订单列表
  1923. * @param int $uid
  1924. * @param $where
  1925. * @return array
  1926. * @throws \think\db\exception\DataNotFoundException
  1927. * @throws \think\db\exception\DbException
  1928. * @throws \think\db\exception\ModelNotFoundException
  1929. */
  1930. public function getUserStairOrderList(int $uid, $where)
  1931. {
  1932. $where_data = [];
  1933. if (isset($where['type'])) {
  1934. switch ((int)$where['type']) {
  1935. case 1:
  1936. $where_data['spread_uid'] = $uid;
  1937. break;
  1938. case 2:
  1939. $where_data['spread_two_uid'] = $uid;
  1940. break;
  1941. default:
  1942. $where_data['spread_or_uid'] = $uid;
  1943. break;
  1944. }
  1945. }
  1946. if (isset($where['data']) && $where['data']) {
  1947. $where_data['time'] = $where['data'];
  1948. }
  1949. if (isset($where['order_id']) && $where['order_id']) {
  1950. $where_data['order_id'] = $where['order_id'];
  1951. }
  1952. //推广订单只显示支付过并且未退款的订单
  1953. $where_data['paid'] = 1;
  1954. $where_data['refund_status'] = 0;
  1955. [$page, $limit] = $this->getPageValue();
  1956. $list = $this->dao->getStairOrderList($where_data, '*', $page, $limit);
  1957. $count = $this->dao->count($where_data);
  1958. return compact('list', 'count');
  1959. }
  1960. /**
  1961. * 订单导出
  1962. * @param array $where
  1963. * @return array
  1964. * @throws \think\db\exception\DataNotFoundException
  1965. * @throws \think\db\exception\DbException
  1966. * @throws \think\db\exception\ModelNotFoundException
  1967. */
  1968. public function getExportList(array $where)
  1969. {
  1970. $list = $this->dao->search($where)->order('id desc')->select()->toArray();
  1971. foreach ($list as &$item) {
  1972. /** @var StoreOrderCartInfoServices $orderCart */
  1973. $orderCart = app()->make(StoreOrderCartInfoServices::class);
  1974. $_info = $orderCart->getCartColunm(['oid' => $item['id']], 'cart_info', 'unique');
  1975. foreach ($_info as $k => $v) {
  1976. $cart_info = is_string($v) ? json_decode($v, true) : $v;
  1977. if (!isset($cart_info['productInfo'])) $cart_info['productInfo'] = [];
  1978. $_info[$k] = $cart_info;
  1979. unset($cart_info);
  1980. }
  1981. $item['_info'] = $_info;
  1982. /** @var WechatUserServices $wechatUserService */
  1983. $wechatUserService = app()->make(WechatUserServices::class);
  1984. $item['sex'] = $wechatUserService->value(['uid' => $item['uid']], 'sex');
  1985. if ($item['pink_id'] || $item['combination_id']) {
  1986. /** @var StorePinkServices $pinkService */
  1987. $pinkService = app()->make(StorePinkServices::class);
  1988. $pinkStatus = $pinkService->value(['order_id_key' => $item['id']], 'status');
  1989. switch ($pinkStatus) {
  1990. case 1:
  1991. $item['pink_name'] = '[拼团订单]正在进行中';
  1992. $item['color'] = '#f00';
  1993. break;
  1994. case 2:
  1995. $item['pink_name'] = '[拼团订单]已完成';
  1996. $item['color'] = '#00f';
  1997. break;
  1998. case 3:
  1999. $item['pink_name'] = '[拼团订单]未完成';
  2000. $item['color'] = '#f0f';
  2001. break;
  2002. default:
  2003. $item['pink_name'] = '[拼团订单]历史订单';
  2004. $item['color'] = '#457856';
  2005. break;
  2006. }
  2007. } elseif ($item['seckill_id']) {
  2008. $item['pink_name'] = '[秒杀订单]';
  2009. $item['color'] = '#32c5e9';
  2010. } elseif ($item['bargain_id']) {
  2011. $item['pink_name'] = '[砍价订单]';
  2012. $item['color'] = '#12c5e9';
  2013. } else {
  2014. if ($item['shipping_type'] == 1) {
  2015. $item['pink_name'] = '[普通订单]';
  2016. $item['color'] = '#895612';
  2017. } else if ($item['shipping_type'] == 2) {
  2018. $item['pink_name'] = '[核销订单]';
  2019. $item['color'] = '#8956E8';
  2020. }
  2021. }
  2022. }
  2023. return $list;
  2024. }
  2025. /**
  2026. * 自动取消订单
  2027. * @return bool
  2028. * @throws \think\db\exception\DataNotFoundException
  2029. * @throws \think\db\exception\DbException
  2030. * @throws \think\db\exception\ModelNotFoundException
  2031. */
  2032. public function orderUnpaidCancel()
  2033. {
  2034. //系统预设取消订单时间段
  2035. $keyValue = ['order_cancel_time', 'order_activity_time', 'order_bargain_time', 'order_seckill_time', 'order_pink_time'];
  2036. //获取配置
  2037. $systemValue = SystemConfigService::more($keyValue);
  2038. //格式化数据
  2039. $systemValue = Arr::setValeTime($keyValue, is_array($systemValue) ? $systemValue : []);
  2040. $list = $this->dao->getOrderUnPaidList();
  2041. /** @var StoreOrderRefundServices $refundServices */
  2042. $refundServices = app()->make(StoreOrderRefundServices::class);
  2043. foreach ($list as $order) {
  2044. if ($order['pink_id'] || $order['combination_id']) {
  2045. $secs = $systemValue['order_pink_time'] ?: $systemValue['order_activity_time'];
  2046. } elseif ($order['seckill_id']) {
  2047. $secs = $systemValue['order_seckill_time'] ?: $systemValue['order_activity_time'];
  2048. } elseif ($order['bargain_id']) {
  2049. $secs = $systemValue['order_bargain_time'] ?: $systemValue['order_activity_time'];
  2050. } else {
  2051. $secs = $systemValue['order_cancel_time'];
  2052. }
  2053. if ($secs == 0) return true;
  2054. if (($order['add_time'] + bcmul($secs, '3600', 0)) < time()) {
  2055. try {
  2056. $this->transaction(function () use ($order, $refundServices) {
  2057. //回退积分和优惠卷
  2058. $res = $refundServices->integralAndCouponBack($order, 'cancel');
  2059. //回退库存和销量
  2060. $res = $res && $refundServices->regressionStock($order);
  2061. //修改订单状态
  2062. $res = $res && $this->dao->update($order['id'], ['is_del' => 1, 'mark' => '订单未支付已超过系统预设时间']);
  2063. if (!$res) {
  2064. Log::error('订单号' . $order['order_id'] . '自动取消订单失败');
  2065. }
  2066. return true;
  2067. });
  2068. /** @var StoreOrderCartInfoServices $cartServices */
  2069. $cartServices = app()->make(StoreOrderCartInfoServices::class);
  2070. $cartInfo = $cartServices->getOrderCartInfo((int)$order['id']);
  2071. } catch (\Throwable $e) {
  2072. Log::error('自动取消订单失败,失败原因:' . $e->getMessage(), $e->getTrace());
  2073. }
  2074. }
  2075. }
  2076. }
  2077. /**根据时间获取当天或昨天订单营业额
  2078. * @param array $where
  2079. * @return float|int
  2080. */
  2081. public function getOrderMoneyByWhere(array $where, string $sum_field, string $selectType, string $group = "")
  2082. {
  2083. switch ($selectType) {
  2084. case "sum" :
  2085. return $this->dao->getDayTotalMoney($where, $sum_field);
  2086. case "group" :
  2087. return $this->dao->getDayGroupMoney($where, $sum_field, $group);
  2088. }
  2089. }
  2090. /**统计时间段订单数
  2091. * @param array $where
  2092. * @param string $sum_field
  2093. */
  2094. public function getOrderCountByWhere(array $where)
  2095. {
  2096. return $this->dao->getDayOrderCount($where);
  2097. }
  2098. /**分组统计时间段订单数
  2099. * @param $where
  2100. * @return mixed
  2101. */
  2102. public function getOrderGroupCountByWhere($where)
  2103. {
  2104. return $this->dao->getOrderGroupCount($where);
  2105. }
  2106. /** 时间段支付订单人数
  2107. * @param $where
  2108. * @return mixed
  2109. */
  2110. public function getPayOrderPeopleByWhere($where)
  2111. {
  2112. return $this->dao->getPayOrderPeople($where);
  2113. }
  2114. /**时间段分组统计支付订单人数
  2115. * @param $where
  2116. * @return mixed
  2117. */
  2118. public function getPayOrderGroupPeopleByWhere($where)
  2119. {
  2120. return $this->dao->getPayOrderGroupPeople($where);
  2121. }
  2122. /**
  2123. * 退款订单列表
  2124. * @param array $where
  2125. * @return array
  2126. * @throws \think\db\exception\DataNotFoundException
  2127. * @throws \think\db\exception\DbException
  2128. * @throws \think\db\exception\ModelNotFoundException
  2129. */
  2130. public function refundList(array $where)
  2131. {
  2132. [$page, $limit] = $this->getPageValue();
  2133. if ($where['refund_reason_time'] != '') $where['refund_reason_time'] = explode('-', $where['refund_reason_time']);
  2134. $data = $this->dao->getRefundList($where, $page, $limit);
  2135. if ($data['list']) $data['list'] = $this->tidyOrderList($data['list']);
  2136. $data['num'] = [
  2137. 0 => ['name' => '全部', 'num' => $this->dao->count(['refund_type' => 0, 'is_system_del' => 0])],
  2138. 1 => ['name' => '仅退款', 'num' => $this->dao->count(['refund_type' => 1, 'is_system_del' => 0])],
  2139. 2 => ['name' => '退货退款', 'num' => $this->dao->count(['refund_type' => 2, 'is_system_del' => 0])],
  2140. 3 => ['name' => '拒绝退款', 'num' => $this->dao->count(['refund_type' => 3, 'is_system_del' => 0])],
  2141. 4 => ['name' => '商品待退货', 'num' => $this->dao->count(['refund_type' => 4, 'is_system_del' => 0])],
  2142. 5 => ['name' => '退货待收货', 'num' => $this->dao->count(['refund_type' => 5, 'is_system_del' => 0])],
  2143. 6 => ['name' => '已退款', 'num' => $this->dao->count(['refund_type' => 6, 'is_system_del' => 0])]
  2144. ];
  2145. return $data;
  2146. }
  2147. /**
  2148. * 商家同意退款,等待客户退货
  2149. * @param $order_id
  2150. * @return bool
  2151. */
  2152. public function agreeRefund($order_id)
  2153. {
  2154. $res = $this->dao->update(['id' => $order_id], ['refund_type' => 4]);
  2155. /** @var StoreOrderStatusServices $statusService */
  2156. $statusService = app()->make(StoreOrderStatusServices::class);
  2157. $statusService->save([
  2158. 'oid' => $order_id,
  2159. 'change_type' => 'refund_express',
  2160. 'change_message' => '等待用户退货',
  2161. 'change_time' => time()
  2162. ]);
  2163. if ($res) return true;
  2164. throw new AdminException(100005);
  2165. }
  2166. /**
  2167. * @param array $where
  2168. * @param array|string[] $field
  2169. * @param array $with
  2170. * @param int $page
  2171. * @param int $limit
  2172. * @param string $order
  2173. * @return array
  2174. * @throws \think\db\exception\DataNotFoundException
  2175. * @throws \think\db\exception\DbException
  2176. * @throws \think\db\exception\ModelNotFoundException
  2177. */
  2178. public function getSplitOrderList(array $where, array $field = ['*'], array $with = [], $page = 0, $limit = 0, $order = 'pay_time DESC,id DESC')
  2179. {
  2180. $data = $this->dao->getOrderList($where, $field, $page, $limit, $with, $order);
  2181. if ($data) {
  2182. $data = $this->tidyOrderList($data);
  2183. }
  2184. return $data;
  2185. }
  2186. /**
  2187. * 代付详情
  2188. * @param $orderId
  2189. * @param $uid
  2190. * @return array
  2191. * @throws \think\db\exception\DataNotFoundException
  2192. * @throws \think\db\exception\DbException
  2193. * @throws \think\db\exception\ModelNotFoundException
  2194. */
  2195. public function getFriendDetail($orderId, $uid)
  2196. {
  2197. $orderInfo = $this->dao->getOne(['order_id' => $orderId, 'is_del' => 0]);
  2198. if ($orderInfo) {
  2199. $orderInfo = $orderInfo->toArray();
  2200. } else {
  2201. throw new ApiException(410173);
  2202. }
  2203. $orderInfo = $this->tidyOrder($orderInfo, true);
  2204. /** @var UserServices $userServices */
  2205. $userServices = app()->make(UserServices::class);
  2206. $userInfo = $userServices->get($orderInfo['uid']);
  2207. $friendInfo = $userServices->get($orderInfo['pay_uid']);
  2208. $info = [
  2209. 'id' => $orderInfo['id'],
  2210. 'order_id' => $orderInfo['order_id'],
  2211. 'uid' => $orderInfo['uid'],
  2212. 'avatar' => $userInfo['avatar'],
  2213. 'nickname' => $userInfo['nickname'],
  2214. 'cartInfo' => $orderInfo['cartInfo'],
  2215. 'paid' => $orderInfo['paid'],
  2216. 'total_num' => $orderInfo['total_num'],
  2217. 'pay_price' => $orderInfo['pay_price'],
  2218. 'type' => $uid == $orderInfo['uid'] ? 0 : 1,
  2219. 'pay_uid' => isset($friendInfo) ? $friendInfo['uid'] : 0,
  2220. 'pay_nickname' => isset($friendInfo) ? $friendInfo['nickname'] : '',
  2221. 'pay_avatar' => isset($friendInfo) ? $friendInfo['avatar'] : '',
  2222. ];
  2223. return $info;
  2224. }
  2225. /**
  2226. * 获取退货商品列表
  2227. * @param array $cart_ids
  2228. * @param int $id
  2229. * @return array
  2230. * @throws \think\db\exception\DataNotFoundException
  2231. * @throws \think\db\exception\DbException
  2232. * @throws \think\db\exception\ModelNotFoundException
  2233. */
  2234. public function refundCartInfoList(array $cart_ids = [], int $id = 0)
  2235. {
  2236. $orderInfo = $this->dao->get($id);
  2237. if (!$orderInfo) {
  2238. throw new ApiException(410173);
  2239. }
  2240. $orderInfo = $this->tidyOrder($orderInfo, true);
  2241. $cartInfo = $orderInfo['cartInfo'] ?? [];
  2242. $data = [];
  2243. if ($cart_ids) {
  2244. foreach ($cart_ids as $cart) {
  2245. if (!isset($cart['cart_id']) || !$cart['cart_id'] || !isset($cart['cart_num']) || !$cart['cart_num'] || $cart['cart_num'] <= 0) {
  2246. throw new ApiException(410223);
  2247. }
  2248. }
  2249. $cart_ids = array_combine(array_column($cart_ids, 'cart_id'), $cart_ids);
  2250. $i = 0;
  2251. foreach ($cartInfo as $item) {
  2252. if (isset($cart_ids[$item['id']])) {
  2253. $data['cartInfo'][$i] = $item;
  2254. if (isset($cart_ids[$item['id']]['cart_num'])) $data['cartInfo'][$i]['cart_num'] = $cart_ids[$item['id']]['cart_num'];
  2255. $i++;
  2256. }
  2257. }
  2258. }
  2259. $data['_status'] = $orderInfo['_status'] ?? [];
  2260. $data['_status']['_is_back'] = $orderInfo['delivery_type'] != 'fictitious' && $orderInfo['virtual_type'] == 0;
  2261. $data['cartInfo'] = $data['cartInfo'] ?? $cartInfo;
  2262. return $data;
  2263. }
  2264. /**
  2265. * 再次下单
  2266. * @param string $uni
  2267. * @param int $uid
  2268. * @return array
  2269. */
  2270. public function againOrder(StoreCartServices $services, string $uni, int $uid): array
  2271. {
  2272. if (!$uni) throw new ApiException(100100);
  2273. $order = $this->getUserOrderDetail($uni, $uid);
  2274. if (!$order) throw new ApiException(410173);
  2275. $order = $this->tidyOrder($order, true);
  2276. $cateId = [];
  2277. foreach ($order['cartInfo'] as $v) {
  2278. if ($v['combination_id']) throw new ApiException(410258);
  2279. elseif ($v['bargain_id']) throw new ApiException(410259);
  2280. elseif ($v['seckill_id']) throw new ApiException(410260);
  2281. elseif ($v['advance_id']) throw new ApiException(410261);
  2282. else $cateId[] = $services->setCart($uid, (int)$v['product_id'], (int)$v['cart_num'], $v['productInfo']['attrInfo']['unique'] ?? '', '0', true);
  2283. }
  2284. if (!$cateId) throw new ApiException(410262);
  2285. return $cateId;
  2286. }
  2287. /**
  2288. * 支付宝单独支付
  2289. * @param OrderPayServices $payServices
  2290. * @param OtherOrderServices $services
  2291. * @param string $key
  2292. * @param string $quitUrl
  2293. * @return array|string
  2294. * @throws \think\db\exception\DataNotFoundException
  2295. * @throws \think\db\exception\DbException
  2296. * @throws \think\db\exception\ModelNotFoundException
  2297. */
  2298. public function aliPayOrder(OrderPayServices $payServices, OtherOrderServices $services, string $key, string $quitUrl)
  2299. {
  2300. if (!$key) {
  2301. throw new ApiException(100100);
  2302. }
  2303. if (!$quitUrl) {
  2304. throw new ApiException(100100);
  2305. }
  2306. $orderCache = CacheService::get($key);
  2307. if (!$orderCache || !isset($orderCache['order_id'])) {
  2308. throw new ApiException(410263);
  2309. }
  2310. $payType = isset($orderCache['other_pay_type']) && $orderCache['other_pay_type'] == true;
  2311. if ($payType) {
  2312. $orderInfo = $services->getOne(['order_id' => $orderCache['order_id'], 'is_del' => 0, 'paid' => 0]);
  2313. } else {
  2314. $orderInfo = $this->get(['order_id' => $orderCache['order_id'], 'paid' => 0, 'is_del' => 0]);
  2315. }
  2316. if (!$orderInfo) {
  2317. throw new ApiException(410264);
  2318. }
  2319. return $payServices->beforePay($orderInfo->toArray(), PayServices::ALIAPY_PAY, ['quitUrl' => $quitUrl]);
  2320. }
  2321. /**
  2322. * 用户订单信息
  2323. * @param StoreOrderEconomizeServices $services
  2324. * @param string $uni
  2325. * @param int $uid
  2326. * @return void
  2327. * @throws \think\db\exception\DataNotFoundException
  2328. * @throws \think\db\exception\DbException
  2329. * @throws \think\db\exception\ModelNotFoundException
  2330. */
  2331. public function getUserOrderByKey(StoreOrderEconomizeServices $services, string $uni, int $uid): array
  2332. {
  2333. $order = $this->getUserOrderDetail($uni, $uid, ['split', 'invoice']);
  2334. if (!$order) throw new ApiException(410294);
  2335. $order = $order->toArray();
  2336. $splitNum = [];
  2337. //是否开启门店自提
  2338. $store_self_mention = sys_config('store_self_mention');
  2339. //关闭门店自提后 订单隐藏门店信息
  2340. if ($store_self_mention == 0) $order['shipping_type'] = 1;
  2341. if ($order['verify_code']) {
  2342. $verify_code = $order['verify_code'];
  2343. $verify[] = substr($verify_code, 0, 4);
  2344. $verify[] = substr($verify_code, 4, 4);
  2345. $verify[] = substr($verify_code, 8);
  2346. $order['_verify_code'] = implode(' ', $verify);
  2347. }
  2348. $order['add_time_y'] = date('Y-m-d', $order['add_time']);
  2349. $order['add_time_h'] = date('H:i:s', $order['add_time']);
  2350. $order['system_store'] = false;
  2351. if ($order['store_id']) {
  2352. /** @var SystemStoreServices $storeServices */
  2353. $storeServices = app()->make(SystemStoreServices::class);
  2354. $order['system_store'] = $storeServices->getStoreDispose($order['store_id']);
  2355. }
  2356. if (($order['shipping_type'] === 2 || $order['delivery_uid'] != 0) && $order['verify_code']) {
  2357. $name = $order['verify_code'] . '.jpg';
  2358. /** @var SystemAttachmentServices $attachmentServices */
  2359. $attachmentServices = app()->make(SystemAttachmentServices::class);
  2360. $imageInfo = $attachmentServices->getInfo(['name' => $name]);
  2361. $siteUrl = sys_config('site_url');
  2362. if (!$imageInfo) {
  2363. $imageInfo = PosterServices::getQRCodePath($order['verify_code'], $name);
  2364. if (is_array($imageInfo)) {
  2365. $attachmentServices->attachmentAdd($imageInfo['name'], $imageInfo['size'], $imageInfo['type'], $imageInfo['dir'], $imageInfo['thumb_path'], 1, $imageInfo['image_type'], $imageInfo['time'], 2);
  2366. $url = $imageInfo['dir'];
  2367. } else
  2368. $url = '';
  2369. } else $url = $imageInfo['att_dir'];
  2370. if (isset($imageInfo['image_type']) && $imageInfo['image_type'] == 1) $url = $siteUrl . $url;
  2371. $order['code'] = $url;
  2372. }
  2373. $order['mapKey'] = sys_config('tengxun_map_key');
  2374. $order['yue_pay_status'] = (int)sys_config('balance_func_status') && (int)sys_config('yue_pay_status') == 1 ? (int)1 : (int)2;//余额支付 1 开启 2 关闭
  2375. $order['pay_weixin_open'] = sys_config('pay_weixin_open') != '0';//微信支付 1 开启 0 关闭
  2376. $order['ali_pay_status'] = sys_config('ali_pay_status', '0') != '0';//支付包支付 1 开启 0 关闭
  2377. $order['friend_pay_status'] = (int)sys_config('friend_pay_status') ?? 0;//好友代付 1 开启 0 关闭
  2378. $orderData = $this->tidyOrder($order, true, true);
  2379. $vipTruePrice = $memberPrice = $levelPrice = 0;
  2380. foreach ($orderData['cartInfo'] ?? [] as $key => $cart) {
  2381. $vipTruePrice = bcadd((string)$vipTruePrice, (string)$cart['vip_sum_truePrice'], 2);
  2382. if ($cart['price_type'] == 'member') $memberPrice = bcadd((string)$memberPrice, (string)$cart['vip_sum_truePrice'], 2);
  2383. if ($cart['price_type'] == 'level') $levelPrice = bcadd((string)$levelPrice, (string)$cart['vip_sum_truePrice'], 2);
  2384. if (isset($splitNum[$cart['id']])) {
  2385. $orderData['cartInfo'][$key]['cart_num'] = $cart['cart_num'] - $splitNum[$cart['id']];
  2386. if ($orderData['cartInfo'][$key]['cart_num'] == 0) unset($orderData['cartInfo'][$key]);
  2387. }
  2388. }
  2389. $orderData['cartInfo'] = array_merge($orderData['cartInfo']);
  2390. $orderData['vip_true_price'] = $vipTruePrice;
  2391. $orderData['levelPrice'] = $levelPrice;
  2392. $orderData['memberPrice'] = $memberPrice;
  2393. $economize = $services->get(['order_id' => $order['order_id']], ['postage_price', 'member_price']);
  2394. if ($economize) {
  2395. $orderData['postage_price'] = $economize['postage_price'];
  2396. $orderData['member_price'] = $economize['member_price'];
  2397. } else {
  2398. $orderData['postage_price'] = 0;
  2399. $orderData['member_price'] = 0;
  2400. }
  2401. $orderData['routine_contact_type'] = sys_config('routine_contact_type', 0);
  2402. /** @var UserInvoiceServices $userInvoice */
  2403. $userInvoice = app()->make(UserInvoiceServices::class);
  2404. $invoice_func = $userInvoice->invoiceFuncStatus();
  2405. $orderData['invoice_func'] = $invoice_func['invoice_func'];
  2406. $orderData['special_invoice'] = $invoice_func['special_invoice'];
  2407. $orderData['refund_cartInfo'] = $orderData['cartInfo'];
  2408. $orderData['refund_total_num'] = $orderData['total_num'];
  2409. $orderData['refund_pay_price'] = $orderData['pay_price'];
  2410. $orderData['is_apply_refund'] = true;
  2411. $orderData['help_info'] = [
  2412. 'pay_uid' => $orderData['pay_uid'],
  2413. 'pay_nickname' => '',
  2414. 'pay_avatar' => '',
  2415. 'help_status' => 0
  2416. ];
  2417. if ($orderData['uid'] != $orderData['pay_uid']) {
  2418. /** @var UserServices $userServices */
  2419. $userServices = app()->make(UserServices::class);
  2420. $payUser = $userServices->get($orderData['pay_uid'], ['nickname', 'avatar']);
  2421. $orderData['help_info'] = [
  2422. 'pay_uid' => $orderData['pay_uid'],
  2423. 'pay_nickname' => $payUser['nickname'],
  2424. 'pay_avatar' => $payUser['avatar'],
  2425. 'help_status' => 1
  2426. ];
  2427. }
  2428. // 判断是否开启小程序订单管理
  2429. $orderData['order_shipping_open'] = false;
  2430. if (sys_config('order_shipping_open', 0) && MiniOrderService::isManaged() && $order['is_channel'] == 1 && $order['pay_type'] == 'weixin') {
  2431. // 判断是否存在子未收货子订单
  2432. if ($order['pid'] > 0) {
  2433. if ($this->checkSubOrderNotTake((int)$order['pid'], (int)$order['id'])) {
  2434. $orderData['order_shipping_open'] = true;
  2435. }
  2436. } else {
  2437. $orderData['order_shipping_open'] = true;
  2438. }
  2439. }
  2440. return $orderData;
  2441. }
  2442. /**
  2443. * 获取确认订单页面是否展示快递配送和到店自提
  2444. * @param $uid
  2445. * @param $cartIds
  2446. * @param $new
  2447. * @return array
  2448. * @throws \Psr\SimpleCache\InvalidArgumentException
  2449. */
  2450. public function checkShipping($uid, $cartIds, $new)
  2451. {
  2452. if ($new) {
  2453. $cartIds = explode(',', $cartIds);
  2454. $cartInfo = [];
  2455. foreach ($cartIds as $key) {
  2456. $info = CacheService::get($key);
  2457. if ($info) {
  2458. $cartInfo[] = $info;
  2459. }
  2460. }
  2461. } else {
  2462. /** @var StoreCartServices $cartServices */
  2463. $cartServices = app()->make(StoreCartServices::class);
  2464. $cartInfo = $cartServices->getCartList(['uid' => $uid, 'status' => 1, 'id' => $cartIds], 0, 0, ['productInfo', 'attrInfo']);
  2465. }
  2466. if (!$cartInfo) {
  2467. throw new ApiException(100026);
  2468. }
  2469. $arr = [];
  2470. foreach ($cartInfo as $item) {
  2471. $arr[] = $item['productInfo']['logistics'];
  2472. }
  2473. $res = array_unique(explode(',', implode(',', $arr)));
  2474. if (count($res) == 2) {
  2475. return ['type' => 0];
  2476. } else {
  2477. if ($res[0] == 2 && sys_config('store_self_mention') == 0) {
  2478. return ['type' => 1];
  2479. }
  2480. return ['type' => (int)$res[0]];
  2481. }
  2482. }
  2483. /**
  2484. * 自动评价
  2485. * @return bool
  2486. */
  2487. public function autoComment()
  2488. {
  2489. //自动评价天数
  2490. $systemCommentTime = sys_config('system_comment_time', 0);
  2491. //0为取消自动默认好评功能
  2492. if ($systemCommentTime == 0) {
  2493. return true;
  2494. }
  2495. $sevenDay = bcsub((string)time(), bcmul((string)$systemCommentTime, '86400'));
  2496. /** @var StoreOrderStoreOrderStatusServices $service */
  2497. $service = app()->make(StoreOrderStoreOrderStatusServices::class);
  2498. $orderList = $service->getTakeOrderIds([
  2499. 'change_time' => $sevenDay,
  2500. 'is_del' => 0,
  2501. 'paid' => 1,
  2502. 'status' => 2,
  2503. 'change_type' => ['take_delivery', 'user_take_delivery']
  2504. ], 30);
  2505. foreach ($orderList as $item) {
  2506. AutoCommentJob::dispatch([$item['id'], $item['cart_id']]);
  2507. }
  2508. return true;
  2509. }
  2510. /**
  2511. * @param int $uid
  2512. * @param string $orderId
  2513. * @param string $type
  2514. * @return array
  2515. * @throws \think\db\exception\DataNotFoundException
  2516. * @throws \think\db\exception\DbException
  2517. * @throws \think\db\exception\ModelNotFoundException
  2518. * @author 等风来
  2519. * @email 136327134@qq.com
  2520. * @date 2023/2/13
  2521. */
  2522. public function getCashierInfo(int $uid, string $orderId, string $type)
  2523. {
  2524. //支付类型开关
  2525. $data = [
  2526. 'ali_pay_status' => sys_config('ali_pay_status', '0') != '0',
  2527. 'wechat_pay_status' => sys_config('pay_weixin_open', '0') != '0',
  2528. 'offline_pay_status' => (int)sys_config('offline_pay_status') == 1,
  2529. 'friend_pay_status' => (int)sys_config('friend_pay_status') == 1,
  2530. 'yue_pay_status' => (int)sys_config('balance_func_status') && (int)sys_config('yue_pay_status') == 1,
  2531. ];
  2532. $data['order_id'] = $orderId;
  2533. $data['pay_price'] = '0';
  2534. $data['now_money'] = app()->make(UserServices::class)->value(['uid' => $uid], 'now_money');
  2535. switch ($type) {
  2536. case 'order':
  2537. $info = $this->dao->get(['order_id' => $orderId], ['pay_price', 'add_time', 'combination_id', 'seckill_id', 'bargain_id', 'pay_postage']);
  2538. if (!$info) {
  2539. throw new PayException('您支付的订单不存在');
  2540. }
  2541. $orderCancelTime = sys_config('order_cancel_time', 0);
  2542. $orderActivityTime = sys_config('order_activity_time', 0);
  2543. if ($info->combination_id) {
  2544. $time = (sys_config('order_pink_time', 0) ?: $orderActivityTime) * 60 * 60 + ((int)$info->add_time);
  2545. } else if ($info->seckill_id) {
  2546. $time = (sys_config('order_seckill_time', 0) ?: $orderActivityTime) * 60 * 60 + ((int)$info->add_time);
  2547. } else if ($info->bargain_id) {
  2548. $time = (sys_config('order_bargain_time', 0) ?: $orderActivityTime) * 60 * 60 + ((int)$info->add_time);
  2549. } else {
  2550. $time = $orderCancelTime * 60 * 60 + ((int)$info->add_time);
  2551. }
  2552. if ($time < 0) {
  2553. $time = 0;
  2554. }
  2555. $data['pay_price'] = $info['pay_price'];
  2556. $data['pay_postage'] = $info['pay_postage'];
  2557. $data['offline_postage'] = (int)sys_config('offline_postage', 0);
  2558. $data['invalid_time'] = $time;
  2559. break;
  2560. case 'svip':
  2561. $info = app()->make(OtherOrderServices::class)->get(['order_id' => $orderId], ['pay_price', 'add_time']);
  2562. if (!$info) {
  2563. throw new PayException('您支付的订单不存在');
  2564. }
  2565. $data['pay_price'] = $info['pay_price'];
  2566. $data['invalid_time'] = $info->add_time + 86400;
  2567. break;
  2568. case 'recharge':
  2569. $info = app()->make(UserRechargeServices::class)->get(['order_id' => $orderId], ['price', 'add_time']);
  2570. if (!$info) {
  2571. throw new PayException('您支付的订单不存在');
  2572. }
  2573. $data['pay_price'] = $info['price'];
  2574. $data['invalid_time'] = $info->add_time + 86400;
  2575. break;
  2576. default:
  2577. throw new PayException('暂不支持其他类型订单支付');
  2578. }
  2579. return $data;
  2580. }
  2581. /**
  2582. * 取消商家寄件
  2583. * @param int $id
  2584. * @param string $msg
  2585. * @return array|mixed
  2586. * @throws \think\db\exception\DataNotFoundException
  2587. * @throws \think\db\exception\DbException
  2588. * @throws \think\db\exception\ModelNotFoundException
  2589. * @author 等风来
  2590. * @email 136327134@qq.com
  2591. * @date 2023/5/15
  2592. */
  2593. public function shipmentCancelOrder(int $id, string $msg)
  2594. {
  2595. $orderInfo = $this->dao->get($id);
  2596. if (!$orderInfo) {
  2597. throw new ValidateException('取消的订单不存在');
  2598. }
  2599. if (!$orderInfo->kuaidi_task_id || !$orderInfo->kuaidi_order_id) {
  2600. throw new ValidateException('商家寄件订单信息不存在,无法取消');
  2601. }
  2602. if ($orderInfo->status != 1) {
  2603. throw new ValidateException('订单状态不正确,无法取消寄件');
  2604. }
  2605. //发起取消商家寄件
  2606. $res = app()->make(ServeServices::class)->express()->shipmentCancelOrder([
  2607. 'task_id' => $orderInfo->kuaidi_task_id,
  2608. 'order_id' => $orderInfo->kuaidi_order_id,
  2609. 'cancel_msg' => $msg,
  2610. ]);
  2611. if ($res['status'] != 200) {
  2612. throw new ValidateException($res['msg'] ?? '一号通:取消失败');
  2613. }
  2614. //订单返回原状态
  2615. $this->transaction(function () use ($id, $msg, $orderInfo) {
  2616. app()->make(StoreOrderStatusServices::class)->save([
  2617. 'oid' => $id,
  2618. 'change_time' => time(),
  2619. 'change_type' => 'delivery_goods_cancel',
  2620. 'change_message' => '已取消发货,取消原因:' . $msg
  2621. ]);
  2622. $orderInfo->status = 0;
  2623. $orderInfo->save();
  2624. });
  2625. return $res;
  2626. }
  2627. public function checkSubOrderNotSend(int $pid, int $order_id)
  2628. {
  2629. $order_count = $this->dao->getSubOrderNotSend($pid, $order_id);
  2630. if ($order_count > 0) {
  2631. return false;
  2632. } else {
  2633. return true;
  2634. }
  2635. }
  2636. public function checkSubOrderNotTake(int $pid, int $order_id)
  2637. {
  2638. $order_count = $this->dao->getSubOrderNotTake($pid, $order_id);
  2639. if ($order_count > 0) {
  2640. return false;
  2641. } else {
  2642. return true;
  2643. }
  2644. }
  2645. }