BossAchievementLevelSetting.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. <?php
  2. namespace addons\BossAchievement\common\models;
  3. use common\enums\StatusEnum;
  4. use Yii;
  5. use yii\helpers\Json;
  6. /**
  7. * This is the model class for table "{{%addons_boss_achievement_level_setting}}".
  8. *
  9. * @property int $id
  10. * @property int $mall_id 商城ID
  11. * @property int $level_id 对应qimall_addons_boss_achievement_level.id
  12. * @property int $level
  13. * @property string $commission_type 分佣类型:积分score;佣金:commission
  14. * @property string $from_type 分佣类型:股东;
  15. * @property float $standard_money 达标业绩,单位:元
  16. * @property float $prize 业绩分红,单位:百分比
  17. * @property string|null $achievement_goods_ids 指定商品id集合
  18. * @property int $status 状态[-1:删除;0:禁用;1启用]
  19. * @property int $created_at
  20. * @property int $updated_at
  21. */
  22. class BossAchievementLevelSetting extends \common\models\base\BaseActiveRecord
  23. {
  24. /**
  25. * {@inheritdoc}
  26. */
  27. public static function tableName()
  28. {
  29. return '{{%addons_boss_achievement_level_setting}}';
  30. }
  31. /**
  32. * {@inheritdoc}
  33. */
  34. public function rules()
  35. {
  36. return [
  37. [['mall_id'], 'required'],
  38. [['mall_id', 'level_id', 'level', 'status', 'created_at', 'updated_at'], 'integer'],
  39. [['standard_money', 'prize'], 'number'],
  40. [['commission_type', 'from_type'], 'string', 'max' => 255],
  41. [['achievement_goods_ids'], 'string', 'max' => 3000],
  42. ];
  43. }
  44. /**
  45. * {@inheritdoc}
  46. */
  47. public function attributeLabels()
  48. {
  49. return [
  50. 'id' => 'ID',
  51. 'mall_id' => '商城ID',
  52. 'level_id' => '对应qimall_addons_boss_achievement_level.id',
  53. 'level' => '等级',
  54. 'commission_type' => '分佣类型:积分score;佣金:commission',
  55. 'from_type' => '分佣类型:股东;',
  56. 'standard_money' => '达标业绩,单位:元',
  57. 'prize' => '业绩分红,单位:百分比',
  58. 'achievement_goods_ids' => '指定商品id集合',
  59. 'status' => '状态[-1:删除;0:禁用;1启用]',
  60. 'created_at' => 'Created At',
  61. 'updated_at' => 'Updated At',
  62. ];
  63. }
  64. /**
  65. * 保存数据
  66. * @Author: lun
  67. * @DateTime: 2021/11/4 0004 14:23
  68. * @Copyright: copyright (c) 2021 广东七件事集团
  69. * @param $params
  70. * @return bool
  71. */
  72. public static function setData($params)
  73. {
  74. try{
  75. $model = self::findOne(['mall_id' => $params['mall_id'], 'level_id' => $params['level_id']]);
  76. if (empty($model)) {
  77. $model = new self();
  78. $model->mall_id = $params['mall_id'];
  79. $model->level = $params['level'];
  80. $model->loadDefaultValues();
  81. }
  82. // 检测是否有业绩商品修改
  83. if ($model->achievement_goods_ids != $params['achievement_goods_ids']) {
  84. $old_goods = Json::decode($model->achievement_goods_ids, true) ?? [];
  85. $new_goods = Json::decode($params['achievement_goods_ids'], true) ?? [];
  86. $diff = array_diff($old_goods, $new_goods);
  87. if ($diff) {
  88. // 查询该商品的当月业绩
  89. $has_achievement = BossAchievementTeam::find()->where(['goods_id' => $diff, 'month' => date('Ym', time()), 'status' => StatusEnum::ENABLED])->asArray()->one();
  90. if ($has_achievement) throw new \Exception('无法更换商品,因为该商品本月已有业绩产生');
  91. }
  92. }
  93. if(!$model->load($params,'') || !$model->save()){
  94. throw new \Exception($model->getErrorMessage());
  95. }
  96. return true;
  97. }catch(\Exception $e){
  98. self::$static_error = $e->getMessage();
  99. return false;
  100. }
  101. }
  102. /**
  103. * 获取所有等级的业绩商品
  104. * @Author: lun
  105. * @DateTime: 2022/7/10 0010 16:06
  106. * @Copyright: copyright (c) 2021 广东七件事集团
  107. * @param $mall_id
  108. */
  109. public static function getAllAchievementGoodsIds($mall_id)
  110. {
  111. $goods_ids = [];
  112. // 获取所有等级
  113. $list = self::find()->select('level,achievement_goods_ids')->where(['mall_id' => $mall_id, 'status' => StatusEnum::ENABLED])->indexBy('level')->asArray()->all();
  114. foreach ($list as $item) {
  115. $goods_ids = array_merge($goods_ids, Json::decode($item['achievement_goods_ids'], true));
  116. }
  117. return $goods_ids;
  118. }
  119. }