StoreOrderServices.php 130 KB

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