UserSignAgreement.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. <?php
  2. namespace common\models\user;
  3. use common\enums\StatusEnum;
  4. use Yii;
  5. /**
  6. * This is the model class for table "{{%user_sign_agreement}}".
  7. *
  8. * @property int $id ID
  9. * @property int $mall_id Mall ID
  10. * @property int $user_id User ID
  11. * @property string $source_id Source ID
  12. * @property string $source 来源
  13. * @property int $status 状态[-1删除; 0禁用; 1正常]
  14. * @property int $created_at 创建时间
  15. * @property int $updated_at 更新时间
  16. */
  17. class UserSignAgreement extends \common\models\base\BaseActiveRecord
  18. {
  19. const SOURCE_POSTER = 'poster';
  20. const SOURCE_GOODS = 'goods';
  21. /**
  22. * {@inheritdoc}
  23. */
  24. public static function tableName()
  25. {
  26. return '{{%user_sign_agreement}}';
  27. }
  28. /**
  29. * {@inheritdoc}
  30. */
  31. public function rules()
  32. {
  33. return [
  34. [['mall_id', 'user_id', 'status', 'created_at', 'updated_at'], 'integer'],
  35. [['source'], 'string', 'max' => 16],
  36. [['source_id'], 'string']
  37. ];
  38. }
  39. /**
  40. * {@inheritdoc}
  41. */
  42. public function attributeLabels()
  43. {
  44. return [
  45. 'id' => 'ID',
  46. 'mall_id' => 'Mall ID',
  47. 'user_id' => 'User ID',
  48. 'source_id' => 'Source ID',
  49. 'source' => '来源',
  50. 'status' => '状态[-1删除; 0禁用; 1正常]',
  51. 'created_at' => '创建时间',
  52. 'updated_at' => '更新时间',
  53. ];
  54. }
  55. /**
  56. * @Description: 保存数据
  57. * @Author: zsan
  58. * @DateTime 2023-08-28 14:08:49
  59. * @param array $params
  60. * @return boolean|self
  61. */
  62. public static function setData(array $params)
  63. {
  64. try {
  65. $cond = ['mall_id' => $params['mall_id'], 'user_id' => $params['user_id'], 'source' => $params['source'], 'status' => StatusEnum::ENABLED];
  66. $model = self::findOne($cond);
  67. if (empty($model)) {
  68. $model = new self();
  69. $model->loadDefaultValues();
  70. }
  71. if (!$model->load($params, '') || !$model->save()) throw new \Exception($model->getErrorMessage());
  72. return $model;
  73. } catch (\Exception $e) {
  74. self::$static_error = $e->getMessage();
  75. return false;
  76. }
  77. }
  78. /**
  79. * @Description: 检测是否需要签署升级协议
  80. * @Author: zsan
  81. * @DateTime 2023-08-29 09:44:34
  82. * @param integer $cur_level 当前用户等级
  83. * @param array $ogoods_ids 当前下单商品ID
  84. * @param integer $mall_id 当前商城ID
  85. * @param integer $user_id 当前用户ID
  86. * @return integer
  87. */
  88. public static function checkIsNeedSign($cur_level, $ogoods_ids, $mall_id, $user_id)
  89. {
  90. try {
  91. $is_sign = 0;
  92. if ($cur_level > 0) throw new \Exception('user_level is reached');
  93. $upgrade_condition_list = UserLevelUpgradeCondition::lists([
  94. 'where' => [
  95. ['mall_id' => $mall_id],
  96. ['status' => StatusEnum::ENABLED],
  97. ['is_auto_upgrade' => StatusEnum::ENABLED],
  98. ['upgrade_type_goods' => StatusEnum::ENABLED],
  99. ['enable_goods_protocol' => StatusEnum::ENABLED]
  100. ],
  101. 'select' => 'goods_type, goods_ids, user_level_id',
  102. 'index' => 'user_level_id'
  103. ]);
  104. if (empty($upgrade_condition_list)) throw new \Exception('user level condition is empty');
  105. $user_level_list = UserLevel::lists([
  106. 'where' => [
  107. ['id' => array_column($upgrade_condition_list, 'user_level_id')],
  108. ['status' => StatusEnum::ENABLED]
  109. ],
  110. 'select' => 'id, level',
  111. 'order' => 'level desc'
  112. ]);
  113. if (empty($user_level_list)) throw new \Exception('user level list is empty');
  114. $sign_agreement_list = self::lists([
  115. 'where' => [
  116. ['user_id' => $user_id],
  117. ['mall_id' => $mall_id],
  118. ['source' => self::SOURCE_GOODS],
  119. ['status' => StatusEnum::ENABLED],
  120. ],
  121. 'select' => 'source_id'
  122. ]);
  123. foreach ($user_level_list as $val) {
  124. $goods_type = $upgrade_condition_list[$val['id']]['goods_type'];
  125. if ($goods_type == StatusEnum::ENABLED) {
  126. // 任意商品
  127. $record = UserSignAgreement::find()->where([
  128. 'and', ['status' => StatusEnum::ENABLED], ['source' => self::SOURCE_GOODS], ['mall_id' => $mall_id], ['user_id' => $user_id]
  129. ])->limit(1)->count();
  130. if ($record > 0) throw new \Exception('already sign');
  131. $is_sign = 1;
  132. break;
  133. } else {
  134. // 指定商品
  135. $goods_ids = $upgrade_condition_list[$val['id']]['goods_ids'];
  136. $goods_ids = json_decode($goods_ids, true) ?? [];
  137. $igoods_ids = array_intersect($goods_ids, $ogoods_ids);
  138. if (empty($igoods_ids)) continue;
  139. foreach ($sign_agreement_list as $sign_agreement) {
  140. $source_id = json_decode($sign_agreement['source_id'], true) ?? [];
  141. if (array_intersect($source_id, $ogoods_ids)) {
  142. throw new \Exception('this goods already sign');
  143. }
  144. }
  145. $is_sign = 1;
  146. break;
  147. }
  148. }
  149. } catch (\Exception $e) {
  150. $is_sign = 0;
  151. }
  152. return $is_sign;
  153. }
  154. public static function getUpgradeAgree($mall_id){
  155. $config = Yii::$app->services->setting->mall->get($mall_id, 'user_agree');
  156. return $config['upgrade_agree'] ?? '';
  157. }
  158. }