| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168 |
- <?php
- namespace common\models\user;
- use common\enums\StatusEnum;
- use Yii;
- /**
- * This is the model class for table "{{%user_sign_agreement}}".
- *
- * @property int $id ID
- * @property int $mall_id Mall ID
- * @property int $user_id User ID
- * @property string $source_id Source ID
- * @property string $source 来源
- * @property int $status 状态[-1删除; 0禁用; 1正常]
- * @property int $created_at 创建时间
- * @property int $updated_at 更新时间
- */
- class UserSignAgreement extends \common\models\base\BaseActiveRecord
- {
- const SOURCE_POSTER = 'poster';
- const SOURCE_GOODS = 'goods';
- /**
- * {@inheritdoc}
- */
- public static function tableName()
- {
- return '{{%user_sign_agreement}}';
- }
- /**
- * {@inheritdoc}
- */
- public function rules()
- {
- return [
- [['mall_id', 'user_id', 'status', 'created_at', 'updated_at'], 'integer'],
- [['source'], 'string', 'max' => 16],
- [['source_id'], 'string']
- ];
- }
- /**
- * {@inheritdoc}
- */
- public function attributeLabels()
- {
- return [
- 'id' => 'ID',
- 'mall_id' => 'Mall ID',
- 'user_id' => 'User ID',
- 'source_id' => 'Source ID',
- 'source' => '来源',
- 'status' => '状态[-1删除; 0禁用; 1正常]',
- 'created_at' => '创建时间',
- 'updated_at' => '更新时间',
- ];
- }
- /**
- * @Description: 保存数据
- * @Author: zsan
- * @DateTime 2023-08-28 14:08:49
- * @param array $params
- * @return boolean|self
- */
- public static function setData(array $params)
- {
- try {
- $cond = ['mall_id' => $params['mall_id'], 'user_id' => $params['user_id'], 'source' => $params['source'], 'status' => StatusEnum::ENABLED];
- $model = self::findOne($cond);
- if (empty($model)) {
- $model = new self();
- $model->loadDefaultValues();
- }
- if (!$model->load($params, '') || !$model->save()) throw new \Exception($model->getErrorMessage());
- return $model;
- } catch (\Exception $e) {
- self::$static_error = $e->getMessage();
- return false;
- }
- }
- /**
- * @Description: 检测是否需要签署升级协议
- * @Author: zsan
- * @DateTime 2023-08-29 09:44:34
- * @param integer $cur_level 当前用户等级
- * @param array $ogoods_ids 当前下单商品ID
- * @param integer $mall_id 当前商城ID
- * @param integer $user_id 当前用户ID
- * @return integer
- */
- public static function checkIsNeedSign($cur_level, $ogoods_ids, $mall_id, $user_id)
- {
- try {
- $is_sign = 0;
- if ($cur_level > 0) throw new \Exception('user_level is reached');
- $upgrade_condition_list = UserLevelUpgradeCondition::lists([
- 'where' => [
- ['mall_id' => $mall_id],
- ['status' => StatusEnum::ENABLED],
- ['is_auto_upgrade' => StatusEnum::ENABLED],
- ['upgrade_type_goods' => StatusEnum::ENABLED],
- ['enable_goods_protocol' => StatusEnum::ENABLED]
- ],
- 'select' => 'goods_type, goods_ids, user_level_id',
- 'index' => 'user_level_id'
- ]);
- if (empty($upgrade_condition_list)) throw new \Exception('user level condition is empty');
- $user_level_list = UserLevel::lists([
- 'where' => [
- ['id' => array_column($upgrade_condition_list, 'user_level_id')],
- ['status' => StatusEnum::ENABLED]
- ],
- 'select' => 'id, level',
- 'order' => 'level desc'
- ]);
- if (empty($user_level_list)) throw new \Exception('user level list is empty');
- $sign_agreement_list = self::lists([
- 'where' => [
- ['user_id' => $user_id],
- ['mall_id' => $mall_id],
- ['source' => self::SOURCE_GOODS],
- ['status' => StatusEnum::ENABLED],
- ],
- 'select' => 'source_id'
- ]);
- foreach ($user_level_list as $val) {
- $goods_type = $upgrade_condition_list[$val['id']]['goods_type'];
- if ($goods_type == StatusEnum::ENABLED) {
- // 任意商品
- $record = UserSignAgreement::find()->where([
- 'and', ['status' => StatusEnum::ENABLED], ['source' => self::SOURCE_GOODS], ['mall_id' => $mall_id], ['user_id' => $user_id]
- ])->limit(1)->count();
- if ($record > 0) throw new \Exception('already sign');
- $is_sign = 1;
- break;
- } else {
- // 指定商品
- $goods_ids = $upgrade_condition_list[$val['id']]['goods_ids'];
- $goods_ids = json_decode($goods_ids, true) ?? [];
- $igoods_ids = array_intersect($goods_ids, $ogoods_ids);
- if (empty($igoods_ids)) continue;
- foreach ($sign_agreement_list as $sign_agreement) {
- $source_id = json_decode($sign_agreement['source_id'], true) ?? [];
- if (array_intersect($source_id, $ogoods_ids)) {
- throw new \Exception('this goods already sign');
- }
- }
- $is_sign = 1;
- break;
- }
- }
- } catch (\Exception $e) {
- $is_sign = 0;
- }
- return $is_sign;
- }
- public static function getUpgradeAgree($mall_id){
- $config = Yii::$app->services->setting->mall->get($mall_id, 'user_agree');
- return $config['upgrade_agree'] ?? '';
- }
- }
|