PersonWorkService.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  1. <?php
  2. namespace App\Service;
  3. use App\Model\Device;
  4. use App\Model\Employee;
  5. use App\Model\Item;
  6. use App\Model\ItemDetails;
  7. use App\Model\MonthlyPwOrder;
  8. use App\Model\MonthlyPwOrderDetails;
  9. use Illuminate\Support\Facades\DB;
  10. class PersonWorkService extends Service
  11. {
  12. public function monthlyPwOrderEdit($data,$user){
  13. list($status,$msg) = $this->monthlyPwOrderRule($data, $user, false);
  14. if(!$status) return [$status,$msg];
  15. try {
  16. DB::beginTransaction();
  17. $model = Item::where('id',$data['id'])->first();
  18. $model->month = $data['month'] ?? 0;
  19. $model->save();
  20. $time = time();
  21. MonthlyPwOrderDetails::where('del_time',0)
  22. ->where('main_id', $model->id)
  23. ->update(['del_time' => $time]);
  24. $this->saveDetail($model->id, $time, $data);
  25. DB::commit();
  26. }catch (\Exception $exception){
  27. DB::rollBack();
  28. return [false,$exception->getMessage()];
  29. }
  30. return [true, ''];
  31. }
  32. public function monthlyPwOrderAdd($data,$user){
  33. list($status,$msg) = $this->monthlyPwOrderRule($data, $user);
  34. if(!$status) return [$status,$msg];
  35. try {
  36. DB::beginTransaction();
  37. $model = new MonthlyPwOrder();
  38. $model->code = $this->generateBillNo([
  39. 'top_depart_id' => $user['top_depart_id'],
  40. 'type' => MonthlyPwOrder::Order_type,
  41. 'period' => date("Ym", $data['month'])
  42. ]);
  43. $model->month = $data['month'] ?? 0;
  44. $model->crt_id = $user['id'];
  45. $model->top_depart_id = $data['top_depart_id'];
  46. $model->save();
  47. $this->saveDetail($model->id, time(), $data);
  48. DB::commit();
  49. }catch (\Exception $exception){
  50. DB::rollBack();
  51. return [false,$exception->getMessage()];
  52. }
  53. return [true, ''];
  54. }
  55. private function saveDetail($id, $time, $data){
  56. if(! empty($data['details'])){
  57. $unit = [];
  58. foreach ($data['details'] as $value){
  59. $unit[] = [
  60. 'main_id' => $id,
  61. 'employee_id' => $value['employee_id'],
  62. 'total_days' => $value['total_days'],
  63. 'rd_total_days' => $value['rd_total_days'],
  64. 'total_hours' => $value['total_hours'],
  65. 'rd_total_hours' => $value['rd_total_hours'],
  66. 'start_time' => $value['start_time'],
  67. 'end_time' => $value['end_time'],
  68. 'crt_time' => $time,
  69. 'top_depart_id' => $value['top_depart_id'],
  70. ];
  71. }
  72. if(! empty($unit)) MonthlyPwOrderDetails::insert($unit);
  73. }
  74. }
  75. private function getDetail($id){
  76. $data = MonthlyPwOrderDetails::where('del_time',0)
  77. ->where('main_id', $id)
  78. ->select('employee_id', 'total_days', 'rd_total_days', 'total_hours', 'rd_total_hours', 'start_time', 'end_time')
  79. ->get()->toArray();
  80. $id = array_column($data,'employee_id');
  81. $map = Employee::whereIn('id', $id)->select('title','id','number')->get()->toArray();
  82. $map = array_column($map,null,'id');
  83. foreach ($data as $key => $value){
  84. $tmp = $map[$value['employee_id']] ?? [];
  85. $merge = [];
  86. $merge['employee_title'] = $tmp['title'];
  87. $merge['employee_number'] = $tmp['number'];
  88. $merge['start_time'] = date("Y-m-d", $value['start_time']);
  89. $merge['end_time'] = date("Y-m-d", $value['end_time']);
  90. $data[$key] = array_merge($value, $merge);
  91. }
  92. $detail = [
  93. 'details' => $data,
  94. ];
  95. foreach ($detail as $key => $value) {
  96. if (empty($value)) {
  97. $detail[$key] = (object)[]; // 转成 stdClass 对象
  98. }
  99. }
  100. return $detail;
  101. }
  102. public function monthlyPwOrderDel($data){
  103. if($this->isEmpty($data,'id')) return [false,'请选择数据!'];
  104. try {
  105. DB::beginTransaction();
  106. $time = time();
  107. MonthlyPwOrder::where('del_time',0)
  108. ->whereIn('id',$data['id'])
  109. ->update(['del_time' => $time]);
  110. MonthlyPwOrderDetails::where('del_time',0)
  111. ->whereIn('main_id', $data['id'])
  112. ->update(['del_time' => $time]);
  113. DB::commit();
  114. }catch (\Exception $exception){
  115. DB::rollBack();
  116. return [false,$exception->getMessage()];
  117. }
  118. return [true, ''];
  119. }
  120. public function monthlyPwOrderDetail($data, $user){
  121. if($this->isEmpty($data,'id')) return [false,'请选择数据!'];
  122. $customer = MonthlyPwOrder::where('del_time',0)
  123. ->where('id',$data['id'])
  124. ->first();
  125. if(empty($customer)) return [false,'项目不存在或已被删除'];
  126. $customer = $customer->toArray();
  127. $customer['crt_name'] = Employee::where('id',$customer['crt_id'])->value('title');
  128. $customer['crt_time'] = $customer['crt_time'] ? date("Y-m-d H:i:s",$customer['crt_time']): '';
  129. $customer['month'] = $customer['month'] ? date("Y-m",$customer['month']): '';
  130. $details = $this->getDetail($data['id']);
  131. $customer = array_merge($customer, $details);
  132. return [true, $customer];
  133. }
  134. public function monthlyPwOrderCommon($data,$user, $field = []){
  135. if(empty($field)) $field = MonthlyPwOrder::$field;
  136. $model = MonthlyPwOrder::Clear($user,$data);
  137. $model = $model->where('del_time',0)
  138. ->select($field)
  139. ->orderby('id', 'desc');
  140. if(! empty($data['code'])) $model->where('code', 'LIKE', '%'.$data['code'].'%');
  141. if(! empty($data['id'])) $model->whereIn('id', $data['id']);
  142. if(! empty($data['crt_time'][0]) && ! empty($data['crt_time'][1])) {
  143. $return = $this->changeDateToTimeStampAboutRange($data['crt_time']);
  144. $model->where('crt_time','>=',$return[0]);
  145. $model->where('crt_time','<=',$return[1]);
  146. }
  147. return $model;
  148. }
  149. public function monthlyPwOrderList($data,$user){
  150. $model = $this->monthlyPwOrderCommon($data, $user);
  151. $list = $this->limit($model,'',$data);
  152. $list = $this->fillData($list);
  153. return [true, $list];
  154. }
  155. public function monthlyPwOrderRule(&$data, $user, $is_add = true){
  156. if(empty($data['month'])) return [false, '月份不能为空'];
  157. $data['month'] = $this->changeDateToDate($data['month']);
  158. $data['top_depart_id'] = $user['top_depart_id'];
  159. if(empty($data['details'])) return [false, '人员月度工时单明细不能为空'];
  160. foreach ($data['details'] as $key => $value){
  161. if(empty($value['employee_id'])) return [false, '人员不能为空'];
  162. $res = $this->checkNumber($value['total_days'],0,'non-negative');
  163. if(! $res['valid']) return [false,'出勤总天数:' . $res['error']];
  164. $res = $this->checkNumber($value['rd_total_days'],0,'non-negative');
  165. if(! $res['valid']) return [false,'研发出勤总天数:' . $res['error']];
  166. $res = $this->checkNumber($value['total_hours'],2,'non-negative');
  167. if(! $res['valid']) return [false,'出勤总工时:' . $res['error']];
  168. $res = $this->checkNumber($value['rd_total_hours'],2,'non-negative');
  169. if(! $res['valid']) return [false,'研发总工时:' . $res['error']];
  170. if(empty($value['start_time'])) return [false, '开始时间不能为空'];
  171. $data['details'][$key]['start_time'] = $this->changeDateToDate($value['start_time']);
  172. if(empty($value['end_time'])) return [false, '结束时间不能为空'];
  173. $data['details'][$key]['end_time'] = $this->changeDateToDate($value['end_time']);
  174. $data['details'][$key]['top_depart_id'] = $data['top_depart_id'];
  175. }
  176. list($status, $msg) = $this->checkArrayRepeat($data['details'],'employee_id','人员');
  177. if(! $status) return [false, $msg];
  178. if($is_add){
  179. $bool = MonthlyPwOrder::where('code',$data['code'])
  180. ->where('top_depart_id', $data['top_depart_id'])
  181. ->where('del_time',0)
  182. ->exists();
  183. }else{
  184. if(empty($data['id'])) return [false,'ID不能为空'];
  185. $bool = Item::where('code',$data['code'])
  186. ->where('top_depart_id', $data['top_depart_id'])
  187. ->where('id','<>',$data['id'])
  188. ->where('del_time',0)
  189. ->exists();
  190. }
  191. if($bool) return [false, date("Y-m", $data['month']) . '已存在人员月度研发工时单'];
  192. return [true, ''];
  193. }
  194. public function fillData($data, $is_export = false){
  195. if(empty($data['data'])) return $data;
  196. $emp = (new EmployeeService())->getEmployeeMap(array_unique(array_column($data['data'],'crt_id')));
  197. $map = [];
  198. if($is_export) $map = $this->getDetailsMap(array_column($data['data'],'id'));
  199. foreach ($data['data'] as $key => $value){
  200. $data['data'][$key]['crt_time'] = $value['crt_time'] ? date('Y-m-d H:i:s',$value['crt_time']) : '';
  201. $data['data'][$key]['month'] = $value['month'] ? date('Y-m',$value['month']) : '';
  202. $data['data'][$key]['crt_name'] = $emp[$value['crt_id']] ?? '';
  203. $tmp = $map[$value['id']] ?? [];
  204. $data['data'][$key]['details'] = $tmp['man'] ?? "";
  205. }
  206. return $data;
  207. }
  208. public function fillDataForExport($data, $column, &$return)
  209. {
  210. if(empty($data)) return;
  211. $mainIds = array_column($data, 'id');
  212. // 获取详情映射 [main_id => [details...]]
  213. $detailsMap = $this->getDetailsMap($mainIds);
  214. // 默认空行模板
  215. $defaultRow = array_fill_keys($column, '');
  216. foreach ($data as $main) {
  217. $mainId = $main['id'];
  218. $details = $detailsMap[$mainId] ?? [];
  219. // 提取主表信息
  220. $mainInfo = [
  221. 'code' => $main['code'],
  222. 'month' => $main['month'] ? date('Y-m', $main['month']) : '',
  223. ];
  224. if (empty($details)) {
  225. // 如果没有详情,至少导出一行主表信息(可选)
  226. $return[] = array_merge($defaultRow, $mainInfo);
  227. } else {
  228. // 核心:遍历详情,每一行详情都合并主表信息
  229. foreach ($details as $sub) {
  230. // 合并主表字段 + 详情字段
  231. $fullRow = array_merge($mainInfo, $sub);
  232. // 过滤掉不在导出列里的字段,并补充缺失列
  233. $return[] = array_merge($defaultRow, array_intersect_key($fullRow, $defaultRow));
  234. }
  235. }
  236. }
  237. }
  238. public function getDetailsMap($main_ids)
  239. {
  240. // 获取详情
  241. $details = MonthlyPwOrderDetails::where('del_time', 0)
  242. ->whereIn('main_id', $main_ids)
  243. ->get();
  244. // 获取人员信息
  245. $empIds = $details->pluck('employee_id')->unique();
  246. $empMap = Employee::whereIn('id', $empIds)->get()->keyBy('id');
  247. $res = [];
  248. foreach ($details as $item) {
  249. $tmpEmp = $empMap[$item->employee_id] ?? null;
  250. // 组装每一行详情需要展示的字段
  251. $res[$item->main_id][] = [
  252. 'employee_number' => $tmpEmp ? $tmpEmp->number : '',
  253. 'total_days' => $item->total_days,
  254. 'rd_total_days' => $item->rd_total_days,
  255. 'total_hours' => $item->total_hours,
  256. 'rd_total_hours' => $item->rd_total_hours,
  257. 'start_time' => $item->start_time ? date("Y-m-d", $item->start_time) : '',
  258. 'end_time' => $item->end_time ? date("Y-m-d", $item->end_time) : '',
  259. ];
  260. }
  261. return $res; // 返回 [main_id => [detail_row, detail_row]]
  262. }
  263. }