StoreOrderServices.php 112 KB

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