StatisticsService.php 11 KB

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