PersonWorkService.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  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 getDetailsMap($item_id){
  209. // 1. 获取明细数据,注意一定要选出 item_id
  210. $data = ItemDetails::where('del_time', 0)
  211. ->whereIn('item_id', $item_id)
  212. ->select('item_id', 'data_id', 'type')
  213. ->get();
  214. // 2. 收集 ID 并按类型分类(去重以减少查询压力)
  215. $manIds = $data->where('type', ItemDetails::type_one)->pluck('data_id')->unique()->toArray();
  216. $devIds = $data->where('type', ItemDetails::type_two)->pluck('data_id')->unique()->toArray();
  217. // 3. 一次性查出对应的映射 (假设人员表字段是 number, 设备表是 code)
  218. // 注意:pluck('显示内容', 'ID')
  219. $manMap = Employee::whereIn('id', $manIds)->pluck('number', 'id')->toArray();
  220. $devMap = Device::whereIn('id', $devIds)->pluck('code', 'id')->toArray();
  221. // 4. 按 item_id 分组处理成字符串
  222. $result = [];
  223. foreach ($item_id as $id) {
  224. // 初始化每个 item_id 的默认结构
  225. $result[$id] = [
  226. 'man' => '',
  227. 'device' => ''
  228. ];
  229. }
  230. // 5. 遍历明细填充数据
  231. $grouped = $data->groupBy('item_id');
  232. foreach ($grouped as $itemId => $items) {
  233. $mans = [];
  234. $devices = [];
  235. foreach ($items as $item) {
  236. if ($item->type == ItemDetails::type_one) {
  237. if (isset($manMap[$item->data_id])) {
  238. $mans[] = $manMap[$item->data_id];
  239. }
  240. } else {
  241. if (isset($devMap[$item->data_id])) {
  242. $devices[] = $devMap[$item->data_id];
  243. }
  244. }
  245. }
  246. $result[$itemId] = [
  247. 'man' => implode(',', $mans),
  248. 'device' => implode(',', $devices)
  249. ];
  250. }
  251. return $result;
  252. }
  253. }