CloudStockGoods.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. <?php
  2. namespace addons\CloudStock\common\models;
  3. use common\enums\AddonsEnum;
  4. use common\enums\RabbitMqEnum;
  5. use common\enums\StatusEnum;
  6. use common\models\base\BaseActiveRecord;
  7. use common\models\goods\Goods;
  8. use common\models\user\UserPartitionRelationship;
  9. use Yii;
  10. /**
  11. * This is the model class for table "{{%addons_cloud_stock_goods".
  12. *
  13. * @property int $id
  14. * @property int $mall_id 商城id
  15. * @property int $goods_id 商品id
  16. * @property string|null $agent_price 代理商进货价设置
  17. * @property int $goods_type 商品类型;0 商城商品
  18. * @property string|null $equal_level_list 平级奖设置
  19. * @property string|null $fill_level_list 补货奖设置
  20. * @property string|null $over_level_list 越级奖设置
  21. * @property string|null $over_level_list_2 越2级奖设置
  22. * @property string|null $price_difference 购买价差礼包价格
  23. * @property int $is_allow_fill 是否允许向平台补货
  24. * @property int $status 状态[-1:删除;0:禁用;1启用]
  25. * @property int $created_at
  26. * @property int $updated_at
  27. * @property string|null $monthly_purchase 月限购
  28. * @property string|null $additional_purchase_restrictions 增加额外限购
  29. * @property float|null $sort 排序值,小排前
  30. * @property int|null $min_replenish 最低进货数量,0不限制
  31. * @property int|null $warning_quantity 预警数量
  32. * @property string|null $restrict_permissions 限制权限
  33. * @property int|null $is_buhuo 补货赠送优惠券
  34. * @property int|null $is_peihuo 配货赠送优惠券
  35. */
  36. class CloudStockGoods extends BaseActiveRecord
  37. {
  38. private static $cloud_stock_goods = null;
  39. private static $goods = null;
  40. /**
  41. * {@inheritdoc}
  42. */
  43. public static function tableName()
  44. {
  45. return '{{%addons_cloud_stock_goods}}';
  46. }
  47. /**
  48. * {@inheritdoc}
  49. */
  50. public function rules()
  51. {
  52. return [
  53. [['mall_id', 'goods_id', 'goods_type', 'is_allow_fill', 'status', 'created_at', 'updated_at', 'is_take_by_range', 'is_find_equal', 'min_replenish',
  54. 'warning_quantity','is_buhuo','is_peihuo'], 'integer'],
  55. [['agent_price', 'equal_level_list', 'fill_level_list', 'over_level_list'], 'string', 'max' => 5120],
  56. [['monthly_purchase','additional_purchase_restrictions'], 'string'],
  57. [['over_level_list_2', 'restrict_permissions', 'price_difference'], 'string'],
  58. [['sort'], 'number'],
  59. ];
  60. }
  61. /**
  62. * {@inheritdoc}
  63. */
  64. public function attributeLabels()
  65. {
  66. return [
  67. 'id' => 'ID',
  68. 'mall_id' => '商城id',
  69. 'goods_id' => '商品id',
  70. 'agent_price' => '代理商进货价设置',
  71. 'goods_type' => '商品类型;0 商城商品',
  72. 'equal_level_list' => '平级奖设置',
  73. 'fill_level_list' => '补货奖设置',
  74. 'over_level_list' => '越级奖设置',
  75. 'over_level_list_2' => '越2级奖设置',
  76. 'is_allow_fill' => '是否允许向平台补货',
  77. 'is_take_by_range' => '是否开启区间拿货价',
  78. 'status' => '状态[-1:删除;0:禁用;1启用]',
  79. 'created_at' => 'Created At',
  80. 'updated_at' => 'Updated At',
  81. 'monthly_purchase' => '月限购',
  82. 'additional_purchase_restrictions' => '增加额外限购',
  83. 'price_difference' => '购买价差礼包价格',
  84. 'is_buhuo' => '补货赠送优惠券',
  85. 'is_peihuo' => '配货赠送优惠券',
  86. ];
  87. }
  88. public static function setData(array $params)
  89. {
  90. $t = Yii::$app->db->beginTransaction();
  91. try {
  92. $new_goods_id = $params['goods_id'];
  93. $query = self::find()->where(['mall_id' => $params['mall_id'], 'goods_id' => $params['goods_id'], 'status' => [StatusEnum::ENABLED, StatusEnum::DISABLED]]);
  94. $additional_purchase_restrictions= null;
  95. if (!empty($params['id'])) {
  96. $query->andWhere(['<>', 'id', $params['id']]);
  97. $model = self::findOne(['mall_id' => $params['mall_id'], 'id' => $params['id'], 'status' => [StatusEnum::ENABLED, StatusEnum::DISABLED]]);
  98. if (empty($model)) throw new \Exception('服务不存在或已删除');
  99. $additional_purchase_restrictions = $model['additional_purchase_restrictions'];
  100. $old_goods_id = $model->goods_id;
  101. if(!empty($new_goods_id) && $new_goods_id!=$old_goods_id){
  102. self::delCloudGoodsAfter($params['mall_id'],$old_goods_id);
  103. }
  104. } else {
  105. $model = new self();
  106. $model->loadDefaultValues();
  107. }
  108. if(empty($params['restrict_permissions'])){
  109. $params['restrict_permissions'] = [];
  110. }
  111. $params['restrict_permissions'] = json_encode($params['restrict_permissions']);
  112. $res = $query->one();
  113. if ($res) throw new \Exception('该商品已经被添加过');
  114. if (!$model->load($params, '')) throw new \Exception($model->getErrorMessage());
  115. if (!$model->save()) throw new \Exception($model->getErrorMessage());
  116. $t->commit();
  117. if(empty($params['id'])){
  118. CloudStockAgentGuota::asynComputeFillNums($params['mall_id']);
  119. }elseif (!empty($model)){
  120. if(!empty($new_goods_id)&&!empty($old_goods_id) && $new_goods_id!=$old_goods_id){
  121. CloudStockAgentGuota::asynComputeFillNums($params['mall_id']);
  122. }elseif(empty($params['additional_purchase_restrictions'])||$additional_purchase_restrictions!=$params['additional_purchase_restrictions']){
  123. CloudStockAgentGuota::asynComputeFillNums($params['mall_id']);
  124. }
  125. }
  126. return $model;
  127. } catch (\Exception $e) {
  128. $t->rollBack();
  129. self::$static_error = $e->getMessage();
  130. return false;
  131. }
  132. }
  133. public static function delCloudGoodsAfter($mall_id,$goods_id){
  134. CloudStockAgentGoods::updateAll(['status'=>StatusEnum::DELETE],['goods_id'=>$goods_id,'mall_id'=>$mall_id]);
  135. $data = ['goods_id'=>$goods_id,'mall_id'=>$mall_id];
  136. \Yii::$app->services->rabbitMq->push(RabbitMqEnum::EXCHANGE_TASK, RabbitMqEnum::TASK_QUEUE, $data, CloudStockAgentGuota::class, 'computeByGoodsDel');
  137. }
  138. public static function patch($params)
  139. {
  140. $model = self::getOne([['goods_id' => $params['goods_id']], ['in', 'status', [StatusEnum::DISABLED, StatusEnum::ENABLED]]]);
  141. if (empty($model)) throw new \Exception('服务不存在或已删除');
  142. if (isset($params['is_allow_fill']) && $params['is_allow_fill'] != '') {
  143. $model->is_allow_fill = $params['is_allow_fill'];
  144. }
  145. if (!$model->save()) {
  146. return false;
  147. }
  148. return true;
  149. }
  150. /**
  151. * @name:
  152. * @msg: 获取商品补货价格
  153. * @param {int} $mall_id
  154. * @param {int} $user_stock_agent_level 会员代理商等级
  155. * @param {array} $good_ids 商品id,必须是数组。如果获取一个商品的补货价格,把商品id放数组内
  156. * @param {int} $index 是否特定获取某一个商品的补货价格,0:否;如果需要获取特定商品补货价格,$index 传入商品 id
  157. * @return {*}
  158. */
  159. public static function getReplenshPrice(int $mall_id, int $user_stock_agent_level, array $good_ids, int $index = 0) : int
  160. {
  161. $good_ids = array_values($good_ids);
  162. if (empty(self::$cloud_stock_goods)) {
  163. $cloud_stock_goods = self::find()
  164. ->select('goods_id,agent_price,is_take_by_range')
  165. ->where(['mall_id' => $mall_id, 'status' => StatusEnum::ENABLED, 'goods_id' => $good_ids])
  166. ->asArray()
  167. ->indexBy('goods_id')
  168. ->all();
  169. self::$cloud_stock_goods = $cloud_stock_goods;
  170. } else {
  171. $cloud_stock_goods = self::$cloud_stock_goods;
  172. }
  173. if (empty(self::$goods)) {
  174. $mall_goods = Goods::find()
  175. ->select('id,goods_name,price,cover_pic')
  176. ->where(['mall_id' => $mall_id, 'status' => StatusEnum::ENABLED, 'id' => $good_ids, 'is_on_sale' => 1, 'is_attr' => 0])
  177. ->asArray()
  178. ->indexBy('id')
  179. ->all();
  180. self::$goods = $mall_goods;
  181. } else {
  182. $mall_goods = self::$goods;
  183. }
  184. if (1 == count($good_ids) && empty($index)) $index = $good_ids[0];
  185. if (!empty($index)) {
  186. $replenish_price = self::parseReplenishPrice($cloud_stock_goods[$index] ?? [], $mall_goods[$index] ?? [], $user_stock_agent_level);
  187. } else {
  188. foreach ($cloud_stock_goods as $index => $cloud_stock_good) {
  189. $replenish_price[$index] = self::parseReplenishPrice($cloud_stock_good, $mall_goods[$index] ?? [], $user_stock_agent_level);
  190. }
  191. }
  192. return $replenish_price;
  193. }
  194. /**
  195. * @name:
  196. * @msg: 提取补货价格
  197. * @param {CloudStockGoods} $cloud_stock_good
  198. * @param {Goods} $goods
  199. * @param {int} $user_stock_agent_level
  200. * @param {int} $index
  201. * @return {*}
  202. */
  203. private static function parseReplenishPrice(array $cloud_stock_good, array $good, int $user_stock_agent_level): int
  204. {
  205. $price = $good['price'] ?? 0;
  206. if (!empty($cloud_stock_good)) {
  207. $all_level_prices = json_decode($cloud_stock_good['agent_price'], true);
  208. foreach ($all_level_prices as $level_price) {
  209. if ($level_price['level'] == $user_stock_agent_level) {
  210. $agent_level_price = $level_price;
  211. }
  212. }
  213. if (!empty($agent_level_price) && 1 == $agent_level_price['is_use']) {
  214. $replenish_price = $agent_level_price['price'] ?: $price;
  215. } else {
  216. $replenish_price = $price;
  217. }
  218. } else {
  219. $replenish_price = $price;
  220. }
  221. if (!is_numeric($replenish_price)) {
  222. $replenish_price = $price;
  223. }
  224. return $replenish_price;
  225. }
  226. /**
  227. * @name:
  228. * @msg: 获取当前会员最大的补货数量
  229. * @param {int} $goods_id 商品 id
  230. * @param {int} $user_id 会员 id
  231. * @param {int} $mall_id 商城 id
  232. * @return mixed 有错误返回 false,否则返回得到的库存数量
  233. */
  234. public static function allPerantAgentGoodStocks(int $goods_id, int $user_id, int $mall_id)
  235. {
  236. $stock_good = self::findOne(['mall_id' => $mall_id, 'status' => StatusEnum::ENABLED, 'goods_id' => $goods_id]);
  237. if (empty($stock_good)) {
  238. self::$static_error = '云库存商品不存在';
  239. return false;
  240. }
  241. if (0 == $stock_good->is_allow_fill) {
  242. // 不允许向平台补货
  243. $current_agent_info = CloudStockAgent::findOne(['mall_id' => $mall_id, 'status' => StatusEnum::ENABLED, 'user_id' => $user_id]);
  244. $parent_id_arr = UserPartitionRelationship::getParentIdArr($user_id);
  245. $parent_id_arr = array_reverse($parent_id_arr);
  246. $configs = \Yii::$app->services->setting->addons->get($mall_id, AddonsEnum::ADDONS_NAME_CLOUD_STOCK, 'basic');
  247. $agent_user_ids = CloudStockAgent::find()
  248. ->select('user_id')
  249. ->where(['mall_id' => $mall_id, 'status' => StatusEnum::ENABLED, 'user_id' => $parent_id_arr])
  250. ->andWhere(['>', 'level', $current_agent_info->level])
  251. ->andWhere(!empty($configs['is_show_setting']) && !empty($configs['is_show_stock_permission']) ? ['=', 'is_stock_deducted_by_subordinate', StatusEnum::DISABLED] : []) // 如果开启了"前端用户库存权限",则只查允许被下级补货的代理
  252. ->asArray()
  253. ->column();
  254. $all_stocks = CloudStockAgentGoods::find()
  255. ->where(['mall_id' => $mall_id, 'status' => StatusEnum::ENABLED, 'goods_id' => $goods_id, 'user_id' => $agent_user_ids])
  256. ->sum('num');
  257. $all_stocks = $all_stocks ?? 0;
  258. } else {
  259. $good = Goods::findOne(['mall_id' => $mall_id, 'status' => StatusEnum::ENABLED, 'id' => $goods_id, 'is_on_sale' => 1, 'is_attr' => 0]);
  260. $all_stocks = $good->stock;
  261. }
  262. return $all_stocks;
  263. }
  264. /**
  265. * @name:
  266. * @msg: 当前会员是否可以补货当前商品
  267. * @param {int} $mall_id 商城 id
  268. * @param {int} $goods_id 商品 id
  269. * @param {int} $user_curr_level 当前会员的代理商等级
  270. * @return bool
  271. */
  272. public static function canFillGood(int $mall_id, int $goods_id, int $user_curr_level, int $user_id)
  273. {
  274. $stock_good = self::findOne(['mall_id' => $mall_id, 'status' => StatusEnum::ENABLED, 'goods_id' => $goods_id]);
  275. if (empty($stock_good)) {
  276. self::$static_error = '云库存商品不存在';
  277. return false;
  278. }
  279. $highest_level = CloudStockLevel::find()
  280. ->select('id,level,name')
  281. ->where(['mall_id' => $mall_id, 'status' => StatusEnum::ENABLED])
  282. ->orderBy('level desc')
  283. ->limit(1)
  284. ->one();
  285. if (0 == $stock_good->is_allow_fill && $highest_level->level == $user_curr_level) {
  286. self::$static_error = null;
  287. return false;
  288. } else {
  289. $res = self::allPerantAgentGoodStocks($goods_id, $user_id, $mall_id);
  290. if (empty($res)) {
  291. self::$static_error = '可补货数量不足';
  292. return false;
  293. }
  294. return true;
  295. }
  296. }
  297. }