StatisticsService.php 12 KB

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