useraccountuserrecordmodel.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. <?php
  2. /*
  3. * @Author: wang jun
  4. * @Date: 2021-09-27 08:39:30
  5. * @Last Modified by: wang jun
  6. * @Last Modified time: 2021-12-29 16:39:24
  7. */
  8. namespace app\index\model;
  9. use think\Model;
  10. class useraccountuserrecordmodel extends Model
  11. {
  12. protected $table = 't_useraccountuserrecord';
  13. public function insertData($data)
  14. {
  15. $id = $this->insertGetId($data);
  16. return empty($id) ? false : $id;
  17. }
  18. /***
  19. * 校验数据是否可添加
  20. * 20211215
  21. * wj
  22. * 20211216 废弃
  23. */
  24. /*public function checkDataForInsert($arr)
  25. {
  26. $where = [
  27. 'userid' => $arr['userid'],
  28. 'usetime' => ['like', '%' . date('Y-m-d') . '%'],
  29. ];
  30. $row = $this->where($where)->count();
  31. if ($row > 0) {
  32. return backarr(0, "数据已存在");
  33. }
  34. return backarr(1, "数据可新增");
  35. }*/
  36. public function getInfo($where, $field = "*", $row = true)
  37. {
  38. $info = $this->field($field)->where($where);
  39. if ($row) {
  40. $info = $info->find();
  41. } else {
  42. $info = $info->select();
  43. }
  44. return empty($info) ? false : $info;
  45. }
  46. public function updateinfo($where, $updateData)
  47. {
  48. $row = $this->where($where)->update($updateData);
  49. return empty($row) ? false : $row;
  50. }
  51. public function deleteinfo($where)
  52. {
  53. $row = $this->where($where)->delete();
  54. return empty($row) ? false : $row;
  55. }
  56. public function getList($where = [], $field = "*", $page = 1, $size = 10, $order = "id desc", $group = "", $row = false)
  57. {
  58. $sqlObj = $this->where($where);
  59. if ("count" != $field) {
  60. $sqlObj = $sqlObj->field($field)->order($order)->group($group)->page($page, $size);
  61. if ($row) {
  62. $data = $sqlObj->find();
  63. } else {
  64. $data = $sqlObj->select();
  65. }
  66. } else {
  67. $data = $sqlObj = $sqlObj->count();
  68. }
  69. return $data;
  70. }
  71. /***
  72. * 获取用户消费的积分数 银积分 通过类型
  73. * type 1 昨天 2今天
  74. * wj
  75. * 20211130
  76. */
  77. public function getsumscorebytype($type, $minsocre = 5, $count = false, $page = 1, $size = 10, $order = "id desc")
  78. {
  79. $field = 'u.id,u.wname,u.telno,sum(us.usescore) as sum, sum(us.usescore)-' . $minsocre . ' as givescore,tab2.givesum';
  80. $sql = "select " . $field . "
  81. from t_useraccountuserrecord as us
  82. join t_userinfo as u on us.userid=u.id ";
  83. $sql2 = "select userid,sum(addscore) as givesum from t_useraccountaddrecord
  84. where sourcetype=2 and actiontype=5 and createdate>=DATE_FORMAT(NOW(),'%Y-%m-%d 00:00:00')
  85. GROUP BY userid";
  86. $sql .= " left join (" . $sql2 . ") as tab2 on tab2.userid=us.userid where ";
  87. $date = '';
  88. switch ($type) {
  89. case 1:
  90. //昨天
  91. $date = "us.usetime >=DATE_SUB(DATE_FORMAT(NOW(),'%Y-%m-%d 00:00:00'),interval 1 day) and us.usetime <DATE_FORMAT(NOW(),'%Y-%m-%d 00:00:00')";
  92. break;
  93. case 2:
  94. //今天
  95. $date = "us.usetime >=DATE_FORMAT(NOW(),'%Y-%m-%d 00:00:00')";
  96. break;
  97. }
  98. if (empty($date)) {
  99. return false;
  100. }
  101. $sql .= $date;
  102. $sql .= " group by us.userid HAVING sum(us.usescore)>=" . $minsocre;
  103. if ($count) {
  104. $sql = "select count(*) as count from ( " . $sql . " ) as tab";
  105. } else {
  106. $index = ($page - 1) * $size;
  107. $sql = "select * from ( " . $sql . " ) as tab order by " . $order . " limit " . $index . "," . $size;
  108. }
  109. $data = $this->query($sql);
  110. if ($count) {
  111. $data = $data[0]['count'];
  112. }
  113. return $data;
  114. }
  115. /***
  116. * 获取用户消费的积分数 银积分 通过查询
  117. * time 数组
  118. * wj
  119. * 20211207
  120. */
  121. public function getsumscorebywhere($where, $minsocre = 5, $count = false, $page = 1, $size = 10, $order = "id desc")
  122. {
  123. $field = 'u.id,u.wname,u.telno,sum(us.usescore) as sum, sum(us.usescore)-' . $minsocre . ' as givescore,tab2.givesum,tab3.sumscore';
  124. $sql = "select " . $field . "
  125. from t_useraccountuserrecord as us
  126. join t_userinfo as u on us.userid=u.id ";
  127. $sql2 = "select userid,sum(addscore) as givesum from t_useraccountaddrecord
  128. where sourcetype=2 and actiontype=5 and createdate>=DATE_FORMAT(NOW(),'%Y-%m-%d 00:00:00')
  129. GROUP BY userid";
  130. $sql3 = "select userid,goldscore+siliverscore as sumscore from t_useraccount";
  131. $sql .= " left join (" . $sql2 . ") as tab2 on tab2.userid=us.userid left join (" . $sql3 . ") as tab3 on tab3.userid=us.userid ";
  132. $useWhere = [];
  133. if (isset($where['time']) && is_array($where['time'])) {
  134. $useWhere[] = "us.usetime >=from_unixtime(unix_timestamp('" . $where['time'][0] . "'), '%Y-%m-%d %H:%i:%s') and us.usetime < from_unixtime(unix_timestamp('" . $where['time'][1] . "'), '%Y-%m-%d %H:%i:%s') ";
  135. }
  136. if (isset($where['wname']) && !empty($where['wname']) && is_string($where['wname'])) {
  137. $useWhere[] = "u.wname like '%" . $where['wname'] . "%'";
  138. }
  139. if (isset($where['telno']) && !empty($where['telno']) && is_string($where['telno'])) {
  140. $useWhere[] = "u.telno like '%" . $where['telno'] . "%'";
  141. }
  142. if (!empty($useWhere)) {
  143. $sql .= 'where ' . implode(' and ', $useWhere);
  144. }
  145. $sql .= " group by us.userid HAVING sum(us.usescore)>=" . $minsocre;
  146. if ($count) {
  147. $sql = "select count(*) as count from ( " . $sql . " ) as tab";
  148. } else {
  149. $index = ($page - 1) * $size;
  150. $sql = "select * from ( " . $sql . " ) as tab order by " . $order . " limit " . $index . "," . $size;
  151. }
  152. $data = $this->query($sql);
  153. if ($count) {
  154. $data = $data[0]['count'];
  155. }
  156. return $data;
  157. }
  158. }