StatisticsService.php 13 KB

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