ThemeDataController.class.php 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. <?php
  2. namespace Home\Controller;
  3. use Think\Model;
  4. /**
  5. * @author Xusong
  6. */
  7. class ThemeDataController extends CommonController {
  8. public function _initialize() {
  9. // 调用父类前置方法
  10. parent::_initialize();
  11. }
  12. /**
  13. * 新增主题运营数据
  14. */
  15. public function addData()
  16. {
  17. $uid = I('uid');
  18. $theme_id = I('theme_id',0,'intval');
  19. $theme_code = I('theme_code');
  20. $user_origin = I('user_origin');
  21. if($theme_id){
  22. $where['id'] = $theme_id;
  23. }else{
  24. $where['code'] = $theme_code;
  25. }
  26. if(!$theme_data = M('theme')->where($where)->find()){
  27. $this->responseError('params invalid');
  28. }
  29. $theme_id = $theme_data['id'];
  30. $insertData = array(
  31. 'uid' =>$uid,
  32. 'theme_id' =>$theme_id,
  33. 'user_origin' =>$user_origin,
  34. 'created_at' =>date('Y-m-d H:i:s'),
  35. 'date' =>date('Ymd')
  36. );
  37. if(!$theme_data = M('theme')->where(['id'=>$theme_id])->find()){
  38. $this->responseError('params invalid');
  39. }
  40. //汇总数据更新
  41. if(M('theme_detail')->where(['uid'=>$uid,'theme_id'=>$theme_id])->find()){
  42. //如果数据存在,只更新pv,不更新uv
  43. $update['page_view'] = $theme_data['page_view'] + 1;
  44. M('theme')->where(['id'=>$theme_id])->save($update);
  45. }else{
  46. //如果数据存在,更新pv,更新uv
  47. $update['page_view'] = $theme_data['page_view'] + 1;
  48. $update['user_view'] = $theme_data['user_view'] + 1;
  49. M('theme')->where(['id'=>$theme_id])->save($update);
  50. }
  51. if(!M('theme_detail')->add($insertData)){
  52. echo M()->getLastSql();
  53. }
  54. $this->responseSuccess();
  55. }
  56. }