StoreOrderServices.php 122 KB

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