DeviceService.php 11 KB

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