SmartFormTemplateGoods.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. <?php
  2. namespace addons\SmartForm\common\models;
  3. use addons\SmartForm\common\enums\SmartFormEnum;
  4. use common\enums\StatusEnum;
  5. use Yii;
  6. /**
  7. * This is the model class for table "{{%addons_smart_form_template_goods}}".
  8. *
  9. * @property int $id
  10. * @property int $mall_id 商城ID
  11. * @property int $template_id 表单ID
  12. * @property int $goods_id 商品ID
  13. * @property int $status 状态[-1:删除;0:禁用;1启用]
  14. * @property int $created_at
  15. * @property int $updated_at
  16. */
  17. class SmartFormTemplateGoods extends \common\models\base\BaseActiveRecord
  18. {
  19. /**
  20. * {@inheritdoc}
  21. */
  22. public static function tableName()
  23. {
  24. return '{{%addons_smart_form_template_goods}}';
  25. }
  26. /**
  27. * {@inheritdoc}
  28. */
  29. public function rules()
  30. {
  31. return [
  32. [['mall_id','goods_id'], 'required'],
  33. [['mall_id', 'template_id', 'goods_id', 'status', 'created_at', 'updated_at'], 'integer'],
  34. ];
  35. }
  36. /**
  37. * {@inheritdoc}
  38. */
  39. public function attributeLabels()
  40. {
  41. return [
  42. 'id' => 'ID',
  43. 'mall_id' => 'Mall ID',
  44. 'template_id' => 'Template ID',
  45. 'goods_id' => 'Goods ID',
  46. 'status' => 'Status',
  47. 'created_at' => 'Created At',
  48. 'updated_at' => 'Updated At',
  49. ];
  50. }
  51. // 关联模型
  52. public function getTemplate()
  53. {
  54. return $this->hasOne(AddonsSmartFormTemplate::class, ['id' => 'template_id']);
  55. }
  56. /**
  57. *
  58. * @param array $data
  59. * @return int
  60. * @throws \Exception
  61. */
  62. public static function setData(array $data)
  63. {
  64. if(isset($data['id']) && $data['id'] > 0 ){
  65. $model = self::findOne([ 'id' => $data['id']]);
  66. if(!$model){
  67. throw new \Exception('记录不存在');
  68. }
  69. }else{
  70. $model = self::findOne(['mall_id' => $data['mall_id'], 'goods_id' => $data['goods_id']]);
  71. if(!$model){
  72. $model = new self();
  73. }
  74. }
  75. $model->attributes = $data;
  76. if (!$model->save()) {
  77. throw new \Exception($model->getErrorMessage());
  78. }
  79. return $model->id;
  80. }
  81. /**
  82. * 获取商品所关联的智能表单
  83. * @param $params
  84. * @return array
  85. */
  86. public function getGoodsTemplate($params)
  87. {
  88. $ret = [];
  89. $info = self::findOne(['mall_id' => $params['mall_id'] , 'goods_id' => $params['goods_id'],'status' => StatusEnum::ENABLED]);
  90. if(!$info){
  91. return $ret;
  92. }
  93. $template_id = $info->template_id;
  94. if($params['user_id']){
  95. // 判断是否可以重复填写 - 这里死循环了 - 需要更改条件
  96. if (AddonsSmartFormTemplate::find()->where(['id' => $info['template_id']])->one()){
  97. $user_form = AddonsSmartFormTemplateUserForm::findOne(['mall_id' => $params['mall_id'], 'user_id' => $params['user_id'],
  98. 'smart_form_template_id' => $info['template_id'],'status' => StatusEnum::ENABLED, 'is_ignore' => StatusEnum::DISABLED]);
  99. if (!empty($user_form)) {
  100. $ret['user_form_id'] = $user_form->id;
  101. }
  102. }
  103. $template_id = empty($user_form) ? $template_id : 0;
  104. }
  105. $ret['smart_form_template_id'] = $template_id;
  106. return $ret;
  107. }
  108. public static function postGoodSmartForm($params)
  109. {
  110. // 后台页面组件展示
  111. if ($params['type'] == 'show_component') {
  112. return [
  113. 'component' => 'com-smartform-goods',
  114. 'path' => Yii::$app->basePath . '/../addons/SmartForm/backend/views/components',
  115. ];
  116. } else {
  117. try {
  118. if (empty($params['setting'][SmartFormEnum::ADDONS_NAME])) return true;
  119. $post = $params['setting'][SmartFormEnum::ADDONS_NAME];
  120. $post['mall_id'] = $params['mall_id'];
  121. $res = self::setData($post);
  122. if($res==false){
  123. throw new \Exception(self::getStaticError());
  124. }
  125. return true;
  126. } catch (\Exception $e) {
  127. self::setStaticError($e->getMessage());
  128. return false;
  129. }
  130. }
  131. }
  132. }