CloudStockTwoGoods.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. <?php
  2. namespace addons\CloudStockTwo\common\models;
  3. use common\enums\StatusEnum;
  4. use common\models\base\BaseActiveRecord;
  5. use common\models\goods\Goods;
  6. use common\models\user\UserPartitionRelationship;
  7. use Yii;
  8. /**
  9. * This is the model class for table "{{%addons_cloud_stock_goods".
  10. *
  11. * @property int $id
  12. * @property int $mall_id 商城id
  13. * @property int $goods_id 商品id
  14. * @property string|null $agent_price 代理商进货价设置
  15. * @property int $goods_type 商品类型;0 商城商品
  16. * @property string|null $equal_level_list 平级奖设置
  17. * @property string|null $fill_level_list 补货奖设置
  18. * @property string|null $over_level_list 越级奖设置
  19. * @property string|null $over_level_list_2 越2级奖设置
  20. * @property int $is_allow_fill 是否允许向平台补货
  21. * @property int $status 状态[-1:删除;0:禁用;1启用]
  22. * @property int $created_at
  23. * @property int $updated_at
  24. */
  25. class CloudStockTwoGoods extends BaseActiveRecord
  26. {
  27. private static $cloud_stock_goods = null;
  28. private static $goods = null;
  29. /**
  30. * {@inheritdoc}
  31. */
  32. public static function tableName()
  33. {
  34. return '{{%addons_cloud_stock_two_goods}}';
  35. }
  36. /**
  37. * {@inheritdoc}
  38. */
  39. public function rules()
  40. {
  41. return [
  42. [['mall_id', 'goods_id', 'goods_type', 'is_allow_fill', 'status', 'created_at', 'updated_at'], 'integer'],
  43. [['agent_price', 'equal_level_list', 'fill_level_list', 'over_level_list'], 'string', 'max' => 5120],
  44. [['over_level_list_2'], 'string'],
  45. ];
  46. }
  47. /**
  48. * {@inheritdoc}
  49. */
  50. public function attributeLabels()
  51. {
  52. return [
  53. 'id' => 'ID',
  54. 'mall_id' => '商城id',
  55. 'goods_id' => '商品id',
  56. 'agent_price' => '代理商进货价设置',
  57. 'goods_type' => '商品类型;0 商城商品',
  58. 'equal_level_list' => '平级奖设置',
  59. 'fill_level_list' => '补货奖设置',
  60. 'over_level_list' => '越级奖设置',
  61. 'over_level_list_2' => '越2级奖设置',
  62. 'is_allow_fill' => '是否允许向平台补货',
  63. 'status' => '状态[-1:删除;0:禁用;1启用]',
  64. 'created_at' => 'Created At',
  65. 'updated_at' => 'Updated At',
  66. ];
  67. }
  68. public static function setData(array $params)
  69. {
  70. try {
  71. $query = self::find()->where(['mall_id' => $params['mall_id'], 'goods_id' => $params['goods_id'], 'status' => [StatusEnum::ENABLED, StatusEnum::DISABLED]]);
  72. if (!empty($params['id'])) {
  73. $query->andWhere(['<>', 'id', $params['id']]);
  74. $model = self::findOne(['mall_id' => $params['mall_id'], 'id' => $params['id'], 'status' => [StatusEnum::ENABLED, StatusEnum::DISABLED]]);
  75. if (empty($model)) throw new \Exception('服务不存在或已删除');
  76. } else {
  77. $model = new self();
  78. $model->loadDefaultValues();
  79. }
  80. $res = $query->one();
  81. if ($res) throw new \Exception('该商品已经被添加过');
  82. if (!$model->load($params, '')) throw new \Exception($model->getErrorMessage());
  83. if (!$model->save()) throw new \Exception($model->getErrorMessage());
  84. return $model;
  85. } catch (\Exception $e) {
  86. self::$static_error = $e->getMessage();
  87. return false;
  88. }
  89. }
  90. public static function patch($params)
  91. {
  92. $model = self::getOne([['goods_id' => $params['goods_id']]]);
  93. if (empty($model)) throw new \Exception('服务不存在或已删除');
  94. if (isset($params['is_allow_fill']) && $params['is_allow_fill'] != '') {
  95. $model->is_allow_fill = $params['is_allow_fill'];
  96. }
  97. if (!$model->save()) {
  98. return false;
  99. }
  100. return true;
  101. }
  102. /**
  103. * @name:
  104. * @msg: 获取商品补货价格
  105. * @param {int} $mall_id
  106. * @param {int} $user_stock_agent_level 会员代理商等级
  107. * @param {array} $good_ids 商品id,必须是数组。如果获取一个商品的补货价格,把商品id放数组内
  108. * @param {int} $index 是否特定获取某一个商品的补货价格,0:否;如果需要获取特定商品补货价格,$index 传入商品 id
  109. * @return {*}
  110. */
  111. public static function getReplenshPrice(int $mall_id, int $user_stock_agent_level, array $good_ids, int $index = 0) : float
  112. {
  113. $good_ids = array_values($good_ids);
  114. if (empty(self::$cloud_stock_goods)) {
  115. $cloud_stock_goods = self::find()
  116. ->select('goods_id,agent_price')
  117. ->where(['mall_id' => $mall_id, 'status' => StatusEnum::ENABLED, 'goods_id' => $good_ids])
  118. ->asArray()
  119. ->indexBy('goods_id')
  120. ->all();
  121. self::$cloud_stock_goods = $cloud_stock_goods;
  122. } else {
  123. $cloud_stock_goods = self::$cloud_stock_goods;
  124. }
  125. if (empty(self::$goods)) {
  126. $mall_goods = Goods::find()
  127. ->select('id,goods_name,price,cover_pic')
  128. ->where(['mall_id' => $mall_id, 'status' => StatusEnum::ENABLED, 'id' => $good_ids, 'is_on_sale' => 1, 'is_attr' => 0])
  129. ->asArray()
  130. ->indexBy('id')
  131. ->all();
  132. self::$goods = $mall_goods;
  133. } else {
  134. $mall_goods = self::$goods;
  135. }
  136. if (1 == count($good_ids) && empty($index)) $index = $good_ids[0];
  137. if (!empty($index)) {
  138. $replenish_price = self::parseReplenishPrice($cloud_stock_goods[$index] ?? [], $mall_goods[$index] ?? [], $user_stock_agent_level);
  139. } else {
  140. foreach ($cloud_stock_goods as $index => $cloud_stock_good) {
  141. $replenish_price[$index] = self::parseReplenishPrice($cloud_stock_good, $mall_goods[$index] ?? [], $user_stock_agent_level);
  142. }
  143. }
  144. return $replenish_price;
  145. }
  146. /**
  147. * @name:
  148. * @msg: 提取补货价格
  149. * @param {CloudStockTwoGoods} $cloud_stock_good
  150. * @param {Goods} $goods
  151. * @param {int} $user_stock_agent_level
  152. * @param {int} $index
  153. * @return {*}
  154. */
  155. private static function parseReplenishPrice(array $cloud_stock_good, array $good, int $user_stock_agent_level): float
  156. {
  157. $price = $good['price'] ?? 0;
  158. if (!empty($cloud_stock_good)) {
  159. $all_level_prices = json_decode($cloud_stock_good['agent_price'], true);
  160. foreach ($all_level_prices as $level_price) {
  161. if ($level_price['level'] == $user_stock_agent_level) {
  162. $agent_level_price = $level_price;
  163. }
  164. }
  165. if (!empty($agent_level_price) && 1 == $agent_level_price['is_use']) {
  166. $replenish_price = $agent_level_price['price'] ?: $price;
  167. } else {
  168. $replenish_price = $price;
  169. }
  170. } else {
  171. $replenish_price = $price;
  172. }
  173. if (!is_numeric($replenish_price)) {
  174. $replenish_price = $price;
  175. }
  176. return $replenish_price;
  177. }
  178. /**
  179. * @name:
  180. * @msg: 获取当前会员最大的补货数量
  181. * @param {int} $goods_id 商品 id
  182. * @param {int} $user_id 会员 id
  183. * @param {int} $mall_id 商城 id
  184. * @return mixed 有错误返回 false,否则返回得到的库存数量
  185. */
  186. public static function allPerantAgentGoodStocks(int $goods_id, int $user_id, int $mall_id)
  187. {
  188. $stock_good = self::findOne(['mall_id' => $mall_id, 'status' => StatusEnum::ENABLED, 'goods_id' => $goods_id]);
  189. if (empty($stock_good)) {
  190. self::$static_error = '云库存商品不存在';
  191. return false;
  192. }
  193. if (0 == $stock_good->is_allow_fill) {
  194. // 不允许向平台补货
  195. $current_agent_info = CloudStockTwoAgent::findOne(['mall_id' => $mall_id, 'status' => StatusEnum::ENABLED, 'user_id' => $user_id]);
  196. $parent_id_arr = UserPartitionRelationship::getParentIdArr($user_id);
  197. $parent_id_arr = array_reverse($parent_id_arr);
  198. $agent_user_ids = CloudStockTwoAgent::find()
  199. ->select('user_id')
  200. ->where(['mall_id' => $mall_id, 'status' => StatusEnum::ENABLED, 'user_id' => $parent_id_arr])
  201. ->andWhere(['>', 'level', $current_agent_info->level])
  202. ->asArray()
  203. ->column();
  204. $all_stocks = CloudStockTwoAgentGoods::find()
  205. ->where(['mall_id' => $mall_id, 'status' => StatusEnum::ENABLED, 'goods_id' => $goods_id, 'user_id' => $agent_user_ids])
  206. ->sum('num');
  207. $all_stocks = $all_stocks ?? 0;
  208. } else {
  209. $good = Goods::findOne(['mall_id' => $mall_id, 'status' => StatusEnum::ENABLED, 'id' => $goods_id, 'is_on_sale' => 1, 'is_attr' => 0]);
  210. $all_stocks = $good->stock;
  211. }
  212. return $all_stocks;
  213. }
  214. /**
  215. * @name:
  216. * @msg: 当前会员是否可以补货当前商品
  217. * @param {int} $mall_id 商城 id
  218. * @param {int} $goods_id 商品 id
  219. * @param {int} $user_curr_level 当前会员的代理商等级
  220. * @return bool
  221. */
  222. public static function canFillGood(int $mall_id, int $goods_id, int $user_curr_level, int $user_id)
  223. {
  224. $stock_good = self::findOne(['mall_id' => $mall_id, 'status' => StatusEnum::ENABLED, 'goods_id' => $goods_id]);
  225. if (empty($stock_good)) {
  226. self::$static_error = '云库存商品不存在';
  227. return false;
  228. }
  229. $highest_level = CloudStockTwoLevel::find()
  230. ->select('id,level,name')
  231. ->where(['mall_id' => $mall_id, 'status' => StatusEnum::ENABLED])
  232. ->orderBy('level desc')
  233. ->limit(1)
  234. ->one();
  235. if (0 == $stock_good->is_allow_fill && $highest_level->level == $user_curr_level) {
  236. self::$static_error = null;
  237. return false;
  238. } else {
  239. $res = self::allPerantAgentGoodStocks($goods_id, $user_id, $mall_id);
  240. if (empty($res)) {
  241. return false;
  242. }
  243. return true;
  244. }
  245. }
  246. }