userconnectrecorddaymodel.php 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. <?php
  2. /*
  3. * @Author: wang jun
  4. * @Date: 2021-09-28 11:13:58
  5. * @Last Modified by: wang jun
  6. * @Last Modified time: 2021-12-24 16:49:03
  7. */
  8. namespace app\index\model;
  9. use think\Config;
  10. use think\Db;
  11. use think\Log;
  12. use think\Model;
  13. class userconnectrecorddaymodel
  14. {
  15. protected $tableBase = "t_userconnectrecord";
  16. protected $db;
  17. public $isCreateFirst; //是否第一次创建
  18. public function __construct()
  19. {
  20. $connection = Config::get('database');
  21. $this->db = Db::connect($connection);
  22. $this->table = $this->tableBase . "_" . date("Ymd");
  23. $this->createTableProcess();
  24. }
  25. private function createTableProcess()
  26. {
  27. $isHasTable = $this->hasTable();
  28. if (!$isHasTable) {
  29. $this->createTable();
  30. } else {
  31. $this->isCreateFirst = false;
  32. }
  33. }
  34. /**
  35. * 构造校验表是否存在
  36. * 20212116
  37. * wj
  38. * */
  39. private function hasTable()
  40. {
  41. $sessionTable = session($this->tableBase, '', 'submeter');
  42. $isCheck = false;
  43. if ($sessionTable) {
  44. if (date("Ymd") != substr($sessionTable, mb_strlen($sessionTable), -8)) {
  45. $isCheck = true;
  46. }
  47. } else {
  48. $isCheck = true;
  49. }
  50. if (!$isCheck) {
  51. return true;
  52. }
  53. session(null, 'submeter');
  54. $sql = "show Table STATUS where Name='" . $this->table . "'";
  55. $data = $this->db->query($sql);
  56. $count = count($data);
  57. if ($count <= 0) {
  58. return false;
  59. }
  60. session($this->tableBase, $this->table, 'submeter');
  61. return true;
  62. }
  63. private function createTable()
  64. {
  65. $sql = "show create table " . $this->tableBase;
  66. $data = $this->db->query($sql);
  67. $createData = $data[0]['Create Table'];
  68. $createData = str_replace("`" . $this->tableBase . "`", "`" . $this->table . "`", $createData);
  69. $this->db->query($createData);
  70. log::info($this->table . " create");
  71. session($this->tableBase, $this->table, 'submeter');
  72. $this->isCreateFirst = true;
  73. }
  74. /***
  75. * 设置表名
  76. * 20211217
  77. * wj
  78. */
  79. public function setTableName($daystr)
  80. {
  81. if (empty($daystr) || !is_string($daystr)) {
  82. return false;
  83. }
  84. $isUse = false;
  85. if (isDate($daystr, 'Ymd')) {
  86. $isUse = true;
  87. }
  88. if (!$isUse) {
  89. return false;
  90. }
  91. $tableName = $this->tableBase . "_" . $daystr;
  92. $this->table = $tableName;
  93. session($this->tableBase, $this->table, 'submeter');
  94. log::info($this->table . " use");
  95. return true;
  96. }
  97. /***
  98. * 判断表是否已存在
  99. * 20211217
  100. * wj
  101. */
  102. public function isexist($daystr)
  103. {
  104. if (!isDate($daystr, 'Ymd')) {
  105. return false;
  106. }
  107. $tableName = $this->tableBase . "_" . $daystr;
  108. $sql = "show Table STATUS where Name='" . $tableName . "'";
  109. $data = $this->db->query($sql);
  110. $count = count($data);
  111. if ($count <= 0) {
  112. return false;
  113. }
  114. return true;
  115. }
  116. /***
  117. * 插入数据
  118. * 20211217
  119. * wj
  120. */
  121. public function insinfo($arr)
  122. {
  123. log::info($this->table . " insert");
  124. if (!isset($arr['calltime']) || empty($arr['calltime'])) {
  125. $arr['calltime'] = date("Y-m-d H:i:s");
  126. }
  127. $id = $this->db->name($this->table)->insertGetId($arr);
  128. return $id;
  129. }
  130. /*
  131. * 202112017
  132. * wj
  133. * 获得联系电话列表
  134. */
  135. public function getconnectlist($where, $count = false, $page = 1, $size = 10)
  136. {
  137. $sql = "select * from (
  138. select uc.id,uc.senduserid,us.wname as swname,us.id as sid,us.telno as stelno,uc.reciveruserid,ur.id as rid,ur.wname as rwname,ur.telno as rtelno,i.code,i.id as iid,i.worktype,uc.calltime
  139. from " . $this->table . " as uc
  140. join t_invent as i on uc.sourceid=i.id
  141. join t_userinfo as us on uc.senduserid=us.id
  142. join t_userinfo as ur on uc.reciveruserid=ur.id
  143. ORDER BY uc.calltime desc)
  144. as tab ";
  145. $tabsql = "select * from (" . $sql . ") as tab";
  146. $usewhere = [];
  147. //打电话人
  148. if (isset($where['swname']) && !empty($where['swname']) && is_string($where['swname'])) {
  149. $usewhere[] = "swname like '%" . $where['swname'] . "%'";
  150. }
  151. //拨打电话
  152. if (isset($where['stelno']) && !empty($where['stelno']) && is_string($where['stelno'])) {
  153. $usewhere[] = "stelno like '%" . $where['stelno'] . "%'";
  154. }
  155. //被联系人
  156. if (isset($where['rwname']) && !empty($where['rwname']) && is_string($where['rwname'])) {
  157. $usewhere[] = "rwname like '%" . $where['rwname'] . "%'";
  158. }
  159. //被联系电话
  160. if (isset($where['rtelno']) && !empty($where['rtelno']) && is_string($where['rtelno'])) {
  161. $usewhere[] = "rtelno like '%" . $where['rtelno'] . "%'";
  162. }
  163. //招工code
  164. if (isset($where['code']) && !empty($where['code']) && is_string($where['code'])) {
  165. $usewhere[] = "code like '%" . $where['code'] . "%'";
  166. }
  167. //打电话时间段
  168. if (isset($where['calltime']) && !empty($where['calltime']) && is_array($where['calltime'])) {
  169. $usewhere[] = "calltime >= '" . $where['calltime'][0] . "'";
  170. $usewhere[] = "calltime < '" . $where['calltime'][1] . "'";
  171. }
  172. //工作类型
  173. if (isset($where['worktype']) && !empty($where['worktype']) && is_string($where['worktype'])) {
  174. $usewhere[] = "worktype like '%" . $where['worktype'] . "%'";
  175. }
  176. if (!empty($usewhere)) {
  177. $tabwhere = implode(' and ', $usewhere);
  178. $tabsql .= ' where ' . $tabwhere;
  179. }
  180. if (!$count) {
  181. $index = ($page - 1) * $size;
  182. $tabsql .= " limit " . $index . "," . $size;
  183. }
  184. if ($count) {
  185. $list = $this->db->query("select count(*) count from (" . $tabsql . ") as t");
  186. } else {
  187. $list = $this->db->query("select * from (" . $tabsql . ") as t");
  188. }
  189. return $list;
  190. }
  191. /*
  192. * 20211220
  193. * wj
  194. * 统计联系电话的时间段
  195. */
  196. public function getconnecttimebucket($where)
  197. {
  198. $tabsql = "select DATE_FORMAT(calltime,'%H') as hour,count(*) count from " . $this->table . " as uc join t_invent as i on uc.sourceid=i.id group by hour ORDER BY hour asc";
  199. $list = $this->db->query("select * from (" . $tabsql . ") as t");
  200. return $list;
  201. }
  202. /*
  203. * 20211222
  204. * wj
  205. * 统计联系电话联系数量
  206. */
  207. public function getconnectbyinvent($where, $count = false, $page = 1, $size = 10)
  208. {
  209. #i.isactive=1 and
  210. $tabsql = "select uc.sourceid,i.id,i.createdate,i.code,i.info,i.isactive,i.ispass,count(uc.id) count
  211. from " . $this->table . " as uc
  212. join t_invent as i on uc.sourceid=i.id where i.ispass=1
  213. GROUP BY uc.sourceid ORDER BY i.id desc";
  214. if ($count) {
  215. $list = $this->db->query("select count(*) count from (" . $tabsql . ") as t");
  216. $count = $list[0]['count'];
  217. return $count;
  218. } else {
  219. $index = ($page - 1) * $size;
  220. $list = $this->db->query("select * from (" . $tabsql . ") as t limit " . $index . "," . $size);
  221. }
  222. return $list;
  223. }
  224. }