ServeBonusLevel.php 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. <?php
  2. namespace addons\ServeBonus\common\models;
  3. use common\enums\StatusEnum;
  4. use Yii;
  5. use yii\db\Exception;
  6. use yii\helpers\Json;
  7. /**
  8. * This is the model class for table "qimall_addons_servebonus_level".
  9. *
  10. * @property int $id
  11. * @property int $mall_id 商城ID
  12. * @property int $level 服务商等级
  13. * @property string $name 等级名称
  14. * @property int $is_auto_upgrade 是否自动升级【0否,1是】
  15. * @property int $upgrade_type_condition 是否开启条件升级【0否,1是】
  16. * @property int $condition_type 满足条件:1满足其一,2满足所有
  17. * @property string|null $checked_condition_values 升级条件的值集合
  18. * @property string|null $checked_condition_keys 被选中的升级条件的key集合
  19. * @property int $upgrade_type_goods 是否开启购买商品升级【0否,1是】
  20. * @property string|null $goods_ids 指定商品id集合
  21. * @property string|null $goods_list 指定商品信息列表json
  22. * @property int $goods_type 商品升级类型:1任意商品,2指定商品
  23. * @property int $buy_goods_type 购买商品升级条件:0订单完成,1支付完成
  24. * @property string|null $explain 等级说明
  25. * @property int|null $status 状态[-1:删除;0:禁用;1启用]
  26. * @property int $created_at
  27. * @property int $updated_at
  28. * @property int $serve_bonus_number //服务商人数
  29. * @property int $is_to_bonus 服务商分红
  30. * @property float $to_bonus_price 达到指定金额停止分红
  31. */
  32. class ServeBonusLevel extends \common\models\base\BaseActiveRecord
  33. {
  34. /**
  35. * {@inheritdoc}
  36. */
  37. public static function tableName()
  38. {
  39. return '{{%addons_servebonus_level}}';
  40. }
  41. /**
  42. * {@inheritdoc}
  43. */
  44. public function rules()
  45. {
  46. return [
  47. [['mall_id', 'level', 'is_auto_upgrade', 'upgrade_type_condition', 'serve_bonus_number', 'condition_type', 'upgrade_type_goods', 'goods_type', 'buy_goods_type', 'status', 'created_at', 'updated_at','is_to_bonus'], 'integer'],
  48. [['checked_condition_values', 'checked_condition_keys', 'explain'], 'string'],
  49. [['name'], 'string', 'max' => 45],
  50. [['to_bonus_price'], 'number'],
  51. [['goods_ids'], 'string', 'max' => 3000],
  52. [['goods_list'], 'string', 'max' => 5000],
  53. ];
  54. }
  55. /**
  56. * {@inheritdoc}
  57. */
  58. public function attributeLabels()
  59. {
  60. return [
  61. 'id' => 'ID',
  62. 'mall_id' => '商城ID',
  63. 'level' => '服务商等级',
  64. 'name' => '等级名称',
  65. 'is_auto_upgrade' => '是否自动升级【0否,1是】',
  66. 'upgrade_type_condition' => '是否开启条件升级【0否,1是】',
  67. 'condition_type' => '满足条件:1满足其一,2满足所有',
  68. 'checked_condition_values' => '升级条件的值集合',
  69. 'checked_condition_keys' => '被选中的升级条件的key集合',
  70. 'upgrade_type_goods' => '是否开启购买商品升级【0否,1是】',
  71. 'goods_ids' => '指定商品id集合',
  72. 'goods_list' => '指定商品信息列表json',
  73. 'goods_type' => '商品升级类型:1任意商品,2指定商品',
  74. 'buy_goods_type' => '购买商品升级条件:0订单完成,1支付完成',
  75. 'explain' => '等级说明',
  76. 'status' => '状态[-1:删除;0:禁用;1启用]',
  77. 'created_at' => 'Created At',
  78. 'updated_at' => 'Updated At',
  79. 'servebonus_muber' => '服务商人数',
  80. 'is_to_bonus' => '服务商分红',
  81. 'to_bonus_price' => '达到指定金额停止分红',
  82. ];
  83. }
  84. public function getSetting()
  85. {
  86. return $this->hasOne(ServeBonusLevelSetting::class, ['level' => 'level']);
  87. }
  88. /**
  89. * 获取等级列表
  90. * @Author: ltm
  91. * @DateTime: 2021/10/21 0021 9:20
  92. * @Copyright: copyright (c) 2021 广东七件事集团
  93. * @param array $cond
  94. * @param array $with
  95. * @param bool $is_array
  96. * @param string $select
  97. * @return array|\yii\db\ActiveRecord[]
  98. */
  99. public static function getServeBonusLevel(array $cond=[], array $with=[],bool $is_array=true,string $select='*',$order=""){
  100. $default_cond = [['=','status',StatusEnum::ENABLED]];
  101. $cond = array_merge($default_cond,$cond);
  102. $query = self::find()->select($select);
  103. self::paramPack(['where'=>$cond, 'with'=>$with],$query);
  104. isset($order) && $query->orderBy($order);
  105. $data = $query->asArray($is_array)->all();
  106. return $data;
  107. }
  108. /**
  109. * 获取等级等级和权重
  110. * @Author: ltm
  111. * @DateTime: 2021/10/21 0021 9:22
  112. * @Copyright: copyright (c) 2021 广东七件事集团
  113. * @param $mall_id
  114. * @return array
  115. */
  116. public static function getLevelWeights($mall_id) {
  117. $list = self::find()->select('level')->where(['status' => [StatusEnum::DISABLED,StatusEnum::ENABLED], 'mall_id' => $mall_id])->column();
  118. $newList = [];
  119. for ($i = 1; $i <= 10; $i++) {
  120. $newList[] = [
  121. 'name' => '等级' . $i,
  122. 'level' => $i,
  123. 'disabled' => in_array($i, $list),
  124. ];
  125. }
  126. return $newList;
  127. }
  128. /**
  129. * 保存数据
  130. * @Author: ltm
  131. * @DateTime: 2021/10/20 0020 18:01
  132. * @Copyright: copyright (c) 2021 广东七件事集团
  133. * @param array $params
  134. * @return ServeBonusLevel|bool
  135. */
  136. public static function setData(array $params){
  137. try{
  138. if(!empty($params['id'])){
  139. $model = self::findOne(['and',['id' => $params['id'],'mall_id' => $params['mall_id']],['<>','status',StatusEnum::DELETE]]);
  140. if(empty($model)) throw new \Exception('等级不存在或已删除');
  141. // 判断等级是否有被修改
  142. if (!empty($params['level']) && $model->level != $params['level'] || in_array($params['status'], [StatusEnum::DISABLED, StatusEnum::DELETE])) {
  143. // 判断该等级下是否有服务商存在
  144. $ServeBonus = ServeBonus::find()->where(['mall_id' => $model->mall_id, 'level' => $model->level, 'status' => StatusEnum::ENABLED])->asArray()->one();
  145. if ($ServeBonus) throw new Exception('该等级下存在服务商,不能被修改或禁用/删除');
  146. }
  147. }else{
  148. $model = new self();
  149. $model->loadDefaultValues();
  150. $model->mall_id = $params['mall_id'];
  151. }
  152. if(!$model->load($params,'') || !$model->save()){
  153. throw new Exception($model->getErrorMessage());
  154. }
  155. // 插入配置详情
  156. $setting = ['mall_id' => $params['mall_id'], 'level' => $model->level, 'status' => $model->status];
  157. $setting = empty($params['setting']) ? $setting : array_merge($setting, Json::decode($params['setting'], true));
  158. $res = ServeBonusLevelSetting::setData($setting);
  159. if ($res == false) throw new Exception(ServeBonusLevelSetting::getStaticError());
  160. return $model;
  161. }catch(\Exception $e){
  162. self::$static_error = $e->getMessage();
  163. return false;
  164. }
  165. }
  166. /**
  167. * @Author: ltm
  168. * @DateTime: 2022/2/10 0010 15:17
  169. * @Copyright: copyright (c) 2021 广东七件事集团
  170. * @param $level['level'] //等级 $level['num']//等级数量
  171. * @return string
  172. */
  173. public static function statisticsServeBonus($level,$last_level=[],$mall_id=0){
  174. //升级后等级进行加
  175. if($level&&$level['level']){
  176. $model = self::findOne(['level' => $level['level'],'mall_id' => $mall_id,'status' => StatusEnum::ENABLED]);
  177. if($model){
  178. $model -> serve_bonus_number += $level['num'];
  179. $model->save();
  180. }
  181. }
  182. //升级前等级进行扣除
  183. if($last_level&&$last_level['level']){
  184. $last_model = self::findOne(['level' => $last_level['level'],'mall_id' => $mall_id, 'status' => StatusEnum::ENABLED]);
  185. if($last_model){
  186. $last_model -> serve_bonus_number -= $last_level['num'];
  187. $last_model->save();
  188. }
  189. }
  190. }
  191. /**
  192. * 获取升级列表
  193. * @Author: ltm
  194. * @DateTime: 2021/10/21 0021 15:05
  195. * @Copyright: copyright (c) 2021 广东七件事集团
  196. * @param $params
  197. * @return array
  198. */
  199. public static function getListByUpgrade($params)
  200. {
  201. $rows = [];
  202. if (!empty($params['mall_id'])) {
  203. $where = ['mall_id' => $params['mall_id'],'status'=>StatusEnum::ENABLED];
  204. $list = self::findAll($where);
  205. foreach ($list as $v) {
  206. $rows[] = ['level' => $v['level'], 'name' => $v['name']];
  207. }
  208. }
  209. return $rows;
  210. }
  211. /**
  212. * 获取等级
  213. * @Author: ltm
  214. * @DateTime: 2021/12/16 0016 9:17
  215. * @Copyright: copyright (c) 2021 广东七件事集团
  216. * @param $mall_id
  217. * @return array
  218. */
  219. public static function getLevelByColumn($mall_id){
  220. return self::find()
  221. ->select('name')
  222. ->where(array('mall_id'=>$mall_id,'status'=>StatusEnum::ENABLED))
  223. ->asArray()
  224. ->indexBy('level')
  225. ->orderBy('level desc')
  226. ->column();
  227. }
  228. }