DeviceWorkService.php 12 KB

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