123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252 |
- <?php
- namespace JinDouYun\Model\Cart;
- use JinDouYun\Cache\CustomerCache;
- use JinDouYun\Model\Customer\MCustomer;
- use JinDouYun\Dao\Cart\DCart;
- use JinDouYun\Model\MBaseModel;
- use JinDouYun\Cache\ActivityLimitCache;
- use Mall\Framework\Core\ErrorCode;
- use Mall\Framework\Core\ResultWrapper;
- use Mall\Framework\Core\StatusCode;
- /**
- * 购物车模型
- * 注: 用户id=>uid 客户id=>cid
- * Class MTestCart
- */
- class MTestCart extends MBaseModel
- {
- /**
- * true=>前台 | false=>后台
- * @var bool
- */
- private $isFront;
- /**
- * 当前选择客户 uid
- * @var integer
- */
- private $onlineUserId;
- /**
- * 当前选择客户 cid
- * @var integer
- */
- private $thisChoiceCustomerId;
- /**
- * 当前企业id
- * @var integer
- */
- private $onlineEnterpriseId;
- /**
- * 当前定位
- * @var string
- */
- private $areaCode;
- /**
- * 客户信息
- * @var array
- */
- private $customerInfo;
- /**
- * 购物车数据库连接对象
- * @var DCart
- */
- private $objDCart;
- /**
- * 活动缓存
- * @var ActivityLimitCache
- */
- private $objActivityLimitCache;
- /**
- * MTestCart constructor.
- * @param $onlineUserId
- * @param $onlineEnterpriseId
- * @param bool $isFront
- * @param null $thisChoiceCustomerId
- * @param null $areaCode
- * @throws \Exception
- */
- public function __construct($onlineUserId, $onlineEnterpriseId, $isFront = false, $thisChoiceCustomerId = null, $areaCode = null)
- {
- parent::__construct($onlineEnterpriseId,$onlineUserId);
- $this->isFront = $isFront;
- $this->onlineUserId = $onlineUserId;
- $this->onlineEnterpriseId = $onlineEnterpriseId;
- $this->areaCode = $areaCode;
- $this->thisChoiceCustomerId = $thisChoiceCustomerId;
- $this->customerInfo = null;
- self::__initCustomer();
- $this->objDCart = new DCart();
- $this->objActivityLimitCache = new ActivityLimitCache($this->onlineEnterpriseId);
- }
- /**
- * 初始化用户(uid)以及客户(cid)
- * @throws \Exception
- */
- private function __initCustomer()
- {
- $objMCustomer = new MCustomer($this->onlineEnterpriseId,$this->onlineUserId);
- switch ($this->isFront){
- case true://前台
- $this->thisChoiceCustomerId = $objMCustomer->getCustomerIdByUserCenterId($this->onlineUserId);
- break;
- case false://后台
- $this->onlineUserId = $objMCustomer->getUserCenterIdByCustomerId($this->thisChoiceCustomerId);
- break;
- }
- $dbResult = $objMCustomer->getCustomerInfoByUserCenterId($this->onlineUserId);
- if (!$dbResult->isSuccess()){
- $this->customerInfo = null;
- }
- $customer = $dbResult->getData();
- $this->customerInfo = [
- 'type' => $customer['type'],//客户类型
- 'name' => $customer['name'],//客户名称
- ];
- }
- public function __destruct()
- {
- // TODO: Implement __destruct() method.
- }
- /**
- * (批量添加购物车)
- * @param $params
- * @return ResultWrapper
- * @throws \Exception
- */
- public function addCart($params)
- {
- if (empty($this->customerInfo)) {
- return ResultWrapper::fail('获取客户信息失败', ErrorCode::$dberror);
- }
- $this->objDCart->beginTransaction();
- $goodsData = $params['goodsData'];
- $dbResult = self::checkCart($goodsData);
- if (!$dbResult->isSuccess()) {
- return ResultWrapper::fail($dbResult->getData(), $dbResult->getErrorCode());
- }
- $checkData = $dbResult->getData();
- //前台验证活动商品剩余数量,和用户限购数量
- if ($this->isFront === true) {
- //将数据拆分
- $checkData = self::checkLimitGroup($checkData);
- $dbResult = self::checkLimit($checkData);
- if (!$dbResult->isSuccess()) {
- return ResultWrapper::fail($dbResult->getData(), $dbResult->getErrorCode());
- }
- $dbResult = $dbResult->getData();
- $mapping = $dbResult['mapping'];
- $checkData = $dbResult['checkData'];
- }
- unset($dbResult);
- $dbResult = self::existCartAndGroup($checkData);
- if (!$dbResult->isSuccess()) {
- return ResultWrapper::fail($dbResult->getData(), $dbResult->getErrorCode());
- }
- $cartData = $dbResult->getData();//分组后的数据
- unset($dbResult);
- $dbResult = true;//初始化
- $oldCart = $cartData['old'];//旧的数据
- $nowCart = $cartData['now'];//新的数据
- if (!empty($oldCart)) {
- foreach ($oldCart as $key => $val) {
- $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']}";
- $dbResult = $this->objDCart->query($sql);
- }
- if ($dbResult === false) {
- $this->objDCart->rollBack();
- return ResultWrapper::fail($this->objDCart->error(), ErrorCode::$dberror);
- }
- }
- if (!empty($nowCart)) {
- $insert = [];
- foreach ($nowCart as $key => $val) {
- $insert[] = [
- 'userCenterId' => $this->onlineUserId,
- 'selection' => StatusCode::$standard,
- 'skuId' => $val['skuId'],
- 'goodsCode' => $val['goodsCode'],
- 'goodsId' => $val['goodsId'],
- 'shopId' => $val['shopId'],
- 'buyNum' => $val['buyNum'],
- 'source' => $val['source'],
- 'goodsBasicId' => $val['goodsBasicId'],
- 'warehouseId' => $val['warehouseId'],
- 'activityId' => isset($val['activityId']) ? $val['activityId'] : 0,//活动id
- ];
- }
- $dbResult = $this->objDCart->insert($insert, true);
- if ($dbResult === false) {
- $this->objDCart->rollBack();
- return ResultWrapper::fail($this->objDCart->error(), ErrorCode::$dberror);
- }
- }
- $objCustomerCache = new CustomerCache();
- $objCustomerCache->incrInterestCustomer($this->customerId, $this->onlineEnterpriseId);
- //用户限购缓存
- if (!empty($mapping)) {
- self::userLimit($mapping);
- }
- $this->objDCart->commit();
- return ResultWrapper::success($dbResult);
- }
- /**
- * 根据限购数量对数据进行分组
- * @param $data
- * @return array
- */
- private function checkLimitGroup($data)
- {
- if (empty($data)) return $data;
- $allMapping = [];
- foreach ($data as &$goods) {
- if ($goods['isActivityPrice'] == StatusCode::$standard) {
- $userLimit = $this->objActivityLimitCache->getLimit($goods['activityId'], $goods['goodsId'], $goods['skuId'], $this->onlineUserId);//用户限购数量
- $allowNum = $goods['limitNum'] - $userLimit;//还可以购买的数量
- $buyNum = $goods['buyNum'];//购买数量
- if ($buyNum >= $allowNum) {
- //拆分
- $goods['buyNum'] = $allowNum;
- $allMapping[] = $goods;
- $newGoods = $goods;
- if ($buyNum - $allowNum > 0) {
- $newGoods['buyNum'] = $buyNum - $allowNum;
- $newGoods['activityId'] = 0;//将活动id重置为0
- $newGoods['isActivityPrice'] = StatusCode::$delete;
- $allMapping[] = $newGoods;
- }
- } else {
- //限购等于加入数量
- $allMapping[] = $goods;
- }
- } else {
- $allMapping[] = $goods;
- }
- }
- return $allMapping;
- }
- }
|