StatisticsService.php 13 KB

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