AddonsSmartFormTemplateStatistics.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. <?php
  2. namespace addons\SmartForm\common\models;
  3. use common\enums\StatusEnum;
  4. use common\models\mall\MallAddons;
  5. use common\models\user\User;
  6. use Yii;
  7. use yii\db\Exception;
  8. use addons\SmartForm\common\models\AddonsSmartFormTemplateUserBehavior; # 用户行为
  9. /**
  10. * This is the model class for table "{{%addons_smart_form_template_statistics}}".
  11. *
  12. * @property int $id
  13. * @property int $mall_id 商城ID
  14. * @property int|null $status 状态[-1:删除;0:禁用;1启用]
  15. * @property int $created_at 创建时间
  16. * @property int $updated_at 修改时间
  17. */
  18. class AddonsSmartFormTemplateStatistics extends \common\models\base\BaseActiveRecord
  19. {
  20. const ADDONS_NAME = 'SmartForm';
  21. /**
  22. * {@inheritdoc}
  23. */
  24. public static function tableName()
  25. {
  26. return '{{%addons_smart_form_template_statistics}}';
  27. }
  28. /**
  29. * {@inheritdoc}
  30. */
  31. public function rules()
  32. {
  33. return [
  34. [['smart_form_template_id'], 'required'],
  35. [['smart_form_template_id','today_filling_times','today_view_times','today_filling_people','filling_times','view_times','filling_people','created_at','updated_at'], 'integer'],
  36. ];
  37. }
  38. /**
  39. * {@inheritdoc}
  40. */
  41. public function attributeLabels()
  42. {
  43. return [
  44. 'id' => '主键' ,
  45. 'smart_form_template_id' => '智能装修表单管理id',
  46. 'today_filling_times' => '今日填写次数-当天填写次数,包含同一个ID用户填写次数',
  47. 'today_view_times' => '今日访问人次-当天访问次数,只访问未填写',
  48. 'today_filling_people' => '今日填写人数-当天填写人数,一个ID用户统计一次',
  49. 'filling_times' => '总填写次数-当天填写次数,包含同一个ID用户填写次数',
  50. 'view_times' => '总访问人次-当天访问次数,只访问未填写',
  51. 'filling_people' => '总填写人数-当天填写人数,一个ID用户统计一次',
  52. 'created_at' => '添加时间',
  53. 'updated_at' => '修改时间',
  54. ];
  55. }
  56. /**
  57. * @method checkTplUse
  58. * 判断当前表单是否填写
  59. * @author zqh
  60. * @datetime 2022-05-06T15:23:56+0800
  61. * @param int $template_id [description]
  62. * @return [type] [description]
  63. */
  64. public static function checkTplUse($template_id=0){
  65. if(!$template_id){
  66. self::$static_error = "模板不存在";
  67. return false;
  68. }
  69. $find = self::getOne([['smart_form_template_id'=>$template_id]],true);
  70. if($find && $find['filling_times'] > 0){
  71. self::$static_error = "模板已填写";
  72. return true;
  73. }
  74. return false;
  75. }
  76. /**
  77. * @method setUserStatistics
  78. * 设置表单信息
  79. * @author zqh
  80. * @datetime 2022-05-07T03:25:02+0800
  81. */
  82. public static function setTplStatistics($params)
  83. {
  84. if(!isset($params['template_id']) || !$params['template_id']){
  85. self::$static_error = "模板不存在";
  86. return false;
  87. }
  88. $tplModel = self::findOne(['smart_form_template_id'=>$params['template_id']]);
  89. if (!$tplModel){
  90. $tplModel = new self();
  91. $tplModel->loadDefaultValues();
  92. $tplModel->smart_form_template_id = $params['template_id'];
  93. }
  94. if ($params['type']=='0') {
  95. # 总访问人次-当天访问次数,只访问未填写
  96. $tplModel->view_times = $tplModel->view_times + 1;
  97. }else{
  98. # 填写表单
  99. # 判断当前用户今天是否已经填写
  100. $filling_people = AddonsSmartFormTemplateUserbehavior::findOne(['smart_form_template_id'=>$params['template_id'],'user_id'=>$params['user_id'],'behavior_type'=>1,'month_day'=>date('Y-m-d')]);
  101. $s = AddonsSmartFormTemplateUserbehavior::updateAll(['is_submit'=>1],['smart_form_template_id'=>$params['template_id'],'user_id'=>$params['user_id'],'behavior_type'=>0,'month_day'=>date('Y-m-d')]);
  102. if(!$filling_people){
  103. # 总填写人数-当天填写人数,一个ID用户统计一次
  104. $tplModel->filling_people = $tplModel->filling_people + 1;
  105. $tplModel->view_times = $tplModel->view_times - $s;
  106. }
  107. # 总填写次数-当天填写次数,包含同一个ID用户填写次数
  108. $tplModel->filling_times = $tplModel->filling_times + 1;
  109. }
  110. if(!$tplModel->save()){
  111. self::$static_error = $tplModel->getErrorMessage();
  112. return false;
  113. }
  114. # 记录行为
  115. $behavior = new AddonsSmartFormTemplateUserbehavior;
  116. $behavior->loadDefaultValues();
  117. $behavior->created_at = time();
  118. $behavior->updated_at = time();
  119. $behavior->diy_page_id = $params['diy_page_id'];
  120. $behavior->smart_form_template_id = $params['template_id'];
  121. $behavior->user_id = $params['user_id'];
  122. $behavior->behavior_type = $params['type'];
  123. $behavior->month_day = date('Y-m-d');
  124. $s = $behavior->insert();
  125. if(!$s){
  126. self::$static_error = $behavior->getErrorMessage();
  127. return false;
  128. }
  129. return true;
  130. }
  131. }