StoreOrderServices.php 122 KB

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