| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394 |
- <?php
- namespace App\Service;
- use App\Model\Archive;
- use Illuminate\Support\Facades\DB;
- class ArchiveService extends Service
- {
- public function archiveAdd($data,$user){
- list($status,$msg) = $this->archiveRule($data,$user);
- if(!$status) return [$status,$msg];
- try {
- DB::beginTransaction();
- $model = new Archive();
- $model->month = $data['month'];
- $model->crt_id = $user['id'];
- $model->top_depart_id = $data['top_depart_id'];
- $model->save();
- DB::commit();
- }catch (\Exception $exception){
- DB::rollBack();
- return [false,$exception->getMessage()];
- }
- return [true,''];
- }
- public function archiveDel($data, $user){
- if(empty($data['id']) || ! is_array($data['id'])) return [false,'ID不能为空或格式错误'];
- Archive::whereIn('id', $data['id'])->update([
- 'del_time'=>time()
- ]);
- return [true,''];
- }
- public function archiveCommon($data,$user, $field = []){
- if(empty($field)) $field = Archive::$field;
- $model = Archive::TopClear($user,$data);
- $model = $model->where('del_time',0)
- ->select($field)
- ->orderby('id', 'desc');
- if(! empty($data['time'][0]) && ! empty($data['time'][1])) {
- $return = $this->changeDateToTimeStampAboutRange($data['time']);
- $model->where('month','>=',$return[0]);
- $model->where('month','<=',$return[1]);
- }
- return $model;
- }
- public function archiveList($data, $user){
- $model = $this->archiveCommon($data, $user);
- $list = $this->limit($model,'',$data);
- $list = $this->fillArchiveList($list);
- return [true, $list];
- }
- public function fillArchiveList($data){
- if(empty($data['data'])) return $data;
- $emp = (new EmployeeService())->getEmployeeMap(array_unique(array_column($data['data'],'crt_id')));
- foreach ($data['data'] as $key => $value){
- $data['data'][$key]['crt_time'] = $value['crt_time'] ? date('Y-m-d H:i:s',$value['crt_time']) : '';
- $data['data'][$key]['month'] = $value['month'] ? date('Y-m',$value['month']) : '';
- $data['data'][$key]['crt_name'] = $emp[$value['crt_id']] ?? '';
- }
- return $data;
- }
- public function archiveRule(&$data,$user, $add = true){
- if(empty($data['month'])) return [false, '年月不能为空'];
- $data['month'] = $this->changeDateToDate($data['month']);
- $data['top_depart_id'] = $user['top_depart_id'];
- $bool = Archive::where('del_time',0)
- ->where('top_depart_id', $data['top_depart_id'])
- ->where('month', $data['month'])
- ->exists();
- if($bool) return [false, "年月" . date("Y-m") . "已归档"];
- return [true, ''];
- }
- }
|