CardSecretGoods.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. <?php
  2. namespace addons\CardSecret\common\models;
  3. use addons\CardSecret\common\enums\CardSecretEnum;
  4. use common\enums\AddonsEnum;
  5. use common\enums\StatusEnum;
  6. use common\models\mall\MallAddons;
  7. use Yii;
  8. use yii\db\Exception;
  9. /**
  10. * This is the model class for table "{{%addons_CardSecret_goods}}".
  11. *
  12. * @property int $id
  13. * @property string|null $cate_id 绑定分类ID
  14. * @property int|null $goods_id 商品ID
  15. * @property int|null $status 状态(-1是已删除,0是禁用,1是正常)
  16. * @property int|null $created_at 创建时间
  17. * @property int|null $updated_at 更新时间
  18. * @property int|null $mall_id 商城ID
  19. */
  20. class CardSecretGoods extends \common\models\base\BaseActiveRecord
  21. {
  22. /**
  23. * {@inheritdoc}
  24. */
  25. public static function tableName()
  26. {
  27. return '{{%addons_card_secret_goods}}';
  28. }
  29. /**
  30. * {@inheritdoc}
  31. */
  32. public function rules()
  33. {
  34. return [
  35. [['goods_id', 'status', 'created_at', 'updated_at', 'mall_id','cate_id'], 'integer'],
  36. ];
  37. }
  38. /**
  39. * {@inheritdoc}
  40. */
  41. public function attributeLabels()
  42. {
  43. return [
  44. 'id' => 'ID',
  45. 'cate_id' => '绑定分类ID',
  46. 'goods_id' => '商品ID',
  47. 'status' => '状态(-1是已删除,0是禁用,1是正常)',
  48. 'created_at' => '创建时间',
  49. 'updated_at' => '更新时间',
  50. 'mall_id' => '商城ID',
  51. ];
  52. }
  53. /**
  54. * 保存数据
  55. * @Author: ltm
  56. * @DateTime: 2022/5/7 0022 17:13
  57. * @Copyright: copyright (c) 2021 广东七件事集团
  58. * @param array $params
  59. * @return string
  60. */
  61. public static function setGoodsData($mall_id,$goods_id,$params){
  62. try{
  63. $model = self::findOne(['goods_id'=>$goods_id,'mall_id'=>$mall_id,'status' => StatusEnum::ENABLED]);
  64. if (empty($model)) {
  65. $model = new self();
  66. $model->loadDefaultValues();
  67. }
  68. $model->mall_id = $mall_id;
  69. $model->goods_id = $goods_id;
  70. $model->cate_id = $params['cate_id'];
  71. if(!$model->load($params,'') || !$model->save()){
  72. throw new Exception($model->getErrorMessage());
  73. }
  74. return $model;
  75. }catch(\Exception $e){
  76. self::$static_error = $e->getMessage();
  77. return false;
  78. }
  79. }
  80. /**
  81. * @Author: ltm
  82. * @DateTime: 2022/5/12 0012 17:30
  83. * @Copyright: copyright (c) 2021 广东七件事集团
  84. * @param $data
  85. * @return string
  86. */
  87. public static function getGoodSetting($params)
  88. {
  89. // 后台页面组件展示
  90. if ($params['type'] == 'show_component') {
  91. return [
  92. 'component' => 'com-card-secret',
  93. 'path' => Yii::$app->basePath . '/../addons/CardSecret/backend/views/goods',
  94. ];
  95. } else {
  96. try {
  97. if (empty($params['setting'][CardSecretEnum::ADDONSNAME])) return true;
  98. $setting = $params['setting'][CardSecretEnum::ADDONSNAME];
  99. // 数据保存
  100. $res = CardSecretGoods::setGoodsData($params['mall_id'], $params['goods_id'], $setting);
  101. if ($res === false) throw new Exception(CardSecretGoods::getStaticError());
  102. return true;
  103. } catch (Exception $e) {
  104. self::setStaticError($e->getMessage());
  105. return false;
  106. }
  107. }
  108. }
  109. /**
  110. * @Author: ltm
  111. * @DateTime: 2022/5/12 0012 12:45
  112. * @Copyright: copyright (c) 2021 广东七件事集团
  113. * @param $data
  114. * @return string
  115. */
  116. public static function isInstall(array $params){
  117. //插件判断 是否显示卡密信息
  118. $card_secret = MallAddons::isInstall($params['mall_id'], CardSecretEnum::ADDONSNAME);
  119. $data = $params['order'];
  120. if($card_secret){
  121. $card_secret = CardSecretUse::getOne([['mall_id' =>$params['mall_id'],'order_id'=> $params['order']['id']]],true);
  122. if($card_secret){
  123. $data[CardSecretEnum::ADDONSNAME]['is_show_card_secret'] = 1;
  124. }else{
  125. $data[CardSecretEnum::ADDONSNAME]['is_show_card_secret'] = 0;
  126. }
  127. }
  128. return $data;
  129. }
  130. }