DeviceService.php 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. <?php
  2. namespace App\Service;
  3. use App\Model\CalendarDetails;
  4. use App\Model\Employee;
  5. use App\Model\Device;
  6. use Illuminate\Support\Facades\DB;
  7. class DeviceService extends Service
  8. {
  9. public function deviceEdit($data,$user){
  10. list($status,$msg) = $this->deviceRule($data, $user, false);
  11. if(!$status) return [$status,$msg];
  12. try {
  13. DB::beginTransaction();
  14. $model = Device::where('id',$data['id'])->first();
  15. $model->code = $data['code'] ?? '';
  16. $model->title = $data['title'] ?? '';
  17. $model->size = $data['size'] ?? '';
  18. $model->mark = $data['mark'] ?? '';
  19. $model->is_use = $data['is_use'] ?? 0;
  20. $model->type = $data['type'] ?? 0;
  21. $model->in_time = $data['in_time'] ?? 0;
  22. $model->power = $data['power'] ?? "";
  23. $model->position = $data['position'] ?? "";
  24. $model->original_value = $data['original_value'] ?? 0;
  25. $model->initial_value = $data['initial_value'] ?? 0;
  26. $model->save();
  27. DB::commit();
  28. }catch (\Exception $exception){
  29. DB::rollBack();
  30. return [false,$exception->getMessage()];
  31. }
  32. return [true, ''];
  33. }
  34. public function deviceAdd($data,$user){
  35. list($status,$msg) = $this->deviceRule($data, $user);
  36. if(!$status) return [$status,$msg];
  37. try {
  38. DB::beginTransaction();
  39. $model = new Device();
  40. $model->code = $data['code'] ?? '';
  41. $model->title = $data['title'] ?? '';
  42. $model->size = $data['size'] ?? '';
  43. $model->mark = $data['mark'] ?? '';
  44. $model->is_use = $data['is_use'] ?? 0;
  45. $model->type = $data['type'] ?? 0;
  46. $model->in_time = $data['in_time'] ?? 0;
  47. $model->power = $data['power'] ?? "";
  48. $model->position = $data['position'] ?? "";
  49. $model->original_value = $data['original_value'] ?? 0;
  50. $model->initial_value = $data['initial_value'] ?? 0;
  51. $model->top_depart_id = $data['top_depart_id'] ?? 0;
  52. $model->crt_id = $user['id'];
  53. $model->save();
  54. DB::commit();
  55. }catch (\Exception $exception){
  56. DB::rollBack();
  57. return [false,$exception->getMessage()];
  58. }
  59. return [true, ''];
  60. }
  61. public function deviceDel($data){
  62. if($this->isEmpty($data,'id')) return [false,'请选择数据'];
  63. try {
  64. DB::beginTransaction();
  65. $time = time();
  66. Device::where('del_time',0)
  67. ->whereIn('id', $data['id'])
  68. ->update(['del_time' => $time]);
  69. DB::commit();
  70. }catch (\Exception $exception){
  71. DB::rollBack();
  72. return [false,$exception->getMessage()];
  73. }
  74. return [true, ''];
  75. }
  76. public function deviceDetail($data, $user){
  77. if($this->isEmpty($data,'id')) return [false,'请选择数据'];
  78. $customer = Device::where('del_time',0)
  79. ->where('id',$data['id'])
  80. ->first();
  81. if(empty($customer)) return [false,'设备不存在或已被删除'];
  82. $customer = $customer->toArray();
  83. $customer['crt_name'] = Employee::where('id',$customer['crt_id'])->value('title');
  84. $customer['crt_time'] = $customer['crt_time'] ? date("Y-m-d H:i:s",$customer['crt_time']): '';
  85. $customer['in_time'] = $customer['in_time'] ? date("Y-m-d",$customer['in_time']): '';
  86. return [true, $customer];
  87. }
  88. public function deviceCommon($data,$user, $field = []){
  89. if(empty($field)) $field = Device::$field;
  90. $model = Device::TopClear($user,$data);
  91. $model = $model->where('del_time',0)
  92. ->select($field)
  93. ->orderby('id', 'desc');
  94. if(! empty($data['title'])) $model->where('title', 'LIKE', '%'.$data['title'].'%');
  95. if(! empty($data['code'])) $model->where('code', 'LIKE', '%'.$data['code'].'%');
  96. if(! empty($data['id'])) $model->whereIn('id', $data['id']);
  97. if(! empty($data['is_use'])) $model->where('is_use', $data['is_use']);
  98. if(! empty($data['type'])) $model->where('type', $data['type']);
  99. if(! empty($data['crt_id'])) $model->whereIn('crt_id', $data['crt_id']);
  100. if(! empty($data['crt_time'][0]) && ! empty($data['crt_time'][1])) {
  101. $return = $this->changeDateToTimeStampAboutRange($data['crt_time']);
  102. $model->where('crt_time','>=',$return[0]);
  103. $model->where('crt_time','<=',$return[1]);
  104. }
  105. return $model;
  106. }
  107. public function deviceList($data,$user){
  108. $model = $this->deviceCommon($data, $user);
  109. $list = $this->limit($model,'',$data);
  110. $list = $this->fillData($list, $data, $user);
  111. return [true, $list];
  112. }
  113. public function deviceRule(&$data, $user, $is_add = true){
  114. if(empty($data['title'])) return [false, '设备名称不能为空'];
  115. if(empty($data['code'])) return [false, '资产编码不能为空'];
  116. if(empty($data['is_use'])) return [false, '是否启用不能为空'];
  117. if(! isset(Device::Use[$data['is_use']])) return [false, '是否启用错误'];
  118. if(empty($data['type'])) return [false, '固定资产类型不能为空'];
  119. if(! isset(Device::$type[$data['type']])) return [false, '固定资产类型错误'];
  120. if(! empty($data['in_time'])) $data['in_time'] = $this->changeDateToDate($data['in_time']);
  121. if(! empty($data['original_value'])){
  122. $res = $this->checkNumber($data['original_value'],0,'positive');
  123. if(! $res['valid']) return [false,'原始价值:' . $res['error']];
  124. }
  125. $res = $this->checkNumber($data['initial_value'],0,'positive');
  126. if(! $res['valid']) return [false,'期初原值:' . $res['error']];
  127. $data['top_depart_id'] = $user['top_depart_id'];
  128. if($is_add){
  129. $bool = Device::where('code',$data['code'])
  130. ->where('top_depart_id', $data['top_depart_id'])
  131. ->where('del_time',0)
  132. ->exists();
  133. }else{
  134. if(empty($data['id'])) return [false,'ID不能为空'];
  135. $bool = Device::where('code',$data['code'])
  136. ->where('top_depart_id', $data['top_depart_id'])
  137. ->where('id','<>',$data['id'])
  138. ->where('del_time',0)
  139. ->exists();
  140. }
  141. if($bool) return [false, '资产编码已存在'];
  142. return [true, ''];
  143. }
  144. public function fillData($data, $ergs, $user){
  145. if(empty($data['data'])) return $data;
  146. $emp = (new EmployeeService())->getEmployeeMap(array_unique(array_column($data['data'],'crt_id')));
  147. $map = [];
  148. if(isset($ergs['search_for_month_work'])) {
  149. $device_ids = array_column($data['data'], 'id');
  150. list($status, $map) = $this->getDevicesMonthStats($device_ids, $ergs['search_for_month_work'], $user);
  151. }
  152. foreach ($data['data'] as $key => $value){
  153. $data['data'][$key]['crt_time'] = $value['crt_time'] ? date('Y-m-d H:i:s',$value['crt_time']) : '';
  154. $data['data'][$key]['in_time'] = $value['in_time'] ? date('Y-m-d',$value['in_time']) : '';
  155. $data['data'][$key]['crt_name'] = $emp[$value['crt_id']] ?? '';
  156. $data['data'][$key]['is_use_title'] = Device::Use[$value['is_use']] ?? '';
  157. $data['data'][$key]['type_title'] = Device::$type[$value['type']] ?? '';
  158. if(isset($ergs['search_for_month_work'])) $data['data'][$key]['month_dw'] = $map[$value['id']] ?? [];
  159. }
  160. return $data;
  161. }
  162. public function getDevicesMonthStats($device_ids, $month, $user)
  163. {
  164. $topDepartId = $user['top_depart_id'];
  165. if(is_numeric($month)){
  166. $monthStart = $month;
  167. }else{
  168. $monthStart = $this->changeDateToDate($month);
  169. $monthStr = date("Y-m", $monthStart);
  170. }
  171. $endTime = strtotime("+1 month", $monthStart) - 1;
  172. // 1. 获取当月标准工作日天数
  173. $standardWorkDays = DB::table('calendar_details')
  174. ->where('top_depart_id', $topDepartId)
  175. ->where('del_time', 0)
  176. ->where('time', '>=', $monthStart)
  177. ->where('time', '<=', $endTime)
  178. ->where('is_work', CalendarDetails::TYPE_ONE)
  179. ->count();
  180. if ($standardWorkDays <= 0) return [false, '工作日信息未设置'];
  181. // 2. 获取公司通用工时设置 (设备统一使用这个)
  182. $commonWorkMin = DB::table('work_range_details')
  183. ->where('top_depart_id', $topDepartId)
  184. ->where('del_time', 0)
  185. ->sum('total_work_min');
  186. if ($commonWorkMin <= 0) return [false, '公司工作时段未设置'];
  187. // 3. 计算结果
  188. $finalWorkMin = $standardWorkDays * $commonWorkMin;
  189. $result = [];
  190. foreach ($device_ids as $deviceId) {
  191. $result[$deviceId] = [
  192. 'attendance_days' => (float)$standardWorkDays, // 设备没有请假加班,出勤天数即标准天数
  193. 'final_work_hour' => round($finalWorkMin / 60, 2), // 标准总工时转小时
  194. ];
  195. }
  196. return [true, $result];
  197. }
  198. }