QuestionTemplateListForm.php 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. <?php
  2. namespace app\modules\client\models\v1;
  3. use app\models\IntegralSetting;
  4. use app\models\QAndAAdSetting;
  5. use app\models\QAndACat;
  6. use app\models\QAndAQa;
  7. use app\models\QAndAQuestionTemplate;
  8. use app\models\QAndASetting;
  9. use app\models\Register;
  10. use yii\base\Model;
  11. use yii\data\Pagination;
  12. use app\models\QAndAAdLog;
  13. use app\models\AccountLog;
  14. /**
  15. * @property \app\models\User $user;
  16. */
  17. class QuestionTemplateListForm extends Model
  18. {
  19. public $page;
  20. public $limit;
  21. public $user;
  22. /**
  23. * @return array
  24. */
  25. public function rules()
  26. {
  27. return [
  28. [['page', 'limit'], 'integer'],
  29. ];
  30. }
  31. public function list()
  32. {
  33. if (!$this->validate())
  34. return [
  35. 'code' => 1,
  36. 'msg' => $this->getErrorSummary(false)[0]
  37. ];
  38. $query = QAndAQuestionTemplate::find()->alias('g')->where(
  39. ['is_delete' => 0, 'state' => 1, 'store_id' => get_store_id()]
  40. );
  41. $count = $query->count();
  42. $pagination = new Pagination(['totalCount' => $count, 'pageSize' => $this->limit, 'page' => $this->page - 1]);
  43. $query->orderBy('id desc');
  44. $list = $query
  45. ->limit($pagination->limit)
  46. ->offset($pagination->offset)
  47. ->asArray()->all();
  48. return [
  49. 'code' => 0,
  50. 'data' => [
  51. 'row_count' => $count,
  52. 'page_count' => $pagination->pageCount,
  53. 'list' => $list,
  54. ]
  55. ];
  56. }
  57. public function hot_list()
  58. {
  59. if (!$this->validate())
  60. return [
  61. 'code' => 1,
  62. 'msg' => $this->getErrorSummary(false)[0]
  63. ];
  64. $query = QAndAQuestionTemplate::find()->alias('g')->where(
  65. ['is_delete' => 0, 'state' => 1, 'store_id' => get_store_id(), 'is_hot' => 1]
  66. );
  67. $count = $query->count();
  68. $pagination = new Pagination(['totalCount' => $count, 'pageSize' => $this->limit, 'page' => $this->page - 1]);
  69. $query->orderBy('id desc');
  70. $list = $query
  71. ->limit($pagination->limit)
  72. ->offset($pagination->offset)
  73. ->asArray()->all();
  74. return [
  75. 'code' => 0,
  76. 'data' => [
  77. 'row_count' => $count,
  78. 'page_count' => $pagination->pageCount,
  79. 'list' => $list,
  80. ]
  81. ];
  82. }
  83. public function template_list()
  84. {
  85. if (!$this->validate())
  86. return [
  87. 'code' => 1,
  88. 'msg' => $this->getErrorSummary(false)[0]
  89. ];
  90. $query = QAndACat::find()->where(['is_delete' => 0, 'store_id' => get_store_id()]);
  91. $count = $query->count();
  92. $pagination = new Pagination(['totalCount' => $count, 'pageSize' => $this->limit, 'page' => $this->page - 1]);
  93. $cat_list = $query
  94. ->limit($pagination->limit)
  95. ->offset($pagination->offset)
  96. ->asArray()->all();
  97. $list = [];
  98. foreach ($cat_list as $v) {
  99. $query = QAndAQuestionTemplate::find()->where(['is_delete' => 0, 'state' => 1, 'store_id' => get_store_id(), 'cat_id' => $v['id']])->orderBy('id desc')->asArray()->all();
  100. $list[$v['name']] = $query;
  101. }
  102. return [
  103. 'code' => 0,
  104. 'data' => [
  105. 'row_count' => $count,
  106. 'page_count' => $pagination->pageCount,
  107. 'list' => $list,
  108. ]
  109. ];
  110. }
  111. public function question_list()
  112. {
  113. if (!$this->validate())
  114. return [
  115. 'code' => 1,
  116. 'msg' => $this->getErrorSummary(false)[0]
  117. ];
  118. $user = $this->user;
  119. $query = QAndAQa::find()
  120. ->where(['is_delete' => 0, 'store_id' => get_store_id(), 'user_id' => $user->id]);
  121. $count = $query->count();
  122. $pagination = new Pagination(['totalCount' => $count, 'pageSize' => $this->limit, 'page' => $this->page - 1]);
  123. $list = $query
  124. ->limit($pagination->limit)
  125. ->offset($pagination->offset)
  126. ->orderBy('id DESC')
  127. ->asArray()->all();
  128. foreach ($list as &$item) {
  129. $item['answer_time'] = date('Y-m-d H:i:s', $item['answer_time']);
  130. }
  131. return [
  132. 'code' => 0,
  133. 'data' => [
  134. 'total' => $count,
  135. 'list' => $list,
  136. ]
  137. ];
  138. }
  139. public function del_question($id)
  140. {
  141. if (!$id) {
  142. return [
  143. 'code' => 1,
  144. 'msg' => '参数错误'
  145. ];
  146. }
  147. $item = QAndAQa::findOne($id);
  148. if (!$item) {
  149. return [
  150. 'code' => 1,
  151. 'msg' => '参数错误'
  152. ];
  153. }
  154. $item->is_delete = 1;
  155. $item->save();
  156. return [
  157. 'code' => 0,
  158. 'msg' => '删除成功'
  159. ];
  160. }
  161. public function index()
  162. {
  163. if (!$this->validate())
  164. return [
  165. 'code' => 1,
  166. 'msg' => $this->getErrorSummary(false)[0]
  167. ];
  168. $ad_s = QAndAAdSetting::find()->where(['store_id' => get_store_id()])->one();
  169. $qs_s = QAndASetting::find()->where(['store_id' => get_store_id()])->one();
  170. $today_day = date('Y-m-d');
  171. $today = Register::find()->where(['register_time' => $today_day, 'store_id' => get_store_id(), 'user_id' => $this->user->id])->asArray()->one();
  172. $setting = IntegralSetting::find()->where(['store_id' => get_store_id()])->one();
  173. $today_time = strtotime(date('Y-m-d'));
  174. $ad_use_num = QAndAAdLog::find()->where(['store_id' => get_store_id(), 'user_id' => get_user_id()])
  175. ->andWhere(['>', 'created_at', $today_time])
  176. ->count();
  177. $all_award_integral = $ad_s->award_integral * $ad_s->see_num + $setting->register_integral;
  178. $use_award_integral = $ad_s->award_integral * $ad_use_num + ($today ? $setting->register_integral : 0);
  179. $job = [
  180. [
  181. 'name' => '观看视频获得积分',
  182. 'award_integral' => $ad_s->award_integral,
  183. 'type' => 1, //前段跳转页面用 1视频 2积分
  184. 'see_num_' => $ad_s->see_num,
  185. 'use_num' => $ad_use_num,
  186. ],
  187. [
  188. 'name' => '积分签到',
  189. 'award_integral' => $setting->register_integral,
  190. 'type' => 2,
  191. 'see_num_' => 1,
  192. 'use_num' => $today ? 1 : 0,
  193. ],
  194. ];
  195. // $appidArr = explode(',', $ad_s->adpid);
  196. // foreach ($appidArr as $k => $v) {
  197. // $appidArr[$k] = intval($v);
  198. // }
  199. return [
  200. 'code' => 0,
  201. 'data' => [
  202. 'integral' => $this->user->integral,
  203. 'adpid' => $ad_s->adpid,
  204. 'rule' => $qs_s->rule,
  205. 'all_award_integral' => $all_award_integral,
  206. 'use_award_integral' => $use_award_integral,
  207. 'job' => $job,
  208. ]
  209. ];
  210. }
  211. public function add_integral()
  212. {
  213. $user = get_user();
  214. $store_id = get_store_id();
  215. $ad_s = QAndAAdSetting::find()->where(['store_id' => get_store_id()])->one();
  216. // 获取今日0点时间戳
  217. $today = strtotime(date('Y-m-d'));
  218. $count = QAndAAdLog::find()->where(['store_id' => $store_id, 'user_id' => $user->id])
  219. ->andWhere(['>', 'created_at', $today])
  220. ->count();
  221. if ($count >= $ad_s->see_num) {
  222. return [
  223. 'code' => 1,
  224. 'msg' => '今日观看次数已达上限'
  225. ];
  226. }
  227. $log = new QAndAAdLog();
  228. $log->store_id = $store_id;
  229. $log->user_id = $user->id;
  230. $log->integral = $ad_s->award_integral;
  231. if ($log->save()) {
  232. AccountLog::saveLog($user->id, $ad_s->award_integral, 1, 1, 0, 0, '观看视频广告获得积分', 18, $log->id, $store_id);
  233. $count = QAndAAdLog::find()->where(['store_id' => $store_id, 'user_id' => $user->id])
  234. ->andWhere(['>', 'created_at', $today])
  235. ->count();
  236. return [
  237. 'code' => 0,
  238. 'msg' => '积分增加成功',
  239. 'count' => $count,
  240. ];
  241. } else {
  242. return [
  243. 'code' => 1,
  244. 'msg' => '积分增加失败'
  245. ];
  246. }
  247. }
  248. }