StatisticsService.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. <?php
  2. namespace App\Service;
  3. use App\Model\Device;
  4. use App\Model\DeviceDetails;
  5. use App\Model\Employee;
  6. use App\Model\EmployeeDetails;
  7. use App\Model\Item;
  8. use App\Model\RdDetails;
  9. use Illuminate\Support\Facades\DB;
  10. class StatisticsService extends Service
  11. {
  12. public function statisticsEmployeeCommon($data,$user){
  13. if(empty($data['order_time'][0]) || ! empty($data['order_time'][1])) return [false, '时间不能为空'];
  14. list($start_time, $end_time) = $this->changeDateToTimeStampAboutRange($data['order_time'],false);
  15. if ($start_time === null || $end_time === null || $start_time > $end_time) return [false, "时间的区间无效"];
  16. $day = $this->returnDays([
  17. 0 => $start_time,
  18. 1 => $end_time,
  19. ]);
  20. if($day > 365) return [false, '查询时间仅支持范围区间在365天内'];
  21. $model = RdDetails::from('rd_details as a')
  22. ->leftJoin('rd as b','b.id','a.rd_id')
  23. ->where('a.del_time',0)
  24. ->where('b.del_time',0)
  25. ->where('b.order_time', '>=', $start_time)
  26. ->where('b.order_time', '<=', $end_time)
  27. ->where('a.type', RdDetails::type_one)
  28. ->select('b.item_id','b.total_hours','a.data_id')
  29. ->orderby('order_time', 'desc');
  30. if(! empty($data['item_code'])) {
  31. $item_id = Item::where('code', 'LIKE', '%'.$data['item_code'].'%')
  32. ->where('del_time',0)
  33. ->pluck('id')
  34. ->toArray();
  35. $model->whereIn('b.item_id', $item_id);
  36. }
  37. if(! empty($data['employee_title'])) {
  38. $data_id = Employee::where('emp_name', 'LIKE', '%'.$data['employee_title'].'%')
  39. ->where('del_time',0)
  40. ->pluck('id')
  41. ->toArray();
  42. $model->whereIn('a.data_id', $data_id);
  43. }
  44. return [true, [$model, [
  45. 0 => $start_time,
  46. 1 => $end_time,
  47. ]]];
  48. }
  49. public function statisticsEmployee($data,$user){
  50. list($status, $return) = $this->statisticsEmployeeCommon($data, $user);
  51. if(! $status) return [false, $return];
  52. list($model, $ergs) = $return;
  53. $list = $model->get()->toArray();
  54. $list = $this->statisticsEmployeeFillData($list, $ergs);
  55. // $list['count'] = $this->countTotal($list, $user['header_default']);
  56. return [true, $list];
  57. }
  58. public function statisticsEmployeeFillData($data, $ergs){
  59. $employee_id = array_unique(array_column($data,'data_id'));
  60. $employee_hours_map = EmployeeDetails::where('del_time', 0)
  61. ->whereIn('employee_id', $employee_id)
  62. ->whereBetween('time', $ergs)
  63. ->groupBy('employee_id')
  64. ->pluck(DB::raw('SUM(total_hours)'), 'employee_id')
  65. ->toArray();
  66. $employee = Employee::where('del_time',0)
  67. ->where('id','<>',Employee::SPECIAL_ADMIN)
  68. ->select('id','emp_name as name')
  69. ->get()->toArray();
  70. $employee_map = $employee_map_2 = $item_id = [];
  71. foreach ($data as $value){
  72. if(isset($employee_map[$value['data_id']])){
  73. $total_hours = bcadd($employee_map[$value['data_id']], $value['total_hours'],2);
  74. $employee_map[$value['data_id']] = $total_hours;
  75. }else{
  76. $employee_map[$value['data_id']] = $value['total_hours'];
  77. }
  78. $key = $value['data_id'];
  79. if(isset($employee_map_2[$key][$value['item_id']])){
  80. $total_hours_2 = bcadd($employee_map_2[$key][$value['item_id']], $value['total_hours'],2);
  81. $employee_map_2[$key][$value['item_id']] = $total_hours_2;
  82. }else{
  83. $employee_map_2[$key][$value['item_id']] = $value['total_hours'];
  84. }
  85. if(! in_array($value['item_id'], $item_id)) $item_id[] = $value['item_id'];
  86. }
  87. unset($data);
  88. $item_total = Item::where('del_time',0)->select('code','id')->get()->toArray();
  89. $item_map = array_column($item_total,'code','id');
  90. $tmp = [];
  91. foreach ($item_total as $value){
  92. $tmp[$value['id']] = 0;
  93. }
  94. foreach ($employee as $key => $value){
  95. //设置的人员在时间段内的总工时
  96. $set_total_hours = $employee_hours_map[$value['id']] ?? 0;
  97. $employee[$key]['set_total_hours'] = $set_total_hours;
  98. //汇总研发工时单里的总工时
  99. $rd_total_hours = $employee_map[$value['id']] ?? 0;
  100. $employee[$key]['rd_total_hours'] = $rd_total_hours;
  101. //每个项目的工时
  102. $every_item_hours = $employee_map_2[$value['id']] ?? [];
  103. $details = $tmp;
  104. $my_item = "";
  105. if(! empty($every_item_hours)){
  106. foreach ($every_item_hours as $item => $item_hour){
  107. if(isset($details[$item])) $details[$item] = $item_hour;
  108. $code = $item_map[$item] ?? "";
  109. if(! empty($code)) $my_item .= $code . ",";
  110. }
  111. $my_item = rtrim($my_item,',');
  112. }
  113. foreach ($details as $it => $d_v){
  114. $employee[$key]['item_' . $it] = $d_v;
  115. }
  116. //自己研发的项目
  117. $employee[$key]['my_item'] = $my_item;
  118. $employee[$key]['position'] = "研发人员";
  119. $employee[$key]['type_title'] = "专职";
  120. $employee[$key]['rate_one'] = "100%";
  121. $employee[$key]['rate_two'] = "100%";
  122. $employee[$key]['rate_three'] = "100%";
  123. }
  124. return $employee;
  125. }
  126. public function statisticsDeviceCommon($data,$user, $field = []){
  127. if(empty($data['order_time'][0]) || ! empty($data['order_time'][1])) return [false, '时间不能为空'];
  128. list($start_time, $end_time) = $this->changeDateToTimeStampAboutRange($data['order_time'],false);
  129. if ($start_time === null || $end_time === null || $start_time > $end_time) return [false, "时间的区间无效"];
  130. $day = $this->returnDays([
  131. 0 => $start_time,
  132. 1 => $end_time,
  133. ]);
  134. if($day > 365) return [false, '查询时间仅支持范围区间在365天内'];
  135. $model = RdDetails::from('rd_details as a')
  136. ->leftJoin('rd as b','b.id','a.rd_id')
  137. ->where('a.del_time',0)
  138. ->where('b.del_time',0)
  139. ->where('b.order_time', '>=', $start_time)
  140. ->where('b.order_time', '<=', $end_time)
  141. ->where('a.type', RdDetails::type_two)
  142. ->select('b.item_id','b.total_hours','a.data_id')
  143. ->orderby('order_time', 'desc');
  144. if(! empty($data['item_code'])) {
  145. $item_id = Item::where('code', 'LIKE', '%'.$data['item_code'].'%')
  146. ->where('del_time',0)
  147. ->pluck('id')
  148. ->toArray();
  149. $model->whereIn('b.item_id', $item_id);
  150. }
  151. if(! empty($data['device_title'])) {
  152. $data_id = Device::where('title', 'LIKE', '%'.$data['device_title'].'%')
  153. ->where('del_time',0)
  154. ->pluck('id')
  155. ->toArray();
  156. $model->whereIn('a.data_id', $data_id);
  157. }
  158. return [true, [$model, [
  159. 0 => $start_time,
  160. 1 => $end_time,
  161. ]]];
  162. }
  163. public function statisticsDevice($data,$user){
  164. list($status, $return) = $this->statisticsDeviceCommon($data, $user);
  165. if(! $status) return [false, $return];
  166. list($model, $ergs) = $return;
  167. $list = $model->get()->toArray();
  168. $list = $this->statisticsDeviceFillData($list,$ergs);
  169. // $list['count'] = $this->countTotal($list['data'], $user['header_default']);
  170. return [true, $list];
  171. }
  172. public function statisticsDeviceFillData($data, $ergs){
  173. $employee_id = array_unique(array_column($data,'data_id'));
  174. $device_hours_map = DeviceDetails::where('del_time', 0)
  175. ->whereIn('device_id', $employee_id)
  176. ->whereBetween('time', $ergs)
  177. ->groupBy('device_id')
  178. ->pluck(DB::raw('SUM(total_hours)'), 'device_id')
  179. ->toArray();
  180. $device = Device::where('del_time',0)
  181. ->select('id','title as name','code','type','type_2','power','in_time','number')
  182. ->get()->toArray();
  183. $device_map = $device_map_2 = $item_id = [];
  184. foreach ($data as $value){
  185. if(isset($device_map[$value['data_id']])){
  186. $total_hours = bcadd($device_map[$value['data_id']], $value['total_hours'],2);
  187. $device_map[$value['data_id']] = $total_hours;
  188. }else{
  189. $device_map[$value['data_id']] = $value['total_hours'];
  190. }
  191. $key = $value['data_id'];
  192. if(isset($device_map_2[$key][$value['item_id']])){
  193. $total_hours_2 = bcadd($device_map_2[$key][$value['item_id']], $value['total_hours'],2);
  194. $device_map_2[$key][$value['item_id']] = $total_hours_2;
  195. }else{
  196. $device_map_2[$key][$value['item_id']] = $value['total_hours'];
  197. }
  198. if(! in_array($value['item_id'], $item_id)) $item_id[] = $value['item_id'];
  199. }
  200. unset($data);
  201. $item_total = Item::where('del_time',0)->select('code','id')->get()->toArray();
  202. $item_map = array_column($item_total,'code','id');
  203. $tmp = [];
  204. foreach ($item_total as $value){
  205. $tmp[$value['id']] = 0;
  206. }
  207. foreach ($device as $key => $value){
  208. //设置的设备在时间段内的总工时
  209. $set_total_hours = $device_hours_map[$value['id']] ?? 0;
  210. $device[$key]['set_total_hours'] = $set_total_hours;
  211. //汇总研发工时单里的总工时
  212. $rd_total_hours = $device_map[$value['id']] ?? 0;
  213. $device[$key]['rd_total_hours'] = $rd_total_hours;
  214. //每个项目的工时
  215. $every_item_hours = $device_map_2[$value['id']] ?? [];
  216. $details = $tmp;
  217. $my_item = "";
  218. if(! empty($every_item_hours)){
  219. foreach ($every_item_hours as $item => $item_hour){
  220. if(isset($details[$item])) $details[$item] = $item_hour;
  221. $code = $item_map[$item] ?? "";
  222. if(! empty($code)) $my_item .= $code . ",";
  223. }
  224. $my_item = rtrim($my_item,',');
  225. }
  226. foreach ($details as $it => $d_v){
  227. $device[$key]['item_' . $it] = $d_v;
  228. }
  229. $device[$key]['my_item'] = $my_item;
  230. $device[$key]['position'] = "研发人员";
  231. $device[$key]['type_title'] = Device::$type[$value['type']] ?? "";
  232. $device[$key]['type_2_title'] = Device::$type_2[$value['type_2']] ?? "";
  233. $device[$key]['rate_one'] = "100%";
  234. $device[$key]['rate_two'] = "100%";
  235. $device[$key]['rate_three'] = "100%";
  236. }
  237. return $device;
  238. }
  239. }