Work.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  1. <?php
  2. namespace app\admin\controller\qingdong\statistic;
  3. use addons\qingdong\model\Daily;
  4. use addons\qingdong\model\Record;
  5. use addons\qingdong\model\Staff;
  6. use app\admin\model\AuthGroup;
  7. use app\common\controller\Backend;
  8. use fast\Tree;
  9. use think\Exception;
  10. /**
  11. * 员工工作日志分析
  12. */
  13. class Work extends Backend
  14. {
  15. public function _initialize()
  16. {
  17. try{
  18. \think\Db::execute("SET @@sql_mode='';");
  19. }catch (Exception $e){
  20. }
  21. parent::_initialize();
  22. $childrenGroupIds = $this->auth->getChildrenGroupIds(true);
  23. $groupList = collection(AuthGroup::where('id', 'in', $childrenGroupIds)->select())->toArray();
  24. Tree::instance()->init($groupList);
  25. $groupList = [];
  26. if ($this->auth->isSuperAdmin()) {
  27. $groupList = Tree::instance()->getTreeList(Tree::instance()->getTreeArray(0));
  28. } else {
  29. $groups = $this->auth->getGroups();
  30. $groupIds = [];
  31. foreach ($groups as $m => $n) {
  32. if (in_array($n['id'], $groupIds) || in_array($n['pid'], $groupIds)) {
  33. continue;
  34. }
  35. $groupList = array_merge($groupList, Tree::instance()->getTreeList(Tree::instance()->getTreeArray($n['pid'])));
  36. foreach ($groupList as $index => $item) {
  37. $groupIds[] = $item['id'];
  38. }
  39. }
  40. }
  41. $this->assign('groupdata',$groupList);
  42. }
  43. /**
  44. * 工作报告
  45. */
  46. public function report(){
  47. $row=input('row/a');
  48. $type=$row['type']??'日报';
  49. $year=$row['year']??date('Y');
  50. $month=$row['month']??date('m');
  51. $group_id=$row['group_id']??0;
  52. $staff_id=$row['staff_id']??0;
  53. $where=['type'=>$type];
  54. $ids=[];
  55. if ($group_id) {//角色组
  56. $ids = Staff::getGroupStaffIds($group_id);
  57. }
  58. if ($staff_id) {
  59. $ids = $row['staff_id'];
  60. }
  61. if($type == '日报'){
  62. $where['createtime'] = ['between', [strtotime("{$year}-{$month}-01"),
  63. strtotime("{$year}-".($month+1).'-01') - 1]];
  64. }elseif($type == '年报'){
  65. }else{
  66. $where['createtime'] = ['between', [strtotime($year . '-01-01'),
  67. strtotime(($year + 1) . '-01-01') - 1]];
  68. }
  69. $dates=[];
  70. $lastDay = date('d', strtotime("{$year}-{$month} +1 month -1 day"));
  71. if($type == '日报'){
  72. for($i=1;$i<=$lastDay;$i++){
  73. $dates[]=['date'=>$i,'between'=>"{$year}-{$month}-{$i}",
  74. 'search'=>"{$year}-{$month}-{$i} 00:00:00 - {$year}-{$month}-{$i} 23:59:59"];
  75. }
  76. }else if($type == '月报'){
  77. for($i=1;$i<=12;$i++){
  78. $e=date('d', strtotime("{$year}-{$i} +1 month -1 day"));
  79. $dates[]=['date'=>$i,'between'=>[$year.'-'.$i,$year.'-'.($i+1)],
  80. 'search'=>"{$year}-{$i}-01 00:00:00 - {$year}-{$i}-{$e} 23:59:59"];
  81. }
  82. }else if($type == '季报'){
  83. for($i=1;$i<=4;$i++){
  84. $r = $i * 3;
  85. $s=($i-1)*3+1;
  86. $e=date('d', strtotime("{$year}-{$r} +1 month -1 day"));
  87. $dates[]=['date'=>$i,
  88. 'search'=>"{$year}-{$s}-01 00:00:00 - {$year}-{$r}-{$e} 23:59:59"];
  89. }
  90. }else if($type == '年报'){
  91. $y=date('Y');
  92. for ($i = 2022; $i <= $y; $i++) {
  93. $dates[] = ['date' => $i,
  94. 'search' => "{$i}-01-01 00:00:00 - {$i}-12-31 23:59:59"];
  95. }
  96. }else if($type == '周报'){
  97. $week = date('W',strtotime($year.'-12-31'));
  98. $s=7-date('w',strtotime($year.'-01-01'));
  99. for ($i = 1; $i <= $week; $i++) {
  100. $s1 = ($i - 2) * 7 + $s;
  101. $s1 = $s1 < 0 ? 0 : $s1;
  102. $s2 = ($i - 1) * 7 + $s;
  103. $sw=date('Y-m-d', strtotime($year . '-01-01 '.'+' . $s1 . ' day'));
  104. $ew=date('Y-m-d', strtotime($year . '-01-01 '.'+' . $s2 . ' day')) ;
  105. $dates[] = ['date' => $i, 'between' => [$sw,$ew],
  106. 'search' => $sw . " 00:00:00 - " .
  107. $ew. " 23:59:59"];
  108. }
  109. }
  110. $daily = Daily::where($where)->field('createtime,create_staff_id')->select();
  111. $whereT=[];
  112. if($group_id || $staff_id){
  113. $whereT=['id'=>['in',$ids]];
  114. }
  115. $staff = Staff::where($whereT)->field('id,name,img')->select();
  116. $staff=collection($staff)->toArray();
  117. $result=[];
  118. foreach ($daily as $d) {
  119. if($type == '日报'){
  120. $time = date('Y-m-d', strtotime($d['createtime']));
  121. }elseif($type == '月报'){
  122. $time = date('Y-m-d', strtotime($d['createtime']));
  123. }elseif($type == '周报'){
  124. $time = date('Y-m-d', strtotime($d['createtime']));
  125. }elseif($type == '季报'){
  126. $time = date('Y-m', strtotime($d['createtime']));
  127. }elseif($type == '年报'){
  128. $time = date('Y', strtotime($d['createtime']));
  129. }
  130. $result[$time][] = $d['create_staff_id'];
  131. }
  132. $list=[];
  133. $startYear=strtotime($year.'-01-01');
  134. $endYear=strtotime($year.'-12-31');
  135. $startMonth=strtotime($year."-{$month}-01");
  136. $endMonth=strtotime($year."-{$month}-".$lastDay);
  137. $allDay=($endYear-$startYear)/86400+1;
  138. foreach ($staff as $s){
  139. $row=$s;
  140. if ($type == '日报') {
  141. $count = Daily::where([
  142. 'type'=>$type,
  143. 'create_staff_id'=>$s['id'],
  144. 'createtime'=>['between',[$startYear,$endYear]]
  145. ])->field("FROM_UNIXTIME(createtime,'%Y-%m-%d') as ctime")->group('ctime')->count();
  146. $row['year']=$count;
  147. $row['year_ratio']= sprintf("%.2f", $count/$allDay * 100);
  148. $count = Daily::where([
  149. 'type' => $type,
  150. 'create_staff_id' => $s['id'],
  151. 'createtime' => ['between', [$startMonth, $endMonth]]
  152. ])->field("FROM_UNIXTIME(createtime,'%Y-%m-%d') as ctime")->group('ctime')->count();
  153. $row['month'] = $count;
  154. $row['month_ratio'] = sprintf("%.2f", $count / $allDay * 100);
  155. }elseif($type =='周报'){
  156. $count = Daily::where([
  157. 'type'=>$type,
  158. 'create_staff_id'=>$s['id'],
  159. 'createtime'=>['between',[$startYear,$endYear]]
  160. ])->field("FROM_UNIXTIME(createtime,'%Y-%m-%d') as ctime")->group('ctime')->count();
  161. $row['year']=$count;
  162. $row['year_ratio']= sprintf("%.2f", $count/12 * 100);
  163. }elseif($type =='月报'){
  164. $count = Daily::where([
  165. 'type'=>$type,
  166. 'create_staff_id'=>$s['id'],
  167. 'createtime'=>['between',[$startYear,$endYear]]
  168. ])->field("FROM_UNIXTIME(createtime,'%Y-%m') as ctime")->group('ctime')->count();
  169. $row['year']=$count;
  170. $row['year_ratio']= sprintf("%.2f", $count/12 * 100);
  171. }elseif($type =='季报'){
  172. $count = Daily::where([
  173. 'type'=>$type,
  174. 'create_staff_id'=>$s['id'],
  175. 'createtime'=>['between',[$startYear,$endYear]]
  176. ])->field("quarter(FROM_UNIXTIME(createtime,'%Y-%m')) as ctime")->group('ctime')->count();
  177. $row['year']=$count;
  178. $row['year_ratio']= sprintf("%.2f", $count/4 * 100);
  179. }else if($type =='年报'){
  180. }
  181. foreach ($dates as $d) {
  182. $row[$d['date']] = 0;
  183. if ($type == '日报') {
  184. $between = date('Y-m-d', strtotime($d['between']));
  185. if (strtotime($d['between']) > time()) {
  186. $row[$d['date']] = 9;
  187. }
  188. if (isset($result[$between]) && in_array($s['id'], $result[$between])) {//已提交日报
  189. $row[$d['date']] = 1;
  190. }
  191. }else if ($type == '月报') {
  192. $between = date('Y-m',strtotime($year.'-'.$d['date']));
  193. if (strtotime($between) > time()) {
  194. $row[$d['date']] = 9;
  195. }
  196. if (isset($result[$between]) && in_array($s['id'], $result[$between])) {//已提交日报
  197. $row[$d['date']] = 1;
  198. }
  199. }else if ($type == '季报') {
  200. $between1 = date('Y-m',strtotime($year.'-'.(($d['date']-1)*3+1)));//第一月
  201. $between2 = date('Y-m',strtotime($year.'-'.(($d['date']-1)*3+2)));//第二月
  202. $between3 = date('Y-m',strtotime($year.'-'.$d['date']*3));//第三月
  203. if (strtotime($between3) > time()) {
  204. $row[$d['date']] = 9;
  205. }
  206. if (isset($result[$between1]) && in_array($s['id'], $result[$between1])) {//
  207. $row[$d['date']] = 1;
  208. }
  209. if (isset($result[$between2]) && in_array($s['id'], $result[$between2])) {//
  210. $row[$d['date']] = 1;
  211. }
  212. if (isset($result[$between3]) && in_array($s['id'], $result[$between3])) {//
  213. $row[$d['date']] = 1;
  214. }
  215. }else if ($type == '年报') {
  216. $between=$d['date'];
  217. if (strtotime($d['date'].'-12-31') > time()) {
  218. $row[$d['date']] = 9;
  219. }
  220. if (isset($result[$between]) && in_array($s['id'], $result[$between])) {//已提交日报
  221. $row[$d['date']] = 1;
  222. }
  223. }else if ($type == '周报') {
  224. $between=$d['between'];
  225. if (strtotime($between[1]) > time()) {
  226. $row[$d['date']] = 9;
  227. }
  228. for ($i=$between[0];strtotime($i)<= strtotime($between[1]);$i=date('Y-m-d',strtotime($i.'+1 day'))){
  229. if (isset($result[$i]) && in_array($s['id'], $result[$i])) {//已提交日报
  230. $row[$d['date']] = 1;
  231. }
  232. }
  233. }
  234. }
  235. $list[]=$row;
  236. }
  237. $years=[];
  238. $end=date('Y')+1;
  239. for ($i=2022;$i<=$end;$i++){
  240. $years[]=$i;
  241. }
  242. $this->view->assign([
  243. 'type' => $type,
  244. 'year' => $year,
  245. 'years' => $years,
  246. 'dates' => $dates,
  247. 'month' => $month,
  248. 'staff_id' => $staff_id,
  249. 'group_id' => $group_id,
  250. 'list' => $list,
  251. ]);
  252. return $this->view->fetch();
  253. }
  254. /**
  255. * 沟通日志
  256. */
  257. public function record(){
  258. $row=input('row/a');
  259. $year=$row['year']??date('Y');
  260. $month=$row['month']??date('m');
  261. $group_id=$row['group_id']??0;
  262. $staff_id=$row['staff_id']??0;
  263. $where=[];
  264. $ids=[];
  265. if ($group_id) {//角色组
  266. $ids = Staff::getGroupStaffIds($group_id);
  267. }
  268. if ($staff_id) {
  269. $ids = $row['staff_id'];
  270. }
  271. $where['createtime'] = ['between', [strtotime("{$year}-{$month}-01"),
  272. strtotime("{$year}-".($month+1).'-01') - 1]];
  273. $dates=[];
  274. $lastDay = date('d', strtotime("{$year}-{$month} +1 month -1 day"));
  275. for ($i = 1; $i < $lastDay; $i++) {
  276. $dates[] = ['date' => $i, 'between' => "{$year}-{$month}-{$i}",
  277. 'search' => "{$year}-{$month}-{$i} 00:00:00 - {$year}-{$month}-{$i} 23:59:59"];
  278. }
  279. $daily = Record::where($where)
  280. ->field("FROM_UNIXTIME(createtime,'%Y-%m-%d') as ctime,create_staff_id,count(*) as num")
  281. ->group('ctime,create_staff_id')->select();
  282. $daily=collection($daily)->toArray();
  283. $whereT=[];
  284. if($group_id || $staff_id){
  285. $whereT=['id'=>['in',$ids]];
  286. }
  287. $staff = Staff::where($whereT)->field('id,name,img')->select();
  288. $staff=collection($staff)->toArray();
  289. $result=[];
  290. foreach ($daily as $d) {
  291. $result[$d['ctime']][$d['create_staff_id']]=$d['num'];
  292. }
  293. $list=[];
  294. $startYear=strtotime($year.'-01-01');
  295. $endYear=strtotime($year.'-12-31');
  296. $startMonth=strtotime($year."-{$month}-01");
  297. $endMonth=strtotime($year."-{$month}-".$lastDay);
  298. $allDay=($endYear-$startYear)/86400+1;
  299. foreach ($staff as $s) {
  300. $row = $s;
  301. $count = Record::where([
  302. 'create_staff_id' => $s['id'],
  303. 'createtime' => ['between', [$startYear, $endYear]]
  304. ])->count();
  305. $row['year'] = $count;
  306. $row['year_ratio'] = sprintf("%.2f", $count / $allDay * 100);
  307. $count = Record::where([
  308. 'create_staff_id' => $s['id'],
  309. 'createtime' => ['between', [$startMonth, $endMonth]]
  310. ])->count();
  311. $row['month'] = $count;
  312. $row['month_ratio'] = sprintf("%.2f", $count / $allDay * 100);
  313. foreach ($dates as $d) {
  314. $row[$d['date']] = 0;
  315. $between = date('Y-m-d', strtotime($d['between']));
  316. if (strtotime($d['between']) > time()) {
  317. $row[$d['date']] = -1;
  318. }
  319. if (isset($result[$between]) && isset($result[$between][$s['id']])) {//已提交日报
  320. $row[$d['date']] = $result[$between][$s['id']];
  321. }
  322. }
  323. $list[] = $row;
  324. }
  325. $years=[];
  326. $end=date('Y')+1;
  327. for ($i=2022;$i<=$end;$i++){
  328. $years[]=$i;
  329. }
  330. $this->view->assign([
  331. 'year' => $year,
  332. 'years' => $years,
  333. 'dates' => $dates,
  334. 'month' => $month,
  335. 'staff_id' => $staff_id,
  336. 'group_id' => $group_id,
  337. 'list' => $list,
  338. ]);
  339. return $this->view->fetch();
  340. }
  341. /**
  342. * 沟通日志列表
  343. */
  344. public function record_list(){
  345. $this->request->filter(['strip_tags']);
  346. if ($this->request->isAjax()) {
  347. if ($this->request->request('keyField')) {
  348. return $this->selectpage();
  349. }
  350. list($where, $sort, $order, $offset, $limit) = $this->buildparams();
  351. $whereT=[];
  352. $staff_id=input('staff_id');
  353. if($staff_id){
  354. $whereT['create_staff_id']=$staff_id;
  355. }
  356. $list = Record::where($where)->where($whereT)->with(['staff','customer'])->order($sort, $order)
  357. ->paginate($limit);
  358. foreach($list as $k=>$v){
  359. if(!isset($v['customer']) || !$v['customer']){
  360. $v['customer'] = array('id'=>'','name'=>'');
  361. }
  362. }
  363. $row = $list->items();
  364. $result = array("total" => $list->total(), "rows" => $row);
  365. return json($result);
  366. }
  367. return $this->view->fetch();
  368. }
  369. }