MTestCart.Class.php 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. <?php
  2. namespace JinDouYun\Model\Cart;
  3. use JinDouYun\Cache\CustomerCache;
  4. use JinDouYun\Model\Customer\MCustomer;
  5. use JinDouYun\Dao\Cart\DCart;
  6. use JinDouYun\Model\MBaseModel;
  7. use JinDouYun\Cache\ActivityLimitCache;
  8. use Mall\Framework\Core\ErrorCode;
  9. use Mall\Framework\Core\ResultWrapper;
  10. use Mall\Framework\Core\StatusCode;
  11. /**
  12. * 购物车模型
  13. * 注: 用户id=>uid 客户id=>cid
  14. * Class MTestCart
  15. */
  16. class MTestCart extends MBaseModel
  17. {
  18. /**
  19. * true=>前台 | false=>后台
  20. * @var bool
  21. */
  22. private $isFront;
  23. /**
  24. * 当前选择客户 uid
  25. * @var integer
  26. */
  27. private $onlineUserId;
  28. /**
  29. * 当前选择客户 cid
  30. * @var integer
  31. */
  32. private $thisChoiceCustomerId;
  33. /**
  34. * 当前企业id
  35. * @var integer
  36. */
  37. private $onlineEnterpriseId;
  38. /**
  39. * 当前定位
  40. * @var string
  41. */
  42. private $areaCode;
  43. /**
  44. * 客户信息
  45. * @var array
  46. */
  47. private $customerInfo;
  48. /**
  49. * 购物车数据库连接对象
  50. * @var DCart
  51. */
  52. private $objDCart;
  53. /**
  54. * 活动缓存
  55. * @var ActivityLimitCache
  56. */
  57. private $objActivityLimitCache;
  58. /**
  59. * MTestCart constructor.
  60. * @param $onlineUserId
  61. * @param $onlineEnterpriseId
  62. * @param bool $isFront
  63. * @param null $thisChoiceCustomerId
  64. * @param null $areaCode
  65. * @throws \Exception
  66. */
  67. public function __construct($onlineUserId, $onlineEnterpriseId, $isFront = false, $thisChoiceCustomerId = null, $areaCode = null)
  68. {
  69. parent::__construct($onlineEnterpriseId,$onlineUserId);
  70. $this->isFront = $isFront;
  71. $this->onlineUserId = $onlineUserId;
  72. $this->onlineEnterpriseId = $onlineEnterpriseId;
  73. $this->areaCode = $areaCode;
  74. $this->thisChoiceCustomerId = $thisChoiceCustomerId;
  75. $this->customerInfo = null;
  76. self::__initCustomer();
  77. $this->objDCart = new DCart();
  78. $this->objActivityLimitCache = new ActivityLimitCache($this->onlineEnterpriseId);
  79. }
  80. /**
  81. * 初始化用户(uid)以及客户(cid)
  82. * @throws \Exception
  83. */
  84. private function __initCustomer()
  85. {
  86. $objMCustomer = new MCustomer($this->onlineEnterpriseId,$this->onlineUserId);
  87. switch ($this->isFront){
  88. case true://前台
  89. $this->thisChoiceCustomerId = $objMCustomer->getCustomerIdByUserCenterId($this->onlineUserId);
  90. break;
  91. case false://后台
  92. $this->onlineUserId = $objMCustomer->getUserCenterIdByCustomerId($this->thisChoiceCustomerId);
  93. break;
  94. }
  95. $dbResult = $objMCustomer->getCustomerInfoByUserCenterId($this->onlineUserId);
  96. if (!$dbResult->isSuccess()){
  97. $this->customerInfo = null;
  98. }
  99. $customer = $dbResult->getData();
  100. $this->customerInfo = [
  101. 'type' => $customer['type'],//客户类型
  102. 'name' => $customer['name'],//客户名称
  103. ];
  104. }
  105. public function __destruct()
  106. {
  107. // TODO: Implement __destruct() method.
  108. }
  109. /**
  110. * (批量添加购物车)
  111. * @param $params
  112. * @return ResultWrapper
  113. * @throws \Exception
  114. */
  115. public function addCart($params)
  116. {
  117. if (empty($this->customerInfo)) {
  118. return ResultWrapper::fail('获取客户信息失败', ErrorCode::$dberror);
  119. }
  120. $this->objDCart->beginTransaction();
  121. $goodsData = $params['goodsData'];
  122. $dbResult = self::checkCart($goodsData);
  123. if (!$dbResult->isSuccess()) {
  124. return ResultWrapper::fail($dbResult->getData(), $dbResult->getErrorCode());
  125. }
  126. $checkData = $dbResult->getData();
  127. //前台验证活动商品剩余数量,和用户限购数量
  128. if ($this->isFront === true) {
  129. //将数据拆分
  130. $checkData = self::checkLimitGroup($checkData);
  131. $dbResult = self::checkLimit($checkData);
  132. if (!$dbResult->isSuccess()) {
  133. return ResultWrapper::fail($dbResult->getData(), $dbResult->getErrorCode());
  134. }
  135. $dbResult = $dbResult->getData();
  136. $mapping = $dbResult['mapping'];
  137. $checkData = $dbResult['checkData'];
  138. }
  139. unset($dbResult);
  140. $dbResult = self::existCartAndGroup($checkData);
  141. if (!$dbResult->isSuccess()) {
  142. return ResultWrapper::fail($dbResult->getData(), $dbResult->getErrorCode());
  143. }
  144. $cartData = $dbResult->getData();//分组后的数据
  145. unset($dbResult);
  146. $dbResult = true;//初始化
  147. $oldCart = $cartData['old'];//旧的数据
  148. $nowCart = $cartData['now'];//新的数据
  149. if (!empty($oldCart)) {
  150. foreach ($oldCart as $key => $val) {
  151. $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']}";
  152. $dbResult = $this->objDCart->query($sql);
  153. }
  154. if ($dbResult === false) {
  155. $this->objDCart->rollBack();
  156. return ResultWrapper::fail($this->objDCart->error(), ErrorCode::$dberror);
  157. }
  158. }
  159. if (!empty($nowCart)) {
  160. $insert = [];
  161. foreach ($nowCart as $key => $val) {
  162. $insert[] = [
  163. 'userCenterId' => $this->onlineUserId,
  164. 'selection' => StatusCode::$standard,
  165. 'skuId' => $val['skuId'],
  166. 'goodsCode' => $val['goodsCode'],
  167. 'goodsId' => $val['goodsId'],
  168. 'shopId' => $val['shopId'],
  169. 'buyNum' => $val['buyNum'],
  170. 'source' => $val['source'],
  171. 'goodsBasicId' => $val['goodsBasicId'],
  172. 'warehouseId' => $val['warehouseId'],
  173. 'activityId' => isset($val['activityId']) ? $val['activityId'] : 0,//活动id
  174. ];
  175. }
  176. $dbResult = $this->objDCart->insert($insert, true);
  177. if ($dbResult === false) {
  178. $this->objDCart->rollBack();
  179. return ResultWrapper::fail($this->objDCart->error(), ErrorCode::$dberror);
  180. }
  181. }
  182. $objCustomerCache = new CustomerCache();
  183. $objCustomerCache->incrInterestCustomer($this->customerId, $this->onlineEnterpriseId);
  184. //用户限购缓存
  185. if (!empty($mapping)) {
  186. self::userLimit($mapping);
  187. }
  188. $this->objDCart->commit();
  189. return ResultWrapper::success($dbResult);
  190. }
  191. /**
  192. * 根据限购数量对数据进行分组
  193. * @param $data
  194. * @return array
  195. */
  196. private function checkLimitGroup($data)
  197. {
  198. if (empty($data)) return $data;
  199. $allMapping = [];
  200. foreach ($data as &$goods) {
  201. if ($goods['isActivityPrice'] == StatusCode::$standard) {
  202. $userLimit = $this->objActivityLimitCache->getLimit($goods['activityId'], $goods['goodsId'], $goods['skuId'], $this->onlineUserId);//用户限购数量
  203. $allowNum = $goods['limitNum'] - $userLimit;//还可以购买的数量
  204. $buyNum = $goods['buyNum'];//购买数量
  205. if ($buyNum >= $allowNum) {
  206. //拆分
  207. $goods['buyNum'] = $allowNum;
  208. $allMapping[] = $goods;
  209. $newGoods = $goods;
  210. if ($buyNum - $allowNum > 0) {
  211. $newGoods['buyNum'] = $buyNum - $allowNum;
  212. $newGoods['activityId'] = 0;//将活动id重置为0
  213. $newGoods['isActivityPrice'] = StatusCode::$delete;
  214. $allMapping[] = $newGoods;
  215. }
  216. } else {
  217. //限购等于加入数量
  218. $allMapping[] = $goods;
  219. }
  220. } else {
  221. $allMapping[] = $goods;
  222. }
  223. }
  224. return $allMapping;
  225. }
  226. }