PersonWorkService.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  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. $unit = [];
  84. foreach ($data as $key => $value){
  85. $tmp = $map[$value['employee_id']] ?? [];
  86. $merge = [];
  87. $merge['employee_title'] = $tmp['title'];
  88. $merge['employee_number'] = $tmp['number'];
  89. $merge['start_time'] = date("Y-m-d", $value['start_time']);
  90. $merge['end_time'] = date("Y-m-d", $value['end_time']);
  91. $data[$key] = array_merge($value, $merge);
  92. }
  93. $detail = [
  94. 'details' => $unit,
  95. ];
  96. foreach ($detail as $key => $value) {
  97. if (empty($value)) {
  98. $detail[$key] = (object)[]; // 转成 stdClass 对象
  99. }
  100. }
  101. return $detail;
  102. }
  103. public function monthlyPwOrderDel($data){
  104. if($this->isEmpty($data,'id')) return [false,'请选择数据!'];
  105. try {
  106. DB::beginTransaction();
  107. $time = time();
  108. MonthlyPwOrder::where('del_time',0)
  109. ->whereIn('id',$data['id'])
  110. ->update(['del_time' => $time]);
  111. MonthlyPwOrderDetails::where('del_time',0)
  112. ->whereIn('main_id', $data['id'])
  113. ->update(['del_time' => $time]);
  114. DB::commit();
  115. }catch (\Exception $exception){
  116. DB::rollBack();
  117. return [false,$exception->getMessage()];
  118. }
  119. return [true, ''];
  120. }
  121. public function monthlyPwOrderDetail($data, $user){
  122. if($this->isEmpty($data,'id')) return [false,'请选择数据!'];
  123. $customer = MonthlyPwOrder::where('del_time',0)
  124. ->where('id',$data['id'])
  125. ->first();
  126. if(empty($customer)) return [false,'项目不存在或已被删除'];
  127. $customer = $customer->toArray();
  128. $customer['crt_name'] = Employee::where('id',$customer['crt_id'])->value('title');
  129. $customer['crt_time'] = $customer['crt_time'] ? date("Y-m-d H:i:s",$customer['crt_time']): '';
  130. $customer['month'] = $customer['month'] ? date("Y-m",$customer['month']): '';
  131. $details = $this->getDetail($data['id']);
  132. $customer = array_merge($customer, $details);
  133. return [true, $customer];
  134. }
  135. public function monthlyPwOrderCommon($data,$user, $field = []){
  136. if(empty($field)) $field = MonthlyPwOrder::$field;
  137. $model = MonthlyPwOrder::Clear($user,$data);
  138. $model = $model->where('del_time',0)
  139. ->select($field)
  140. ->orderby('id', 'desc');
  141. if(! empty($data['code'])) $model->where('code', 'LIKE', '%'.$data['code'].'%');
  142. if(! empty($data['id'])) $model->whereIn('id', $data['id']);
  143. if(! empty($data['crt_time'][0]) && ! empty($data['crt_time'][1])) {
  144. $return = $this->changeDateToTimeStampAboutRange($data['crt_time']);
  145. $model->where('crt_time','>=',$return[0]);
  146. $model->where('crt_time','<=',$return[1]);
  147. }
  148. return $model;
  149. }
  150. public function monthlyPwOrderList($data,$user){
  151. $model = $this->monthlyPwOrderCommon($data, $user);
  152. $list = $this->limit($model,'',$data);
  153. $list = $this->fillData($list);
  154. return [true, $list];
  155. }
  156. public function monthlyPwOrderRule(&$data, $user, $is_add = true){
  157. if(empty($data['month'])) return [false, '月份不能为空'];
  158. $data['month'] = $this->changeDateToDate($data['month']);
  159. $data['top_depart_id'] = $user['top_depart_id'];
  160. if(empty($data['details'])) return [false, '人员月度工时单明细不能为空'];
  161. foreach ($data['details'] as $key => $value){
  162. if(empty($value['employee_id'])) return [false, '人员不能为空'];
  163. $res = $this->checkNumber($data['total_days'],0,'non-negative');
  164. if(! $res['valid']) return [false,'出勤总天数:' . $res['error']];
  165. $res = $this->checkNumber($data['rd_total_days'],0,'non-negative');
  166. if(! $res['valid']) return [false,'研发出勤总天数:' . $res['error']];
  167. $res = $this->checkNumber($data['total_hours'],2,'non-negative');
  168. if(! $res['valid']) return [false,'出勤总工时:' . $res['error']];
  169. $res = $this->checkNumber($data['rd_total_hours'],2,'non-negative');
  170. if(! $res['valid']) return [false,'研发总工时:' . $res['error']];
  171. if(empty($value['start_time'])) return [false, '开始时间不能为空'];
  172. $data['details'][$key]['start_time'] = $this->changeDateToDate($data['start_time']);
  173. if(empty($value['end_time'])) return [false, '结束时间不能为空'];
  174. $data['details'][$key]['end_time'] = $this->changeDateToDate($data['end_time']);
  175. $data['details'][$key]['top_depart_id'] = $data['top_depart_id'];
  176. }
  177. list($status, $msg) = $this->checkArrayRepeat($data['details'],'employee_id','人员');
  178. if(! $status) return [false, $msg];
  179. if($is_add){
  180. $bool = MonthlyPwOrder::where('code',$data['code'])
  181. ->where('top_depart_id', $data['top_depart_id'])
  182. ->where('del_time',0)
  183. ->exists();
  184. }else{
  185. if(empty($data['id'])) return [false,'ID不能为空'];
  186. $bool = Item::where('code',$data['code'])
  187. ->where('top_depart_id', $data['top_depart_id'])
  188. ->where('id','<>',$data['id'])
  189. ->where('del_time',0)
  190. ->exists();
  191. }
  192. if($bool) return [false, date("Y-m", $data['month']) . '已存在人员月度研发工时单'];
  193. return [true, ''];
  194. }
  195. public function fillData($data, $is_export = false){
  196. if(empty($data['data'])) return $data;
  197. $emp = (new EmployeeService())->getEmployeeMap(array_unique(array_column($data['data'],'crt_id')));
  198. $map = [];
  199. if($is_export) $map = $this->getDetailsMap(array_column($data['data'],'id'));
  200. foreach ($data['data'] as $key => $value){
  201. $data['data'][$key]['crt_time'] = $value['crt_time'] ? date('Y-m-d H:i:s',$value['crt_time']) : '';
  202. $data['data'][$key]['month'] = $value['month'] ? date('Y-m',$value['month']) : '';
  203. $data['data'][$key]['crt_name'] = $emp[$value['crt_id']] ?? '';
  204. $tmp = $map[$value['id']] ?? [];
  205. $data['data'][$key]['details'] = $tmp['man'] ?? "";
  206. }
  207. return $data;
  208. }
  209. public function getDetailsMap($item_id){
  210. // 1. 获取明细数据,注意一定要选出 item_id
  211. $data = ItemDetails::where('del_time', 0)
  212. ->whereIn('item_id', $item_id)
  213. ->select('item_id', 'data_id', 'type')
  214. ->get();
  215. // 2. 收集 ID 并按类型分类(去重以减少查询压力)
  216. $manIds = $data->where('type', ItemDetails::type_one)->pluck('data_id')->unique()->toArray();
  217. $devIds = $data->where('type', ItemDetails::type_two)->pluck('data_id')->unique()->toArray();
  218. // 3. 一次性查出对应的映射 (假设人员表字段是 number, 设备表是 code)
  219. // 注意:pluck('显示内容', 'ID')
  220. $manMap = Employee::whereIn('id', $manIds)->pluck('number', 'id')->toArray();
  221. $devMap = Device::whereIn('id', $devIds)->pluck('code', 'id')->toArray();
  222. // 4. 按 item_id 分组处理成字符串
  223. $result = [];
  224. foreach ($item_id as $id) {
  225. // 初始化每个 item_id 的默认结构
  226. $result[$id] = [
  227. 'man' => '',
  228. 'device' => ''
  229. ];
  230. }
  231. // 5. 遍历明细填充数据
  232. $grouped = $data->groupBy('item_id');
  233. foreach ($grouped as $itemId => $items) {
  234. $mans = [];
  235. $devices = [];
  236. foreach ($items as $item) {
  237. if ($item->type == ItemDetails::type_one) {
  238. if (isset($manMap[$item->data_id])) {
  239. $mans[] = $manMap[$item->data_id];
  240. }
  241. } else {
  242. if (isset($devMap[$item->data_id])) {
  243. $devices[] = $devMap[$item->data_id];
  244. }
  245. }
  246. }
  247. $result[$itemId] = [
  248. 'man' => implode(',', $mans),
  249. 'device' => implode(',', $devices)
  250. ];
  251. }
  252. return $result;
  253. }
  254. }