DoubleCopyLevel.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. <?php
  2. namespace addons\DoubleCopy\common\models;
  3. use addons\DoubleCopy\common\enums\DoubleCopyEnum;
  4. use common\enums\StatusEnum;
  5. use Yii;
  6. use yii\db\Exception;
  7. use yii\helpers\Json;
  8. /**
  9. * This is the model class for table "{{%addons_double_copy_level}}".
  10. *
  11. * @property int $id
  12. * @property int $mall_id 商城id
  13. * @property int $level 等级权重
  14. * @property string $name 等级名称
  15. * @property int $is_auto_upgrade 是否启用自动升级:0-不;1-是;
  16. * @property int $upgrade_type_condition 条件升级 0关闭 1 开启
  17. * @property int $condition_type 0 未选择、1满足其一 2、满足所有
  18. * @property string|null $checked_condition_keys 升级条件选中的key集合
  19. * @property string|null $checked_condition_values 升级条件
  20. * @property int $upgrade_type_goods 购买商品升级 0关闭 1 开启
  21. * @property int $buy_goods_type 0订单完成 1下单 完成
  22. * @property int $goods_type 商品升级类型
  23. * @property string|null $goods_ids 商品仓库的ID
  24. * @property string|null $goods_list 商品列表
  25. * @property string|null $explain 等级说明
  26. * @property int $open_ladder 是否开启阶梯解冻设置[0=否,1=是]
  27. * @property string|null $ladder_setting 阶梯解冻设置
  28. * @property int $status 是否启用
  29. * @property int $created_at
  30. * @property int $updated_at
  31. * @property int $store_cashier_order_is_up 收银台支付任意一笔订单升级:0否;1:是
  32. */
  33. class DoubleCopyLevel extends \common\models\base\BaseActiveRecord
  34. {
  35. /**
  36. * {@inheritdoc}
  37. */
  38. public static function tableName()
  39. {
  40. return '{{%addons_double_copy_level}}';
  41. }
  42. /**
  43. * {@inheritdoc}
  44. */
  45. public function rules()
  46. {
  47. return [
  48. [['mall_id', 'level', 'is_auto_upgrade', 'upgrade_type_condition', 'condition_type', 'upgrade_type_goods', 'buy_goods_type', 'goods_type', 'open_ladder', 'status',
  49. 'created_at', 'updated_at','upgrade_type_store_goods','buy_store_goods_type','store_goods_type','store_cashier_order_is_up'], 'integer'],
  50. [['checked_condition_keys', 'checked_condition_values', 'goods_ids', 'goods_list', 'explain', 'ladder_setting','store_goods_ids','store_goods_list'], 'string'],
  51. [['name'], 'string', 'max' => 45],
  52. ];
  53. }
  54. /**
  55. * {@inheritdoc}
  56. */
  57. public function attributeLabels()
  58. {
  59. return [
  60. 'id' => 'ID',
  61. 'mall_id' => '商城id',
  62. 'level' => '等级权重',
  63. 'name' => '等级名称',
  64. 'is_auto_upgrade' => '是否启用自动升级:0-不;1-是;',
  65. 'upgrade_type_condition' => '条件升级 0关闭 1 开启',
  66. 'condition_type' => '0 未选择、1满足其一 2、满足所有',
  67. 'checked_condition_keys' => '升级条件选中的key集合',
  68. 'checked_condition_values' => '升级条件',
  69. 'upgrade_type_goods' => '购买商品升级 0关闭 1 开启',
  70. 'buy_goods_type' => '0订单完成 1下单 完成',
  71. 'goods_type' => '商品升级类型',
  72. 'goods_ids' => '商品仓库的ID',
  73. 'goods_list' => '商品列表',
  74. 'explain' => '等级说明',
  75. 'open_ladder' => '是否开启阶梯解冻设置[0=否,1=是]',
  76. 'ladder_setting' => '阶梯解冻设置',
  77. 'status' => '是否启用',
  78. 'created_at' => 'Created At',
  79. 'updated_at' => 'Updated At',
  80. 'store_cashier_order_is_up' => '收银台支付任意一笔订单升级:0否;1:是',
  81. ];
  82. }
  83. /**
  84. * 获取等级配置
  85. * @Author: lun
  86. * @DateTime: 2022/5/14 0014 9:57
  87. * @Copyright: copyright (c) 2021 广东七件事集团
  88. * @return \yii\db\ActiveQuery
  89. */
  90. public function getSetting()
  91. {
  92. return $this->hasOne(DoubleCopyLevelSetting::class, ['level_id' => 'id'])->andWhere(['commission_type' => 'commission']);
  93. }
  94. /**
  95. * 获取等级配置
  96. * @Author: lun
  97. * @DateTime: 2022/5/14 0014 9:57
  98. * @Copyright: copyright (c) 2021 广东七件事集团
  99. * @return \yii\db\ActiveQuery
  100. */
  101. public function getScoreSetting()
  102. {
  103. return $this->hasOne(DoubleCopyLevelSetting::class, ['level_id' => 'id'])->andWhere(['commission_type' => 'score']);
  104. }
  105. /**
  106. * 获取等级配置
  107. * @Author: lun
  108. * @DateTime: 2022/5/14 0014 9:57
  109. * @Copyright: copyright (c) 2021 广东七件事集团
  110. * @return \yii\db\ActiveQuery
  111. */
  112. public function getAdditionalScoreSetting()
  113. {
  114. return $this->hasOne(DoubleCopyLevelSetting::class, ['level_id' => 'id'])->andWhere(['commission_type' => 'additional_score']);
  115. }
  116. /**
  117. * 获取权重
  118. * @Author: lun
  119. * @DateTime: 2022/5/14 0014 10:00
  120. * @Copyright: copyright (c) 2021 广东七件事集团
  121. * @param $mall_id
  122. * @return array
  123. */
  124. public static function getLevelWeights($mall_id) {
  125. $list = self::find()->select('level')->where(['status' => [StatusEnum::DISABLED,StatusEnum::ENABLED], 'mall_id' => $mall_id])->column();
  126. $newList = [];
  127. for ($i = 1; $i <= 15; $i++) {
  128. $newList[] = [
  129. 'name' => '权重' . $i,
  130. 'level' => $i,
  131. 'disabled' => in_array($i, $list),
  132. ];
  133. }
  134. return $newList;
  135. }
  136. /**
  137. * 保存数据
  138. * @Author: lun
  139. * @DateTime: 2022/5/14 0014 10:09
  140. * @Copyright: copyright (c) 2021 广东七件事集团
  141. * @param array $params
  142. * @return array|bool
  143. */
  144. public static function setData(array $params){
  145. // 因为创建等级时, 可能等级设置等数据没有成功导致数据无法显示所以这里需要事务
  146. $transaction = \Yii::$app->db->beginTransaction();
  147. try{
  148. if(!empty($params['id'])){
  149. $model = self::findOne(['and',['id' => $params['id'],'mall_id' => $params['mall_id']],['<>','status',StatusEnum::DELETE]]);
  150. if(empty($model)) throw new \Exception('等级不存在或已删除');
  151. // 判断等级是否有被修改
  152. if ($model->level != $params['level']) {
  153. // 判断该等级下是否有股东存在
  154. $user = DoubleCopyUser::find()->where(['mall_id' => $model->mall_id, 'level' => $model->level, 'status' => StatusEnum::ENABLED])->asArray()->one();
  155. if ($user) throw new Exception('该等级下存在用户,不能被修改');
  156. }
  157. }else{
  158. $model = new self();
  159. $model->loadDefaultValues();
  160. $model->mall_id = $params['mall_id'];
  161. }
  162. if(!$model->load($params,'') || !$model->save()){
  163. throw new Exception($model->getErrorMessage());
  164. }
  165. // 插入配置详情
  166. $setting = ['mall_id' => $params['mall_id'], 'level_id' => $model->id, 'level' => $model->level];
  167. $setting = empty($params['setting']) ? $setting : array_merge($setting, Json::decode($params['setting'], true));
  168. $setting['commission_type'] = isset($setting['commission_type']) ? $setting['commission_type'] : 'commission';
  169. $setting['status'] = $model->status;
  170. $res = DoubleCopyLevelSetting::setData($setting);
  171. if ($res == false) throw new Exception(DoubleCopyLevelSetting::getStaticError());
  172. $score_setting = ['mall_id' => $params['mall_id'], 'level_id' => $model->id, 'level' => $model->level];
  173. $score_setting = empty($params['score_setting']) ? $score_setting : array_merge($score_setting, Json::decode($params['score_setting'], true));
  174. $score_setting['status'] = $model->status;
  175. $score_setting['freeze_fund_prize'] = $setting['freeze_fund_prize'] ?? 0;
  176. $score_setting['freeze_fund_num'] = $setting['freeze_fund_num'] ?? 0;
  177. $score_setting['is_thaw'] = $setting['is_thaw'] ?? 0;
  178. $score_setting['first_level'] = $setting['first_level'] ?? '';
  179. $score_setting['push_num'] = $setting['push_num'] ?? 0;
  180. $score_setting['second_level'] = $setting['second_level'] ?? '';
  181. $score_setting['commission_type'] = isset($score_setting['commission_type']) ? $score_setting['commission_type'] : 'score';
  182. $res = DoubleCopyLevelSetting::setData($score_setting);
  183. if ($res == false) throw new Exception(DoubleCopyLevelSetting::getStaticError());
  184. // 额外积分
  185. $additional_score_setting = ['mall_id' => $params['mall_id'], 'level_id' => $model->id, 'level' => $model->level];
  186. $additional_score_setting = empty($params['additional_score_setting']) ? $additional_score_setting : array_merge($additional_score_setting, Json::decode($params['additional_score_setting'], true));
  187. $additional_score_setting['status'] = $model->status;
  188. $additional_score_setting['freeze_fund_prize'] = $setting['freeze_fund_prize'] ?? 0;
  189. $additional_score_setting['freeze_fund_num'] = $setting['freeze_fund_num'] ?? 0;
  190. $additional_score_setting['is_thaw'] = $setting['is_thaw'] ?? 0;
  191. $additional_score_setting['first_level'] = $setting['first_level'] ?? '';
  192. $additional_score_setting['push_num'] = $setting['push_num'] ?? 0;
  193. $additional_score_setting['second_level'] = $setting['second_level'] ?? '';
  194. $additional_score_setting['commission_type'] = isset($additional_score_setting['commission_type']) ? $additional_score_setting['commission_type'] : 'additional_score';
  195. $res = DoubleCopyLevelSetting::setData($additional_score_setting);
  196. if ($res == false) throw new Exception(DoubleCopyLevelSetting::getStaticError());
  197. $transaction->commit();
  198. return $model->toArray();
  199. }catch(\Exception $e){
  200. $transaction->rollBack();
  201. self::$static_error = $e->getMessage();
  202. return false;
  203. }
  204. }
  205. /**
  206. * 安装插件初始化等级
  207. * @Author: lun
  208. * @DateTime: 2022/5/14 0014 11:03
  209. * @Copyright: copyright (c) 2021 广东七件事集团
  210. */
  211. public static function InitLevel($init_level)
  212. {
  213. try {
  214. foreach ($init_level as $item) {
  215. $level_id = self::find()->select('id')->where(['mall_id' => $item['mall_id'], 'level' => $item['level']])->andWhere(['<>', 'status', StatusEnum::DELETE])->scalar();
  216. if (empty($level_id)) {
  217. $res = self::setData($item);
  218. if ($res == false) throw new \Exception(self::getStaticError());
  219. }
  220. }
  221. return true;
  222. } catch (\Exception $e) {
  223. self::setStaticError("初始化等级失败:" . $e->getMessage());
  224. return false;
  225. }
  226. }
  227. /**
  228. * 获取等级
  229. * @Author: lun
  230. * @DateTime: 2022/5/14 0014 15:23
  231. * @Copyright: copyright (c) 2021 广东七件事集团
  232. * @param $mall_id
  233. * @return array|\yii\db\ActiveRecord[]
  234. */
  235. public static function getLevelMap($mall_id, $type = 1)
  236. {
  237. $select = "level,name";
  238. $list = self::find()->select($select)->where(['mall_id' => $mall_id,'status' => [StatusEnum::ENABLED, StatusEnum::DISABLED]])->orderBy('level asc')->asArray()->all();
  239. if ($type == 2) {
  240. $new_list = [];
  241. foreach ($list as $item) {
  242. $new_list[$item['level']] = $item['name'];
  243. }
  244. return $new_list;
  245. }
  246. return $list;
  247. }
  248. /**
  249. * 根据等级获取等级名称
  250. * @Author: lun
  251. * @DateTime: 2022/5/16 0016 17:45
  252. * @Copyright: copyright (c) 2021 广东七件事集团
  253. * @param $mall_id
  254. * @param $level
  255. * @return false|int|string|null
  256. */
  257. public static function getLevelNameByLevel($mall_id, $level)
  258. {
  259. return self::find()->select('name')->where(['mall_id' => $mall_id, 'level' => $level, 'status' => StatusEnum::ENABLED])->scalar();
  260. }
  261. /**
  262. * 获取推荐的位数
  263. * @param $mall_id
  264. * @return void
  265. */
  266. public static function getUpgradeNum($mall_id)
  267. {
  268. $boss_level = self::find()->select('upgrade_type_condition, checked_condition_values')->where(['mall_id' => $mall_id, 'level' => DoubleCopyEnum::LEVEL_BOSS, 'status' => StatusEnum::ENABLED])->asArray()->one();
  269. if ($boss_level['upgrade_type_condition'] == 1) {
  270. // json解析升级条件数量
  271. $json = Json::decode($boss_level['checked_condition_values'], true);
  272. return !empty($json[0]['value']['val0']) ? intval($json[0]['value']['val0']) : 0;
  273. }
  274. return 0;
  275. }
  276. }