StoreOrderServices.php 123 KB

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