| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326 |
- <?php
- namespace App\Service;
- use App\Model\Employee;
- use App\Model\Organization;
- use App\Model\OrganizationDetails;
- use Illuminate\Support\Facades\DB;
- class OrganizationService extends Service
- {
- public function organizationEdit($data,$user){
- list($status,$msg) = $this->organizationRule($data, $user, false);
- if(!$status) return [$status,$msg];
- try {
- DB::beginTransaction();
- $model = Organization::where('id',$data['id'])->first();
- $model->title = $data['title'] ?? '';
- $model->industry_name = $data['industry_name'] ?? "";
- $model->industry_ranking = $data['industry_ranking'] ?? "";
- $model->address = $data['address'] ?? "";
- $model->founding_time = $data['founding_time'] ?? 0;
- $model->type = $data['type'] ?? 0;
- $model->decision_depart = $data['decision_depart'] ?? "";
- $model->decision_maker = $data['decision_maker'] ?? "";
- $model->new_product_rule = $data['new_product_rule'] ?? "";
- $model->old_product_rule = $data['old_product_rule'] ?? "";
- $model->save();
- $time = time();
- OrganizationDetails::where('del_time',0)
- ->where('organization_id', $model->id)
- ->update(['del_time' => $time]);
- $this->saveDetail($model->id, $time, $data);
- DB::commit();
- }catch (\Exception $exception){
- DB::rollBack();
- return [false,$exception->getMessage()];
- }
- return [true, ''];
- }
- public function organizationAdd($data,$user){
- list($status,$msg) = $this->organizationRule($data, $user);
- if(!$status) return [$status,$msg];
- try {
- DB::beginTransaction();
- $model = new Organization();
- $model->code = $this->generateCode($user['id']);
- $model->title = $data['title'] ?? '';
- $model->industry_name = $data['industry_name'] ?? "";
- $model->industry_ranking = $data['industry_ranking'] ?? "";
- $model->address = $data['address'] ?? "";
- $model->founding_time = $data['founding_time'] ?? 0;
- $model->type = $data['type'] ?? 0;
- $model->decision_depart = $data['decision_depart'] ?? "";
- $model->decision_maker = $data['decision_maker'] ?? "";
- $model->new_product_rule = $data['new_product_rule'] ?? "";
- $model->old_product_rule = $data['old_product_rule'] ?? "";
- $model->crt_id = $user['id'];
- $model->save();
- $this->saveDetail($model->id, time(), $data);
- DB::commit();
- }catch (\Exception $exception){
- DB::rollBack();
- if (str_contains($exception->getMessage(), '1062') || str_contains($exception->getMessage(), 'Duplicate entry')) {
- return [false, '网络波动,请重新操作!'];
- }
- return [false,$exception->getMessage()];
- }
- return [true, ''];
- }
- private function saveDetail($id, $time, $data){
- if(! empty($data['unit_list'])){
- $unit = [];
- foreach ($data['unit_list'] as $value){
- $unit[] = [
- 'organization_id' => $id,
- 'type' => $value['type'],
- 'unit' => $value['unit'],
- 'crt_time' => $time,
- ];
- }
- if(! empty($unit)) OrganizationDetails::insert($unit);
- }
- if(! empty($data['receipt_list'])){
- $receipt = [];
- foreach ($data['receipt_list'] as $value){
- if(empty($value['receipt'])) continue;
- $receipt[] = [
- 'organization_id' => $id,
- 'type' => $value['type'],
- 'year' => $value['year'],
- 'receipt' => $value['receipt'],
- 'crt_time' => $time,
- ];
- }
- if(! empty($receipt)) OrganizationDetails::insert($receipt);
- }
- if(! empty($data['requirement_list'])){
- $requirement = [];
- foreach ($data['requirement_list'] as $value){
- $requirement[] = [
- 'organization_id' => $id,
- 'type' => $value['type'],
- 'requirement' => $value['requirement'],
- 'month_usage' => $value['month_usage'],
- 'reference_value' => $value['reference_value'],
- 'crt_time' => $time,
- ];
- }
- if(! empty($requirement)) OrganizationDetails::insert($requirement);
- }
- }
- private function getDetail($id){
- $data = OrganizationDetails::where('del_time',0)
- ->where('organization_id', $id)
- ->get()->toArray();
- $unit = $receipt = $requirement = [];
- foreach ($data as $value){
- if(in_array($value['type'], [OrganizationDetails::type_one, OrganizationDetails::type_two])) {
- $unit[] = [
- 'type' => $value['type'],
- 'unit' => $value['unit'],
- ];
- }elseif(in_array($value['type'], [OrganizationDetails::type_three])){
- $receipt[] = [
- 'type' => $value['type'],
- 'year' => $value['year'],
- 'receipt' => $value['receipt'],
- ];
- }else{
- $requirement[] = [
- 'type' => $value['type'],
- 'requirement' => $value['requirement'],
- 'month_usage' => $value['month_usage'],
- 'reference_value' => $value['reference_value'],
- ];
- }
- }
- $detail = [
- 'unit_list' => $unit,
- 'receipt_list' => $receipt,
- 'requirement_list' => $requirement,
- ];
- foreach ($detail as $key => $value) {
- if (empty($value)) {
- $detail[$key] = (object)[]; // 转成 stdClass 对象
- }
- }
- return $detail;
- }
- public function organizationDel($data){
- if($this->isEmpty($data,'id')) return [false,'请选择数据!'];
- try {
- DB::beginTransaction();
- $time = time();
- Organization::where('del_time',0)
- ->whereIn('id',$data['id'])
- ->update(['del_time' => $time]);
- OrganizationDetails::where('del_time',0)
- ->where('organization_id', $data['id'])
- ->update(['del_time' => $time]);
- DB::commit();
- }catch (\Exception $exception){
- DB::rollBack();
- return [false,$exception->getMessage()];
- }
- return [true, ''];
- }
- public function organizationDetail($data, $user){
- if($this->isEmpty($data,'id')) return [false,'请选择数据!'];
- $customer = Organization::where('del_time',0)
- ->where('id',$data['id'])
- ->first();
- if(empty($customer)) return [false,'组织不存在或已被删除'];
- $customer = $customer->toArray();
- $customer['founding_time'] = ! empty($customer['founding_time']) ? date("Y-m-d", $customer['founding_time']) : "";
- $customer['crt_name'] = Employee::where('id',$customer['crt_id'])->value('emp_name');
- $customer['crt_time'] = $customer['crt_time'] ? date("Y-m-d H:i:s",$customer['crt_time']): '';
- $details = $this->getDetail($data['id']);
- $customer = array_merge($customer, $details);
- return [true, $customer];
- }
- public function organizationCommon($data,$user, $field = []){
- if(empty($field)) $field = Organization::$field;
- $model = Organization::Clear($user,$data);
- $model = $model->where('del_time',0)
- ->select($field)
- ->orderby('id', 'desc');
- if(! empty($data['title'])) $model->where('title', 'LIKE', '%'.$data['title'].'%');
- if(! empty($data['code'])) $model->where('code', 'LIKE', '%'.$data['code'].'%');
- if(! empty($data['type'])) $model->where('type', $data['type']);
- if(! empty($data['id'])) $model->whereIn('id', $data['id']);
- if(! empty($data['crt_time'][0]) && ! empty($data['crt_time'][1])) {
- $return = $this->changeDateToTimeStampAboutRange($data['crt_time']);
- $model->where('crt_time','>=',$return[0]);
- $model->where('crt_time','<=',$return[1]);
- }
- return $model;
- }
- public function organizationList($data,$user){
- $model = $this->organizationCommon($data, $user);
- $list = $this->limit($model,'',$data);
- $list = $this->fillData($list,$user,$data);
- return [true, $list];
- }
- public function organizationRule(&$data, $user, $is_add = true){
- if(empty($data['title'])) return [false, '名称不能为空'];
- if(empty($data['type']) || ! isset(Organization::$type_name[$data['type']])) return [false, '组织类型错误'];
- if(! empty($data['founding_time'])) $data['founding_time'] = $this->changeDateToDate($data['founding_time']);
- if(! empty($data['unit_list'])){
- foreach ($data['unit_list'] as $value){
- if(empty($value['type'])) return [false, '上下级单位类型不能为空'];
- if(empty($value['unit'])) return [false, '上下级单位名称不能为空'];
- }
- }
- if(! empty($data['receipt_list'])){
- foreach ($data['receipt_list'] as $value){
- if(empty($value['type'])) return [false, '营收信息类型不能为空'];
- if(empty($value['year'])) return [false, '营收年份不能为空'];
- // $start_time = strtotime($value['start_time'] . "-01");
- // $end_time = strtotime($value['end_time'] . "-01");
- // if(! $start_time) return [false, '营收日期范围错误'];
- if(empty($value['receipt'])) return [false, '营收数据不能为空'];
- }
- }
- if(! empty($data['requirement_list'])){
- foreach ($data['requirement_list'] as $value){
- if(empty($value['type'])) return [false, '需求信息类型不能为空'];
- if(empty($value['requirement'])) return [false, '需求信息名不能为空'];
- if(empty($value['month_usage'])) return [false, '月用量不能为空'];
- if(empty($value['reference_value'])) return [false, '参考价不能为空'];
- }
- }
- if($is_add){
- }else{
- if(empty($data['id'])) return [false,'ID不能为空'];
- $bool = Organization::where('id', $data['id'])
- ->where('del_time',0)
- ->exists();
- if(! $bool) return [false, '组织不存在或已被删除'];
- }
- return [true, $data];
- }
- public function fillData($data, $user, $search){
- 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]['founding_time'] = $value['founding_time'] ? date('Y-m-d',$value['founding_time']) : '';
- $data['data'][$key]['crt_name'] = $emp[$value['crt_id']] ?? '';
- $data['data'][$key]['type_title'] = Organization::$type_name[$value['type']] ?? '';
- }
- return $data;
- }
- public function generateCode($crt_id)
- {
- return DB::transaction(function () use ($crt_id) {
- // 加行级锁 FOR UPDATE,避免并发重复
- $maxCode = Organization::where('crt_id', $crt_id)
- ->lockForUpdate()
- ->max('code');
- // 转数字
- $num = intval($maxCode);
- // +1
- $num++;
- // 小于 10000 → 保留 4 位前导零
- if ($num < 10000) {
- $code = str_pad($num, 4, '0', STR_PAD_LEFT);
- } else {
- $code = (string)$num; // 大于等于10000则直接用数字
- }
- return $code;
- });
- }
- }
|