StatisticsService.php 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605
  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. use Carbon\Carbon;
  12. class StatisticsService extends Service
  13. {
  14. public function statisticsEmployeeCommon($data,$user){
  15. if(empty($data['order_time'][0]) || empty($data['order_time'][1])) return [false, '时间不能为空'];
  16. $return = $this->changeDateToTimeStampAboutRange($data['order_time'],false);
  17. $start_time = $return[0];
  18. $end_time = $return[1];
  19. if ($start_time === null || $end_time === null || $start_time > $end_time) return [false, "时间的区间无效"];
  20. $day = $this->returnDays([
  21. 0 => $start_time,
  22. 1 => $end_time,
  23. ]);
  24. if($day > 365) return [false, '查询时间仅支持范围区间在365天内'];
  25. $model = RdDetails::from('rd_details as a')
  26. ->leftJoin('rd as b','b.id','a.rd_id')
  27. ->where('a.del_time',0)
  28. ->where('b.del_time',0)
  29. ->where('b.order_time', '>=', $start_time)
  30. ->where('b.order_time', '<=', $end_time)
  31. ->where('a.type', RdDetails::type_one)
  32. ->select('b.item_id','b.total_hours','a.data_id')
  33. ->orderby('b.order_time', 'desc');
  34. if(! empty($data['item_code'])) {
  35. $item_id = Item::where('code', 'LIKE', '%'.$data['item_code'].'%')
  36. ->where('del_time',0)
  37. ->pluck('id')
  38. ->toArray();
  39. $model->whereIn('b.item_id', $item_id);
  40. }
  41. if(! empty($data['id'])){
  42. $model->whereIn('a.data_id', $data['id']);
  43. }
  44. if(! empty($data['employee_title'])) {
  45. $data_id = Employee::where('emp_name', 'LIKE', '%'.$data['employee_title'].'%')
  46. ->where('del_time',0)
  47. ->pluck('id')
  48. ->toArray();
  49. $model->whereIn('a.data_id', $data_id);
  50. }
  51. $time = $this->getMonthsFirstDayTimestamps([
  52. 0 => $start_time,
  53. 1 => $end_time,
  54. ]);
  55. $day = CalendarDetails::where('del_time',0)
  56. ->where('time', '>=', $start_time)
  57. ->where('time', '<=', $end_time)
  58. ->where('is_work', 1)
  59. ->count();
  60. return [true, [$model, $time, $day]];
  61. }
  62. public function statisticsEmployee($data,$user){
  63. list($status, $return) = $this->statisticsEmployeeCommon($data, $user);
  64. if(! $status) return [false, $return];
  65. list($model, $ergs, $day) = $return;
  66. $list = $model->get()->toArray();
  67. $list = $this->statisticsEmployeeFillData($list, $ergs, $day);
  68. // $list['count'] = $this->countTotal($list, $user['header_default']);
  69. return [true, $list];
  70. }
  71. public function statisticsEmployeeFillData($data, $ergs, $day){
  72. $employee_id = EmployeeRole::where('del_time',0)
  73. ->where('role_id', 87)
  74. ->pluck('employee_id')
  75. ->toArray();
  76. $employee_hours_map = EmployeeDetails::where('del_time', 0)
  77. ->whereIn('time', $ergs)
  78. ->whereIn('employee_id',$employee_id)
  79. ->selectRaw('employee_id, SUM(total_hours) as total_hours')
  80. ->groupBy('employee_id')
  81. ->pluck('total_hours', 'employee_id')
  82. ->toArray();
  83. $employee = Employee::where('del_time',0)
  84. ->whereIn('id',$employee_id)
  85. ->where('id','<>',Employee::SPECIAL_ADMIN)
  86. ->select('id','emp_name as name')
  87. ->get()->toArray();
  88. $employee_map = $employee_map_2 = $item_id = [];
  89. foreach ($data as $value){
  90. if(isset($employee_map[$value['data_id']])){
  91. $total_hours = bcadd($employee_map[$value['data_id']], $value['total_hours'],2);
  92. $employee_map[$value['data_id']] = $total_hours;
  93. }else{
  94. $employee_map[$value['data_id']] = $value['total_hours'];
  95. }
  96. $key = $value['data_id'];
  97. if(isset($employee_map_2[$key][$value['item_id']])){
  98. $total_hours_2 = bcadd($employee_map_2[$key][$value['item_id']], $value['total_hours'],2);
  99. $employee_map_2[$key][$value['item_id']] = $total_hours_2;
  100. }else{
  101. $employee_map_2[$key][$value['item_id']] = $value['total_hours'];
  102. }
  103. if(! in_array($value['item_id'], $item_id)) $item_id[] = $value['item_id'];
  104. }
  105. unset($data);
  106. $item_total = Item::where('del_time',0)->select('code','id')->get()->toArray();
  107. $item_map = array_column($item_total,'code','id');
  108. $tmp = [];
  109. foreach ($item_total as $value){
  110. $tmp[$value['id']] = 0;
  111. }
  112. foreach ($employee as $key => $value){
  113. //设置的人员在时间段内的总工时
  114. $set_total_hours = $employee_hours_map[$value['id']] ?? 0;
  115. $hours = bcmul($day,8);
  116. $employee[$key]['set_total_hours'] = $hours;
  117. //汇总研发工时单里的总工时
  118. $rd_total_hours = $employee_map[$value['id']] ?? 0;
  119. $rd_total_hours = bcdiv($rd_total_hours,60,2);
  120. $employee[$key]['rd_total_hours'] = $rd_total_hours;
  121. //每个项目的工时
  122. $every_item_hours = $employee_map_2[$value['id']] ?? [];
  123. $details = $tmp;
  124. $my_item = "";
  125. if(! empty($every_item_hours)){
  126. foreach ($every_item_hours as $item => $item_hour){
  127. if(isset($details[$item])) {
  128. $details[$item] = bcdiv($item_hour,60,2);
  129. }
  130. $code = $item_map[$item] ?? "";
  131. if(! empty($code)) $my_item .= $code . ",";
  132. }
  133. $my_item = rtrim($my_item,',');
  134. }
  135. foreach ($details as $it => $d_v){
  136. $employee[$key]['extra_' . $it] = $d_v;
  137. }
  138. //自己研发的项目
  139. $employee[$key]['my_item'] = $my_item;
  140. $employee[$key]['position'] = "研发人员";
  141. $employee[$key]['type_title'] = "专职";
  142. $rate_one = "100";
  143. $employee[$key]['rate_one'] = $rate_one . "%";
  144. $rate_two = floatval($hours) > 0.0 ? bcdiv($rd_total_hours, $hours,2) : 0;
  145. $rate_two = bcmul($rate_two, 100,2);
  146. $employee[$key]['rate_two'] = $rate_two . "%";
  147. $employee[$key]['rate_three'] = $rate_two . "%";
  148. }
  149. return $employee;
  150. }
  151. public function statisticsDeviceCommon($data,$user, $field = []){
  152. if(empty($data['order_time'][0]) || empty($data['order_time'][1])) return [false, '时间不能为空'];
  153. $return = $this->changeDateToTimeStampAboutRange($data['order_time'],false);
  154. $start_time = $return[0];
  155. $end_time = $return[1];
  156. if ($start_time === null || $end_time === null || $start_time > $end_time) return [false, "时间的区间无效"];
  157. $day = $this->returnDays([
  158. 0 => $start_time,
  159. 1 => $end_time,
  160. ]);
  161. if($day > 365) return [false, '查询时间仅支持范围区间在365天内'];
  162. $model = RdDetails::from('rd_details as a')
  163. ->leftJoin('rd as b','b.id','a.rd_id')
  164. ->where('a.del_time',0)
  165. ->where('b.del_time',0)
  166. ->where('b.order_time', '>=', $start_time)
  167. ->where('b.order_time', '<=', $end_time)
  168. ->where('a.type', RdDetails::type_two)
  169. ->select('b.item_id','b.total_hours','a.data_id')
  170. ->orderby('b.order_time', 'desc');
  171. if(! empty($data['item_code'])) {
  172. $item_id = Item::where('code', 'LIKE', '%'.$data['item_code'].'%')
  173. ->where('del_time',0)
  174. ->pluck('id')
  175. ->toArray();
  176. $model->whereIn('b.item_id', $item_id);
  177. }
  178. if(! empty($data['id'])){
  179. $model->whereIn('a.data_id', $data['id']);
  180. }
  181. if(! empty($data['device_title'])) {
  182. $data_id = Device::where('title', 'LIKE', '%'.$data['device_title'].'%')
  183. ->where('del_time',0)
  184. ->pluck('id')
  185. ->toArray();
  186. $model->whereIn('a.data_id', $data_id);
  187. }
  188. $time = $this->getMonthsFirstDayTimestamps([
  189. 0 => $start_time,
  190. 1 => $end_time,
  191. ]);
  192. return [true, [$model, $time]];
  193. }
  194. public function statisticsDevice($data,$user){
  195. list($status, $return) = $this->statisticsDeviceCommon($data, $user);
  196. if(! $status) return [false, $return];
  197. list($model, $ergs) = $return;
  198. $list = $model->get()->toArray();
  199. $list = $this->statisticsDeviceFillData($list,$ergs);
  200. // $list['count'] = $this->countTotal($list['data'], $user['header_default']);
  201. return [true, $list];
  202. }
  203. public function statisticsDeviceFillData($data, $ergs){
  204. $device_hours_map = DeviceDetails::where('del_time', 0)
  205. ->whereIn('time', $ergs)
  206. ->selectRaw('device_id, SUM(total_hours) as total_hours')
  207. ->groupBy('device_id')
  208. ->pluck('total_hours', 'device_id')
  209. ->toArray();
  210. $device = Device::where('del_time',0)
  211. ->select('id','title as name','code','type','type_2','power','in_time','number')
  212. ->get()->toArray();
  213. $device_map = $device_map_2 = $item_id = [];
  214. foreach ($data as $value){
  215. if(isset($device_map[$value['data_id']])){
  216. $total_hours = bcadd($device_map[$value['data_id']], $value['total_hours'],2);
  217. $device_map[$value['data_id']] = $total_hours;
  218. }else{
  219. $device_map[$value['data_id']] = $value['total_hours'];
  220. }
  221. $key = $value['data_id'];
  222. if(isset($device_map_2[$key][$value['item_id']])){
  223. $total_hours_2 = bcadd($device_map_2[$key][$value['item_id']], $value['total_hours'],2);
  224. $device_map_2[$key][$value['item_id']] = $total_hours_2;
  225. }else{
  226. $device_map_2[$key][$value['item_id']] = $value['total_hours'];
  227. }
  228. if(! in_array($value['item_id'], $item_id)) $item_id[] = $value['item_id'];
  229. }
  230. unset($data);
  231. $item_total = Item::where('del_time',0)->select('code','id')->get()->toArray();
  232. $item_map = array_column($item_total,'code','id');
  233. $tmp = [];
  234. foreach ($item_total as $value){
  235. $tmp[$value['id']] = 0;
  236. }
  237. foreach ($device as $key => $value){
  238. //设置的设备在时间段内的总工时
  239. $set_total_hours = $device_hours_map[$value['id']] ?? 0;
  240. $device[$key]['set_total_hours'] = $set_total_hours;
  241. //汇总研发工时单里的总工时
  242. $rd_total_hours = $device_map[$value['id']] ?? 0;
  243. $rd_total_hours = bcdiv($rd_total_hours,60,2);
  244. $device[$key]['rd_total_hours'] = $rd_total_hours;
  245. // $device[$key]['set_total_hours'] = $rd_total_hours;
  246. //每个项目的工时
  247. $every_item_hours = $device_map_2[$value['id']] ?? [];
  248. $details = $tmp;
  249. $my_item = "";
  250. if(! empty($every_item_hours)){
  251. foreach ($every_item_hours as $item => $item_hour){
  252. if(isset($details[$item])) {
  253. $details[$item] = bcdiv($item_hour,60,2);
  254. }
  255. $code = $item_map[$item] ?? "";
  256. if(! empty($code)) $my_item .= $code . ",";
  257. }
  258. $my_item = rtrim($my_item,',');
  259. }
  260. foreach ($details as $it => $d_v){
  261. $device[$key]['extra_' . $it] = $d_v;
  262. }
  263. $device[$key]['my_item'] = $my_item;
  264. $device[$key]['type_title'] = Device::$type[$value['type']] ?? "";
  265. $device[$key]['type_2_title'] = Device::$type_2[$value['type_2']] ?? "";
  266. //应有占比
  267. if($value['type_2'] == Device::type_one){
  268. $rate_one = "100";
  269. } else{
  270. $rate_one = "10";
  271. }
  272. $rate_one = $rate_one . "%";
  273. $device[$key]['rate_one'] = $rate_one . "%";
  274. $rate_two = floatval($rd_total_hours) > 0.0 ? bcdiv($rd_total_hours, $set_total_hours,2) : 0;
  275. $rate_two = bcmul($rate_two, 100,2);
  276. $device[$key]['rate_two'] = $rate_two . "%";
  277. $rate_three = bcmul(bcdiv($rate_two, $rate_one,2),100,2);
  278. $device[$key]['rate_three'] = $rate_three . "%";
  279. }
  280. return $device;
  281. }
  282. function getMonthsFirstDayTimestamps(array $ergs)
  283. {
  284. if (empty($ergs) || count($ergs) < 2) return [];
  285. $start = (int)$ergs[0];
  286. $end = (int)$ergs[1];
  287. $timestamps = [];
  288. // 从开始时间创建 DateTime 对象
  289. $current = (new \DateTime())->setTimestamp($start)->modify('first day of this month')->setTime(0, 0, 0);
  290. // 结束时间
  291. $endDate = (new \DateTime())->setTimestamp($end)->modify('first day of this month')->setTime(0, 0, 0);
  292. while ($current <= $endDate) {
  293. $timestamps[] = $current->getTimestamp();
  294. $current->modify('+1 month'); // 下个月第一天
  295. }
  296. return $timestamps;
  297. }
  298. public function statisticsEmployeeExportCommon($data, $user){
  299. if(empty($data['month'])) return [false, '请选择导出的月份'];
  300. $month = $data['month'];
  301. $start_time = Carbon::createFromFormat('Y-m', $month)->startOfMonth()->timestamp;
  302. $end_time = Carbon::createFromFormat('Y-m', $month)->endOfMonth()->timestamp;
  303. $model = RdDetails::from('rd_details as a')
  304. ->leftJoin('rd as b','b.id','a.rd_id')
  305. ->where('a.del_time',0)
  306. ->where('b.del_time',0)
  307. ->where('b.order_time', '>=', $start_time)
  308. ->where('b.order_time', '<=', $end_time)
  309. ->where('a.type', RdDetails::type_one)
  310. ->select('b.item_id','b.total_hours','a.data_id','b.order_time')
  311. ->orderby('b.order_time', 'asc');
  312. $time = $this->getMonthsFirstDayTimestamps([
  313. 0 => $start_time,
  314. 1 => $end_time,
  315. ]);
  316. $day = CalendarDetails::where('del_time',0)
  317. ->where('time', '>=', $start_time)
  318. ->where('time', '<=', $end_time)
  319. ->where('is_work', 1)
  320. ->count();
  321. return [true, [$model, $time, $day]];
  322. }
  323. public function statisticsEmployeeFillExportData($data, $ergs, $day,$employee,$item_total){
  324. // 3. 统计传入数据中的工时(可能是月度,也可能是某一天)
  325. $employee_map = [];
  326. $employee_map_2 = [];
  327. foreach ($data as $value) {
  328. $eid = $value['data_id'];
  329. $iid = $value['item_id'];
  330. $hours = $value['total_hours'];
  331. $employee_map[$eid] = bcadd($employee_map[$eid] ?? 0, $hours, 2);
  332. $employee_map_2[$eid][$iid] = bcadd($employee_map_2[$eid][$iid] ?? 0, $hours, 2);
  333. }
  334. $item_map = array_column($item_total, 'code', 'id');
  335. $empty_item_structure = array_fill_keys(array_keys($item_map), 0);
  336. // 4. 组装最终行数据
  337. foreach ($employee as $key => $value) {
  338. $curr_id = $value['id'];
  339. // 计算额定工时
  340. $hours = bcmul($day, 8);
  341. $employee[$key]['set_total_hours'] = $hours;
  342. // 计算研发工时
  343. $rd_total_minutes = $employee_map[$curr_id] ?? 0;
  344. $rd_total_hours = bcdiv($rd_total_minutes, 60, 2);
  345. $employee[$key]['rd_total_hours'] = $rd_total_hours;
  346. // 项目明细列
  347. $every_item_hours = $employee_map_2[$curr_id] ?? [];
  348. $details = $empty_item_structure;
  349. $my_items = [];
  350. foreach ($every_item_hours as $item_id => $item_minutes) {
  351. $details[$item_id] = bcdiv($item_minutes, 60, 2);
  352. if (isset($item_map[$item_id])) {
  353. $my_items[] = $item_map[$item_id];
  354. }
  355. }
  356. foreach ($details as $it => $d_v) {
  357. $employee[$key]['extra_' . $it] = $d_v;
  358. }
  359. $employee[$key]['my_item'] = implode(',', $my_items);
  360. $employee[$key]['position'] = "研发人员";
  361. $employee[$key]['type_title'] = "专职";
  362. // 计算比率
  363. $rate_val = (floatval($hours) > 0) ? bcdiv($rd_total_hours, $hours, 2) : 0;
  364. $rate_percent = bcmul($rate_val, 100, 2) . "%";
  365. $employee[$key]['rate_one'] = "100%";
  366. $employee[$key]['rate_two'] = $rate_percent;
  367. $employee[$key]['rate_three'] = $rate_percent;
  368. }
  369. return $employee;
  370. }
  371. public function statisticsDeviceExportCommon($data,$user){
  372. if(empty($data['month'])) return [false, '请选择导出的月份'];
  373. $month = $data['month'];
  374. $start_time = Carbon::createFromFormat('Y-m', $month)->startOfMonth()->timestamp;
  375. $end_time = Carbon::createFromFormat('Y-m', $month)->endOfMonth()->timestamp;
  376. $model = RdDetails::from('rd_details as a')
  377. ->leftJoin('rd as b','b.id','a.rd_id')
  378. ->where('a.del_time',0)
  379. ->where('b.del_time',0)
  380. ->where('b.order_time', '>=', $start_time)
  381. ->where('b.order_time', '<=', $end_time)
  382. ->where('a.type', RdDetails::type_two)
  383. ->select('b.item_id','b.total_hours','a.data_id','b.order_time')
  384. ->orderby('b.order_time', 'asc');
  385. $time = $this->getMonthsFirstDayTimestamps([
  386. 0 => $start_time,
  387. 1 => $end_time,
  388. ]);
  389. return [true, [$model, $time]];
  390. }
  391. public function statisticsDeviceFillExportData1($data, $ergs, $device, $item_total){
  392. $device_map = $device_map_2 = [];
  393. foreach ($data as $value){
  394. if(isset($device_map[$value['data_id']])){
  395. $total_hours = bcadd($device_map[$value['data_id']], $value['total_hours'],2);
  396. $device_map[$value['data_id']] = $total_hours;
  397. }else{
  398. $device_map[$value['data_id']] = $value['total_hours'];
  399. }
  400. $key = $value['data_id'];
  401. if(isset($device_map_2[$key][$value['item_id']])){
  402. $total_hours_2 = bcadd($device_map_2[$key][$value['item_id']], $value['total_hours'],2);
  403. $device_map_2[$key][$value['item_id']] = $total_hours_2;
  404. }else{
  405. $device_map_2[$key][$value['item_id']] = $value['total_hours'];
  406. }
  407. }
  408. unset($data);
  409. $tmp = [];
  410. foreach ($item_total as $value){
  411. $tmp[$value['id']] = 0;
  412. }
  413. foreach ($device as $key => $value){
  414. $device[$key]['in_time'] = $value['in_time'] ? date("Y-m-d", $value['in_time']) : '';
  415. //汇总研发工时单里的总工时
  416. $rd_total_hours = $device_map[$value['id']] ?? 0;
  417. $rd_total_hours = bcdiv($rd_total_hours,60,2);
  418. $device[$key]['rd_total_hours'] = $rd_total_hours;
  419. $device[$key]['set_total_hours'] = $rd_total_hours;
  420. //每个项目的工时
  421. $every_item_hours = $device_map_2[$value['id']] ?? [];
  422. $details = $tmp;
  423. $my_item = "";
  424. if(! empty($every_item_hours)){
  425. foreach ($every_item_hours as $item => $item_hour){
  426. if(isset($details[$item])) {
  427. $details[$item] = bcdiv($item_hour,60,2);
  428. }
  429. // $code = $item_map[$item] ?? "";
  430. // if(! empty($code)) $my_item .= $code . ",";
  431. }
  432. // $my_item = rtrim($my_item,',');
  433. }
  434. foreach ($details as $it => $d_v){
  435. $device[$key]['extra_' . $it] = $d_v;
  436. }
  437. $device[$key]['my_item'] = $my_item;
  438. $device[$key]['type_title'] = Device::$type[$value['type']] ?? "";
  439. $device[$key]['type_2_title'] = Device::$type_2[$value['type_2']] ?? "";
  440. $rate_one = "100";
  441. $device[$key]['rate_one'] = $rate_one . "%";
  442. $rate_two = floatval($rd_total_hours) > 0.0 ? bcdiv($rd_total_hours, $rd_total_hours,2) : 0;
  443. $rate_two = bcmul($rate_two, 100,2);
  444. $device[$key]['rate_two'] = $rate_two . "%";
  445. $device[$key]['rate_three'] = $rate_two . "%";
  446. }
  447. return $device;
  448. }
  449. public function statisticsDeviceFillExportData($data, $ergs, $device, $item_total, $is_count = false, $set_map = []) {
  450. $device_map = $device_map_2 = [];
  451. foreach ($data as $value) {
  452. $device_map[$value['data_id']] = bcadd($device_map[$value['data_id']] ?? 0, $value['total_hours'], 2);
  453. $device_map_2[$value['data_id']][$value['item_id']] = bcadd($device_map_2[$value['data_id']][$value['item_id']] ?? 0, $value['total_hours'], 2);
  454. }
  455. unset($data);
  456. $tmp = [];
  457. foreach ($item_total as $value) {
  458. $tmp[$value['id']] = 0;
  459. }
  460. foreach ($device as $key => $value) {
  461. $device[$key]['in_time'] = $value['in_time'] ? date("Y-m-d", $value['in_time']) : '';
  462. $rd_total_hours = bcdiv($device_map[$value['id']] ?? 0, 60, 2);
  463. $device[$key]['rd_total_hours'] = $rd_total_hours;
  464. $set_total_hours = $set_map[$value['id']] ?? 0;
  465. $device[$key]['set_total_hours'] = $set_total_hours;
  466. $every_item_hours = $device_map_2[$value['id']] ?? [];
  467. $details = $tmp;
  468. if (!empty($every_item_hours)) {
  469. foreach ($every_item_hours as $item => $item_hour) {
  470. if (isset($details[$item])) {
  471. $details[$item] = bcdiv($item_hour, 60, 2);
  472. }
  473. }
  474. }
  475. foreach ($details as $it => $d_v) {
  476. $device[$key]['extra_' . $it] = $d_v;
  477. }
  478. // 基础信息补充
  479. $device[$key]['my_item'] = "";
  480. $device[$key]['type_title'] = Device::$type[$value['type']] ?? "";
  481. $device[$key]['type_2_title'] = Device::$type_2[$value['type_2']] ?? "";
  482. // 百分比计算
  483. //应有占比
  484. if($value['type_2'] == Device::type_one){
  485. $rate_one = "100";
  486. } else{
  487. $rate_one = "10";
  488. }
  489. $device[$key]['rate_one'] = $rate_one . "%";
  490. $rate_two = floatval($rd_total_hours) > 0.0 ? bcdiv($rd_total_hours, $set_total_hours,2) : 0;
  491. $rate_two = bcmul($rate_two, 100,2);
  492. $device[$key]['rate_two'] = $rate_two;
  493. $rate_three = bcmul(bcdiv($rate_two, $rate_one,2),100,2);
  494. $device[$key]['rate_three'] = $rate_three . "%";
  495. }
  496. // --- 合计逻辑 ---
  497. if ($is_count && !empty($device)) {
  498. //取第一行作为模板,所有值初始化为空字符串
  499. $summaryRow = array_map(fn() => '', $device[array_key_first($device)]);
  500. foreach ($device as $row) {
  501. // 动态累加所有 extra_ 开头的列
  502. foreach ($row as $k => $v) {
  503. if (strpos($k, 'extra_') === 0) {
  504. $summaryRow[$k] = bcadd($summaryRow[$k] ?: 0, $v, 2);
  505. }
  506. }
  507. }
  508. $device[] = $summaryRow;
  509. }
  510. return $device;
  511. }
  512. }