StatisticsService.php 12 KB

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