Yuejian.php 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. <?php
  2. namespace app\company\controller;
  3. use app\common\controller\Apic;
  4. use think\Db;
  5. use app\common\library\ExcelCsv;
  6. /**
  7. * 月检进度表
  8. */
  9. class Yuejian extends Apic
  10. {
  11. protected $noNeedLogin = [];
  12. protected $noNeedRight = [];
  13. protected $table = 'jiance';
  14. public function index(){
  15. $search = [];
  16. //搜师傅
  17. $worker_id = input('worker_id',0);
  18. if(!empty($worker_id)){
  19. $search['jiance.worker_id'] = $worker_id;
  20. }
  21. //搜年份
  22. $year = input('year',date('Y'));
  23. $starttime = strtotime($year.'-01-01');
  24. $endtime = strtotime($year + 1 .'-01-01');
  25. //查出本公司,该师傅,本年所有的服务项目
  26. $jiance_list = Db::name('jiance')
  27. ->where('jiance.deletetime',NULL)
  28. ->where('jiance.company_id',$this->auth->company_id)
  29. ->where('jiance.worker_id',$worker_id)
  30. ->where('jiance.tongjitime','BETWEEN',[$starttime,$endtime])
  31. ->order('jiance.tongjitime','asc')
  32. // ->select(false);echo $jiance_list;exit;
  33. ->select();
  34. // dump($jiance_list);
  35. //公司名
  36. $companyname = Db::name('company')->where('id',$this->auth->company_id)->value('companyname');
  37. //各个项目铺满,并去重
  38. $project_ids = array_column($jiance_list,'uc_id');
  39. $project_list = Db::name('user_company')->where('id','IN',$project_ids)->field('id,projectname,endtime')->select();
  40. // dump($project_list);
  41. //双循环,得到各项目数据
  42. $bottom_list = [];
  43. foreach($project_list as $key => $project){
  44. //每个project填充
  45. $newline = [
  46. // 'uc_id' => $project['id'],
  47. 'num' => $key+1,
  48. 'companyname' => $companyname,
  49. 'projectname' => $project['projectname'],
  50. 'endtime' => $project['endtime'],
  51. 'month_01' => 'moren',
  52. 'month_02' => 'moren',
  53. 'month_03' => 'moren',
  54. 'month_04' => 'moren',
  55. 'month_05' => 'moren',
  56. 'month_06' => 'moren',
  57. 'month_07' => 'moren',
  58. 'month_08' => 'moren',
  59. 'month_09' => 'moren',
  60. 'month_10' => 'moren',
  61. 'month_11' => 'moren',
  62. 'month_12' => 'moren',
  63. ];
  64. //修正每个月的数据
  65. foreach($jiance_list as $key => $jiance){
  66. if($project['id'] == $jiance['uc_id']){
  67. //找到对应月份
  68. $month_key = 'month_'.date('m',$jiance['tongjitime']);
  69. //赋值完成情况
  70. $newline[$month_key] = $jiance['status'];
  71. }
  72. }
  73. $bottom_list[] = $newline;
  74. }
  75. //dump($bottom_list);
  76. //循环每月数据,得到顶部统计数据
  77. $mk = ['01','02','03','04','05','06','07','08','09','10','11','12'];
  78. $month_data = [];
  79. foreach($mk as $monnum){
  80. $key = 'month_'.$monnum;
  81. $$key = array_count_values(array_column($bottom_list,$key));
  82. // $$key['count'] = count($bottom_list);
  83. // $$key['moren'] = isset($$key['moren']) ? $$key['moren'] : 0;
  84. $$key['status0'] = isset($$key[0]) ? $$key[0] : 0;
  85. $$key['status1'] = isset($$key[1]) ? $$key[1] : 0;
  86. $$key['total'] = $$key['status0'] + $$key['status1'];
  87. $$key['bili'] = ( $$key['total'] == 0 ? 0 : bcmul(bcdiv($$key['status1'],$$key['total'],4),100,2) ) . '%';
  88. $month_data[$key] = $$key;
  89. }
  90. //顶部数据
  91. //dump($month_data);
  92. //赋值三行顶部数据
  93. $top1 = [
  94. 'num' => '',
  95. 'companyname' => '比例',
  96. 'projectname' => '',
  97. 'endtime' => '',
  98. 'month_01' => $month_data['month_01']['bili'],
  99. 'month_02' => $month_data['month_02']['bili'],
  100. 'month_03' => $month_data['month_03']['bili'],
  101. 'month_04' => $month_data['month_04']['bili'],
  102. 'month_05' => $month_data['month_05']['bili'],
  103. 'month_06' => $month_data['month_06']['bili'],
  104. 'month_07' => $month_data['month_07']['bili'],
  105. 'month_08' => $month_data['month_08']['bili'],
  106. 'month_09' => $month_data['month_09']['bili'],
  107. 'month_10' => $month_data['month_10']['bili'],
  108. 'month_11' => $month_data['month_11']['bili'],
  109. 'month_12' => $month_data['month_12']['bili'],
  110. ];
  111. $top2 = [
  112. 'num' => '',
  113. 'companyname' => '实际完成数',
  114. 'projectname' => '',
  115. 'endtime' => '',
  116. 'month_01' => $month_data['month_01']['status1'],
  117. 'month_02' => $month_data['month_02']['status1'],
  118. 'month_03' => $month_data['month_03']['status1'],
  119. 'month_04' => $month_data['month_04']['status1'],
  120. 'month_05' => $month_data['month_05']['status1'],
  121. 'month_06' => $month_data['month_06']['status1'],
  122. 'month_07' => $month_data['month_07']['status1'],
  123. 'month_08' => $month_data['month_08']['status1'],
  124. 'month_09' => $month_data['month_09']['status1'],
  125. 'month_10' => $month_data['month_10']['status1'],
  126. 'month_11' => $month_data['month_11']['status1'],
  127. 'month_12' => $month_data['month_12']['status1'],
  128. ];
  129. $top3 = [
  130. 'num' => '',
  131. 'companyname' => '应该完成数',
  132. 'projectname' => '',
  133. 'endtime' => '',
  134. 'month_01' => $month_data['month_01']['total'],
  135. 'month_02' => $month_data['month_02']['total'],
  136. 'month_03' => $month_data['month_03']['total'],
  137. 'month_04' => $month_data['month_04']['total'],
  138. 'month_05' => $month_data['month_05']['total'],
  139. 'month_06' => $month_data['month_06']['total'],
  140. 'month_07' => $month_data['month_07']['total'],
  141. 'month_08' => $month_data['month_08']['total'],
  142. 'month_09' => $month_data['month_09']['total'],
  143. 'month_10' => $month_data['month_10']['total'],
  144. 'month_11' => $month_data['month_11']['total'],
  145. 'month_12' => $month_data['month_12']['total'],
  146. ];
  147. $bottom_list_export = $bottom_list;
  148. //重整结果集,替换,对号,错号,空白
  149. foreach($bottom_list as $bk => $bottom){
  150. foreach($bottom as $key => $val){
  151. if(strpos($key,'month_') !== false){
  152. //这里严格用===是因为,php7.4里: '一切字符串'==0 为true
  153. if($val === 'moren'){$val = '';}
  154. elseif($val === 1){$val = '✔';}
  155. elseif($val === 0){$val = '✖';}
  156. }
  157. $bottom[$key] = $val;
  158. }
  159. $bottom['endtime'] = date('Y-m-d',$bottom['endtime']);
  160. $bottom_list[$bk] = $bottom;
  161. }
  162. //重整结果集,替换,对号,错号,空白
  163. foreach($bottom_list_export as $bk => $bottom){
  164. foreach($bottom as $key => $val){
  165. if(strpos($key,'month_') !== false){
  166. //这里严格用===是因为,php7.4里: '一切字符串'==0 为true
  167. if($val === 'moren'){$val = '';}
  168. elseif($val === 1){$val = 'V';}
  169. elseif($val === 0){$val = 'X';}
  170. }
  171. $bottom[$key] = $val;
  172. }
  173. $bottom['endtime'] = date('Y-m-d',$bottom['endtime']);
  174. $bottom_list_export[$bk] = $bottom;
  175. }
  176. //组合结果
  177. $result[] = $top1;
  178. $result[] = $top2;
  179. $result[] = $top3;
  180. $result = array_merge($result,$bottom_list);
  181. //导出excel
  182. $action = input('action','');
  183. if($action == 'export'){
  184. //表头
  185. $excel_header = [
  186. 'num' => '序号',
  187. 'companyname' => '合同单位',
  188. 'projectname' => '项目名称',
  189. 'endtime' => '终止日期',
  190. 'month_01' => '1月',
  191. 'month_02' => '2月',
  192. 'month_03' => '3月',
  193. 'month_04' => '4月',
  194. 'month_05' => '5月',
  195. 'month_06' => '6月',
  196. 'month_07' => '7月',
  197. 'month_08' => '8月',
  198. 'month_09' => '9月',
  199. 'month_10' => '10月',
  200. 'month_11' => '11月',
  201. 'month_12' => '12月',
  202. ];
  203. //内容
  204. $excel_result[] = $top1;
  205. $excel_result[] = $top2;
  206. $excel_result[] = $top3;
  207. $excel_result = array_merge($excel_result,$bottom_list_export);
  208. //文件输出
  209. $worker_info = Db::name('worker')->where('id',$worker_id)->value('truename');
  210. $fileName = $year.'年'.$worker_info.'月检-' . date('Y-m-d-H-i-s');
  211. $ExcelCsv = new ExcelCsv($excel_header, $excel_result);
  212. $data = $ExcelCsv->collection();
  213. $ExcelCsv->download($fileName, $excel_header, $data);
  214. }else{
  215. $this->success(1,$result);
  216. }
  217. }
  218. }