DeviceService.php 9.1 KB

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