StatisticsService.php 12 KB

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