Work.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400
  1. <?php
  2. namespace app\admin\controller\qingdongams\statistic;
  3. use addons\qingdongams\model\Daily;
  4. use addons\qingdongams\model\Record;
  5. use addons\qingdongams\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. $whereT['status'] =1;
  116. $staff = Staff::where($whereT)->field('id,name,img')->select();
  117. $staff=collection($staff)->toArray();
  118. $result=[];
  119. foreach ($daily as $d) {
  120. if($type == '日报'){
  121. $time = date('Y-m-d', strtotime($d['createtime']));
  122. }elseif($type == '月报'){
  123. $time = date('Y-m-d', strtotime($d['createtime']));
  124. }elseif($type == '周报'){
  125. $time = date('Y-m-d', strtotime($d['createtime']));
  126. }elseif($type == '季报'){
  127. $time = date('Y-m', strtotime($d['createtime']));
  128. }elseif($type == '年报'){
  129. $time = date('Y', strtotime($d['createtime']));
  130. }
  131. $result[$time][] = $d['create_staff_id'];
  132. }
  133. $list=[];
  134. $startYear=strtotime($year.'-01-01');
  135. $endYear=strtotime($year.'-12-31');
  136. $startMonth=strtotime($year."-{$month}-01");
  137. $endMonth=strtotime($year."-{$month}-".$lastDay);
  138. $allDay=($endYear-$startYear)/86400+1;
  139. foreach ($staff as $s){
  140. $row=$s;
  141. if ($type == '日报') {
  142. $count = Daily::where([
  143. 'type'=>$type,
  144. 'create_staff_id'=>$s['id'],
  145. 'createtime'=>['between',[$startYear,$endYear]]
  146. ])->field("FROM_UNIXTIME(createtime,'%Y-%m-%d') as ctime")->group('ctime')->count();
  147. $row['year']=$count;
  148. $row['year_ratio']= sprintf("%.2f", $count/$allDay * 100);
  149. $count = Daily::where([
  150. 'type' => $type,
  151. 'create_staff_id' => $s['id'],
  152. 'createtime' => ['between', [$startMonth, $endMonth]]
  153. ])->field("FROM_UNIXTIME(createtime,'%Y-%m-%d') as ctime")->group('ctime')->count();
  154. $row['month'] = $count;
  155. $row['month_ratio'] = sprintf("%.2f", $count / $allDay * 100);
  156. }elseif($type =='周报'){
  157. $count = Daily::where([
  158. 'type'=>$type,
  159. 'create_staff_id'=>$s['id'],
  160. 'createtime'=>['between',[$startYear,$endYear]]
  161. ])->field("FROM_UNIXTIME(createtime,'%Y-%m-%d') as ctime")->group('ctime')->count();
  162. $row['year']=$count;
  163. $row['year_ratio']= sprintf("%.2f", $count/12 * 100);
  164. }elseif($type =='月报'){
  165. $count = Daily::where([
  166. 'type'=>$type,
  167. 'create_staff_id'=>$s['id'],
  168. 'createtime'=>['between',[$startYear,$endYear]]
  169. ])->field("FROM_UNIXTIME(createtime,'%Y-%m') as ctime")->group('ctime')->count();
  170. $row['year']=$count;
  171. $row['year_ratio']= sprintf("%.2f", $count/12 * 100);
  172. }elseif($type =='季报'){
  173. $count = Daily::where([
  174. 'type'=>$type,
  175. 'create_staff_id'=>$s['id'],
  176. 'createtime'=>['between',[$startYear,$endYear]]
  177. ])->field("quarter(FROM_UNIXTIME(createtime,'%Y-%m')) as ctime")->group('ctime')->count();
  178. $row['year']=$count;
  179. $row['year_ratio']= sprintf("%.2f", $count/4 * 100);
  180. }else if($type =='年报'){
  181. }
  182. foreach ($dates as $d) {
  183. $row[$d['date']] = 0;
  184. if ($type == '日报') {
  185. $between = date('Y-m-d', strtotime($d['between']));
  186. if (strtotime($d['between']) > time()) {
  187. $row[$d['date']] = 9;
  188. }
  189. if (isset($result[$between]) && in_array($s['id'], $result[$between])) {//已提交日报
  190. $row[$d['date']] = 1;
  191. }
  192. }else if ($type == '月报') {
  193. $between = date('Y-m',strtotime($year.'-'.$d['date']));
  194. if (strtotime($between) > time()) {
  195. $row[$d['date']] = 9;
  196. }
  197. if (isset($result[$between]) && in_array($s['id'], $result[$between])) {//已提交日报
  198. $row[$d['date']] = 1;
  199. }
  200. }else if ($type == '季报') {
  201. $between1 = date('Y-m',strtotime($year.'-'.(($d['date']-1)*3+1)));//第一月
  202. $between2 = date('Y-m',strtotime($year.'-'.(($d['date']-1)*3+2)));//第二月
  203. $between3 = date('Y-m',strtotime($year.'-'.$d['date']*3));//第三月
  204. if (strtotime($between3) > time()) {
  205. $row[$d['date']] = 9;
  206. }
  207. if (isset($result[$between1]) && in_array($s['id'], $result[$between1])) {//
  208. $row[$d['date']] = 1;
  209. }
  210. if (isset($result[$between2]) && in_array($s['id'], $result[$between2])) {//
  211. $row[$d['date']] = 1;
  212. }
  213. if (isset($result[$between3]) && in_array($s['id'], $result[$between3])) {//
  214. $row[$d['date']] = 1;
  215. }
  216. }else if ($type == '年报') {
  217. $between=$d['date'];
  218. if (strtotime($d['date'].'-12-31') > time()) {
  219. $row[$d['date']] = 9;
  220. }
  221. if (isset($result[$between]) && in_array($s['id'], $result[$between])) {//已提交日报
  222. $row[$d['date']] = 1;
  223. }
  224. }else if ($type == '周报') {
  225. $between=$d['between'];
  226. if (strtotime($between[1]) > time()) {
  227. $row[$d['date']] = 9;
  228. }
  229. for ($i=$between[0];strtotime($i)<= strtotime($between[1]);$i=date('Y-m-d',strtotime($i.'+1 day'))){
  230. if (isset($result[$i]) && in_array($s['id'], $result[$i])) {//已提交日报
  231. $row[$d['date']] = 1;
  232. }
  233. }
  234. }
  235. }
  236. $list[]=$row;
  237. }
  238. $years=[];
  239. $end=date('Y')+1;
  240. for ($i=2022;$i<=$end;$i++){
  241. $years[]=$i;
  242. }
  243. $this->view->assign([
  244. 'type' => $type,
  245. 'year' => $year,
  246. 'years' => $years,
  247. 'dates' => $dates,
  248. 'month' => $month,
  249. 'staff_id' => $staff_id,
  250. 'group_id' => $group_id,
  251. 'list' => $list,
  252. ]);
  253. return $this->view->fetch();
  254. }
  255. /**
  256. * 沟通日志
  257. */
  258. public function record(){
  259. $row=input('row/a');
  260. $year=$row['year']??date('Y');
  261. $month=$row['month']??date('m');
  262. $group_id=$row['group_id']??0;
  263. $staff_id=$row['staff_id']??0;
  264. $where=[];
  265. $ids=[];
  266. if ($group_id) {//角色组
  267. $ids = Staff::getGroupStaffIds($group_id);
  268. }
  269. if ($staff_id) {
  270. $ids = $row['staff_id'];
  271. }
  272. $where['createtime'] = ['between', [strtotime("{$year}-{$month}-01"),
  273. strtotime("{$year}-".($month+1).'-01') - 1]];
  274. $dates=[];
  275. $lastDay = date('d', strtotime("{$year}-{$month} +1 month -1 day"));
  276. for ($i = 1; $i <= $lastDay; $i++) {
  277. $dates[] = ['date' => $i, 'between' => "{$year}-{$month}-{$i}",
  278. 'search' => "{$year}-{$month}-{$i} 00:00:00 - {$year}-{$month}-{$i} 23:59:59"];
  279. }
  280. $daily = Record::where($where)
  281. ->field("FROM_UNIXTIME(createtime,'%Y-%m-%d') as ctime,create_staff_id,count(*) as num")
  282. ->group('ctime,create_staff_id')->select();
  283. $daily=collection($daily)->toArray();
  284. $whereT=[];
  285. if($group_id || $staff_id){
  286. $whereT=['id'=>['in',$ids]];
  287. }
  288. $whereT['status'] =1;
  289. $staff = Staff::where($whereT)->field('id,name,img')->select();
  290. $staff=collection($staff)->toArray();
  291. $result=[];
  292. foreach ($daily as $d) {
  293. $result[$d['ctime']][$d['create_staff_id']]=$d['num'];
  294. }
  295. $list=[];
  296. $startYear=strtotime($year.'-01-01');
  297. $endYear=strtotime($year.'-12-31');
  298. $startMonth=strtotime($year."-{$month}-01");
  299. $endMonth=strtotime($year."-{$month}-".$lastDay);
  300. $allDay=($endYear-$startYear)/86400+1;
  301. foreach ($staff as $s) {
  302. $row = $s;
  303. $count = Record::where([
  304. 'create_staff_id' => $s['id'],
  305. 'createtime' => ['between', [$startYear, $endYear]]
  306. ])->count();
  307. $row['year'] = $count;
  308. $row['year_ratio'] = sprintf("%.2f", $count / $allDay * 100);
  309. $count = Record::where([
  310. 'create_staff_id' => $s['id'],
  311. 'createtime' => ['between', [$startMonth, $endMonth]]
  312. ])->count();
  313. $row['month'] = $count;
  314. $row['month_ratio'] = sprintf("%.2f", $count / $allDay * 100);
  315. foreach ($dates as $d) {
  316. $row[$d['date']] = 0;
  317. $between = date('Y-m-d', strtotime($d['between']));
  318. if (strtotime($d['between']) > time()) {
  319. $row[$d['date']] = -1;
  320. }
  321. if (isset($result[$between]) && isset($result[$between][$s['id']])) {//已提交日报
  322. $row[$d['date']] = $result[$between][$s['id']];
  323. }
  324. }
  325. $list[] = $row;
  326. }
  327. $years=[];
  328. $end=date('Y')+1;
  329. for ($i=2022;$i<=$end;$i++){
  330. $years[]=$i;
  331. }
  332. $this->view->assign([
  333. 'year' => $year,
  334. 'years' => $years,
  335. 'dates' => $dates,
  336. 'month' => $month,
  337. 'staff_id' => $staff_id,
  338. 'group_id' => $group_id,
  339. 'list' => $list,
  340. ]);
  341. return $this->view->fetch();
  342. }
  343. /**
  344. * 沟通日志列表
  345. */
  346. public function record_list(){
  347. $this->request->filter(['strip_tags']);
  348. if ($this->request->isAjax()) {
  349. if ($this->request->request('keyField')) {
  350. return $this->selectpage();
  351. }
  352. list($where, $sort, $order, $offset, $limit) = $this->buildparams();
  353. $whereT=[];
  354. $staff_id=input('staff_id');
  355. if($staff_id){
  356. $whereT['create_staff_id']=$staff_id;
  357. }
  358. $list = Record::where($where)->where($whereT)->with(['staff','customer'])->order($sort, $order)
  359. ->paginate($limit);
  360. foreach($list as $k=>$v){
  361. if(!isset($v['customer']) || !$v['customer']){
  362. $v['customer'] = array('id'=>'','name'=>'');
  363. }
  364. }
  365. $row = $list->items();
  366. $result = array("total" => $list->total(), "rows" => $row);
  367. return json($result);
  368. }
  369. return $this->view->fetch();
  370. }
  371. }