Workorder.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. <?php
  2. namespace app\admin\controller\qingdongams\statistic;
  3. use app\admin\controller\qingdongams\Base;
  4. use addons\qingdongams\model\Workorder as WorkorderModel;
  5. use addons\qingdongams\model\Staff;
  6. use think\Exception;
  7. /**
  8. * 工单分析
  9. *
  10. * @icon fa fa-circle-o
  11. */
  12. class Workorder extends Base
  13. {
  14. /**
  15. * PartyBuilding模型对象
  16. * @var \addons\qingdongams\model\Workorder
  17. */
  18. protected $model = null;
  19. public function _initialize()
  20. {
  21. parent::_initialize();
  22. $this->model = new WorkorderModel();
  23. $this->assign('typeList',[0=>'请选择', 1=>'部门',2=>'员工']);
  24. try{
  25. \think\Db::execute("SET @@sql_mode='';");
  26. }catch (Exception $e){
  27. }
  28. }
  29. //工单数量分析
  30. public function index() {
  31. list($where,$times,$row) = $this->getWhere();//获取where
  32. $addOrder = WorkorderModel::where($where)//新增工单
  33. ->field('count(*) as count,FROM_UNIXTIME(createtime,"%Y-%m-%d") as time')
  34. ->group('time')->select();
  35. $endWhere = $where;
  36. unset($endWhere['createtime']);
  37. $endWhere['end_time'] = ['between', $times];
  38. $endOrder = WorkorderModel::where($endWhere)->where(['status' => 3])//完成工单
  39. ->field('count(*) as count,FROM_UNIXTIME(end_time,"%Y-%m-%d") as time')->select();
  40. $addOrder = $this->getArr($addOrder,'time');
  41. $endOrder = $this->getArr($endOrder,'time');
  42. $dateList = getDateList($times[0], $times[1]);
  43. $result = [];
  44. foreach ($dateList as $date) {
  45. $result['date'][]=$date;
  46. $result['add'][]=intval($addOrder[$date] ?? 0);
  47. $result['end'][]=intval($endOrder[$date] ?? 0);
  48. }
  49. // 表格 create_staff_id
  50. // 新增数
  51. $addPeople = WorkorderModel::where($where)->field('count(*) as count,owner_staff_id')->group('owner_staff_id')->select();
  52. // 完成数
  53. $endPeople = WorkorderModel::where($endWhere)->field('count(*) as count,owner_staff_id')->group('owner_staff_id')->select();
  54. $staffIds = array_unique(array_merge(array_column($addPeople,'owner_staff_id'),array_column($endPeople,'owner_staff_id')));
  55. $staffList = Staff::where('id','in',$staffIds)->column('name','id');
  56. $addPeople = $this->getArr($addPeople,'owner_staff_id');
  57. $endPeople = $this->getArr($endPeople,'owner_staff_id');
  58. $totalAdd = array_sum($addPeople);
  59. $totalEnd = array_sum($endPeople);
  60. $table[] = [
  61. 'id' => '',
  62. 'name' => '总计',
  63. 'add' => $totalAdd,
  64. 'end' => $totalEnd,
  65. ];
  66. foreach ($staffList as $key=>$item) {
  67. $table[] = [
  68. 'id' => $key,
  69. 'name' => $staffList[$key],
  70. 'add' => isset($addPeople[$key]) ? $addPeople[$key] : 0,
  71. 'end' => isset($endPeople[$key]) ? $endPeople[$key] : 0,
  72. ];
  73. }
  74. $this->view->assign([
  75. 'result' => $result,
  76. 'table' => $table,
  77. 'row' => $row
  78. ]);
  79. $this->assignconfig('result',$result);
  80. $this->assignconfig('ids',$row['ids']);
  81. return $this->view->fetch();
  82. }
  83. public function getWhere(){
  84. $beforeDays = strtotime(date('Y-m-d',strtotime('-30 day', time())));
  85. $times = [$beforeDays,time()];
  86. $row['type_id'] = 0;
  87. $row['ids'] = 0;
  88. if ($this->request->get()) {
  89. $params = $this->request->get('row/a');
  90. if(isset($params['times']) && !empty($params['times'])){
  91. $timesArr = explode('-', $params['times']);
  92. $times[0] = $timesArr[0].'-'.$timesArr[1].'-'.$timesArr[2];
  93. $times[1] = $timesArr[3].'-'.$timesArr[4].'-'.$timesArr[5];
  94. $times = [strtotime($times[0]),strtotime($times[1])+86400-1];
  95. }
  96. $row['type_id'] = $params['type_id'];
  97. $row['ids'] = $params['ids']?? 0;
  98. if($params['type_id'] == 1 && $params['ids']){
  99. $l_ids = Staff::getYkListIds($params['ids']);
  100. $where['dep_id'] = $params['ids'];
  101. }elseif($params['type_id'] == 2 && $params['ids']){
  102. $where['owner_staff_id'] = $params['ids'];
  103. }
  104. }
  105. $row['times'] = implode(' - ',[date("Y-m-d",$times[0]),date("Y-m-d",$times[1])]);
  106. $where['createtime'] = ['between',$times];
  107. return [$where,$times,$row];
  108. }
  109. public function getArr($data,$key){
  110. $tradeCount = [];
  111. foreach ($data as $v) {
  112. $tradeCount[$v[$key]] = $v['count'];
  113. }
  114. return $tradeCount;
  115. }
  116. }