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; } }