MCart.Class.php 151 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517
  1. <?php
  2. /**
  3. * 购物车
  4. * Created by PhpStorm.
  5. * User: XiaoMing
  6. * Date: 2019/11/6
  7. * Time: 16:20
  8. */
  9. namespace JinDouYun\Model\Cart;
  10. use JinDouYun\Dao\Cashier\DCashierCustomerPrice;
  11. use JinDouYun\Dao\System\DDeliveryRule;
  12. use JinDouYun\Model\Cashier\MCashierSettings;
  13. use JinDouYun\Model\Market\MComBinPackage;
  14. use Mall\Framework\Core\ErrorCode;
  15. use Mall\Framework\Core\ResultWrapper;
  16. use Mall\Framework\Core\StatusCode;
  17. use JinDouYun\Dao\Cart\DCart;
  18. use JinDouYun\Cache\ActivityLimitCache;
  19. use JinDouYun\Controller\Common\Logger;
  20. use JinDouYun\Model\Market\MVipCard;
  21. use JinDouYun\Model\System\MBasicSetup;
  22. use JinDouYun\Model\Market\MActivity;
  23. use JinDouYun\Model\Market\MUserCoupon;
  24. use JinDouYun\Model\System\MEnterpriseBindPayment;
  25. use JinDouYun\Model\Customer\MCustomer;
  26. use JinDouYun\Model\Customer\MShippingAddress;
  27. use JinDouYun\Model\GoodsManage\MSku;
  28. use JinDouYun\Model\Price\MPrice;
  29. use JinDouYun\Model\Stock\MInventory;
  30. use JinDouYun\Model\SysAreaChina\MSysAreaChina;
  31. use JinDouYun\Model\System\MDeliverySetting;
  32. use JinDouYun\Model\Goods\MGoods;
  33. use JinDouYun\Model\Shop\MShop;
  34. use JinDouYun\Cache\CustomerCache;
  35. use JinDouYun\Cache\GoodsBasicRelevant;
  36. use JinDouYun\Dao\Customer\DCustomer;
  37. use JinDouYun\Model\Cart\MPreferentialProcess;
  38. class MCart
  39. {
  40. /**
  41. * @var DCart
  42. */
  43. private $objDCart;
  44. /**
  45. * @var int 用户id
  46. */
  47. private $onlineUserId;
  48. /**
  49. * @var int 企业id
  50. */
  51. private $onlineEnterpriseId;
  52. /**
  53. * @var MCustomer
  54. */
  55. private $objMCustomer;
  56. /**
  57. * @var ActivityLimitCache
  58. */
  59. private $objActivityLimitCache;
  60. /**
  61. * @var bool
  62. */
  63. private $isFront;//是否是前台调用此Model 前台=>true
  64. /**
  65. * @var int 客户id
  66. */
  67. private $customerId;//用户对应的客户id
  68. /**
  69. * @var MActivity
  70. */
  71. private $objMActivity;
  72. /**
  73. * @var array 失效商品集合
  74. */
  75. private $invalidData = [];
  76. /**
  77. * @var array 操作类型
  78. */
  79. private static $type = [
  80. 'single' => 1,//单个商品
  81. 'shop' => 2,//按店铺
  82. 'all' => 3,//全选
  83. ];
  84. /**
  85. * @var array 阶梯价
  86. */
  87. private static $enabledLadder = [
  88. 'open' => 1,//启用阶梯价
  89. 'close' => 0,//未启用阶梯价
  90. ];
  91. /**
  92. * @var int[] 立即购买来源业务
  93. */
  94. public static $sourceType = [
  95. 'comBin' => 1,//组合套餐
  96. 'goods' => 2, // 普通商品
  97. ];
  98. /**
  99. * @var MSku
  100. */
  101. private $objMSku;
  102. /**
  103. * @var MGoods
  104. */
  105. private $objMGoods;
  106. /**
  107. * @var MBasicSetup
  108. */
  109. private $objMBasicSetup;
  110. /**
  111. * @var int
  112. */
  113. private $onlineUserDefaultDeliveryType = null;
  114. /**
  115. * @var float
  116. */
  117. private $onlineUserAddressCode = null;
  118. /**
  119. * @var float
  120. */
  121. private $freeExpressPrice = null;
  122. /**
  123. * @var string
  124. */
  125. private $cashierUId;
  126. /**
  127. * @var DCustomer
  128. */
  129. private $objDCustomer;
  130. /**
  131. * @var MCashierSettings
  132. */
  133. private $objMCashierSettings;
  134. /**
  135. * @var DCashierCustomerPrice
  136. */
  137. private $objDCashierCustomerPrice;
  138. /**
  139. * @var boolean
  140. */
  141. private $isCashier;
  142. /**
  143. * @var float 运费金额
  144. */
  145. private $expressMoney = 0;
  146. /**
  147. * @var array 店铺运费
  148. */
  149. private $expressShopMoney = [];
  150. /**
  151. * @var array
  152. */
  153. private $selectStateMap = [];
  154. /**
  155. * @var bool
  156. */
  157. private $preSale;
  158. /**
  159. * MCart constructor.
  160. * @param $onlineUserId
  161. * @param $onlineEnterpriseId
  162. * @param bool $isFront
  163. * @param string $areaCode
  164. * @param string $cashierUId
  165. * @param boolean $isCashier
  166. * @throws \Exception
  167. */
  168. public function __construct($onlineUserId, $onlineEnterpriseId, $isFront = false, $areaCode = '', $cashierUId = '', $isCashier = false)
  169. {
  170. $this->isCashier = $isCashier;
  171. $this->cashierUId = $cashierUId;
  172. $this->isFront = $isFront;
  173. $this->onlineUserId = $onlineUserId;
  174. $this->onlineEnterpriseId = $onlineEnterpriseId;
  175. $this->objDCart = new DCart('default');
  176. $this->objDCart->setTable($this->objDCart->get_Table() . '_' . $onlineEnterpriseId);
  177. $this->objMCustomer = new MCustomer($this->onlineEnterpriseId, $this->onlineUserId);
  178. $this->objActivityLimitCache = new ActivityLimitCache($this->onlineEnterpriseId);
  179. $this->objMActivity = new MActivity($this->onlineUserId, $this->onlineEnterpriseId, $areaCode);
  180. $this->objMSku = new MSku($this->onlineUserId, $this->onlineEnterpriseId);
  181. $this->objMGoods = new MGoods($this->onlineEnterpriseId, $this->isFront, $this->onlineUserId);
  182. $this->objMBasicSetup = new MBasicSetup($this->onlineEnterpriseId);
  183. $this->objDCustomer = new DCustomer();
  184. $this->objDCustomer->setTable('qianniao_customer_' . $this->onlineEnterpriseId);
  185. $this->objMCashierSettings = new MCashierSettings($this->onlineUserId, $this->onlineEnterpriseId);
  186. $this->objDCashierCustomerPrice = new DCashierCustomerPrice();
  187. $this->objDCashierCustomerPrice->setTable('qianniao_cashier_customer_price_' . $this->onlineEnterpriseId);
  188. self::getCustomerInfo();
  189. }
  190. /**
  191. * Doc: (des="")
  192. * User: XMing
  193. * Date: 2020/12/25
  194. * Time: 11:50 上午
  195. * @return ResultWrapper
  196. */
  197. public function getPreSale(): ResultWrapper
  198. {
  199. $objMBasicSetup = new MBasicSetup($this->onlineEnterpriseId);
  200. $setResult = $objMBasicSetup->getBasicField('preSale');
  201. $this->preSale = StatusCode::$delete;
  202. if (!$setResult->isSuccess()) {
  203. return ResultWrapper::fail($setResult->getData(), $setResult->getErrorCode());
  204. }
  205. $set = (array)$setResult->getData();
  206. $this->preSale = getArrayItem($set, 'preSale', StatusCode::$delete);
  207. if (empty($this->preSale)) $this->preSale = StatusCode::$delete;
  208. return ResultWrapper::success($set);
  209. }
  210. /**
  211. * 获取客户id
  212. */
  213. private function getCustomerInfo()
  214. {
  215. if ($this->isFront === false) {
  216. $this->customerId = $this->onlineUserId;//TODO()
  217. } else {
  218. $this->customerId = $this->objMCustomer->getCustomerIdByUserCenterId($this->onlineUserId);
  219. }
  220. }
  221. /**
  222. * 根据限购数量对数据进行分组
  223. * @param $data
  224. * @return array
  225. */
  226. private function checkLimitGroup($data)
  227. {
  228. if (empty($data)) return $data;
  229. $allMapping = [];
  230. foreach ($data as &$goods) {
  231. if ($goods['isActivityPrice'] == StatusCode::$standard) {
  232. $userLimit = $this->objActivityLimitCache->getLimit($goods['activityId'], $goods['goodsId'], $goods['skuId'], $this->onlineUserId);//用户限购数量
  233. $allowNum = $goods['limitNum'] - $userLimit;//还可以购买的数量
  234. $buyNum = $goods['buyNum'];//购买数量
  235. if ($buyNum >= $allowNum) {
  236. //拆分
  237. $goods['buyNum'] = $allowNum;
  238. $allMapping[] = $goods;
  239. $newGoods = $goods;
  240. if ($buyNum - $allowNum > 0) {
  241. $newGoods['buyNum'] = $buyNum - $allowNum;
  242. $newGoods['activityId'] = 0;//将活动id重置为0
  243. $newGoods['isActivityPrice'] = StatusCode::$delete;
  244. $allMapping[] = $newGoods;
  245. }
  246. } else {
  247. //限购等于加入数量
  248. $allMapping[] = $goods;
  249. }
  250. } else {
  251. $allMapping[] = $goods;
  252. }
  253. }
  254. return $allMapping;
  255. }
  256. /**
  257. * Doc: (des="小程序端加入购物车,最新")
  258. * User: XMing
  259. * Date: 2020/8/4
  260. * Time: 2:35 下午
  261. * @param array $params
  262. * @return ResultWrapper
  263. * @throws \Exception
  264. */
  265. public function addCartApi(array $params)
  266. {
  267. if (empty( $params['goodsData'] )) {
  268. return ResultWrapper::fail('加入购物车数据为空', ErrorCode::$paramError);
  269. }
  270. foreach ( $params['goodsData'] as $key => $data ){
  271. // 从商品表查询商品信息
  272. $goodsInfoResult = $this->objMGoods->getGoodsInfo($data['goodsId']);
  273. if (!$goodsInfoResult->isSuccess()) {
  274. return ResultWrapper::fail($goodsInfoResult->getData(), $goodsInfoResult->getErrorCode());
  275. }
  276. $goodsInfo = $goodsInfoResult->getData();
  277. // 验证此商品是否有效
  278. $checkResult = self::checkGoods($goodsInfo, $data, 'add');
  279. unset($data);
  280. if (!$checkResult->isSuccess()) {
  281. return ResultWrapper::fail($checkResult->getData(), $checkResult->getErrorCode());
  282. }
  283. $data = $checkResult->getData();
  284. // 判断此商品是否是活动商品
  285. $addMap = [
  286. 'goodsBasicId' => $data['goodsBasicId'],
  287. 'goodsId' => $data['goodsId'],
  288. 'skuId' => $data['skuId'],
  289. 'buyNum' => $data['buyNum'],
  290. 'shopId' => $data['shopId'],
  291. 'source' => $data['source'],
  292. 'setNum' => $data['setNum'],
  293. 'activityId' => $data['activityId'],
  294. 'goodsCode' => $data['goodsCode'],
  295. 'warehouseId' => $data['warehouseId']
  296. ];
  297. // 将加入购物车的数据进行分组[old=>在购物车中已存在,now=>新加入购物车商品]
  298. $groupResult = self::existCartAndGroup([$addMap]);
  299. if (!$groupResult->isSuccess()) {
  300. return ResultWrapper::fail($groupResult->getData(), $groupResult->getErrorCode());
  301. }
  302. $groupData = $groupResult->getData();
  303. if (empty($groupData)) {
  304. return ResultWrapper::fail('数组分组失败', ErrorCode::$paramError);
  305. }
  306. // 在购物车中已存在的商品
  307. $updates = isset($groupData['old']) ? $groupData['old'] : [];
  308. // 新加入购物车商品
  309. $inserts = isset($groupData['now']) ? $groupData['now'] : [];
  310. // 操作数据库
  311. $this->objDCart->beginTransaction();
  312. // 需要更新的数据
  313. if (!empty($updates)) {
  314. $update = array_shift($updates);
  315. $tableName = 'qianniao_cart_' . $this->onlineEnterpriseId;
  316. $sql = 'UPDATE ' . $tableName . ' SET buyNum = buyNum+' . $update['buyNum'] . '
  317. WHERE goodsId = ' . $update['goodsId'] . ' AND
  318. skuId = ' . $update['skuId'] . ' AND
  319. userCenterId = ' . $this->onlineUserId . ' AND
  320. activityId = ' . $update['activityId'];
  321. $updateRes = $this->objDCart->query($sql);
  322. if ($updateRes === false) {
  323. $this->objDCart->rollBack();
  324. return ResultWrapper::fail($this->objDCart->error(), ErrorCode::$dberror);
  325. }
  326. }
  327. // 需要插入的数据
  328. if (!empty($inserts)) {
  329. $insert = array_shift($inserts);
  330. $insertMap = [
  331. 'userCenterId' => $this->onlineUserId,
  332. 'selection' => StatusCode::$standard,
  333. 'goodsBasicId' => $insert['goodsBasicId'],
  334. 'goodsId' => $insert['goodsId'],
  335. 'skuId' => $insert['skuId'],
  336. 'buyNum' => $insert['buyNum'],
  337. 'shopId' => $insert['shopId'],
  338. 'source' => $insert['source'],
  339. 'activityId' => $insert['activityId'],
  340. 'goodsCode' => $insert['goodsCode'],
  341. 'warehouseId' => $insert['warehouseId']
  342. ];//要插入的数据
  343. $insertRes = $this->objDCart->insert($insertMap);
  344. if ($insertRes === false) {
  345. $this->objDCart->rollBack();
  346. return ResultWrapper::fail($this->objDCart->error(), ErrorCode::$dberror);
  347. }
  348. }
  349. }
  350. $this->objDCart->commit();
  351. return ResultWrapper::success('加入成功');
  352. }
  353. /**
  354. * Doc: (des="检测商品")
  355. * User: XMing
  356. * Date: 2020/8/4
  357. * Time: 3:21 下午
  358. * @param array $goodsData
  359. * @param array $addCart
  360. * @param string $action
  361. * @return ResultWrapper
  362. */
  363. private function checkGoods(array $goodsData, array $addCart, $action = 'add')
  364. {
  365. if (empty($goodsData)) {
  366. return ResultWrapper::fail('商品信息为空', ErrorCode::$paramError);
  367. }
  368. //验证商品的状态以及库存
  369. if ($goodsData['enableStatus'] == StatusCode::$delete || $goodsData['deleteStatus'] == StatusCode::$delete) {
  370. return ResultWrapper::fail('商品已下架', ErrorCode::$paramError);
  371. }
  372. $specMultiple = $goodsData['specMultiple'];//spec数据
  373. if (empty($specMultiple)) {
  374. return ResultWrapper::fail('商品规格信息获取失败', ErrorCode::$paramError);
  375. }
  376. $specMultipleMap = [];//商品规格映射数据
  377. foreach ($specMultiple as $value) {
  378. $specMultipleMap[$value['id']] = $value;
  379. }
  380. if (!isset($specMultipleMap[$addCart['skuId']])) {
  381. return ResultWrapper::fail('规格信息不存在', ErrorCode::$paramError);
  382. }
  383. $subBuyNumResult = self::getCartBuyNumBySku($addCart['skuId'], $this->onlineUserId);
  384. if (!$subBuyNumResult->isSuccess()) {
  385. return ResultWrapper::fail($subBuyNumResult->getData(), $subBuyNumResult->getErrorCode());
  386. }
  387. $subBuyNum = floatval($subBuyNumResult->getData());
  388. //是否开启预售
  389. switch ($action){
  390. case 'add':
  391. if ($goodsData['isDistribution'] == StatusCode::$delete){
  392. if ($specMultipleMap[$addCart['skuId']]['inventory'] < $addCart['buyNum'] + $subBuyNum) {
  393. //取库存最大数
  394. $allowNum = bcsub($specMultipleMap[$addCart['skuId']]['inventory'], $subBuyNum);//最多还可以增加的数量
  395. if ($allowNum <= 0) {
  396. $addCart['buyNum'] = 0;
  397. } else {
  398. $addCart['buyNum'] = $allowNum;
  399. }
  400. //return ResultWrapper::fail('商品库存不足', ErrorCode::$paramError);
  401. }
  402. }
  403. break;
  404. case 'update':
  405. if ($goodsData['isDistribution'] == StatusCode::$delete){
  406. if ($specMultipleMap[$addCart['skuId']]['inventory'] < $addCart['buyNum']) {
  407. return ResultWrapper::success($specMultipleMap[$addCart['skuId']]['inventory']);
  408. } else {
  409. return ResultWrapper::success($addCart['buyNum']);
  410. }
  411. }else{
  412. return ResultWrapper::success($addCart['buyNum']);
  413. }
  414. break;
  415. }
  416. $addCart['isActivity'] = $specMultipleMap[$addCart['skuId']]['isActivity'];//是否是活动商品
  417. $addCart['setNum'] = $specMultipleMap[$addCart['skuId']]['setNum'];//起订数量
  418. $addCart['warehouseId'] =getArrayItem($goodsData,'warehouseId','');
  419. switch ($specMultipleMap[$addCart['skuId']]['isActivity']) {
  420. case StatusCode::$standard:
  421. $addCart['activityId'] = $specMultipleMap[$addCart['skuId']]['activity']['activityId'];
  422. $addCart['limitNum'] = $specMultipleMap[$addCart['skuId']]['activity']['limitNum'];//商品活动限购数量
  423. break;
  424. case StatusCode::$delete:
  425. $addCart['activityId'] = 0;
  426. break;
  427. }
  428. return ResultWrapper::success($addCart);
  429. }
  430. /**
  431. * app加入购物车
  432. * @param $params
  433. * @return ResultWrapper
  434. * @throws \Exception
  435. */
  436. public function addCart($params)
  437. {
  438. $this->objDCart->beginTransaction();
  439. $goodsData = $params['goodsData'];
  440. $dbResult = self::checkCart($goodsData);
  441. if (!$dbResult->isSuccess()) {
  442. return ResultWrapper::fail($dbResult->getData(), $dbResult->getErrorCode());
  443. }
  444. $checkData = $dbResult->getData();
  445. //前台验证活动商品剩余数量,和用户限购数量
  446. if ($this->isFront === true) {
  447. //将数据拆分
  448. $checkData = self::checkLimitGroup($checkData);
  449. $dbResult = self::checkLimit($checkData);
  450. if (!$dbResult->isSuccess()) {
  451. return ResultWrapper::fail($dbResult->getData(), $dbResult->getErrorCode());
  452. }
  453. $dbResult = $dbResult->getData();
  454. $mapping = $dbResult['mapping'];
  455. $checkData = $dbResult['checkData'];
  456. }
  457. unset($dbResult);
  458. $dbResult = self::existCartAndGroup($checkData);
  459. if (!$dbResult->isSuccess()) {
  460. return ResultWrapper::fail($dbResult->getData(), $dbResult->getErrorCode());
  461. }
  462. $cartData = $dbResult->getData();//分组后的数据
  463. unset($dbResult);
  464. $dbResult = true;//初始化
  465. $oldCart = $cartData['old'];//旧的数据
  466. $nowCart = $cartData['now'];//新的数据
  467. if (!empty($oldCart)) {
  468. foreach ($oldCart as $key => $val) {
  469. $sql = "UPDATE qianniao_cart_{$this->onlineEnterpriseId} SET buyNum=buyNum+{$val['buyNum']} WHERE goodsId={$val['goodsId']} AND skuId={$val['skuId']} AND userCenterId={$this->onlineUserId} AND activityId={$val['activityId']}";
  470. $dbResult = $this->objDCart->query($sql);
  471. }
  472. if ($dbResult === false) {
  473. $this->objDCart->rollBack();
  474. return ResultWrapper::fail($this->objDCart->error(), ErrorCode::$dberror);
  475. }
  476. }
  477. if (!empty($nowCart)) {
  478. $insert = [];
  479. foreach ($nowCart as $key => $val) {
  480. $insert[] = [
  481. 'userCenterId' => $this->onlineUserId,
  482. 'selection' => StatusCode::$standard,
  483. 'skuId' => $val['skuId'],
  484. 'goodsCode' => $val['goodsCode'],
  485. 'goodsId' => $val['goodsId'],
  486. 'shopId' => $val['shopId'],
  487. 'buyNum' => $val['buyNum'],
  488. 'source' => $val['source'],
  489. 'goodsBasicId' => $val['goodsBasicId'],
  490. 'warehouseId' => $val['warehouseId'],
  491. 'activityId' => isset($val['activityId']) ? $val['activityId'] : 0,//活动id
  492. ];
  493. }
  494. $dbResult = $this->objDCart->insert($insert, true);
  495. if ($dbResult === false) {
  496. $this->objDCart->rollBack();
  497. return ResultWrapper::fail($this->objDCart->error(), ErrorCode::$dberror);
  498. }
  499. }
  500. $objCustomerCache = new CustomerCache();
  501. $objCustomerCache->incrInterestCustomer($this->customerId, $this->onlineEnterpriseId);
  502. //用户限购缓存
  503. if (!empty($mapping)) {
  504. self::userLimit($mapping);
  505. }
  506. $this->objDCart->commit();
  507. return ResultWrapper::success($dbResult);
  508. }
  509. /**
  510. * 后台加入购物车
  511. * @param $params
  512. * @return ResultWrapper
  513. * @throws \Exception
  514. */
  515. public function manageAddCart($params)
  516. {
  517. $this->objDCart->beginTransaction();
  518. $goodsData = $params['goodsData'];
  519. $dbResult = self::checkCart($goodsData);
  520. if (!$dbResult->isSuccess()) {
  521. return ResultWrapper::fail($dbResult->getData(), $dbResult->getErrorCode());
  522. }
  523. $checkData = $dbResult->getData();//验证后数据
  524. $dbResult = $this->existCartAndGroup($checkData);//数据分组
  525. if (!$dbResult->isSuccess()) {
  526. return ResultWrapper::fail($dbResult->getData(), $dbResult->getErrorCode());
  527. }
  528. $cartData = $dbResult->getData();//分组后的数据
  529. unset($dbResult);
  530. $dbResult = true;//初始化
  531. $update = $cartData['old'];//旧的数据
  532. $insert = $cartData['now'];//新的数据
  533. //更新数量
  534. if (!empty($update)) {
  535. foreach ($update as $row) {
  536. $dbResult = $this->objDCart->set_inc(
  537. 'buyNum',
  538. [
  539. 'goodsId' => $row['goodsId'],
  540. 'skuId' => $row['skuId'],
  541. 'userCenterId' => $this->onlineUserId,
  542. 'activityId' => $row['activityId'],
  543. ],
  544. $row['buyNum']
  545. );
  546. if ($dbResult === false) {
  547. $this->objDCart->rollBack();
  548. return ResultWrapper::fail($this->objDCart->error(), ErrorCode::$dberror);
  549. }
  550. }
  551. }
  552. //insert
  553. if (!empty($insert)) {
  554. $add = [];
  555. foreach ($insert as $row) {
  556. $add[] = [
  557. 'userCenterId' => $this->onlineUserId,
  558. 'selection' => StatusCode::$standard,
  559. 'skuId' => $row['skuId'],
  560. 'goodsCode' => $row['goodsCode'],
  561. 'goodsId' => $row['goodsId'],
  562. 'shopId' => $row['shopId'],
  563. 'buyNum' => $row['buyNum'],
  564. 'source' => $row['source'],
  565. 'goodsBasicId' => $row['goodsBasicId'],
  566. 'warehouseId' => $row['warehouseId'],
  567. 'activityId' => isset($val['activityId']) ? $row['activityId'] : 0,//活动id
  568. ];
  569. }
  570. $dbResult = $this->objDCart->insert($add, true);
  571. if ($dbResult === false) {
  572. $this->objDCart->rollBack();
  573. return ResultWrapper::fail($this->objDCart->error(), ErrorCode::$dberror);
  574. }
  575. }
  576. $objCustomerCache = new CustomerCache();
  577. $objCustomerCache->incrInterestCustomer($this->customerId, $this->onlineEnterpriseId);
  578. $this->objDCart->commit();
  579. return ResultWrapper::success($dbResult);
  580. }
  581. /**
  582. * adj 1:+ 2:-
  583. * isUpdate true false
  584. * step 调整量
  585. * @param $checkData
  586. * @param int $adj
  587. * @param bool $isUpdate
  588. * @return ResultWrapper
  589. */
  590. private function checkLimit($checkData, $adj = 1, $isUpdate = false, $step = null)
  591. {
  592. if (empty($checkData)) return ResultWrapper::success($checkData);
  593. $mapping = [];
  594. foreach ($checkData as $key => $goods) {
  595. $skuData = $goods['specMultiple'];
  596. $skuMapping = [];
  597. foreach ($skuData as $sku) {
  598. $skuMapping[$sku['id']] = $sku;
  599. }
  600. $thisSku = $skuMapping[$goods['skuId']];//当前加入购物车规格sku详情
  601. if (
  602. $goods['isActivityPrice'] == StatusCode::$delete ||
  603. ($thisSku['isActivity'] == StatusCode::$delete && $thisSku['activityId'] == 0)) {
  604. //活动商品,已购买完,或已限购,将以原价购买
  605. continue;
  606. }
  607. $activityId = $goods['activityId'];//活动id
  608. $activityDetails = $this->objActivityLimitCache->getActivity($activityId);
  609. if (empty($activityDetails)) {
  610. return ResultWrapper::fail('活动不存在', ErrorCode::$paramError);
  611. }
  612. if ($activityDetails['startTime'] > time()) {
  613. return ResultWrapper::fail($goods['title'] . '活动尚未开始', ErrorCode::$paramError);
  614. }
  615. if ($activityDetails['endTime'] < time()) {
  616. return ResultWrapper::fail($goods['title'] . '活动已结束', ErrorCode::$paramError);
  617. }
  618. //获取活动商品剩余数量
  619. $surplusNum = $this->objActivityLimitCache->getLen($activityId, $goods['goodsId'], $goods['skuId']);
  620. if ($surplusNum == 0) {
  621. return ResultWrapper::fail($goods['title'] . '抢光了', ErrorCode::$paramError);
  622. }
  623. if ($surplusNum < $goods['buyNum']) {
  624. return ResultWrapper::fail($goods['title'] . '活动剩余数量不足', ErrorCode::$paramError);
  625. }
  626. //获取每人限购数量
  627. $userLimit = self::getLimitNum($activityDetails, $goods['goodsId'], $goods['skuId']);//用户限购数量
  628. $userSurplusNum = $this->objActivityLimitCache->getLimit($activityId, $goods['goodsId'], $goods['skuId'], $this->onlineUserId);
  629. $buyNum = 0;//初始化
  630. if ($isUpdate) {
  631. if ($adj == 1) {
  632. //加操作
  633. if ($userLimit < ($userSurplusNum + $step)) {
  634. return ResultWrapper::fail($goods['title'] . '每人限购' . $userLimit . $thisSku['unitName'], ErrorCode::$paramError);
  635. }
  636. }
  637. $buyNum = $userSurplusNum + $step;
  638. } else {
  639. //addCart
  640. if ($userLimit < ($userSurplusNum + $goods['buyNum'])) {
  641. return ResultWrapper::fail($goods['title'] . '每人限购' . $userLimit . $thisSku['unitName'], ErrorCode::$paramError);
  642. }
  643. $buyNum = $userSurplusNum + $goods['buyNum'];//
  644. }
  645. $mapping[] = [
  646. 'activityId' => $activityId,
  647. 'goodsId' => $goods['goodsId'],
  648. 'skuId' => $goods['skuId'],
  649. 'buyNum' => $buyNum,
  650. ];
  651. }
  652. $data = [
  653. 'checkData' => $checkData,
  654. 'mapping' => $mapping,
  655. ];
  656. return ResultWrapper::success($data);
  657. }
  658. /**
  659. * 缓存用户限购
  660. * @param $data
  661. * @return mixed
  662. */
  663. private function userLimit($data)
  664. {
  665. $result = false;
  666. foreach ($data as $goods) {
  667. $result = $this->objActivityLimitCache->writeLimit($goods['activityId'], $goods['goodsId'], $goods['skuId'], $this->onlineUserId, $goods['buyNum']);
  668. }
  669. return $result;
  670. }
  671. /**
  672. * 获取商品的限购数量
  673. * @param $data
  674. * @param $goodsId
  675. * @param $skuId
  676. * @return int|mixed
  677. */
  678. private function getLimitNum($data, $goodsId, $skuId)
  679. {
  680. if (empty($data)) return $data;
  681. $goodsData = $data['activityGoods'];
  682. $mapping = [];
  683. foreach ($goodsData as $goods) {
  684. $mapping[$goods['goodsId'] . $goods['skuId']] = $goods;
  685. }
  686. return isset($mapping[$goodsId . $skuId]['limitNum']) ? $mapping[$goodsId . $skuId]['limitNum'] : 0;
  687. }
  688. /**
  689. * 检测购物车商品(最新的方法)
  690. * @param $data
  691. * @return ResultWrapper
  692. * @throws \Exception
  693. */
  694. public function checkCart($data)
  695. {
  696. if (empty($data)) return ResultWrapper::fail('购物车数据为空', ErrorCode::$paramError);
  697. $objMGoods = new MGoods($this->onlineEnterpriseId, $this->isFront, $this->onlineUserId);
  698. //验证商品数据
  699. foreach ($data as $key => $val) {
  700. //获取商品详情
  701. $details = $objMGoods->getGoodsInfo($val['goodsId']);
  702. if (!$details->isSuccess()) {
  703. return ResultWrapper::fail($details->getData(), $details->getErrorCode());
  704. }
  705. $goods = $details->getData();//商品数据
  706. if (empty($details)) {
  707. return ResultWrapper::fail($val['goodsCode'] . '查询商品失败', ErrorCode::$paramError);
  708. }
  709. //验证商品状态
  710. if ($goods['enableStatus'] == StatusCode::$delete || $goods['deleteStatus'] == StatusCode::$delete) {
  711. return ResultWrapper::fail($goods['title'] . ' 已下架', ErrorCode::$paramError);
  712. }
  713. $specMultiple = $goods['specMultiple'];//spec数据
  714. $specMapping = [];//Map
  715. foreach ($specMultiple as $spec) {
  716. $specMapping[$spec['id']] = $spec;
  717. }
  718. if (!isset($specMapping[$val['skuId']])) {
  719. Logger::logs(E_USER_ERROR, '规格获取失败', __CLASS__, __LINE__, $specMapping);
  720. return ResultWrapper::fail($goods['title'] . '规格获取失败', ErrorCode::$paramError);
  721. }
  722. //验证商品库存
  723. if ($this->preSale == StatusCode::$delete) {
  724. if ($specMapping[$val['skuId']]['inventory'] < $val['buyNum']) {
  725. $specName = self::createSpecName($specMapping[$val['skuId']]);
  726. return ResultWrapper::fail($goods['title'] . $specName . ' 库存不足', ErrorCode::$paramError);
  727. }
  728. }
  729. $data[$key]['basicGoodsId'] = $val['goodsBasicId'];//为了兼容之前的代码
  730. $data[$key]['skuData'] = [];//兼容之前数据
  731. $data[$key]['specMultiple'] = $specMultiple;//spec
  732. $data[$key]['warehouseId'] = $goods['warehouseId'];//店铺所在仓库
  733. $data[$key]['title'] = $goods['title'];//商品名称
  734. $data[$key]['setNum'] = $specMapping[$val['skuId']]['setNum'];//加入购物车起订量
  735. $data[$key]['activityId'] = 0;//初始化活动id
  736. //todo(优化秒杀商品活动)
  737. if ($specMapping[$val['skuId']]['isActivity'] == StatusCode::$standard) {
  738. //商品存在活动时,没有起订量
  739. $data[$key]['setNum'] = 1;
  740. }
  741. $data[$key]['inventory'] = $specMapping[$val['skuId']]['inventory'];//加入购物车商品规格的可用库存
  742. $data[$key]['isActivityPrice'] = $specMapping[$val['skuId']]['isActivityPrice'];
  743. if ($specMapping[$val['skuId']]['isActivityPrice'] == StatusCode::$standard) {
  744. $data[$key]['activityId'] = $specMapping[$val['skuId']]['activity']['activityId'];
  745. }
  746. $data[$key]['limitNum'] = $specMapping[$val['skuId']]['limitNum'];//商品限购数量
  747. }
  748. return ResultWrapper::success($data);
  749. }
  750. /**
  751. * 生成属性名
  752. * @param $data
  753. * @return mixed|string
  754. */
  755. public static function createSpecName($data)
  756. {
  757. if (empty($data)) {
  758. return '';
  759. }
  760. $unitName = $data['unitName'];
  761. if (!empty($data['specGroup'])) {
  762. $arrSpecName = array_column($data['specGroup'], 'specValueName');
  763. $specName = rtrim(implode('_', $arrSpecName), '_');
  764. $unitName .= '_' . $specName;
  765. }
  766. return $unitName;
  767. }
  768. /**
  769. * 将加入购物车的数据进行分组[old=>在购物车中已存在,now=>新加入购物车商品]
  770. * @param $nowData [加入购物车的数据]
  771. * @return ResultWrapper
  772. */
  773. private function existCartAndGroup($nowData)
  774. {
  775. // 查询当前购物车所有商品数据
  776. $dbResult = self::getCartByUid();
  777. if (!$dbResult->isSuccess()) {
  778. return ResultWrapper::fail('获取购物车数据失败', ErrorCode::$dberror);
  779. }
  780. $oldCartData = $dbResult->getData();
  781. // 对所有商品数据生成hash值,用于判断新加入的商品是否已经存在于购物车之中了
  782. $allHash = [];
  783. foreach ($oldCartData as $key => $val) {
  784. $allHash[] = md5($val['goodsId'] . $val['shopId'] . $val['skuId'] . $val['activityId']);
  785. }
  786. $old = [];//旧的数据
  787. $now = [];//新的数据
  788. foreach ($nowData as $key => &$val) {
  789. $md5 = md5($val['goodsId'] . $val['shopId'] . $val['skuId'] . $val['activityId']);
  790. if (in_array($md5, $allHash)) {
  791. $old[] = $val;
  792. } else {
  793. //之前购物车中没有此商品,检查起订量
  794. if ($val['setNum'] != 0) {
  795. //具有起订量
  796. if ($val['buyNum'] < $val['setNum']) {
  797. $val['buyNum'] = $val['setNum'];
  798. }
  799. }
  800. $now[] = $val;
  801. }
  802. }
  803. $groupData = [
  804. 'old' => $old,
  805. 'now' => $now
  806. ];
  807. return ResultWrapper::success($groupData);
  808. }
  809. /**
  810. * Doc: (des="更新购车商品数量")
  811. * User: XMing
  812. * Date: 2020/8/6
  813. * Time: 9:43 上午
  814. * @param array $params
  815. * @return ResultWrapper
  816. * @throws \Exception
  817. */
  818. public function updateBuyNumApi(array $params)
  819. {
  820. // 如果数量改成0则删除
  821. if (bccomp($params['buyNum'], 0, 0) === 0) {
  822. $dbResult = $this->objDCart->delete(['id' => $params['cartId']]);
  823. if ($dbResult === false) {
  824. return ResultWrapper::fail($this->objDCart->error(), ErrorCode::$dberror);
  825. }
  826. return ResultWrapper::success('操作成功');
  827. }
  828. $cart = $this->objDCart->get($params['cartId']);
  829. if ($cart === false) {
  830. return ResultWrapper::fail($this->objDCart->error(), ErrorCode::$dberror);
  831. }
  832. if (empty($cart)) {
  833. return ResultWrapper::success(true);
  834. }
  835. $goodsInfoResult = $this->objMGoods->getGoodsInfo($cart['goodsId']);
  836. if (!$goodsInfoResult->isSuccess()) {
  837. return ResultWrapper::fail($goodsInfoResult->getData(), $goodsInfoResult->getErrorCode());
  838. }
  839. $goodsInfo = $goodsInfoResult->getData();
  840. $checkResult = self::checkGoods($goodsInfo, [
  841. 'skuId' => $cart['skuId'],
  842. 'buyNum' => $params['buyNum'],
  843. ], 'update');
  844. unset($data);
  845. if (!$checkResult->isSuccess()) {
  846. return ResultWrapper::fail($checkResult->getData(), $checkResult->getErrorCode());
  847. }
  848. $allBuyNum = $checkResult->getData();
  849. $params['buyNum'] = $allBuyNum;
  850. $dbResult = $this->objDCart->update(['buyNum' => $params['buyNum']], $params['cartId']);
  851. if ($dbResult === false) {
  852. return ResultWrapper::fail($this->objDCart->error(), ErrorCode::$dberror);
  853. }
  854. return ResultWrapper::success('操作成功');
  855. }
  856. /**
  857. * 修改购物车商品购买数量
  858. * @param $params
  859. * @return ResultWrapper
  860. * @throws \Exception
  861. */
  862. public function updateBuyNum($params)
  863. {
  864. if (bccomp($params['buyNum'], 0, 0) === 0) {
  865. $dbResult = $this->objDCart->delete(['id' => $params['cartId']]);
  866. if ($dbResult === false) {
  867. return ResultWrapper::fail($this->objDCart->error(), ErrorCode::$dberror);
  868. }
  869. return ResultWrapper::success('操作成功');
  870. } else {
  871. $goods = self::getCartInventory(['id' => $params['cartId']]);
  872. if (!$goods->isSuccess()) {
  873. return ResultWrapper::fail($goods->getData(), ErrorCode::$dberror);
  874. }
  875. $goods = $goods->getData();
  876. $inventoryNum = isset($goods[0]['inventory']) ? $goods[0]['inventory'] : 0;//商品剩余库存
  877. $setNum = isset($goods[0]['setNum']) ? $goods[0]['setNum'] : 0;//商品规格起订数量
  878. $title = isset($goods[0]['title']) ? $goods[0]['title'] : '';//商品名称
  879. if (bccomp($inventoryNum, $params['buyNum'], 8) === -1) {
  880. return ResultWrapper::fail('商品库存不足', ErrorCode::$paramError);
  881. }
  882. if (bccomp($params['buyNum'], $setNum, 0) === -1) {
  883. return ResultWrapper::fail($title . '最小起订数量' . $setNum, ErrorCode::$paramError);
  884. }
  885. if ($this->isFront) {
  886. $dbResult = $this->objDCart->get(['id' => $params['cartId']]);
  887. if ($dbResult === false) {
  888. return ResultWrapper::fail($this->objDCart->error(), ErrorCode::$dberror);
  889. }
  890. $step = bcsub($params['buyNum'], $dbResult['buyNum'], 0);//调整量
  891. //所有减操作正常进行
  892. if (bccomp($params['buyNum'], $dbResult['buyNum']) === 0) {
  893. return ResultWrapper::success('操作成功');
  894. } elseif (bccomp($params['buyNum'], $dbResult['buyNum'], 0) === -1) {
  895. $adj = 2;//减操作
  896. $result = $this->objDCart->update(['buyNum' => $params['buyNum']], ['id' => $params['cartId']]);
  897. if ($result === false) {
  898. return ResultWrapper::fail($this->objDCart->error(), ErrorCode::$dberror);
  899. }
  900. return ResultWrapper::success('操作成功');
  901. } elseif (bccomp($params['buyNum'], $dbResult['buyNum'], 0) === 1) {
  902. $adj = 1;//加操作
  903. $insert = [
  904. 'goodsId' => $dbResult['goodsId'],
  905. 'goodsBasicId' => $dbResult['goodsBasicId'],
  906. 'shopId' => $dbResult['shopId'],
  907. 'source' => $dbResult['source'],
  908. 'skuId' => $dbResult['skuId'],
  909. 'buyNum' => $step,
  910. 'goodsCode' => $dbResult['goodsCode']
  911. ];
  912. $result = self::addCart(['goodsData' => [$insert]]);
  913. if (!$result->isSuccess()) {
  914. return ResultWrapper::fail($result->getData(), $result->getErrorCode());
  915. }
  916. return ResultWrapper::success('操作成功');
  917. }
  918. } else {
  919. $dbResult = $this->objDCart->update(['buyNum' => $params['buyNum']], $params['cartId']);
  920. if ($dbResult === false) {
  921. return ResultWrapper::fail($this->objDCart->error(), ErrorCode::$dberror);
  922. }
  923. return ResultWrapper::success('操作成功');
  924. }
  925. }
  926. }
  927. /**
  928. * Doc: (des="获取用户的购物车数据")
  929. * User: XMing
  930. * Date: 2020/8/5
  931. * Time: 3:15 下午
  932. * @param integer $isZero
  933. * @param boolean $isCashier
  934. * @return ResultWrapper
  935. * @throws \Exception
  936. */
  937. public function getCartByUserCenterIdApi(): ResultWrapper
  938. {
  939. // 查询购物车数据
  940. $sql = 'SELECT id,goodsId,goodsCode,buyNum,shopId,source,goodsBasicId,selection,skuId,warehouseId,activityId,cashierUid FROM qianniao_cart_'.$this->onlineEnterpriseId.' WHERE userCenterId = '.$this->onlineUserId.' FOR UPDATE';
  941. $cartList = $this->objDCart->query($sql);
  942. if ($cartList === false){
  943. return ResultWrapper::fail($this->objDCart->error(),ErrorCode::$dberror);
  944. }
  945. if (empty($cartList)) {
  946. return ResultWrapper::success(['data' => [], 'total' => 0]);
  947. }
  948. // 提取活动ids
  949. $activityIds = [];
  950. foreach ($cartList as $item){
  951. if(!empty($item['activityId']) && !in_array($item['activityId'],$activityIds)){
  952. $activityIds[] = $item['activityId'];
  953. }
  954. }
  955. if (!empty($activityIds)){
  956. $groupResult = self::cartGroupData($cartList);
  957. if (!$groupResult->isSuccess()) {
  958. return ResultWrapper::fail($groupResult->getData(), $groupResult->getErrorCode());
  959. }
  960. $cartList = $groupResult->getData();
  961. if (empty($cartList)) {
  962. return ResultWrapper::success(['data' => [], 'total' => 0]);
  963. }
  964. }
  965. $dataResult = self::formatGoodsAndShop($cartList);
  966. if (!$dataResult->isSuccess()) {
  967. return ResultWrapper::fail($dataResult->getData(), $dataResult->getErrorCode());
  968. }
  969. $data = $dataResult->getData();
  970. $return = [
  971. 'data' => $data,
  972. 'total' => empty($data) ? 0 : count($data),
  973. ];
  974. return ResultWrapper::success($return);
  975. }
  976. /**
  977. * Doc: (des="抹零操作")
  978. * User: XMing
  979. * Date: 2020/9/12
  980. * Time: 7:08 下午
  981. * @param array $data
  982. * @param int $isZero
  983. * @return ResultWrapper
  984. */
  985. private function buildZero($data, $isZero)
  986. {
  987. //是否开启抹零
  988. $data['rem_money'] = '0.00';
  989. $settingResult = $this->objMCashierSettings->get();
  990. if (!$settingResult->isSuccess()) {
  991. return ResultWrapper::fail($settingResult->getData(), $settingResult->getErrorCode());
  992. }
  993. $setting = $settingResult->getData();
  994. if (!is_array($setting)) {
  995. //不是数组时,说明未配置
  996. return ResultWrapper::success($data);
  997. }
  998. if (!isset($setting['add_form'])) {
  999. return ResultWrapper::fail('收银台配置错误', ErrorCode::$paramError);
  1000. }
  1001. if (!isset($setting['add_form']['zero_set'])) {
  1002. return ResultWrapper::fail('收银台参数配置错误', ErrorCode::$paramError);
  1003. }
  1004. $zeroSet = $setting['add_form']['zero_set'];
  1005. if ($zeroSet['status'] == StatusCode::$delete) {
  1006. //没有开启抹零,前台需要在抹零那判断是否开启
  1007. return ResultWrapper::success($data);
  1008. }
  1009. $payMoney = $data['payMoney'];
  1010. if ($zeroSet['auto_zero'] == StatusCode::$standard) {
  1011. //开启了自动抹零
  1012. switch ($zeroSet['type']) {
  1013. case 1:
  1014. //分
  1015. $newPatMoney = bcadd($payMoney, 0, 1);
  1016. break;
  1017. case 2:
  1018. //角
  1019. $newPatMoney = bcadd($payMoney, 0, 0);
  1020. break;
  1021. case 3:
  1022. //到角
  1023. $newPatMoney = round($payMoney, 1);
  1024. break;
  1025. case 4:
  1026. //到元
  1027. $newPatMoney = round($payMoney, 0);
  1028. break;
  1029. default:
  1030. $newPatMoney = 0;
  1031. break;
  1032. }
  1033. $rem_money = bcsub($payMoney, $newPatMoney, 2);
  1034. $data['payMoney'] = $newPatMoney;
  1035. $data['rem_money'] = $rem_money;
  1036. } else {
  1037. if ($isZero == StatusCode::$standard) {
  1038. //手动抹零
  1039. switch ($zeroSet['type']) {
  1040. case 1:
  1041. //分
  1042. $newPatMoney = bcadd($payMoney, 0, 1);
  1043. break;
  1044. case 2:
  1045. //角
  1046. $newPatMoney = bcadd($payMoney, 0, 0);
  1047. break;
  1048. case 3:
  1049. //到角
  1050. $newPatMoney = round($payMoney, 1);
  1051. break;
  1052. case 4:
  1053. //到元
  1054. $newPatMoney = round($payMoney, 0);
  1055. break;
  1056. default:
  1057. $newPatMoney = 0;
  1058. break;
  1059. }
  1060. $rem_money = bcsub($payMoney, $newPatMoney, 2);
  1061. $data['payMoney'] = $newPatMoney;
  1062. $data['rem_money'] = $rem_money;
  1063. }
  1064. }
  1065. return ResultWrapper::success($data);
  1066. }
  1067. /**
  1068. * Doc: (des="将购物车的数据进行分组 活动商品的限购级别以及价格,全部都基于此方法组装的数据")
  1069. * User: XMing
  1070. * Date: 2020/8/5
  1071. * Time: 3:21 下午
  1072. * @param array $data
  1073. * @return ResultWrapper
  1074. */
  1075. private function cartGroupData(array $data)
  1076. {
  1077. if (empty($data)) {
  1078. return ResultWrapper::success([]);
  1079. }
  1080. // 获取限购级别设置项
  1081. $BasicSetupResult = $this->objMBasicSetup->getBasicField('limitLevel');
  1082. if (!$BasicSetupResult->isSuccess()) {
  1083. return ResultWrapper::fail($BasicSetupResult->getData(), $BasicSetupResult->getErrorCode());
  1084. }
  1085. $BasicSetup = $BasicSetupResult->getData();//之前的企业可鞥未设置此字段,给默认值5
  1086. $limitLevel = isset($BasicSetup['limitLevel']) ? $BasicSetup['limitLevel'] : StatusCode::$standard;//限购级别
  1087. $selectStateMap = [];
  1088. //将同sku合并到一起s
  1089. $dataMap = [];
  1090. $skuBuyMap = [];
  1091. foreach ($data as $datum) {
  1092. $selectStateMap[$datum['skuId'] . ':' . $datum['activityId']] = $datum['selection'];
  1093. $dataMap[$datum['skuId']] = [
  1094. 'id' => $datum['id'],
  1095. 'goodsId' => $datum['goodsId'],
  1096. 'goodsCode' => $datum['goodsCode'],
  1097. 'shopId' => $datum['shopId'],
  1098. 'source' => $datum['source'],
  1099. 'goodsBasicId' => $datum['goodsBasicId'],
  1100. 'selection' => $datum['selection'],
  1101. 'skuId' => $datum['skuId'],
  1102. 'warehouseId' => $datum['warehouseId'],
  1103. 'activityId' => 0,
  1104. ];
  1105. $skuBuyMap[$datum['skuId']][] = $datum['buyNum'];
  1106. }
  1107. $this->selectStateMap = $selectStateMap;
  1108. foreach ($dataMap as $skuId => $value) {
  1109. $dataMap[$skuId]['buyNum'] = array_sum($skuBuyMap[$skuId]);
  1110. }
  1111. $data = array_values($dataMap);
  1112. //获取客户信息
  1113. $customer = $this->objDCustomer->get(['userCenterId' => $this->onlineUserId], 'id,type');
  1114. if ($customer === false) {
  1115. return ResultWrapper::fail($this->objDCustomer->error(), ErrorCode::$dberror);
  1116. }
  1117. if (empty($customer)) {
  1118. $customer = [];
  1119. }
  1120. //获取商品是否是活动商品
  1121. $allSkuIds = [];
  1122. foreach ($data as $datum) {
  1123. $allSkuIds[] = $datum['skuId'];
  1124. }
  1125. $activityResult = $this->objMActivity->getActivity([
  1126. 'skuIds' => implode(',', $allSkuIds),
  1127. 'customerType' => isset($customer['type']) ? $customer['type'] : 0,
  1128. ]);
  1129. if (!$activityResult->isSuccess()) {
  1130. return ResultWrapper::fail($activityResult->getData(), $activityResult->getErrorCode());
  1131. }
  1132. $activity = $activityResult->getData();
  1133. $activityMap = [];
  1134. foreach ($activity as $item) {
  1135. $activityMap[$item['skuId']] = $item;
  1136. }
  1137. $update = [];
  1138. foreach ($data as $item) {
  1139. if (!isset($activityMap[$item['skuId']])) {
  1140. //没有活动不用处理
  1141. $update[] = self::buildData($item, $item['buyNum'], 0, $this->onlineUserId);
  1142. continue;
  1143. }
  1144. //是活动商品
  1145. //1。活动商品数量是否足够
  1146. $len = $this->objActivityLimitCache->getLen($activityMap[$item['skuId']]['activityId'], $item['goodsId'], $item['skuId']);
  1147. if ($len == 0) {
  1148. //活动商品已购买完
  1149. $update[] = self::buildData($item, $item['buyNum'], 0, $this->onlineUserId);
  1150. continue;
  1151. }
  1152. //2。剩余数量
  1153. //2.1 剩余数量足够
  1154. if ($len >= $item['buyNum']) {
  1155. //获取用户已购数量
  1156. $userLimit = $this->objActivityLimitCache->getLimit($activityMap[$item['skuId']]['activityId'], $item['goodsId'], $item['skuId'], $this->onlineUserId);
  1157. //3。用户限购
  1158. switch ($limitLevel) {
  1159. case StatusCode::$standard:
  1160. if ($activityMap[$item['skuId']]['limitNum'] == 0) {
  1161. $update[] = self::buildData($item, $item['buyNum'], 0, $this->onlineUserId);
  1162. continue 2;
  1163. }
  1164. //3。1 活动期间限购
  1165. if ($userLimit > $activityMap[$item['skuId']]['limitNum']) {
  1166. //已经购买过活动商品了,剩下的都恢复原价
  1167. $update[] = self::buildData($item, $item['buyNum'], 0, $this->onlineUserId);
  1168. continue 2;
  1169. }
  1170. $allowBuyNum = $activityMap[$item['skuId']]['limitNum'] - $userLimit;
  1171. if ($allowBuyNum == 0) {
  1172. $update[] = self::buildData($item, $item['buyNum'], 0, $this->onlineUserId);
  1173. continue 2;
  1174. }
  1175. if ($item['buyNum'] <= $allowBuyNum) {
  1176. $update[] = self::buildData($item, $item['buyNum'], $activityMap[$item['skuId']]['activityId'], $this->onlineUserId);
  1177. continue 2;
  1178. }
  1179. if ($item['buyNum'] > $allowBuyNum) {
  1180. //分解
  1181. $update[] = self::buildData($item, $allowBuyNum, $activityMap[$item['skuId']]['activityId'], $this->onlineUserId);
  1182. $update[] = self::buildData($item, $item['buyNum'] - $allowBuyNum, 0, $this->onlineUserId);//原价不参与活动
  1183. continue 2;
  1184. }
  1185. break;
  1186. case StatusCode::$delete:
  1187. //3。2 单笔订单限购买
  1188. if ($activityMap[$item['skuId']]['limitNum'] < $item['buyNum']) {
  1189. //购车中的商品大于单笔限购,分解
  1190. $update[] = self::buildData($item, $activityMap[$item['skuId']]['limitNum'], $activityMap[$item['skuId']]['activityId'], $this->onlineUserId);
  1191. $update[] = self::buildData($item, $item['buyNum'] - $activityMap[$item['skuId']]['limitNum'], 0, $this->onlineUserId);
  1192. continue 2;
  1193. }
  1194. if ($activityMap[$item['skuId']]['limitNum'] >= $item['buyNum']) {
  1195. //都是活动价格
  1196. $update[] = self::buildData($item, $item['buyNum'], $activityMap[$item['skuId']]['activityId'], $this->onlineUserId);
  1197. continue 2;
  1198. }
  1199. break;
  1200. }
  1201. } else {
  1202. //2。2 剩余数量不足
  1203. $update[] = self::buildData($item, $item['buyNum'], 0, $this->onlineUserId);
  1204. continue;
  1205. }
  1206. }
  1207. if (empty($update)) {
  1208. return ResultWrapper::fail('获取购物车数据失败', ErrorCode::$paramError);
  1209. }
  1210. $this->objDCart->beginTransaction();
  1211. $deleteRes = $this->objDCart->delete(['userCenterId' => $this->onlineUserId]);
  1212. if ($deleteRes === false) {
  1213. $this->objDCart->rollBack();
  1214. return ResultWrapper::fail($this->objDCart->error(), ErrorCode::$dberror);
  1215. }
  1216. $insertRes = $this->objDCart->insert($update, true);
  1217. if ($insertRes === false) {
  1218. $this->objDCart->rollBack();
  1219. return ResultWrapper::fail($this->objDCart->error(), ErrorCode::$dberror);
  1220. }
  1221. $this->objDCart->commit();
  1222. $where['userCenterId'] = $this->onlineUserId;
  1223. $cartList = $this->objDCart->select($where);
  1224. if ($cartList === false) {
  1225. $this->objDCart->rollBack();
  1226. return ResultWrapper::fail($this->objDCart->error(), ErrorCode::$dberror);
  1227. }
  1228. return ResultWrapper::success($cartList);
  1229. }
  1230. /**
  1231. * Doc: (des="")
  1232. * User: XMing
  1233. * Date: 2020/8/5
  1234. * Time: 5:22 下午
  1235. * @param array $item
  1236. * @param int $buyNum
  1237. * @param int $activityId
  1238. * @param int $userCenterId
  1239. * @return array
  1240. */
  1241. private function buildData(array $item, int $buyNum, int $activityId, int $userCenterId)
  1242. {
  1243. $data = [
  1244. 'goodsId' => $item['goodsId'],
  1245. 'goodsCode' => $item['goodsCode'],
  1246. 'shopId' => $item['shopId'],
  1247. 'source' => $item['source'],
  1248. 'goodsBasicId' => $item['goodsBasicId'],
  1249. 'selection' => isset($this->selectStateMap[$item['skuId'] . ':' . $activityId]) ? $this->selectStateMap[$item['skuId'] . ':' . $activityId] : StatusCode::$delete,
  1250. 'skuId' => $item['skuId'],
  1251. 'warehouseId' => $item['warehouseId'],
  1252. 'activityId' => $activityId,
  1253. 'buyNum' => $buyNum,
  1254. 'userCenterId' => $userCenterId
  1255. ];
  1256. return $data;
  1257. }
  1258. /**
  1259. * app获取购物车内的数据
  1260. * @return ResultWrapper
  1261. * @throws \Exception
  1262. */
  1263. public function getCartByUserCenterId()
  1264. {
  1265. $result = $this->objDCart->select(['userCenterId' => $this->onlineUserId], 'id,goodsId,goodsCode,buyNum,shopId,source,goodsBasicId,selection,skuId,warehouseId,activityId', 'createTime DESC');
  1266. if ($result === false) {
  1267. return ResultWrapper::fail($this->objDCart->error(), ErrorCode::$dberror);
  1268. }
  1269. $result = self::formatMerge($result);
  1270. if (!$result->isSuccess()) {
  1271. return ResultWrapper::fail($result->getData(), $result->getErrorCode());
  1272. }
  1273. $dataResult = self::formatGoodsAndShop($result->getData());
  1274. if (!$dataResult->isSuccess()) {
  1275. return ResultWrapper::fail($dataResult->getData(), $dataResult->getErrorCode());
  1276. }
  1277. $data = $dataResult->getData();
  1278. $return = [
  1279. 'data' => $data,
  1280. 'total' => empty($data) ? 0 : count($data),
  1281. ];
  1282. return ResultWrapper::success($return);
  1283. }
  1284. /**
  1285. * 后台获取购车数据
  1286. * @return ResultWrapper
  1287. * @throws \Exception
  1288. */
  1289. public function getManageCartByUserCenterId()
  1290. {
  1291. $result = $this->objDCart->select(['userCenterId' => $this->onlineUserId], 'id,goodsId,goodsCode,buyNum,shopId,goodsBasicId,selection,skuId,warehouseId,activityId', 'createTime DESC');
  1292. if ($result === false) {
  1293. return ResultWrapper::fail($this->objDCart->error(), ErrorCode::$dberror);
  1294. }
  1295. $dataResult = self::formatGoodsAndShop($result);
  1296. if (!$dataResult->isSuccess()) {
  1297. return ResultWrapper::fail($dataResult->getData(), $dataResult->getErrorCode());
  1298. }
  1299. $data = $dataResult->getData();
  1300. $return = [
  1301. 'data' => $data,
  1302. 'total' => empty($data) ? 0 : count($data),
  1303. ];
  1304. return ResultWrapper::success($return);
  1305. }
  1306. /**
  1307. * 获取当前用户的购物车数据
  1308. */
  1309. private function getCartByUid()
  1310. {
  1311. $result = $this->objDCart->select(['userCenterId' => $this->onlineUserId], 'id,goodsId,goodsCode,buyNum,shopId,goodsBasicId,selection,skuId,warehouseId,activityId,cashierUid', 'createTime DESC');
  1312. if ($result === false) {
  1313. return ResultWrapper::fail($this->objDCart->error(), ErrorCode::$dberror);
  1314. }
  1315. return ResultWrapper::success($result);
  1316. }
  1317. /**
  1318. * 更新选中状态
  1319. * @param $params
  1320. * @return ResultWrapper
  1321. * @throws \Exception
  1322. */
  1323. public function updateSelection($params)
  1324. {
  1325. $dbResult = false;
  1326. switch ($params['type']) {
  1327. case self::$type['single']:
  1328. if (empty($params['id'])) {
  1329. return ResultWrapper::fail('缺少cartId', ErrorCode::$paramError);
  1330. }
  1331. $dbResult = $this->objDCart->update(['selection' => $params['selection']], $params['id']);
  1332. break;
  1333. case self::$type['shop']:
  1334. if (empty($params['shopId'])) {
  1335. return ResultWrapper::fail('缺少shopId', ErrorCode::$paramError);
  1336. }
  1337. $dbResult = $this->objDCart->update(['selection' => $params['selection']], ['shopId' => $params['shopId'], 'userCenterId' => $this->onlineUserId]);
  1338. break;
  1339. case self::$type['all']:
  1340. $dbResult = $this->objDCart->update(['selection' => $params['selection']], ['userCenterId' => $this->onlineUserId]);
  1341. break;
  1342. }
  1343. if ($dbResult === false) {
  1344. return ResultWrapper::fail($this->objDCart->error(), ErrorCode::$dberror);
  1345. }
  1346. return ResultWrapper::success($dbResult);
  1347. }
  1348. /**
  1349. * 删除购物车中商品(可批量)
  1350. * @param $ids
  1351. * @param boolean $order 是否是来自订单
  1352. * @return ResultWrapper
  1353. */
  1354. public function delCart($ids, $order = false)
  1355. {
  1356. $dbResult = $this->objDCart->select($ids, 'id,goodsId,buyNum,skuId,activityId');
  1357. if ($dbResult === false) {
  1358. return ResultWrapper::fail($this->objDCart->error(), ErrorCode::$dberror);
  1359. }
  1360. //限购减对应数据
  1361. if ($order === true) {
  1362. foreach ($dbResult as $goods) {
  1363. if ($goods['activityId'] != 0) {
  1364. $userSurplusNum = $this->objActivityLimitCache->getLimit($goods['activityId'], $goods['goodsId'], $goods['skuId'], $this->onlineUserId);
  1365. $mapping[] = [
  1366. 'activityId' => $goods['activityId'],
  1367. 'goodsId' => $goods['goodsId'],
  1368. 'skuId' => $goods['skuId'],
  1369. 'buyNum' => $userSurplusNum + $goods['buyNum'],
  1370. ];
  1371. self::userLimit($mapping);
  1372. }
  1373. }
  1374. }
  1375. $dbResult = $this->objDCart->delete($ids);
  1376. if ($dbResult === false) {
  1377. return ResultWrapper::fail($this->objDCart->error(), ErrorCode::$dberror);
  1378. }
  1379. return ResultWrapper::success($dbResult);
  1380. }
  1381. /**
  1382. * Doc: (des="判断字符串是否是一个整数")
  1383. * User: XMing
  1384. * Date: 2020/10/12
  1385. * Time: 10:29 上午
  1386. * @param $num
  1387. * @return bool
  1388. */
  1389. private static function isInteger($num)
  1390. {
  1391. if ($num - floor($num) != 0) {
  1392. return false;
  1393. }
  1394. return true;
  1395. }
  1396. /**
  1397. * 拼装购物车数据(商铺->商品->规格->库存->价格->分组)
  1398. * 此方法正在修改,勿动
  1399. * @param $data
  1400. * @return ResultWrapper
  1401. * @throws \Exception
  1402. */
  1403. private function formatGoodsAndShop($data, $userCouponId = null, $vipCardId = null)
  1404. {
  1405. foreach ($data as &$value) {
  1406. $value['buyNum'] = self::isInteger($value['buyNum']) ? (int)$value['buyNum'] : $value['buyNum'];
  1407. }
  1408. unset($value);
  1409. $result = self::formatShop($data);//格式化店铺数据
  1410. $result = self::formatGoods($result);//格式化商品数据
  1411. if ($this->isFront === true) {
  1412. $result = self::calExpress($result);
  1413. if (!$result->isSuccess()) {
  1414. return ResultWrapper::fail($result->getData(), $result->getErrorCode());
  1415. }
  1416. $result = $result->getData();
  1417. }
  1418. // 价格处理
  1419. $priceResult = self::formatPrice($result);
  1420. if (!$priceResult->isSuccess()) {
  1421. return ResultWrapper::fail($priceResult->getData(), $priceResult->getErrorCode());
  1422. }
  1423. $result = $priceResult->getData();
  1424. $inventoryResult = self::formatSkuInventory($result);//处理库存
  1425. if (!$inventoryResult->isSuccess()){
  1426. return ResultWrapper::fail($inventoryResult->getData(),$inventoryResult->getErrorCode());
  1427. }
  1428. $result = $inventoryResult->getData();
  1429. $result = self::formatGroup($result, $userCouponId, $vipCardId);
  1430. if(!$result->isSuccess()){
  1431. return ResultWrapper::fail($result->getData(), $result->getErrorCode());
  1432. }
  1433. return ResultWrapper::success($result->getData());
  1434. }
  1435. /**
  1436. * @param $data
  1437. * @return ResultWrapper
  1438. */
  1439. private function formatMerge($data)
  1440. {
  1441. if (empty($data)) return ResultWrapper::success($data);
  1442. foreach ($data as $goods) {
  1443. if ($goods['activityId'] == 0) {
  1444. continue;
  1445. }
  1446. //获取活动详情
  1447. $activity = $this->objActivityLimitCache->getActivity($goods['activityId']);
  1448. if ($activity['enableStatus'] == StatusCode::$delete
  1449. || time() < $activity['startTime']
  1450. || time() > $activity['endTime']) {
  1451. //活动被下架,将此条数据合并到普通商品
  1452. $cartResult = $this->objDCart->get(['goodsId' => $goods['goodsId'], 'shopId' => $goods['shopId'], 'skuId' => $goods['skuId'], 'activityId' => 0, 'userCenterId' => $this->onlineUserId]);
  1453. if (empty($cartResult)) {
  1454. //addCart
  1455. $insert = [
  1456. 'userCenterId' => $this->onlineUserId,
  1457. 'selection' => StatusCode::$standard,
  1458. 'skuId' => $goods['skuId'],
  1459. 'goodsCode' => $goods['goodsCode'],
  1460. 'goodsId' => $goods['goodsId'],
  1461. 'shopId' => $goods['shopId'],
  1462. 'buyNum' => $goods['buyNum'],//购买数量
  1463. 'source' => $goods['source'],
  1464. 'goodsBasicId' => $goods['goodsBasicId'],
  1465. 'warehouseId' => $goods['warehouseId'],
  1466. 'activityId' => 0,//活动id
  1467. ];
  1468. $dbResult = $this->objDCart->insert($insert);
  1469. } else {
  1470. //update
  1471. $dbResult = $this->objDCart->set_inc('buyNum', ['id' => $cartResult['id']], $goods['buyNum']);
  1472. }
  1473. $delResult = $this->objDCart->delete(['id' => $goods['id']]);
  1474. //获取用户限购数量
  1475. $userLimit = $this->objActivityLimitCache->getLimit($goods['activityId'], $goods['goodsId'], $goods['skuId'], $this->onlineUserId);
  1476. $this->objActivityLimitCache->writeLimit($goods['activityId'], $goods['goodsId'], $goods['skuId'], $this->onlineUserId, $userLimit - $goods['buyNum']);
  1477. if ($delResult === false) {
  1478. return ResultWrapper::fail($this->objDCart->error(), ErrorCode::$dberror);
  1479. }
  1480. if ($dbResult === false) {
  1481. return ResultWrapper::fail($this->objDCart->error(), ErrorCode::$dberror);
  1482. }
  1483. }
  1484. }
  1485. $result = $this->objDCart->select(['userCenterId' => $this->onlineUserId], 'id,goodsId,goodsCode,buyNum,shopId,goodsBasicId,selection,skuId,warehouseId,activityId', 'createTime DESC');
  1486. if ($result === false) {
  1487. return ResultWrapper::fail($this->objDCart->error(), ErrorCode::$dberror);
  1488. }
  1489. return ResultWrapper::success($result);
  1490. }
  1491. /**
  1492. * 获取店铺信息
  1493. * (原数据上+店铺名称+店铺logo)
  1494. * @param $data
  1495. * @return mixed
  1496. * @throws \Exception
  1497. */
  1498. private function formatShop($data)
  1499. {
  1500. if (empty($data)) return $data;
  1501. $objMGoods = new MShop($this->onlineEnterpriseId, $this->onlineUserId);
  1502. $shopData = $objMGoods->getShopName(array_unique(array_column($data, 'shopId')));
  1503. if (!empty($shopData)) {
  1504. foreach ($data as &$shop) {
  1505. $shop['shopName'] = isset($shopData[$shop['shopId']]['name']) ? $shopData[$shop['shopId']]['name'] : '';
  1506. $shop['shopLogo'] = isset($shopData[$shop['shopId']]['logo']) ? $shopData[$shop['shopId']]['logo'] : '';
  1507. $shop['cartId'] =getArrayItem($shop,'id',0);
  1508. }
  1509. }
  1510. return $data;
  1511. }
  1512. /**
  1513. * 商品信息
  1514. * (原数据基础上+商品名称+商品图片)商品的状态 下架商品计入失效
  1515. * @param $data
  1516. * @return mixed
  1517. * @throws \Exception
  1518. */
  1519. private function formatGoods($data)
  1520. {
  1521. if (empty($data)) return $data;
  1522. $objGoodsBasicRelevantCache = new GoodsBasicRelevant($this->onlineEnterpriseId);
  1523. $objMGoods = new MGoods($this->onlineEnterpriseId, false, $this->onlineUserId);
  1524. $goodsData = $objMGoods->getGoodsNames(array_unique(array_column($data, 'goodsId')));
  1525. if (!empty($goodsData)) {
  1526. $dbResult = $objMGoods->getNameByGoodsIds(array_values(array_unique(array_column($goodsData, 'basicGoodsId'))));
  1527. if ($dbResult->isSuccess()) {
  1528. $basicData = $dbResult->getData();
  1529. foreach ($goodsData as &$goods) {
  1530. $goods['goodsName'] = isset($basicData[$goods['basicGoodsId']]['title']) ? $basicData[$goods['basicGoodsId']]['title'] : '';
  1531. $goods['describe'] = isset($basicData[$goods['basicGoodsId']]['describe']) ? $basicData[$goods['basicGoodsId']]['describe'] : '';
  1532. $goods['categoryId'] = isset($basicData[$goods['basicGoodsId']]['categoryId']) ? $basicData[$goods['basicGoodsId']]['categoryId'] : '';
  1533. $goods['brandId'] = isset($basicData[$goods['basicGoodsId']]['brandId']) ? $basicData[$goods['basicGoodsId']]['brandId'] : '';
  1534. $goods['categoryPath'] = isset($basicData[$goods['basicGoodsId']]['categoryPath']) ? $basicData[$goods['basicGoodsId']]['categoryPath'] : '';
  1535. $goods['specType'] = isset($basicData[$goods['basicGoodsId']]['specType']) ? $basicData[$goods['basicGoodsId']]['specType'] : StatusCode::$specType['single'];
  1536. $goods['storageCode'] = isset($basicData[$goods['basicGoodsId']]['storageCode']) ? $basicData[$goods['basicGoodsId']]['storageCode'] : '';
  1537. $goods['categoryName'] = empty($goods['categoryId']) ? '' : $objGoodsBasicRelevantCache->getNameByCategoryId($goods['categoryId']);
  1538. $goods['brandName'] = empty($goods['brandId']) ? '' : $objGoodsBasicRelevantCache->getNameByBrandId($goods['brandId']);
  1539. $goods['images'] = isset($basicData[$goods['basicGoodsId']]['images']) ? $basicData[$goods['basicGoodsId']]['images'] : [];
  1540. $goods['isEq'] = isset($basicData[$goods['basicGoodsId']]['isEq']) ? $basicData[$goods['basicGoodsId']]['isEq'] : StatusCode::$delete;
  1541. }
  1542. }
  1543. }
  1544. $objMSku = new MSku($this->onlineUserId, $this->onlineEnterpriseId);
  1545. $specNameMapping = $objMSku->getSpecNameBySkuId(array_column($data, 'skuId'));
  1546. if (!$specNameMapping->isSuccess()) {
  1547. $specNameMapping = [];
  1548. } else {
  1549. $specNameMapping = $specNameMapping->getData();
  1550. }
  1551. foreach ($data as $key => &$val) {
  1552. $val['goodsCode'] = createCode(StatusCode::$code['goodsBasic']['prefix'], $val['goodsBasicId'], StatusCode::$code['goodsBasic']['length']);
  1553. $val['brandName'] = isset($goodsData[$val['goodsId']]) ? $goodsData[$val['goodsId']]['brandName'] : '';
  1554. $val['categoryName'] = isset($goodsData[$val['goodsId']]) ? $goodsData[$val['goodsId']]['categoryName'] : '';
  1555. $val['barCode'] = isset($specNameMapping[$val['skuId']]) ? $specNameMapping[$val['skuId']]['barCode'] : '';
  1556. $val['storageCode'] = isset($goodsData[$val['goodsId']]['storageCode']) ? $goodsData[$val['goodsId']]['storageCode'] : '';
  1557. $val['goodsName'] = isset($goodsData[$val['goodsId']]['goodsName']) ? $goodsData[$val['goodsId']]['goodsName'] : '';
  1558. $images = isset($goodsData[$val['goodsId']]['images']) ? $goodsData[$val['goodsId']]['images'] : [];
  1559. $val['goodsImages'] = empty($images) ? '' : $images[0];
  1560. $val['isInvalid'] = isset($goodsData[$val['goodsId']]) ? $goodsData[$val['goodsId']]['enableStatus'] : StatusCode::$delete;//4=>失效商品
  1561. $val['describe'] = isset($goodsData[$val['goodsId']]) ? $goodsData[$val['goodsId']]['describe'] : '';
  1562. $val['categoryId'] = isset($goodsData[$val['goodsId']]) ? $goodsData[$val['goodsId']]['categoryId'] : '';
  1563. $val['brandId'] = isset($goodsData[$val['goodsId']]) ? $goodsData[$val['goodsId']]['brandId'] : '';
  1564. $val['categoryPath'] = isset($goodsData[$val['goodsId']]) ? $goodsData[$val['goodsId']]['categoryPath'] : '';
  1565. $val['isActivity'] = empty($val['activityId']) ? StatusCode::$delete : StatusCode::$standard;//是否是活动商品
  1566. $val['specType'] = isset($goodsData[$val['goodsId']]) ? $goodsData[$val['goodsId']]['specType'] : StatusCode::$specType['single'];
  1567. $val['unitName'] = isset($specNameMapping[$val['skuId']]) ? $specNameMapping[$val['skuId']]['unitName'] : '';
  1568. $val['specGroup'] = isset($specNameMapping[$val['skuId']]) ? $specNameMapping[$val['skuId']]['specGroup'] : [];
  1569. $val['notExpress'] = StatusCode::$standard;
  1570. $val['supplierId'] = isset($goodsData[$val['goodsId']]) ? $goodsData[$val['goodsId']]['supplierId'] : 0;
  1571. $val['supplierName'] = isset($goodsData[$val['goodsId']]) ? $goodsData[$val['goodsId']]['supplierName'] : '';
  1572. $val['isEq'] = isset($goodsData[$val['goodsId']]) ? $goodsData[$val['goodsId']]['isEq'] : StatusCode::$delete;
  1573. $val['isDistribution'] = isset($goodsData[$val['goodsId']]) ? $goodsData[$val['goodsId']]['isDistribution'] : StatusCode::$delete;
  1574. $val['express'] = [
  1575. 'weight' => isset($specNameMapping[$val['skuId']]) ? $specNameMapping[$val['skuId']]['weight'] : 0,
  1576. 'expressType' => isset($goodsData[$val['goodsId']]) ? $goodsData[$val['goodsId']]['expressType'] : 0,
  1577. 'ruleId' => isset($goodsData[$val['goodsId']]) ? $goodsData[$val['goodsId']]['ruleId'] : 0,
  1578. 'expressFee' => isset($goodsData[$val['goodsId']]) ? $goodsData[$val['goodsId']]['expressFee'] : 0,
  1579. ];
  1580. if ($val['isEq'] == StatusCode::$standard){
  1581. $isMaster = isset($specNameMapping[$val['skuId']]) ? $specNameMapping[$val['skuId']]['isMaster'] : StatusCode::$delete;
  1582. //超码商品
  1583. if ($isMaster == StatusCode::$delete){
  1584. $masterSkuResult = $this->objMSku->getMasterById($val['goodsBasicId']);
  1585. if ($masterSkuResult->isSuccess()){
  1586. $masterSku = $masterSkuResult->getData();
  1587. $conversion = isset($specNameMapping[$val['skuId']]) ? $specNameMapping[$val['skuId']]['conversion'] : 0;
  1588. $u = isset($masterSku['unitName']) ? $masterSku['unitName'] : '';
  1589. $u_1 = $val['unitName'];
  1590. $val['extends'] = ['uId' => $masterSku['id'], 'uId_1' => $val['skuId'], 'u' => $u, 'u_1' => $u_1, 'u_1_buy'=> $val['buyNum'],'conversion' => $conversion];
  1591. }
  1592. }
  1593. }
  1594. if (isset($goodsData[$val['goodsId']]) && ($goodsData[$val['goodsId']]['enableStatus'] == StatusCode::$delete || $goodsData[$val['goodsId']]['deleteStatus'] == StatusCode::$delete)) {
  1595. $val['invalidMsg'] = '此商品已下架';
  1596. $this->invalidData[] = $val;//失效商品集合
  1597. unset($data[$key]);
  1598. }
  1599. }
  1600. return $data;
  1601. }
  1602. /**
  1603. * 获取规格信息
  1604. * (原数据+skuData+unitName)
  1605. * @param $data
  1606. * @return mixed
  1607. * @throws \Exception
  1608. */
  1609. private function formatSku($data)
  1610. {
  1611. if (empty($data)) return $data;
  1612. $objMSku = new MSku($this->onlineUserId, $this->onlineEnterpriseId);
  1613. $skuData = $objMSku->getSku(array_unique(array_column($data, 'skuId')));
  1614. if (!empty($skuData)) {
  1615. foreach ($data as &$goods) {
  1616. $goods['skuData'][] = isset($skuData[$goods['skuId']]) ? $skuData[$goods['skuId']] : [];
  1617. $goods['unitName'] = isset($skuData[$goods['skuId']]['unitName']) ? $skuData[$goods['skuId']]['unitName'] : '';
  1618. $goods['conversion'] = isset($skuData[$goods['skuId']]['conversion']) ? $skuData[$goods['skuId']]['conversion'] : '';
  1619. }
  1620. }
  1621. return $data;
  1622. }
  1623. /**
  1624. * 查询库存以及成本(*goodsBasicId *warehouseId) 库存不足的商品计入失效
  1625. * (原数据+costPrice+inventoryNum)
  1626. * @param $data
  1627. * @return mixed
  1628. * @throws \Exception
  1629. */
  1630. private function formatSkuInventory($data): ResultWrapper
  1631. {
  1632. $inventorySelectParams = [];
  1633. $skuIds = [];
  1634. foreach ($data as &$goods) {
  1635. $skuIds[] = $goods['skuId'];
  1636. $goods['inventory'] = 0;
  1637. $inventorySelectParams[$goods['shopId']][] = $goods['skuId'];
  1638. }
  1639. unset($goods);
  1640. $objMInventory = new MInventory($this->onlineEnterpriseId, $this->onlineUserId);
  1641. $inventoryMap = [];
  1642. foreach ($inventorySelectParams as $shopId => $skuIds){
  1643. $inventoryResult = $objMInventory->getInventoryByShopIdAndSkuIds($shopId,$skuIds);
  1644. if (!$inventoryResult->isSuccess()) {
  1645. return ResultWrapper::fail($inventoryResult->getData(),$inventoryResult->getErrorCode());
  1646. }
  1647. $inventoryMap[$shopId] = $inventoryResult->getData();
  1648. }
  1649. //获取当前skuIds的主单位,及换算比例
  1650. $skuConversionResult = $this->objMSku->getConversion($skuIds);
  1651. if (!$skuConversionResult->isSuccess()) {
  1652. return ResultWrapper::fail($skuConversionResult->getData(),$skuConversionResult->getErrorCode());
  1653. }
  1654. $skuConversionMapping = $skuConversionResult->getData();
  1655. foreach ($data as $key => &$goods) {
  1656. if(isset($skuConversionMapping[$goods['skuId']])) {
  1657. $thisSku = $skuConversionMapping[$goods['skuId']];
  1658. if (!isset($inventoryMap[$goods['shopId']])) {
  1659. return ResultWrapper::fail('未获取到库存信息', ErrorCode::$paramError);
  1660. }
  1661. $row = $inventoryMap[$goods['shopId']];
  1662. $goods['inventoryNum'] = isset($row[$goods['skuId']]['num']) ? $row[$goods['skuId']]['num'] : 0;
  1663. $goods['costPrice'] = 0;
  1664. $goods['conversion'] = $thisSku['conversion'];//换算比例
  1665. //判断是否开启预售
  1666. if ($goods['isDistribution'] == StatusCode::$delete) {
  1667. //没有开启需要检测库存
  1668. if ($goods['inventoryNum'] < $goods['buyNum']) {
  1669. $goods['isInvalid'] = StatusCode::$delete;
  1670. $goods['invalidMsg'] = '商品库存不足';
  1671. $this->invalidData[] = $goods;
  1672. unset($data[$key]);
  1673. }
  1674. }
  1675. }
  1676. }
  1677. return ResultWrapper::success($data);
  1678. }
  1679. /**
  1680. * Doc: (des="计算运费")
  1681. * User: XMing
  1682. * Date: 2020/9/8
  1683. * Time: 10:10 上午
  1684. * @param array $data
  1685. * @return ResultWrapper
  1686. * @throws \Exception
  1687. */
  1688. private function calExpress(array $data)
  1689. {
  1690. if (empty($data)) return ResultWrapper::success($data);
  1691. if (empty($this->onlineUserDefaultDeliveryType)) {
  1692. return ResultWrapper::success($data);
  1693. }
  1694. //判断选择是否是快递配送方式
  1695. if ($this->onlineUserDefaultDeliveryType != StatusCode::$deliveryType['goodsDelivery']) {
  1696. return ResultWrapper::success($data);
  1697. }
  1698. $map = [];
  1699. foreach ($data as $val) {
  1700. $map[$val['shopId']][] = $val;
  1701. }
  1702. unset($data);
  1703. //收货地址为空,按全国计算
  1704. $data = [];
  1705. foreach ($map as $shopId => $item) {
  1706. $buildDataResult = self::buildExpressData($item, $shopId, $this->onlineUserAddressCode);
  1707. if (!$buildDataResult->isSuccess()) {
  1708. return ResultWrapper::fail($buildDataResult->getData(), $buildDataResult->getErrorCode());
  1709. }
  1710. $data = array_merge($data, $buildDataResult->getData());
  1711. }
  1712. return ResultWrapper::success($data);
  1713. }
  1714. /**
  1715. * Doc: (des="运费计算规则")
  1716. * User: XMing
  1717. * Date: 2020/9/8
  1718. * Time: 11:36 上午
  1719. * @example https://www.xiaokeduo.com/help/11584.html
  1720. * 1.按商品累加运费
  1721. * 规则:分别计算出来商品使用模板的运费,和统一运费的最大值,再进行累加。
  1722. * 1)不同或相同的商品,设置同一运费模板:按该模板设置的规则计算
  1723. * 更新:不足续件数目的时候,仍然按照续件数目进行计算。
  1724. * 例如商品A,B都是用模板M(首件10块,续2件5块),如果购买商品A和B,各一件,则一共购买两件,运费=10+5=15元。
  1725. * @param array $data
  1726. * @param string $code
  1727. * @param int $shopId
  1728. * @throws \Exception
  1729. * @return ResultWrapper
  1730. */
  1731. private function buildExpressData(array $data, int $shopId, $code = null)
  1732. {
  1733. //提取设置了运费模版的模版规则id
  1734. $allRuleIds = [];
  1735. foreach ($data as $item) {
  1736. if ($item['express']['expressType'] == StatusCode::$expressType['rule']) {
  1737. $allRuleIds[] = $item['express']['ruleId'];
  1738. continue;
  1739. }
  1740. }
  1741. $allRuleIds = array_values(array_unique($allRuleIds));
  1742. $roleMap = [];
  1743. $objDDeliveryRule = new DDeliveryRule();
  1744. $objDDeliveryRule->setTable('qianniao_delivery_rule_' . $this->onlineEnterpriseId);
  1745. if (!empty($allRuleIds)) {
  1746. $ruleLists = $objDDeliveryRule->select(['id' => $allRuleIds]);
  1747. if ($ruleLists === false) {
  1748. return ResultWrapper::fail($objDDeliveryRule->error(), ErrorCode::$dberror);
  1749. }
  1750. foreach ($ruleLists as $item) {
  1751. $roleMap[$item['id']] = empty($item['setData']) ? [] : json_decode($item['setData'], true);
  1752. }
  1753. }
  1754. // 查询基础设置-商品设置运费规则
  1755. $objMBasicSetup = new MBasicSetup($this->onlineEnterpriseId);
  1756. $BasicSetupResult = $objMBasicSetup->getBasicField('calculateExpressType');
  1757. if (!$BasicSetupResult->isSuccess()) {
  1758. return ResultWrapper::fail($BasicSetupResult->getData(), $BasicSetupResult->getErrorCode());
  1759. }
  1760. $BasicSetup = $BasicSetupResult->getData();
  1761. $calculateExpressType = isset($BasicSetup['calculateExpressType']) ? $BasicSetup['calculateExpressType'] : StatusCode::$standard;
  1762. switch ($calculateExpressType) {
  1763. case StatusCode::$delete: // 累加运费规则
  1764. $calResult = self::ruleAdd($data, $roleMap, $code, $shopId);
  1765. break;
  1766. case StatusCode::$standard: // 组合运费规则
  1767. $calResult = self::ruleGroup($data, $roleMap, $code, $shopId);
  1768. break;
  1769. default: // 默认使用组合运费规则
  1770. $calResult = self::ruleGroup($data, $roleMap, $code, $shopId);
  1771. break;
  1772. }
  1773. if (!$calResult->isSuccess()) {
  1774. return ResultWrapper::fail($calResult->getData(), $calResult->getErrorCode());
  1775. }
  1776. $data = $calResult->getData();
  1777. return ResultWrapper::success($data);
  1778. }
  1779. /**
  1780. * Doc: (des="(规则一)按商品累加运费")
  1781. * User: XMing
  1782. * Date: 2020/9/25
  1783. * Time: 9:14 上午
  1784. * @param array $data
  1785. * @param array $roleMap
  1786. * @param string $code
  1787. * @return ResultWrapper
  1788. * @example https://www.xiaokeduo.com/help/11584.html
  1789. * 1)不同或相同的商品,设置同一运费模板:按该模板设置的规则计算
  1790. * 更新:不足续件数目的时候,仍然按照续件数目进行计算。
  1791. * 例如商品A,B都是用模板M(首件10块,续2件5块),如果购买商品A和B,各一件,则一共购买两件,运费=10+5=15元。
  1792. * 2)多种商品,分别设置不同金额的统一运费:以最高运费金额收取。
  1793. * 更新:例如 商品A,B,C的统一运费分别为1元,2元和3元,一期购买这三个商品,则运费为3元。
  1794. * 3)不同的商品,设置不同的运费模板:分别计算每个运费模板规则应收运费,再累加计算合计运费;
  1795. * 例如: 例如商品A使用用模板M(首件,10块,续1件,5块),商品B使用模板N(首件,12块,续3件,5块),如果购买商品A和B,各2件,则运费=模板M的运费+模板N的运费=(10+5)+(12+5)=32元。
  1796. * 4)统一运费商品,和运费模板商品一同结算:单独计算统一运费商品应收运费,再累加运费模板应收运费;
  1797. * 例如:商品A和B使用统一运费,分别为2元和10元,商品C使用模板M(首1件,10块,续2件,5块),商品D使用模板N(首件,12块,续3件,5块),购买A,B,C和D各两件。
  1798. * 此时统一运费(商品A和B)=10元;运费模板运费(商品C和D)=模板M的运费+模板N的运费=(10+5)+(12+5)=32元。
  1799. * 则总运费=统一运费+运费模板运费=(10)+(32)=42元。
  1800. */
  1801. private function ruleAdd($data, $roleMap, $code, $shopId)
  1802. {
  1803. //初始化所有运费
  1804. $freeExpressMoney = 0;//免邮费
  1805. $ruleExpressMoney = 0;//模版规则运费
  1806. $unifyExpressMoney = 0;//统一运费
  1807. $ruleIdRelGoodsMap = [];//模版=>[商品数量,商品重量]
  1808. foreach ($data as &$item) {
  1809. //判断运费设置
  1810. switch ($item['express']['expressType']) {
  1811. case StatusCode::$expressType['free']:
  1812. //免邮费
  1813. $freeExpressMoney = bcadd($freeExpressMoney, 0, 2);
  1814. break;
  1815. case StatusCode::$expressType['rule']:
  1816. $ruleId = $item['express']['ruleId'];
  1817. //运费模版
  1818. if (!isset($roleMap[$ruleId])) {
  1819. //运费模版规则不存在,不计算运费
  1820. continue 2;
  1821. }
  1822. if (!empty($code)) {
  1823. //地区
  1824. //先判断商品是否在配送区域
  1825. $notDispatchAreas = $roleMap[$ruleId]['not_dispatch_areas'];
  1826. $isExist = self::searchInArray($code, $notDispatchAreas);
  1827. if ($isExist === true) {
  1828. $item['notExpress'] = StatusCode::$delete;
  1829. continue 2;
  1830. }
  1831. }
  1832. $ruleIdRelGoodsMap[$ruleId]['num'] = (isset($ruleIdRelGoodsMap[$ruleId]['num']) ? $ruleIdRelGoodsMap[$ruleId]['num'] : 0) + $item['buyNum'];
  1833. $ruleIdRelGoodsMap[$ruleId]['weight'] = (isset($ruleIdRelGoodsMap[$ruleId]['weight']) ? $ruleIdRelGoodsMap[$ruleId]['weight'] : 0) + bcmul($item['express']['weight'], $item['buyNum'], 2);
  1834. break;
  1835. case StatusCode::$expressType['unify']:
  1836. //统一运费,求其中最大值
  1837. if ($item['express']['expressFee'] > $unifyExpressMoney) {
  1838. //如果当前值大于目前的最大值,则使用当前的值覆盖
  1839. $unifyExpressMoney = $item['express']['expressFee'];
  1840. }
  1841. break;
  1842. default:
  1843. //没有设置默认不计算运费
  1844. break;
  1845. }
  1846. }
  1847. if (!empty($ruleIdRelGoodsMap)) {
  1848. //计算模版运费
  1849. foreach ($ruleIdRelGoodsMap as $ruleId => $value) {
  1850. $type = $roleMap[$ruleId]['type'];//4:按件 5:按重量
  1851. if (empty($code)) {
  1852. //没有地区code,使用全国统一运费的规则进行计算
  1853. $uniform = $roleMap[$ruleId]['uniform'];
  1854. $expressMoney = self::calculateExpress($type, $uniform, $value);
  1855. $ruleExpressMoney = bcadd($ruleExpressMoney, $expressMoney, 2);
  1856. } else {
  1857. //匹配模版
  1858. $dispatchAreas = $roleMap[$ruleId]['areas'];
  1859. $searchRow = self::searchRowByCode($code, $dispatchAreas);
  1860. if ($searchRow === false) {
  1861. //当前地区没有匹配到,使用全国统一计算
  1862. $uniform = $roleMap[$ruleId]['uniform'];
  1863. $expressMoney = self::calculateExpress($type, $uniform, $value);
  1864. $ruleExpressMoney = bcadd($ruleExpressMoney, $expressMoney, 2);
  1865. } else {
  1866. //匹配到了规则
  1867. $rule = [
  1868. 'first_num' => $searchRow['first_num'],
  1869. 'first_price' => $searchRow['first_price'],
  1870. 'second_num' => $searchRow['second_num'],
  1871. 'second_price' => $searchRow['second_price']
  1872. ];
  1873. $expressMoney = self::calculateExpress($type, $rule, $value);
  1874. $ruleExpressMoney = bcadd($ruleExpressMoney, $expressMoney, 2);
  1875. }
  1876. }
  1877. }
  1878. }
  1879. $this->expressShopMoney[$shopId] = bcadd($ruleExpressMoney, $unifyExpressMoney, 2);
  1880. $this->expressMoney = bcadd($this->expressMoney, $this->expressShopMoney[$shopId], 2);
  1881. return ResultWrapper::success($data);
  1882. }
  1883. /**
  1884. * Doc: (des=" (规则二) 组合运费")
  1885. * User: XMing
  1886. * Date: 2020/9/25
  1887. * Time: 12:03 下午
  1888. * @param array $data
  1889. * @param array $roleMap
  1890. * @param string $code
  1891. * @return ResultWrapper
  1892. * @example https://www.xiaokeduo.com/help/11584.html
  1893. * 规则:先将使用统一运费和运费模板的商品分开计算,再取二者较大的值,作为最终运费。
  1894. * 1)计算使用运费模板的运费价格x:取所有商品中,首件金额最大的运费模板,计算使用该模板的所有商品运费;
  1895. * 使用其他模板的所有商品都按照该商品所试用的续件金额来计算;
  1896. * 最后再求和。
  1897. * 2)计算使用统一运费的商品运费y:取最大的统一运费。
  1898. * 3)比较x和y,运费=x和y的较大值。
  1899. * 例如:
  1900. * 1)不同或相同的商品,设置同一运费模板:同按商品累加运费的计算方式,按该模板设置的规则计算。
  1901. * 例如商品A,B都是用模板M(首件10块,续2件5块),如果购买商品A和B,各一件,则一共购买两件,运费=10+5=15元。
  1902. * 2)多种商品,分别设置不同金额的统一运费:同按商品累加运费的计算方式,以最高运费金额收取。
  1903. * 例如 商品A,B,C的统一运费分别为1元,2元和3元,一起购买这三个商品,则运费为3元。
  1904. * 3)不同的商品,设置不同的运费模板:不同于按照商品累加的计算方式。
  1905. * 例如: 例如商品A(1件)使用用模板M(首1件,10块,续1件,5块),商品B(1件)使用模板N(首2件,12块,续1件,5块),如果购买商品A和B,各2件,则运费=模板N的运费(首费为12,大于模板M的10)+模板M的运费(按照续费计算)=(12)+(5*2)=22元。
  1906. * 如果按商品累加计费,则运费=模板M的运费+模板N的运费=(10+5)+(12)=27元。
  1907. * 4)统一运费商品,和运费模板商品一同结算:单独计算统一运费和运费模板,再取较大的数值作为最终运费;
  1908. * 例如:商品A和B使用统一运费,分别为2元和10元,商品C使用模板M(首1件,10块,续2件,5块),商品D(1件)使用模板N(首2件,12块,续1件,5块),购买A,B,C和D各两件。
  1909. * 此时统一运费(商品A和B)=10元;运费模板运费(商品C和D)=模板N的运费(首费为12,大于模板M的10)+模板M的运费(按照续费计算)=(12)+(5*2)=22元。总运费=统一运费和运费模板运费的较大值=22元。
  1910. * 如果按商品累加计费,运费按照之前的计算=统一运费+运费模板运费=(10)+(27)=37元。
  1911. */
  1912. private function ruleGroup($data, $roleMap, $code, $shopId)
  1913. {
  1914. //初始化所有运费
  1915. $freeExpressMoney = 0;//免邮费
  1916. $ruleExpressMoney = 0;//模版规则运费
  1917. $unifyExpressMoney = 0;//统一运费
  1918. $ruleIdRelGoodsMap = [];//模版=>[商品数量,商品重量]
  1919. foreach ($data as &$item) {
  1920. //判断运费设置
  1921. switch ($item['express']['expressType']) {
  1922. case StatusCode::$expressType['free']:
  1923. //免邮费
  1924. $freeExpressMoney = bcadd($freeExpressMoney, 0, 2);
  1925. break;
  1926. case StatusCode::$expressType['rule']:
  1927. $ruleId = $item['express']['ruleId'];
  1928. //运费模版
  1929. if (!isset($roleMap[$ruleId])) {
  1930. //运费模版规则不存在,不计算运费
  1931. continue 2;
  1932. }
  1933. if (!empty($code)) {
  1934. //地区
  1935. //先判断商品是否在配送区域
  1936. $notDispatchAreas = $roleMap[$ruleId]['not_dispatch_areas'];
  1937. $isExist = self::searchInArray($code, $notDispatchAreas);
  1938. if ($isExist === true) {
  1939. $item['notExpress'] = StatusCode::$delete;
  1940. continue 2;
  1941. }
  1942. }
  1943. $ruleIdRelGoodsMap[$ruleId]['num'] = (isset($ruleIdRelGoodsMap[$ruleId]['num']) ? $ruleIdRelGoodsMap[$ruleId]['num'] : 0) + $item['buyNum'];
  1944. $ruleIdRelGoodsMap[$ruleId]['weight'] = (isset($ruleIdRelGoodsMap[$ruleId]['weight']) ? $ruleIdRelGoodsMap[$ruleId]['weight'] : 0) + bcmul($item['express']['weight'], $item['buyNum'], 2);
  1945. break;
  1946. case StatusCode::$expressType['unify']:
  1947. //统一运费,求其中最大值
  1948. if ($item['express']['expressFee'] > $unifyExpressMoney) {
  1949. //如果当前值大于目前的最大值,则使用当前的值覆盖
  1950. $unifyExpressMoney = $item['express']['expressFee'];
  1951. }
  1952. break;
  1953. default:
  1954. //没有设置默认不计算运费
  1955. break;
  1956. }
  1957. }
  1958. //选获取模版规则中首件的最高价格,然后减去首件的都按续件处理
  1959. //唯一的区别,多个运费模版首费只算一次
  1960. if (!empty($ruleIdRelGoodsMap)) {
  1961. //计算模版运费
  1962. foreach ($ruleIdRelGoodsMap as $ruleId => &$value) {
  1963. $value['type'] = $roleMap[$ruleId]['type'];//4:按件 5:按重量
  1964. if (empty($code)) {
  1965. //没有地区code,使用全国统一运费的规则进行计算
  1966. $uniform = $roleMap[$ruleId]['uniform'];
  1967. $value['rule'] = $uniform;
  1968. } else {
  1969. //匹配模版
  1970. $dispatchAreas = $roleMap[$ruleId]['areas'];
  1971. $searchRow = self::searchRowByCode($code, $dispatchAreas);
  1972. if ($searchRow === false) {
  1973. //当前地区没有匹配到,使用全国统一计算
  1974. $uniform = $roleMap[$ruleId]['uniform'];
  1975. $value['rule'] = $uniform;
  1976. } else {
  1977. //匹配到了规则
  1978. $rule = [
  1979. 'first_num' => $searchRow['first_num'],
  1980. 'first_price' => $searchRow['first_price'],
  1981. 'second_num' => $searchRow['second_num'],
  1982. 'second_price' => $searchRow['second_price']
  1983. ];
  1984. $value['rule'] = $rule;
  1985. }
  1986. }
  1987. }
  1988. //使用组合运费计算规则,计算运费
  1989. $ruleExpressMoney = self::calculateExpressGroup($ruleIdRelGoodsMap);
  1990. }
  1991. $this->expressShopMoney[$shopId] = bcadd($ruleExpressMoney, $unifyExpressMoney, 2);
  1992. $this->expressMoney = bcadd($this->expressMoney, $this->expressShopMoney[$shopId], 2);
  1993. return ResultWrapper::success($data);
  1994. }
  1995. /**
  1996. * Doc: (des="以组合运费方式计算快递费用")
  1997. * User: XMing
  1998. * Date: 2020/9/25
  1999. * Time: 2:44 下午
  2000. * @param array $data
  2001. * @return float
  2002. */
  2003. private static function calculateExpressGroup($data)
  2004. {
  2005. $ruleExpressMoney = 0;
  2006. $max_first_price = 0;
  2007. $max_rule_id = 0;
  2008. foreach ($data as $ruleId => $item) {
  2009. //获取首费用最高的
  2010. if ($item['rule']['first_price'] > $max_first_price) {
  2011. $max_first_price = $item['rule']['first_price'];
  2012. $max_rule_id = $ruleId;
  2013. }
  2014. }
  2015. unset($item);
  2016. foreach ($data as $ruleId => &$item) {
  2017. if ($ruleId != $max_rule_id) {
  2018. continue;
  2019. }
  2020. //减去首重的件/重量
  2021. switch ($item['type']) {
  2022. case StatusCode::$delete:
  2023. //剩余续件的
  2024. $num = bcsub($item['num'], $item['rule']['first_num'], 2);
  2025. $item['num'] = ($num < 0) ? 0 : $num;
  2026. break;
  2027. case StatusCode::$standard:
  2028. //剩余续重的
  2029. $weight = bcsub($item['weight'], $item['rule']['first_num'], 2);
  2030. $item['weight'] = ($weight < 0) ? 0 : $weight;
  2031. break;
  2032. }
  2033. }
  2034. unset($item);
  2035. //剩下的都是续
  2036. foreach ($data as $ruleId => $item) {
  2037. switch ($item['type']) {
  2038. case StatusCode::$delete:
  2039. $mod = ($item['rule']['second_num'] == 0) ? 0 : ceil($item['num'] / $item['rule']['second_num']);
  2040. $expressMoney = bcmul($mod, $item['rule']['second_price'], 2);
  2041. $ruleExpressMoney = bcadd($ruleExpressMoney, $expressMoney, 2);
  2042. break;
  2043. case StatusCode::$standard:
  2044. $mod = ($item['rule']['second_num'] == 0) ? 0 : ceil($item['weight'] / $item['rule']['second_num']);
  2045. $expressMoney = bcmul($mod, $item['rule']['second_price'], 2);
  2046. $ruleExpressMoney = bcadd($ruleExpressMoney, $expressMoney, 2);
  2047. break;
  2048. }
  2049. unset($expressMoney);
  2050. unset($mod);
  2051. }
  2052. //最高首费用 + 各自模版续运费
  2053. return bcadd($max_first_price, $ruleExpressMoney, 2);
  2054. }
  2055. /**
  2056. * Doc: (des="根据模版规则,去计算运费")
  2057. * User: XMing
  2058. * Date: 2020/9/25
  2059. * Time: 9:42 上午
  2060. * @param int $type
  2061. * @param array $rule
  2062. * @param array $item
  2063. * @return float
  2064. */
  2065. private static function calculateExpress($type, $rule, $item)
  2066. {
  2067. switch ($type) {
  2068. case StatusCode::$delete:
  2069. //按件
  2070. //1.按件
  2071. if ($item['num'] <= $rule['first_num']) return $rule['first_price'];
  2072. $mod = ($rule['second_num'] == 0) ? 0 : ceil(($item['num'] - $rule['first_num']) / $rule['second_num']);
  2073. return bcadd($rule['first_price'], bcmul($mod, $rule['second_price'], 2), 2);
  2074. break;
  2075. case StatusCode::$standard:
  2076. //按重量
  2077. $thisWeight = bcmul($item['weight'], $item['num'], 2);
  2078. if ($thisWeight <= $rule['first_num']) return $rule['first_price'];
  2079. $mod = ($rule['second_num'] == 0) ? 0 : ceil(($thisWeight - $rule['first_num']) / $rule['second_num']);
  2080. return bcadd($rule['first_price'], bcmul($mod, $rule['second_price'], 2), 2);
  2081. break;
  2082. default:
  2083. break;
  2084. }
  2085. }
  2086. /**
  2087. * Doc: (des="")
  2088. * User: XMing
  2089. * Date: 2020/9/8
  2090. * Time: 3:29 下午
  2091. * @param array $data
  2092. * @param string $code
  2093. * @return array|boolean
  2094. */
  2095. public function searchRowByCode($code, $data)
  2096. {
  2097. $arr = explode('-', $code);
  2098. $provinceCode = isset($arr[0]) ? $arr[0] : 0;
  2099. $cityCode = isset($arr[1]) ? $arr[1] : 0;
  2100. $areaCode = isset($arr[2]) ? $arr[2] : 0;
  2101. foreach ($data as $item) {
  2102. $allProvinces = isset($item['provinces']) ? $item['provinces'] : [];
  2103. $allCitys = isset($item['citys']) ? $item['citys'] : [];
  2104. $allAreas = isset($item['areas']) ? $item['areas'] : [];
  2105. //查看当前省是否存在,如果省不存在市肯定不存在
  2106. if (!in_array($provinceCode, $allProvinces)) {
  2107. continue;
  2108. }
  2109. //查看市是否存在,如果市不存在区肯定不存在
  2110. if (!in_array($cityCode, $allCitys)) {
  2111. continue;
  2112. }
  2113. if (!in_array($areaCode, $allAreas)) {
  2114. continue;
  2115. }
  2116. //存在返回此条数据
  2117. return $item;
  2118. }
  2119. return false;
  2120. }
  2121. /**
  2122. * Doc: (des="查询某只是否在数组中")
  2123. * User: XMing
  2124. * Date: 2020/9/8
  2125. * Time: 3:06 下午
  2126. * @param array $data
  2127. * @param string $code
  2128. * @return boolean 不存在返回false 存在返回true
  2129. */
  2130. public function searchInArray($code, $data)
  2131. {
  2132. $arr = explode('-', $code);
  2133. $provinceCode = isset($arr[0]) ? $arr[0] : 0;
  2134. $cityCode = isset($arr[1]) ? $arr[1] : 0;
  2135. $areaCode = isset($arr[2]) ? $arr[2] : 0;
  2136. $allProvinces = isset($data['provinces']) ? $data['provinces'] : [];
  2137. $allCitys = isset($data['citys']) ? $data['citys'] : [];
  2138. $allAreas = isset($data['areas']) ? $data['areas'] : [];
  2139. //查看当前省是否存在,如果省不存在市肯定不存在
  2140. if (!in_array($provinceCode, $allProvinces)) {
  2141. return false;
  2142. }
  2143. //查看市是否存在,如果市不存在区肯定不存在
  2144. if (!in_array($cityCode, $allCitys)) {
  2145. return false;
  2146. }
  2147. if (!in_array($areaCode, $allAreas)) {
  2148. return false;
  2149. }
  2150. return true;
  2151. }
  2152. /**
  2153. * 获取商品价格,及计算价格 ,(有促销活动的用促销价)
  2154. * (原数据+price+originPrice+preferential+totalMoney)
  2155. * @param $data
  2156. * @return ResultWrapper
  2157. * @throws \Exception
  2158. */
  2159. private function formatPrice($data)
  2160. {
  2161. if (empty($data)) return ResultWrapper::success($data);
  2162. $objMPrice = new MPrice($this->onlineUserId, $this->onlineEnterpriseId);
  2163. $objMActivity = new MActivity($this->onlineUserId, $this->onlineEnterpriseId);
  2164. $objMCustomer = new MCustomer($this->onlineEnterpriseId, $this->onlineUserId);
  2165. $selectParam['material'] = [];
  2166. $allGoodsId = [];
  2167. foreach ($data as &$goods) {
  2168. $allGoodsId[] = $goods['goodsId'];
  2169. $selectParam['material'][$goods['shopId']][] = $goods['goodsId'];
  2170. $goods['originPrice'] = 0;//原价
  2171. $goods['price'] = 0;//单价
  2172. $goods['preferential'] = 0;//优惠差价
  2173. $goods['totalMoney'] = 0;//小计(unitPrice*buyNum)
  2174. $goods['activityMoney'] = 0;//互斥活动商品总金额
  2175. $goods['isMutex'] = StatusCode::$delete;//初始化互斥状态
  2176. }
  2177. unset($goods);
  2178. // 查询价格
  2179. $selectParam['customerId'] = $this->customerId;
  2180. $dbResult = $objMPrice->getPrice($selectParam);
  2181. if (!$dbResult->isSuccess()) {
  2182. return ResultWrapper::fail($dbResult->getData(), $dbResult->getErrorCode());
  2183. }
  2184. $priceResult = $dbResult->getData();
  2185. unset($dbResult);
  2186. $customerResult = $objMCustomer->getCustomerInfoByUserCenterId($this->onlineUserId);
  2187. $activityArr = [];
  2188. if ($customerResult->isSuccess()) {
  2189. $customer = $customerResult->getData();
  2190. //获取促销活动价格
  2191. $dbResult = $objMActivity->getActivity([
  2192. 'goodsId' => implode(',', $allGoodsId),
  2193. 'customerType' => isset($customer['type']) ? $customer['type'] : 0,
  2194. ]);//TODO
  2195. $activityResult = $dbResult->getData();
  2196. unset($dbResult);
  2197. foreach ($activityResult as $activity) {
  2198. $activityArr[$activity['goodsId'] . $activity['skuId']] = $activity;
  2199. }
  2200. }
  2201. //商品价格信息
  2202. $goodsArr = [];
  2203. foreach ($priceResult as $shopPriceArr) {
  2204. foreach ($shopPriceArr as $goodsId => $goodsSkuArr) {
  2205. $goodsArr[$goodsId] = $goodsSkuArr;
  2206. }
  2207. }
  2208. // 查询当前用户是否购买过vip会员卡
  2209. $enableMemberPrice = false;
  2210. $objMVipCard = new MVipCard($this->onlineEnterpriseId, $this->onlineUserId, true);
  2211. $dbResult = $objMVipCard->getAllVipCardByCustomerId($this->customerId);
  2212. if (!$dbResult->isSuccess()) {
  2213. return ResultWrapper::fail($dbResult->getData(), ErrorCode::$dberror);
  2214. }
  2215. $vipCard = $dbResult->getData();
  2216. if (!empty($vipCard)){
  2217. foreach ($vipCard as $key => $value){
  2218. if($value['memberSpecialPrice'] == StatusCode::$standard){
  2219. $enableMemberPrice = true;
  2220. }
  2221. }
  2222. }
  2223. foreach ($data as &$goods) {
  2224. //此商品存在促销价
  2225. if (isset($activityArr[$goods['goodsId'] . $goods['skuId']])) {
  2226. $price = $goodsArr[$goods['goodsId']][$goods['skuId']]['salePrice'];//商品正常销售价格
  2227. //从缓存中获取活动详情
  2228. if (isset($goods['activityId']) && !empty($goods['activityId'])) {
  2229. $price = $activityArr[$goods['goodsId'] . $goods['skuId']]['price'];//商品活动价格
  2230. $activityDetail = $this->objActivityLimitCache->getActivity($goods['activityId']);
  2231. if (!empty($activityDetail) && $activityDetail['isMutex'] == StatusCode::$standard) {
  2232. //开启互斥商品的总金额
  2233. $goods['activityMoney'] = bcmul($price, $goods['buyNum'], 2);
  2234. $goods['isMutex'] = StatusCode::$standard;
  2235. }
  2236. }
  2237. } else {
  2238. if (isset($goodsArr[$goods['goodsId']][$goods['skuId']])) {
  2239. $priceInfo = $goodsArr[$goods['goodsId']][$goods['skuId']];//此商品规格的价格信息
  2240. $price = isset($priceInfo['salePrice']) ? $priceInfo['salePrice'] : 0;
  2241. // 会员价 启用会员价不生效阶梯价了
  2242. $memberPrice = getArrayItem($priceInfo, 'memberPrice', 0);
  2243. if($enableMemberPrice && $memberPrice){
  2244. $price = $memberPrice;
  2245. }else{
  2246. // 开启阶梯价
  2247. if ($priceInfo['enabledLadder']) {
  2248. foreach ($priceInfo['ladderPrice'] as $ladder) {
  2249. $ladder['to'] = empty($ladder['to']) ? 9999 : $ladder['to'];
  2250. if ($goods['buyNum'] >= $ladder['from'] && $goods['buyNum'] <= $ladder['to']) {
  2251. $price = isset($ladder['price']) ? $ladder['price'] : 0;
  2252. continue;
  2253. }
  2254. }
  2255. }
  2256. }
  2257. }
  2258. }
  2259. if (!isset($price)) $price = 0;
  2260. $price = floatval($price);
  2261. $goods['price'] = sprintf("%.2f", $price);
  2262. $goods['originPrice'] = sprintf("%.2f", $price);//没有优惠活动暂时原价=销售价
  2263. $goods['preferential'] = 0;//优惠券优惠金额
  2264. $goods['totalMoney'] = bcmul($price, $goods['buyNum'], 2);//商品小计金额
  2265. $goods['vipDiscount'] = 0;//会员卡优惠金额
  2266. $goods['setNum'] = isset( $goodsArr[$goods['goodsId']][$goods['skuId']]['setNum'] ) ? $goodsArr[$goods['goodsId']][$goods['skuId']]['setNum'] : 0;//起定数量
  2267. if (empty($goods['totalMoney'])) {
  2268. return ResultWrapper::fail('计算商品价格时出现异常', ErrorCode::$paramError);
  2269. }
  2270. }
  2271. return ResultWrapper::success($data);
  2272. }
  2273. /**
  2274. * 数据分组
  2275. *
  2276. * @param $data
  2277. * @param null $userCouponId
  2278. * @param null $vipCardId
  2279. * @return ResultWrapper
  2280. */
  2281. private function formatGroup($data, $userCouponId = null, $vipCardId = null)
  2282. {
  2283. static $checkNum = 0;//购物车中选中商品数量
  2284. static $totalMoney = 0;//原总额
  2285. static $payMoney = 0;//实际支付金额
  2286. static $preferential = 0;//优惠券优惠
  2287. static $cartNum = 0;
  2288. static $goodsNum = 0;//购物车商品数量
  2289. static $vipDiscount = 0;//会员卡优惠金额
  2290. static $vipDoubleDiscount = 0;//会员卡折上折优惠金额
  2291. static $activityMoney = 0;//互斥活动商品总金额
  2292. static $expressMoney = 0;//运费
  2293. $goodsData = [];
  2294. foreach ($data as $key => $val) {
  2295. if (!isset($val['expressMoney'])) {
  2296. $val['expressMoney'] = '0.00';
  2297. }
  2298. $shopGoodsData[$val['shopId']][] = $val;
  2299. if (!isset($goodsData[$val['shopId']]['totalMoney'])) {
  2300. $goodsData[$val['shopId']]['totalMoney'] = '0.00';
  2301. }
  2302. if (!isset($goodsData[$val['shopId']]['preferential'])) {
  2303. $goodsData[$val['shopId']]['preferential'] = '0.00';
  2304. }
  2305. if (!isset($goodsData[$val['shopId']]['expressMoney'])) {
  2306. $goodsData[$val['shopId']]['expressMoney'] = '0.00';
  2307. }
  2308. $goodsData[$val['shopId']] = [
  2309. 'shopId' => $val['shopId'],
  2310. 'shopName' => $val['shopName'],
  2311. 'shopLogo' => $val['shopLogo'],
  2312. 'expressMoney' => isset($this->expressShopMoney[$val['shopId']]) ? $this->expressShopMoney[$val['shopId']] : 0,
  2313. 'totalMoney' => bcadd($goodsData[$val['shopId']]['totalMoney'], $val['totalMoney'], 2),//(购物车总金额)
  2314. 'preferential' => bcadd($goodsData[$val['shopId']]['preferential'], bcmul($val['preferential'], $val['buyNum']), 2),//店铺总优惠金额
  2315. 'payMoney' => bcadd((isset($this->expressShopMoney[$val['shopId']]) ? $this->expressShopMoney[$val['shopId']] : 0), bcsub(bcadd($goodsData[$val['shopId']]['totalMoney'], $val['totalMoney']), bcadd($goodsData[$val['shopId']]['preferential'], bcmul($val['preferential'], $val['buyNum'])), 2), 2),//(购物实付金额)
  2316. 'shopGoodsData' => $shopGoodsData[$val['shopId']],//商品数据
  2317. ];
  2318. //统计选中商品总数
  2319. if ($val['selection'] == StatusCode::$standard) {
  2320. $checkNum = bcadd($checkNum, 1);
  2321. $totalMoney = bcadd($totalMoney, $val['totalMoney'], 2);
  2322. $payMoney = bcadd(0, bcadd($payMoney, bcsub($val['totalMoney'], bcmul($val['buyNum'], $val['preferential'])), 2), 2);
  2323. $preferential = bcadd($preferential, bcmul($val['buyNum'], $val['preferential']), 2);
  2324. $activityMoney = bcadd($activityMoney, $val['activityMoney'], 2);//互斥商品总金额
  2325. }
  2326. $cartNum = bcadd($cartNum, 1);
  2327. $goodsNum = bcadd($goodsNum, $val['buyNum']);
  2328. }
  2329. $result = [
  2330. 'totalMoney' => $totalMoney,
  2331. 'payMoney' => $payMoney,
  2332. 'preferential' => $preferential,
  2333. 'vipDiscount' => $vipDiscount,
  2334. 'vipDoubleDiscount' => $vipDoubleDiscount,
  2335. 'activityMoney' => $activityMoney,
  2336. 'expressMoney' => $this->expressMoney,
  2337. 'checkNum' => (int)$checkNum,
  2338. 'cartNum' => (int)$cartNum,
  2339. 'goodsNum' => (int)$goodsNum,
  2340. 'goodsData' => array_values($goodsData),
  2341. 'invalidData' => array_values($this->invalidData)
  2342. ];
  2343. $objMPreferentialProcess = new MPreferentialProcess($this->onlineUserId, $this->onlineEnterpriseId);
  2344. $result = $objMPreferentialProcess->Preferential($result, $vipCardId, $userCouponId);
  2345. if(!$result->isSuccess()){
  2346. return ResultWrapper::fail($result->getData(), $result->getErrorCode());
  2347. }
  2348. $result = $result->getData();
  2349. if (
  2350. (!empty($vipCardId) && $vipCardId != 0)
  2351. || (!empty($userCouponId) && $userCouponId != 0)
  2352. ) {
  2353. //优惠券优惠,会员卡优惠分摊到商品
  2354. $result = self::calAvg($result);
  2355. }
  2356. //处理运费
  2357. $result = self::freeExpressPrice($result);
  2358. $result['expressMoney'] != 0 && $result['payMoney'] = bcadd($result['payMoney'], $result['expressMoney'], 2);
  2359. return ResultWrapper::success($result);
  2360. }
  2361. /**
  2362. * Doc: (des="")
  2363. * User: XMing
  2364. * Date: 2020/9/18
  2365. * Time: 2:56 下午
  2366. * @param $result
  2367. * @return mixed
  2368. */
  2369. private function freeExpressPrice($result)
  2370. {
  2371. $expressMoney = $result['expressMoney'];
  2372. $payMoney = $result['payMoney'];
  2373. $goodsData = $result['goodsData'];
  2374. if ($expressMoney != 0 && $this->freeExpressPrice <= $payMoney && $this->freeExpressPrice != 0) {
  2375. foreach ($goodsData as &$datum) {
  2376. $shopExpressMoney = $datum['expressMoney'];
  2377. $datum['expressMoney'] = '0.00';
  2378. $datum['payMoney'] = bcsub($datum['payMoney'], $shopExpressMoney, 2);
  2379. }
  2380. $expressMoney = '0.00';
  2381. }
  2382. $result['goodsData'] = $goodsData;
  2383. $result['expressMoney'] = $expressMoney;
  2384. return $result;
  2385. }
  2386. /**
  2387. *
  2388. * 优惠计算分摊规则
  2389. * A商品 100元
  2390. * B商品 300元
  2391. * 商品总价 400元
  2392. *
  2393. * A商品享受的优惠 = 优惠券金额*(A商品原价/商品总价)
  2394. * B商品享受的优惠 = 优惠券金额*(B商品原价/商品总价)
  2395. *
  2396. * 优惠券(满100减20) -20
  2397. * 共 -220
  2398. * 实际付款 180
  2399. * A总(220*(100/400)=55) 优惠券(20*(100/400)=5)
  2400. * B总(220*(300/400)=165) 优惠券(20*(300/400)=15)
  2401. * @param $data
  2402. * @return mixed
  2403. */
  2404. private function calAvg($data)
  2405. {
  2406. $account = 0; // 优惠劵优惠金额
  2407. foreach ($data['goodsData'] as &$shops) {
  2408. $count = count($shops['shopGoodsData']);
  2409. foreach ($shops['shopGoodsData'] as $key => &$goods) {
  2410. // 是否为互斥商品
  2411. if ($goods['isMutex'] == StatusCode::$delete) {
  2412. //活动商品不分摊
  2413. if ($data['preferential'] != 0) {
  2414. //有优惠券优惠,分摊金额
  2415. if ($key + 1 != $count) {
  2416. $goods['preferential'] = bcmul($data['preferential'], bcdiv($goods['totalMoney'], $data['totalMoney'], 2), 2);
  2417. $account = bcadd($account, $goods['preferential'], 2);
  2418. } else {
  2419. //最后一个商品分摊金额=总优惠-其他均摊
  2420. $goods['preferential'] = bcsub($data['preferential'], $account, 2);
  2421. }
  2422. }
  2423. }
  2424. if ($data['vipDiscount'] != 0) {
  2425. /**
  2426. * A商品 10 B商品 20 合计30元 折扣1折 A打折9 B打折18 总折扣27
  2427. * A商品折扣金额 = 10 - (10 X (30 - 27)) / 30
  2428. */
  2429. // 会员卡总优惠金额 = 会员卡优惠后的总金额 = 订单实际支付金额
  2430. $vipCardEndDiscountMoney = bcsub($data['totalMoney'],$data['vipDiscount'], 2);
  2431. // 当前商品优惠后的金额 = 当前商品价格 * 会员卡优惠后总金额 / 订单实际支付金额
  2432. $goodsVipCardEndDiscountMoney = bcdiv(bcmul($goods['totalMoney'],$vipCardEndDiscountMoney,2), $data['totalMoney'],2);
  2433. // 当前商品优惠金额 = 当前商品价格 - 当前商品优惠后的金额
  2434. $goods['vipDiscount'] = bcsub($goods['totalMoney'], $goodsVipCardEndDiscountMoney, 2);
  2435. }
  2436. }
  2437. }
  2438. return $data;
  2439. }
  2440. /**
  2441. * 后台--清空用户购物车
  2442. */
  2443. public function clearCart()
  2444. {
  2445. $where['userCenterId'] = $this->onlineUserId;
  2446. $dbResult = $this->objDCart->delete($where);
  2447. if ($dbResult === false) {
  2448. return ResultWrapper::fail($this->objDCart->error(), ErrorCode::$dberror);
  2449. }
  2450. return ResultWrapper::success($dbResult);
  2451. }
  2452. /**
  2453. * 批量删除购物车商品
  2454. * @param $params
  2455. * @return ResultWrapper
  2456. */
  2457. public function clearCartByGoodsId($params)
  2458. {
  2459. $where['userCenterId'] = $this->onlineUserId;
  2460. $where['goodsId'] = $params['goodsId'];
  2461. $dbResult = $this->objDCart->delete($where);
  2462. if ($dbResult === false) {
  2463. return ResultWrapper::fail($this->objDCart->error(), ErrorCode::$dberror);
  2464. }
  2465. return ResultWrapper::success($dbResult);
  2466. }
  2467. /**
  2468. * 确认订单
  2469. *
  2470. * @param int $userCouponId 优惠券id
  2471. * @param string $vipCardId 会员卡id
  2472. * @param string $addressId
  2473. * @param string $deliveryId
  2474. * @return ResultWrapper
  2475. * @throws \Exception
  2476. */
  2477. public function confirmationOfOrder($userCouponId = '', $vipCardId = '', $addressId = '', $deliveryId = '')
  2478. {
  2479. // 获取客户信息
  2480. $objMCustomer = new MCustomer($this->onlineEnterpriseId, $this->onlineUserId);
  2481. $result = $objMCustomer->getCustomerData(['userCenterId'=>$this->onlineUserId],'*', true);
  2482. if(!$result->isSuccess()){
  2483. return ResultWrapper::fail($result->getData(), $result->getErrorCode());
  2484. }
  2485. $customerData = $result->getData();
  2486. if (empty($customerData)) {
  2487. return ResultWrapper::fail('未找到客户信息', ErrorCode::$paramError);
  2488. }
  2489. $customerId = $customerData['id'];
  2490. // 获取客户的默认收货地址
  2491. $objMShippingAddress = new MShippingAddress($this->onlineEnterpriseId);
  2492. $selectParams = [
  2493. 'deleteStatus' => StatusCode::$standard,
  2494. 'defaultStatus' => StatusCode::$standard,
  2495. 'customerId' => $customerId
  2496. ];
  2497. $dbResult = $objMShippingAddress->getShippingAddressInfo($selectParams);
  2498. if (!$dbResult->isSuccess()) {
  2499. return ResultWrapper::fail($dbResult->getData(), ErrorCode::$dberror);
  2500. }
  2501. $addRessData = $dbResult->getData();
  2502. unset($dbResult);
  2503. // 获取自提点
  2504. $objMDeliverySetting = new MDeliverySetting($this->onlineUserId, $this->onlineEnterpriseId);
  2505. $selfExpressRuleResult = $objMDeliverySetting->getAllSelfExpressRule();
  2506. if (!$selfExpressRuleResult->isSuccess()) {
  2507. return ResultWrapper::fail($selfExpressRuleResult->getData(), $selfExpressRuleResult->getErrorCode());
  2508. }
  2509. $selfExpressRule = $selfExpressRuleResult->getData();
  2510. //支付方式
  2511. $objMEnterpriseBindPayment = new MEnterpriseBindPayment($this->onlineUserId, $this->onlineEnterpriseId);
  2512. $paymentData = $objMEnterpriseBindPayment->getPaymentType();
  2513. if (!$paymentData->isSuccess()) {
  2514. $paymentData = [];
  2515. } else {
  2516. $paymentData = $paymentData->getData();
  2517. }
  2518. //配送方式
  2519. $objMDeliverySetting = new MDeliverySetting($this->onlineUserId, $this->onlineEnterpriseId);
  2520. $DeliveryData = $objMDeliverySetting->allDelivery();
  2521. if (!$DeliveryData->isSuccess()) {
  2522. $DeliveryData = [];
  2523. } else {
  2524. $DeliveryData = array_values($DeliveryData->getData()['data']);
  2525. }
  2526. //当前用户选择的配送方式
  2527. if (!empty($deliveryId)) {
  2528. $this->onlineUserDefaultDeliveryType = $deliveryId;
  2529. } else {
  2530. if (!empty($DeliveryData)) {
  2531. foreach ($DeliveryData as $value) {
  2532. if ($value['defaultStatus'] == StatusCode::$standard) {
  2533. $this->onlineUserDefaultDeliveryType = $value['deliveryType'];
  2534. break;
  2535. }
  2536. }
  2537. }
  2538. }
  2539. //当前用户的收货地址code 省-市-区
  2540. if (!empty($addressId)) {
  2541. $addressResult = $objMShippingAddress->getShippingAddressInfo(['id' => $addressId]);
  2542. if (!$addressResult->isSuccess()) {
  2543. return ResultWrapper::fail($addressResult->getData(), ErrorCode::$dberror);
  2544. }
  2545. $addressInfo = $addressResult->getData();
  2546. if (!empty($addressInfo)) {
  2547. $this->onlineUserAddressCode = $addressInfo['provinceCode'] . '-' . $addressInfo['cityCode'] . '-' . $addressInfo['districtCode'];
  2548. }
  2549. } else {
  2550. if (!empty($addRessData)) {
  2551. $this->onlineUserAddressCode = $addRessData['provinceCode'] . '-' . $addRessData['cityCode'] . '-' . $addRessData['districtCode'];
  2552. }
  2553. }
  2554. //获取当前企业设置的包邮价格
  2555. $objMBasicSetup = new MBasicSetup($this->onlineEnterpriseId);
  2556. $freeExpressPriceResult = $objMBasicSetup->getBasicField('freeExpressPrice');
  2557. if (!$freeExpressPriceResult->isSuccess()) {
  2558. return ResultWrapper::fail($freeExpressPriceResult->getData(), $freeExpressPriceResult->getData());
  2559. }
  2560. $freeExpressPrice = $freeExpressPriceResult->getData();
  2561. $this->freeExpressPrice = (isset($freeExpressPrice['freeExpressPrice']) && !empty($freeExpressPrice['freeExpressPrice'])) ? $freeExpressPrice['freeExpressPrice'] : 0;
  2562. //获取购物车选中的数据
  2563. $dbResult = $this->objDCart->select(['selection' => StatusCode::$standard, 'userCenterId' => $this->onlineUserId], 'id,goodsId,goodsCode,buyNum,shopId,goodsBasicId,selection,skuId,warehouseId,activityId', 'createTime DESC');
  2564. if ($dbResult === false) {
  2565. return ResultWrapper::fail($this->objDCart->error(), ErrorCode::$dberror);
  2566. }
  2567. if (empty($dbResult)) {
  2568. return ResultWrapper::fail('购物车中没有选定的商品', ErrorCode::$paramError);
  2569. }
  2570. //所有结算商品id
  2571. $goodsIds = array_values(array_column($dbResult, 'goodsId'));
  2572. $cartDataResult = self::formatGoodsAndShop($dbResult, $userCouponId, $vipCardId);
  2573. if (!$cartDataResult->isSuccess()) {
  2574. return ResultWrapper::fail($cartDataResult->getData(), $cartDataResult->getErrorCode());
  2575. }
  2576. $cartData = $cartDataResult->getData();
  2577. if (empty($cartData['goodsData'])) {
  2578. return ResultWrapper::fail('购物车中商品已失效,不能进行结算', ErrorCode::$paramError);
  2579. }
  2580. if (empty($addRessData)) {
  2581. $cartData['address'] = (object)[];
  2582. } else {
  2583. $objMSysAreaChina = new MSysAreaChina();
  2584. $areaName = $objMSysAreaChina->getNameByCode([
  2585. $addRessData['provinceCode'],
  2586. $addRessData['cityCode'],
  2587. $addRessData['districtCode']
  2588. ]);
  2589. $cartData['address'] = [
  2590. 'name' => isset($addRessData['name']) ? $addRessData['name'] : '',
  2591. 'mobile' => isset($addRessData['mobile']) ? $addRessData['mobile'] : '',
  2592. 'address' => isset($addRessData['address']) ? $addRessData['address'] : '',
  2593. 'id' => isset($addRessData['id']) ? $addRessData['id'] : '',
  2594. 'provinceName' => isset($areaName[$addRessData['provinceCode']]) ? $areaName[$addRessData['provinceCode']] : '',
  2595. 'cityName' => isset($areaName[$addRessData['cityCode']]) ? $areaName[$addRessData['cityCode']] : '',
  2596. 'districtName' => isset($areaName[$addRessData['districtCode']]) ? $areaName[$addRessData['districtCode']] : '',
  2597. ];
  2598. }
  2599. //订单使用的优惠券
  2600. $userCouponInfo = [];
  2601. if (!empty($userCouponId)) {
  2602. $objMUserCoupon = new MUserCoupon($this->onlineUserId, $this->onlineEnterpriseId);
  2603. $dbResult = $objMUserCoupon->getUserCoupon(['id' => $userCouponId]);
  2604. if ($dbResult->isSuccess()) $userCouponInfo = $dbResult->getData();
  2605. $userCouponInfo = isset($userCouponInfo[0]) ? $userCouponInfo[0] : [];
  2606. }
  2607. $cartData['useCoupon'] = $userCouponInfo;
  2608. // 订单金额小于银行打款设置的启用金额,不支持银行打款
  2609. if(!empty($paymentData)){
  2610. foreach ($paymentData as $key => $value){
  2611. if($value['id'] == StatusCode::$payType['bankLoans'] && $cartData['payMoney'] < $value['limit']){
  2612. unset($paymentData[$key]);
  2613. }
  2614. }
  2615. }
  2616. $cartData['payment'] = array_values($paymentData);
  2617. $cartData['delivery'] = $DeliveryData;
  2618. $cartData['selfExpressRule'] = $selfExpressRule;
  2619. //优惠券数据
  2620. $cartData = self::findCoupon($cartData);
  2621. //获取订单会员卡
  2622. $vipCard = self::getVipCard($goodsIds);
  2623. if ($vipCard->isSuccess()) {
  2624. $vipCard = $vipCard->getData();
  2625. } else {
  2626. $vipCard = [];
  2627. }
  2628. $cartData['vipCard'][] = $vipCard;
  2629. // 客户余额
  2630. $cartData['balance'] = $customerData['money'];
  2631. // 会员余额
  2632. $cartData['memberBalance'] = $customerData['memberBalance'];
  2633. return ResultWrapper::success($cartData);
  2634. }
  2635. /**
  2636. * @param $goodsIds
  2637. * @return array|ResultWrapper
  2638. */
  2639. public function getVipCard($goodsIds)
  2640. {
  2641. $objMVipCard = new MVipCard($this->onlineEnterpriseId, $this->onlineUserId, true);
  2642. $pageParams = pageToOffset(1, 10);
  2643. $selectParams['limit'] = $pageParams['limit'];
  2644. $selectParams['offset'] = $pageParams['offset'];
  2645. $dbResult = $objMVipCard->getMyVipCards($selectParams);
  2646. if (!$dbResult->isSuccess()) {
  2647. return ResultWrapper::fail($dbResult->getData(), ErrorCode::$dberror);
  2648. }
  2649. $vipCard = $dbResult->getData()['data'];
  2650. if (empty($vipCard)) return ResultWrapper::success([]);
  2651. if (!empty($goodsIds)) {
  2652. foreach ($vipCard as $key => $card) {
  2653. if ($card['mode'] == StatusCode::$delete) {
  2654. $cardGoodsArr = explode(',', $card['goodsIds']);
  2655. foreach ($goodsIds as $goodsId) {
  2656. if (!in_array($goodsId, $cardGoodsArr)) {
  2657. unset($vipCard[$key]);
  2658. }
  2659. }
  2660. }
  2661. }
  2662. }
  2663. $vipCard = empty($vipCard) ? [] : array_shift($vipCard);
  2664. return ResultWrapper::success($vipCard);
  2665. }
  2666. /**
  2667. * 判断在使用范围的店铺,指定商品是否满足最小金额
  2668. * 查询可用的优惠券
  2669. * @param $data
  2670. * @return mixed
  2671. * @throws \Exception
  2672. */
  2673. private function findCoupon($data)
  2674. {
  2675. $objMUserCoupon = new MUserCoupon($this->onlineUserId, $this->onlineEnterpriseId);
  2676. $objGoodsBasicRelevant = new GoodsBasicRelevant($this->onlineEnterpriseId);
  2677. $payAmount = $data['payMoney'];
  2678. if ($data['activityMoney'] != 0) {
  2679. $payAmount = bcsub($payAmount, $data['activityMoney'], 2);
  2680. }
  2681. $dbResult = $objMUserCoupon->availableCoupon([
  2682. 'payAmount' => $payAmount
  2683. ]);
  2684. if (!$dbResult->isSuccess()) {
  2685. return ResultWrapper::fail($dbResult->getData(), ErrorCode::$dberror);
  2686. }
  2687. $coupon = $dbResult->getData();
  2688. foreach ($coupon as $key => $val) {
  2689. $useShop = explode(',', $val['useShop']);//此优惠券可以使用的店铺
  2690. $applyRange = $val['applyRange'];//优惠券使用范围
  2691. $cart[$val['id']]['money'] = 0;
  2692. foreach ($data['goodsData'] as $shopGoodsData) {
  2693. foreach ($shopGoodsData['shopGoodsData'] as $goodsDetail) {
  2694. $categoryId = $objGoodsBasicRelevant->getNameByBasicId($goodsDetail['goodsBasicId'], 'categoryId');
  2695. $brandId = $objGoodsBasicRelevant->getNameByBasicId($goodsDetail['goodsBasicId'], 'brandId');
  2696. if (in_array($shopGoodsData['shopId'], $useShop) &&
  2697. ($applyRange == StatusCode::$applyRange['allGoods'] ||
  2698. in_array($categoryId, explode(',', $val['categoryCollect'])) ||
  2699. in_array($goodsDetail['goodsId'], explode(',', $val['goodsCollect'])) ||
  2700. in_array($brandId, explode(',', $val['brandCollect'])))
  2701. ) {
  2702. $cart[$val['id']]['money'] = bcadd($cart[$val['id']]['money'], $goodsDetail['totalMoney']);//规定范围内的商品总金额
  2703. }
  2704. }
  2705. }
  2706. if ($val['minPrice'] != 0 && $cart[$val['id']]['money'] < $val['minPrice']) {
  2707. unset($coupon[$key]);//不符合要求
  2708. }
  2709. }
  2710. $data['coupon'] = array_values($coupon);
  2711. return $data;
  2712. }
  2713. /**
  2714. * 获取商品,规格 购物车数量
  2715. * @param $goodsIds
  2716. * @return array
  2717. */
  2718. public function getCartNumByGoodsId($goodsIds)
  2719. {
  2720. $cartData = $this->objDCart->select(['goodsId' => $goodsIds, 'userCenterId' => $this->onlineUserId], 'buyNum,goodsId,skuId');
  2721. if ($cartData === false) {
  2722. return [];
  2723. }
  2724. $return = [];
  2725. foreach ($cartData as $key => $val) {
  2726. $return[$val['goodsId']][$val['skuId']] = $val['buyNum'];
  2727. }
  2728. return $return;
  2729. }
  2730. /**
  2731. * 商品列表页调整购买数量
  2732. * @param $params
  2733. * @return ResultWrapper
  2734. * @throws \Exception
  2735. */
  2736. public function updateCartNum($params)
  2737. {
  2738. if ($params['buyNum'] == 0) {
  2739. $dbResult = $this->objDCart->delete([
  2740. 'goodsId' => $params['goodsId'],
  2741. 'skuId' => $params['skuId'],
  2742. 'userCenterId' => $this->onlineUserId
  2743. ]);
  2744. } else {
  2745. $goods = self::getCartInventory([
  2746. 'goodsId' => $params['goodsId'],
  2747. 'skuId' => $params['skuId'],
  2748. 'userCenterId' => $this->onlineUserId
  2749. ]);
  2750. if (!$goods->isSuccess()) {
  2751. return ResultWrapper::fail($goods->getData(), ErrorCode::$dberror);
  2752. }
  2753. $goods = $goods->getData();
  2754. $inventoryNum = isset($goods[0]['inventory']) ? $goods[0]['inventory'] : 0;//商品剩余库存
  2755. $setNum = isset($goods[0]['setNum']) ? $goods[0]['setNum'] : 0;//商品规格起订数量
  2756. $title = isset($goods[0]['title']) ? $goods[0]['title'] : '';//商品名称
  2757. if ($inventoryNum < $params['buyNum']) {
  2758. return ResultWrapper::fail('商品库存不足', ErrorCode::$paramError);
  2759. }
  2760. if ($params['buyNum'] < $setNum) {
  2761. return ResultWrapper::fail($title . '最小起订数量' . $setNum, ErrorCode::$paramError);
  2762. }
  2763. $dbResult = $this->objDCart->update(
  2764. [
  2765. 'buyNum' => $params['buyNum']
  2766. ],
  2767. [
  2768. 'goodsId' => $params['goodsId'],
  2769. 'skuId' => $params['skuId'],
  2770. 'userCenterId' => $this->onlineUserId
  2771. ]
  2772. );
  2773. }
  2774. if ($dbResult === false) {
  2775. return ResultWrapper::fail($this->objDCart->error(), ErrorCode::$dberror);
  2776. }
  2777. return ResultWrapper::success($dbResult);
  2778. }
  2779. /**
  2780. * 验证购物车内商品规格的库存
  2781. * @param $selectParams
  2782. * @return int|ResultWrapper
  2783. * @throws \Exception
  2784. */
  2785. private function getCartInventory($selectParams)
  2786. {
  2787. $dbResult = $this->objDCart->get($selectParams);
  2788. if ($dbResult === false) {
  2789. return ResultWrapper::fail($this->objDCart->error(), ErrorCode::$dberror);
  2790. }
  2791. if (empty($dbResult)) {
  2792. return ResultWrapper::success([]);
  2793. }
  2794. $result = self::checkCart([$dbResult]);
  2795. if (!$result->isSuccess()) {
  2796. return ResultWrapper::fail($result->getData(), $result->getErrorCode());
  2797. }
  2798. return ResultWrapper::success($result->getData());
  2799. }
  2800. /**
  2801. * Doc: (des="")
  2802. * User: XMing
  2803. * Date: 2021/1/8
  2804. * Time: 10:30 上午
  2805. * @param int $skuId
  2806. * @param int $userId
  2807. * @return ResultWrapper
  2808. */
  2809. public function getCartBuyNumBySku(int $skuId, int $userId): ResultWrapper
  2810. {
  2811. $sql = 'SELECT SUM(`buyNum`) AS subBuyNum FROM qianniao_cart_' . $this->onlineEnterpriseId . ' WHERE skuId = ' . $skuId . ' AND userCenterId = ' . $userId;
  2812. $sum = $this->objDCart->query($sql);
  2813. if ($sum === false) {
  2814. return ResultWrapper::fail($this->objDCart->error(), ErrorCode::$dberror);
  2815. }
  2816. $subBuyNum = isset($sum[0]['subBuyNum']) ? $sum[0]['subBuyNum'] : 0;
  2817. return ResultWrapper::success($subBuyNum);
  2818. }
  2819. /**
  2820. * Doc: (des="组团立即购买")
  2821. * User: XMing
  2822. * Date: 2021/1/22
  2823. * Time: 4:18 下午
  2824. * @param array $lists
  2825. * @param array $data
  2826. * @return ResultWrapper
  2827. * @throws \Exception
  2828. */
  2829. public function buyNow(array $lists, array $data): ResultWrapper
  2830. {
  2831. switch ($data['sourceType']) {
  2832. case self::$sourceType['comBin']:
  2833. $buyResult = self::buyComBin($lists, $data);
  2834. break;
  2835. }
  2836. if (!$buyResult->isSuccess()) {
  2837. return ResultWrapper::fail($buyResult->getData(), $buyResult->getErrorCode());
  2838. }
  2839. return ResultWrapper::success($buyResult->getData());
  2840. }
  2841. /**
  2842. * 普通商品立即购买
  2843. */
  2844. public function goodsBuyNow(array $goods, array $data)
  2845. {
  2846. //获取当前用户的客户id
  2847. $objMCustomer = new MCustomer($this->onlineEnterpriseId, $this->onlineUserId);
  2848. $result = $objMCustomer->getCustomerData(['userCenterId'=>$this->onlineUserId],'*', true);
  2849. if(!$result->isSuccess()){
  2850. return ResultWrapper::fail($result->getData(), $result->getErrorCode());
  2851. }
  2852. $customerData = $result->getData();
  2853. if (empty($customerData)) {
  2854. return ResultWrapper::fail('未找到客户信息', ErrorCode::$paramError);
  2855. }
  2856. $customerId = $customerData['id'];
  2857. //获取客户的默认收货地址
  2858. $objMShippingAddress = new MShippingAddress($this->onlineEnterpriseId);
  2859. $selectParams = [
  2860. 'deleteStatus' => StatusCode::$standard,
  2861. 'defaultStatus' => StatusCode::$standard,
  2862. 'customerId' => $customerId
  2863. ];
  2864. $dbResult = $objMShippingAddress->getShippingAddressInfo($selectParams);
  2865. if (!$dbResult->isSuccess()) {
  2866. return ResultWrapper::fail($dbResult->getData(), ErrorCode::$dberror);
  2867. }
  2868. $addRessData = $dbResult->getData();
  2869. unset($dbResult);
  2870. //获取自提点
  2871. $objMDeliverySetting = new MDeliverySetting($this->onlineUserId, $this->onlineEnterpriseId);
  2872. $selfExpressRuleResult = $objMDeliverySetting->getAllSelfExpressRule();
  2873. if (!$selfExpressRuleResult->isSuccess()) {
  2874. return ResultWrapper::fail($selfExpressRuleResult->getData(), $selfExpressRuleResult->getErrorCode());
  2875. }
  2876. $selfExpressRule = $selfExpressRuleResult->getData();
  2877. //支付方式
  2878. $objMEnterpriseBindPayment = new MEnterpriseBindPayment($this->onlineUserId, $this->onlineEnterpriseId);
  2879. $paymentData = $objMEnterpriseBindPayment->getPaymentType();
  2880. if (!$paymentData->isSuccess()) {
  2881. $paymentData = [];
  2882. } else {
  2883. $paymentData = $paymentData->getData();
  2884. }
  2885. //配送方式
  2886. $objMDeliverySetting = new MDeliverySetting($this->onlineUserId, $this->onlineEnterpriseId);
  2887. $DeliveryData = $objMDeliverySetting->allDelivery();
  2888. if (!$DeliveryData->isSuccess()) {
  2889. $DeliveryData = [];
  2890. } else {
  2891. $DeliveryData = array_values($DeliveryData->getData()['data']);
  2892. }
  2893. //当前用户选择的配送方式
  2894. if (!empty($data['deliveryId'])) {
  2895. $this->onlineUserDefaultDeliveryType = $data['deliveryId'];
  2896. } else {
  2897. if (!empty($DeliveryData)) {
  2898. foreach ($DeliveryData as $value) {
  2899. if ($value['defaultStatus'] == StatusCode::$standard) {
  2900. $this->onlineUserDefaultDeliveryType = $value['deliveryType'];
  2901. break;
  2902. }
  2903. }
  2904. }
  2905. }
  2906. //当前用户的收货地址code 省-市-区
  2907. if (!empty($addressId)) {
  2908. $addressResult = $objMShippingAddress->getShippingAddressInfo(['id' => $data['addressId']]);
  2909. if (!$addressResult->isSuccess()) {
  2910. return ResultWrapper::fail($addressResult->getData(), ErrorCode::$dberror);
  2911. }
  2912. $addressInfo = $addressResult->getData();
  2913. if (!empty($addressInfo)) {
  2914. $this->onlineUserAddressCode = $addressInfo['provinceCode'] . '-' . $addressInfo['cityCode'] . '-' . $addressInfo['districtCode'];
  2915. }
  2916. } else {
  2917. if (!empty($addRessData)) {
  2918. $this->onlineUserAddressCode = $addRessData['provinceCode'] . '-' . $addRessData['cityCode'] . '-' . $addRessData['districtCode'];
  2919. }
  2920. }
  2921. //获取当前企业设置的包邮价格
  2922. $objMBasicSetup = new MBasicSetup($this->onlineEnterpriseId);
  2923. $freeExpressPriceResult = $objMBasicSetup->getBasicField('freeExpressPrice');
  2924. if (!$freeExpressPriceResult->isSuccess()) {
  2925. return ResultWrapper::fail($freeExpressPriceResult->getData(), $freeExpressPriceResult->getData());
  2926. }
  2927. $freeExpressPrice = $freeExpressPriceResult->getData();
  2928. $this->freeExpressPrice = (isset($freeExpressPrice['freeExpressPrice']) && !empty($freeExpressPrice['freeExpressPrice'])) ? $freeExpressPrice['freeExpressPrice'] : 0;
  2929. //所有结算商品id
  2930. $goodsIds = [$goods['goodsId']];
  2931. $cartDataResult = self::formatGoodsAndShop([$goods], $data['couponId'], $data['vipCardId']);
  2932. if (!$cartDataResult->isSuccess()) {
  2933. return ResultWrapper::fail($cartDataResult->getData(), $cartDataResult->getErrorCode());
  2934. }
  2935. $cartData = $cartDataResult->getData();
  2936. if (empty($cartData['goodsData'])) {
  2937. return ResultWrapper::fail('商品已失效,不能进行结算', ErrorCode::$paramError);
  2938. }
  2939. if (empty($addRessData)) {
  2940. $cartData['address'] = (object)[];
  2941. } else {
  2942. $objMSysAreaChina = new MSysAreaChina();
  2943. $areaName = $objMSysAreaChina->getNameByCode([
  2944. $addRessData['provinceCode'],
  2945. $addRessData['cityCode'],
  2946. $addRessData['districtCode']
  2947. ]);
  2948. $cartData['address'] = [
  2949. 'name' => isset($addRessData['name']) ? $addRessData['name'] : '',
  2950. 'mobile' => isset($addRessData['mobile']) ? $addRessData['mobile'] : '',
  2951. 'address' => isset($addRessData['address']) ? $addRessData['address'] : '',
  2952. 'id' => isset($addRessData['id']) ? $addRessData['id'] : '',
  2953. 'provinceName' => isset($areaName[$addRessData['provinceCode']]) ? $areaName[$addRessData['provinceCode']] : '',
  2954. 'cityName' => isset($areaName[$addRessData['cityCode']]) ? $areaName[$addRessData['cityCode']] : '',
  2955. 'districtName' => isset($areaName[$addRessData['districtCode']]) ? $areaName[$addRessData['districtCode']] : '',
  2956. ];
  2957. }
  2958. //订单使用的优惠券
  2959. $userCouponInfo = [];
  2960. if (!empty($data['couponId'])) {
  2961. $objMUserCoupon = new MUserCoupon($this->onlineUserId, $this->onlineEnterpriseId);
  2962. $dbResult = $objMUserCoupon->getUserCoupon(['id' => $data['couponId']]);
  2963. if ($dbResult->isSuccess()) $userCouponInfo = $dbResult->getData();
  2964. $userCouponInfo = isset($userCouponInfo[0]) ? $userCouponInfo[0] : [];
  2965. }
  2966. // 订单金额小于银行打款设置的启用金额,不支持银行打款
  2967. if(!empty($paymentData)){
  2968. foreach ($paymentData as $key => $value){
  2969. if($value['id'] == StatusCode::$payType['bankLoans'] && $cartData['payMoney'] <= $value['limit']){
  2970. unset($paymentData[$key]);
  2971. }
  2972. }
  2973. }
  2974. $cartData['payment'] = array_values($paymentData);
  2975. $cartData['useCoupon'] = $userCouponInfo;
  2976. $cartData['delivery'] = $DeliveryData;
  2977. $cartData['selfExpressRule'] = $selfExpressRule;
  2978. //优惠券数据
  2979. $cartData = self::findCoupon($cartData);
  2980. //获取订单会员卡
  2981. $vipCard = self::getVipCard($goodsIds);
  2982. if ($vipCard->isSuccess()) {
  2983. $vipCard = $vipCard->getData();
  2984. } else {
  2985. $vipCard = [];
  2986. }
  2987. $cartData['vipCard'][] = $vipCard;
  2988. // 客户余额
  2989. $cartData['balance'] = $customerData['money'];
  2990. // 会员余额
  2991. $cartData['memberBalance'] = $customerData['memberBalance'];
  2992. return ResultWrapper::success($cartData);
  2993. }
  2994. /**
  2995. * Doc: (des="购买组合套餐")
  2996. * User: XMing
  2997. * Date: 2021/1/22
  2998. * Time: 4:34 下午
  2999. * @param array $lists
  3000. * @param array $data
  3001. * @return ResultWrapper
  3002. * @throws \Exception
  3003. */
  3004. private function buyComBin(array $lists, array $data): ResultWrapper
  3005. {
  3006. $objMCustomer = new MCustomer($this->onlineEnterpriseId,$this->onlineUserId);
  3007. $customerId = $objMCustomer->getCustomerIdByUserCenterId($this->onlineUserId);
  3008. //验证组合套餐
  3009. if (empty($data['comBinId'])) {
  3010. return ResultWrapper::fail('组合套餐错误', ErrorCode::$paramError);
  3011. }
  3012. $objMComBinPackage = new MComBinPackage($this->onlineEnterpriseId, $this->onlineUserId);
  3013. $detailsResult = $objMComBinPackage->details($data['comBinId']);
  3014. if (!$detailsResult->isSuccess()) {
  3015. return ResultWrapper::fail($detailsResult->getData(), $detailsResult->getErrorCode());
  3016. }
  3017. $details = $detailsResult->getData();
  3018. if (empty($details)) {
  3019. return ResultWrapper::fail('未获取到指定的组合套餐', ErrorCode::$paramError);
  3020. }
  3021. $time = time();
  3022. if ($details['isExpire'] == StatusCode::$standard) {
  3023. if ($time < $details['startTime']) {
  3024. return ResultWrapper::fail('组合套餐活动未开始', ErrorCode::$paramError);
  3025. }
  3026. if ($time > $details['endTime']) {
  3027. return ResultWrapper::fail('组合套餐活动已结束', ErrorCode::$paramError);
  3028. }
  3029. }
  3030. if ($details['isLimit'] == StatusCode::$standard) {
  3031. //获取用户组合套餐购买数量(TODO)
  3032. $user_log_num = 0;
  3033. if ($user_log_num >= $details['limitNum']) {
  3034. return ResultWrapper::fail('你已经购买过此组合套餐了', ErrorCode::$paramError);
  3035. }
  3036. }
  3037. //验证商品在售状态,sku是否参与组合,库存
  3038. $allowSkuArr = [];
  3039. $goodsData = $details['goodsData'];
  3040. $mapping = [];//商品映射
  3041. foreach ($goodsData as $goods) {
  3042. $sku = $goods['joinSku'];
  3043. $allowSkuArr = array_merge($sku, $allowSkuArr);
  3044. $mapping[$goods['id']] = $goods;
  3045. }
  3046. //验证商品完整性
  3047. $goodsIdArr = explode(',', $details['goodsIds']);
  3048. $selectGoodsIdArr = [];
  3049. foreach ($lists as $val) {
  3050. if (!in_array($val['goodsId'], $selectGoodsIdArr)) {
  3051. $selectGoodsIdArr[] = $val['goodsId'];
  3052. }
  3053. $thisRow = getArrayItem($mapping,$val['goodsId'],[]);
  3054. if (isset($thisRow['joinSku']) && !empty($thisRow['joinSku'])){
  3055. if (!in_array($val['skuId'], $allowSkuArr)) {
  3056. return ResultWrapper::fail('商品规格选择异常', ErrorCode::$paramError);
  3057. }
  3058. }
  3059. }
  3060. $diffBool = array_diff($goodsIdArr, $selectGoodsIdArr);
  3061. if (!empty($diffBool)) {
  3062. return ResultWrapper::fail('你还没选择完组合商品', ErrorCode::$paramError);
  3063. }
  3064. unset($diffBool);
  3065. //店铺
  3066. $objMGoods = new MShop($this->onlineEnterpriseId, $this->onlineUserId);
  3067. $shopResult = $objMGoods->getShopByIdd($details['shopId']);
  3068. if (!$shopResult->isSuccess()) {
  3069. return ResultWrapper::fail($shopResult->getData(), $shopResult->getErrorCode());
  3070. }
  3071. $shop = $shopResult->getData();
  3072. $c_goods_Lists = [];
  3073. foreach ($lists as $val) {
  3074. if (!isset($mapping[$val['goodsId']])) {
  3075. return ResultWrapper::fail('商品信息异常', ErrorCode::$paramError);
  3076. }
  3077. $row = getArrayItem($mapping, $val['goodsId'], []);
  3078. $specMultiple = getArrayItem($row, 'specMultiple', []);
  3079. $specMultipleMap = [];//sku映射
  3080. foreach ($specMultiple as $value) {
  3081. $specMultipleMap[$value['id']] = $value;
  3082. }
  3083. if ($row['enableStatus'] == StatusCode::$delete) {
  3084. return ResultWrapper::fail('组合套餐商品下架了', ErrorCode::$paramError);
  3085. }
  3086. $skuRow = getArrayItem($specMultipleMap, $val['skuId'], []);
  3087. if ($this->preSale == StatusCode::$delete) {
  3088. if ($skuRow['inventory'] < 1) {
  3089. return ResultWrapper::fail('组合套餐库存不足', ErrorCode::$paramError);
  3090. }
  3091. }
  3092. $c_goods_Lists[] = [
  3093. 'goodsId' => getArrayItem($row, 'id', 0),
  3094. 'goodsCode' => createCode(StatusCode::$code['goodsBasic']['prefix'], $row['basicGoodsId'], StatusCode::$code['goodsBasic']['length']),
  3095. 'buyNum' => 1,
  3096. 'shopId' => getArrayItem($row, 'shopId', 0),
  3097. 'shopLogo' => getArrayItem($shop, 'logo', ''),
  3098. 'goodsBasicId' => getArrayItem($row, 'basicGoodsId', 0),
  3099. 'selection' => StatusCode::$standard,
  3100. 'skuId' => $val['skuId'],
  3101. 'warehouseId' => getArrayItem($row, 'warehouseId', 0),
  3102. 'activityId' => 0,
  3103. 'shopName' => getArrayItem($row, 'shopName', ''),
  3104. 'brandName' => getArrayItem($row, 'brandName', ''),
  3105. 'categoryName' => getArrayItem($row, 'categoryName', ''),
  3106. 'barCode' => getArrayItem($skuRow, 'barCode', ''),
  3107. 'storageCode' => getArrayItem($row, 'storageCode', ''),
  3108. 'goodsName' => getArrayItem($row, 'title', ''),
  3109. 'goodsImages' => array_shift($row['images']),
  3110. 'isInvalid' => StatusCode::$standard,
  3111. 'describe' => getArrayItem($row, 'describe', ''),
  3112. 'categoryId' => getArrayItem($row, 'categoryId', 0),
  3113. 'brandId' => getArrayItem($row, 'brandId', 0),
  3114. 'categoryPath' => getArrayItem($row, 'categoryPath', ''),
  3115. 'isActivity' => StatusCode::$delete,
  3116. 'specType' => getArrayItem($row, 'specType', 0),
  3117. 'unitName' => getArrayItem($skuRow, 'unitName', ''),
  3118. 'specGroup' => getArrayItem($skuRow, 'specGroup', []),
  3119. 'notExpress' => StatusCode::$standard,
  3120. 'supplierId' => getArrayItem($row, 'supplierId', 0),
  3121. 'express' => [
  3122. "weight" => getArrayItem($skuRow, 'weight', 0),
  3123. "expressType" => getArrayItem($row, 'expressType', 0),
  3124. "ruleId" => getArrayItem($row, 'ruleId', 0),
  3125. "expressFee" => getArrayItem($row, 'expressFee', 0),
  3126. ],
  3127. 'originPrice' => getArrayItem($skuRow, 'marketPrice', 0),
  3128. 'price' => getArrayItem($skuRow, 'salePrice', 0),
  3129. 'preferential' => 0,
  3130. 'totalMoney' => getArrayItem($skuRow, 'salePrice', 0),
  3131. 'activityMoney' => 0,
  3132. 'isMutex' => StatusCode::$delete,
  3133. 'costPrice' => getArrayItem($skuRow, 'costPrice', 0),
  3134. 'inventoryNum' => getArrayItem($skuRow, 'inventory', 0),
  3135. 'conversion' => getArrayItem($skuRow, 'conversion', 0),
  3136. 'expressMoney' => 0,
  3137. ];
  3138. }
  3139. //获取客户的默认收货地址
  3140. $objMShippingAddress = new MShippingAddress($this->onlineEnterpriseId);
  3141. $selectParams = [
  3142. 'deleteStatus' => StatusCode::$standard,
  3143. 'defaultStatus' => StatusCode::$standard,
  3144. 'customerId' => $customerId
  3145. ];
  3146. $dbResult = $objMShippingAddress->getShippingAddressInfo($selectParams);
  3147. if (!$dbResult->isSuccess()) {
  3148. return ResultWrapper::fail($dbResult->getData(), ErrorCode::$dberror);
  3149. }
  3150. $addRessData = $dbResult->getData();
  3151. unset($dbResult);
  3152. //获取自提点
  3153. $objMDeliverySetting = new MDeliverySetting($this->onlineUserId, $this->onlineEnterpriseId);
  3154. $selfExpressRuleResult = $objMDeliverySetting->getAllSelfExpressRule();
  3155. if (!$selfExpressRuleResult->isSuccess()) {
  3156. return ResultWrapper::fail($selfExpressRuleResult->getData(), $selfExpressRuleResult->getErrorCode());
  3157. }
  3158. $selfExpressRule = $selfExpressRuleResult->getData();
  3159. //支付方式
  3160. $objMEnterpriseBindPayment = new MEnterpriseBindPayment($this->onlineUserId, $this->onlineEnterpriseId);
  3161. $paymentData = $objMEnterpriseBindPayment->getPaymentType();
  3162. if (!$paymentData->isSuccess()) {
  3163. return ResultWrapper::fail($paymentData->getData(),$paymentData->getErrorCode());
  3164. }
  3165. $paymentData = $paymentData->getData();
  3166. //配送方式
  3167. $objMDeliverySetting = new MDeliverySetting($this->onlineUserId, $this->onlineEnterpriseId);
  3168. $DeliveryData = $objMDeliverySetting->allDelivery();
  3169. if (!$DeliveryData->isSuccess()) {
  3170. return ResultWrapper::fail($DeliveryData->getData(),$DeliveryData->getErrorCode());
  3171. }
  3172. $DeliveryData = array_values($DeliveryData->getData()['data']);
  3173. $deliveryId = getArrayItem($data,'deliveryId',0);
  3174. //当前用户选择的配送方式
  3175. if (!empty($deliveryId)) {
  3176. $this->onlineUserDefaultDeliveryType = $deliveryId;
  3177. } else {
  3178. if (!empty($DeliveryData)) {
  3179. foreach ($DeliveryData as $value) {
  3180. if ($value['defaultStatus'] == StatusCode::$standard) {
  3181. $this->onlineUserDefaultDeliveryType = $value['deliveryType'];
  3182. break;
  3183. }
  3184. }
  3185. }
  3186. }
  3187. //当前用户的收货地址code 省-市-区
  3188. $addressId = getArrayItem($data,'addressId',0);
  3189. if (!empty($addressId)) {
  3190. $addressResult = $objMShippingAddress->getShippingAddressInfo(['id' => $addressId]);
  3191. if (!$addressResult->isSuccess()) {
  3192. return ResultWrapper::fail($addressResult->getData(), ErrorCode::$dberror);
  3193. }
  3194. $addressInfo = $addressResult->getData();
  3195. if (!empty($addressInfo)) {
  3196. $this->onlineUserAddressCode = $addressInfo['provinceCode'] . '-' . $addressInfo['cityCode'] . '-' . $addressInfo['districtCode'];
  3197. }
  3198. } else {
  3199. if (!empty($addRessData)) {
  3200. $this->onlineUserAddressCode = $addRessData['provinceCode'] . '-' . $addRessData['cityCode'] . '-' . $addRessData['districtCode'];
  3201. }
  3202. }
  3203. if (empty($addRessData)) {
  3204. $address = (object)[];
  3205. } else {
  3206. $objMSysAreaChina = new MSysAreaChina();
  3207. $areaName = $objMSysAreaChina->getNameByCode([
  3208. $addRessData['provinceCode'],
  3209. $addRessData['cityCode'],
  3210. $addRessData['districtCode']
  3211. ]);
  3212. $address = [
  3213. 'name' => isset($addRessData['name']) ? $addRessData['name'] : '',
  3214. 'mobile' => isset($addRessData['mobile']) ? $addRessData['mobile'] : '',
  3215. 'address' => isset($addRessData['address']) ? $addRessData['address'] : '',
  3216. 'id' => isset($addRessData['id']) ? $addRessData['id'] : '',
  3217. 'provinceName' => isset($areaName[$addRessData['provinceCode']]) ? $areaName[$addRessData['provinceCode']] : '',
  3218. 'cityName' => isset($areaName[$addRessData['cityCode']]) ? $areaName[$addRessData['cityCode']] : '',
  3219. 'districtName' => isset($areaName[$addRessData['districtCode']]) ? $areaName[$addRessData['districtCode']] : '',
  3220. ];
  3221. }
  3222. //获取当前企业设置的包邮价格
  3223. $objMBasicSetup = new MBasicSetup($this->onlineEnterpriseId);
  3224. $freeExpressPriceResult = $objMBasicSetup->getBasicField('freeExpressPrice');
  3225. if (!$freeExpressPriceResult->isSuccess()) {
  3226. return ResultWrapper::fail($freeExpressPriceResult->getData(), $freeExpressPriceResult->getData());
  3227. }
  3228. $freeExpressPrice = $freeExpressPriceResult->getData();
  3229. $this->freeExpressPrice = (isset($freeExpressPrice['freeExpressPrice']) && !empty($freeExpressPrice['freeExpressPrice'])) ? $freeExpressPrice['freeExpressPrice'] : 0;
  3230. //组合套餐是否计算运费
  3231. if ($details['expressType'] = StatusCode::$delete){
  3232. //计算运费
  3233. $result = self::calExpress($c_goods_Lists);
  3234. if (!$result->isSuccess()) {
  3235. return ResultWrapper::fail($result->getData(), $result->getErrorCode());
  3236. }
  3237. }
  3238. $c_shop = [
  3239. 'shopId' => getArrayItem($shop, 'id', 0),
  3240. 'shopName' => getArrayItem($shop, 'name', ''),
  3241. 'shopLogo' => getArrayItem($shop, 'logo', ''),
  3242. 'expressMoney' => $this->expressMoney,
  3243. 'totalMoney' => floatval($details['price']),
  3244. 'preferential' => 0,
  3245. 'payMoney' => bcadd(floatval($details['price']),$this->expressMoney,2),
  3246. 'shopGoodsData' => $c_goods_Lists
  3247. ];
  3248. $ret = [
  3249. 'goodsData' => [$c_shop],
  3250. 'totalMoney' => floatval($details['price']),
  3251. 'payMoney' => bcadd(floatval($details['price']),$this->expressMoney,2),
  3252. 'preferential' => 0,
  3253. 'vipDiscount' => 0,
  3254. 'vipDoubleDiscount' => 0,
  3255. 'activityMoney' => 0,
  3256. 'expressMoney' => $this->expressMoney,
  3257. 'checkNum' => 0,
  3258. 'cartNum' => 0,
  3259. 'goodsNum' => 0,
  3260. 'invalidData' => [],
  3261. 'address' => $address,
  3262. 'useCoupon' => [],
  3263. 'payment' => $paymentData,
  3264. 'delivery' => $DeliveryData,
  3265. 'selfExpressRule' => $selfExpressRule,
  3266. 'coupon' => [],
  3267. 'vipCard' => [],
  3268. 'comBinId' => $data['comBinId']
  3269. ];
  3270. return ResultWrapper::success($ret);
  3271. }
  3272. }