StatisticsService.php 12 KB

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