| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267 | <?phpnamespace App\Service;use App\Model\BasicType;use App\Model\Depart;use Illuminate\Support\Facades\DB;/** * 基础分类设置相关 */class BasicTypeService extends Service{    /**     * 基础类型编辑     * @param $data     * @return array     */    public function basicTypeEdit($data, $user){        list($status,$msg) = $this->basicTypeRule($data,$user,false);        if(!$status) return [$status,$msg];        $update = $msg['data'][0];        BasicType::where('id',$data['id'])->update($update);        return [true,''];    }    /**     * 基础类型新增     * @param $data     * @return array     */    public function basicTypeAdd($data,$user){        list($status,$msg) = $this->basicTypeRule($data, $user);        if(!$status) return [$status,$msg];        BasicType::insert($msg['data']);        return [true,''];    }    /**     * 基础类型删除     * @param $data     * @return array     */    public function basicTypeDel($data){        if($this->isEmpty($data,'id')) return [false,'请选择数据!'];        BasicType::whereIn('id',$data['id'])->update([            'del_time'=>time()        ]);        return [true,''];    }    /**     * 基础类型列表     * @param $data     * @return array     */    public function basicTypeList($data, $user){        $model = BasicType::TopClear($user,$data);        $model = $model->where('del_time',0)            ->select('title','id','type','code','top_depart_id')            ->orderby('id', 'asc');        if(! empty($data['type'])) $model->where('type',$data['type']);        if(! empty($data['title'])) $model->where('title', 'LIKE', '%'.$data['title'].'%');        $list = $this->limit($model,'',$data);        $list = $this->fillData($list);        return [true, $list];    }    public function basicTypeCustomerList($data, $user){        $data['top_depart_id'] = $user['head']['id'];        $model = BasicType::TopClear($user,$data);        $model = $model->where('del_time',0)            ->select('title','id','type','code','top_depart_id')            ->orderby('id', 'asc');        if(! empty($data['type'])) $model->where('type',$data['type']);        if(! empty($data['title'])) $model->where('title', 'LIKE', '%'.$data['title'].'%');        $list = $this->limit($model,'',$data);        $list = $this->fillData($list);        return [true, $list];    }    /**     * 基础类型参数规则     * @param $data     * @param $is_check     * @return array     */    public function basicTypeRule($data,$user, $is_check = true){        if($this->isEmpty($data,'data')) return [false,'数据不能为空!'];        //所属部门 以及  顶级部门        if(empty($data['depart_id'])) $data['depart_id'] = $this->getDepart($user);        $data['top_depart_id'] = $user['depart_map'][$data['depart_id']] ?? 0;        $type = array_column($data['data'],'type');        $type = array_map(function($val) {            return $val !== null ? $val : 0;        }, $type);        foreach ($type as $value){            if(empty($value)) return [false,'类型不能为空!'];            if(! isset(BasicType::$type[$value])) return [false,'类型不存在!'];        }        $map = [];        foreach ($data['data'] as $value){            if(! isset($map[$value['title']])){                $map[$value['title']][] = $value['type'];            }else{                if(! in_array($value['type'],$map[$value['title']])){                    $map[$value['title']][] = $value['type'];                }            }        }        $title = array_column($data['data'],'title');        $title = array_map(function($val) {            return $val !== null ? $val : 0;        }, $title);        $title_count = array_count_values($title);        foreach ($title as $value){            if(empty($value)) return [false,'名称不能为空!'];            if($title_count[$value] > 1 && count($map[$value]) != $title_count[$value]) return [false,'同一归属类别下,名称不能重复'];        }        foreach ($data['data'] as $key => $value){            $data['data'][$key]['type'] = $value['type'];            $data['data'][$key]['upd_time'] = time();            if($is_check){                $bool = BasicType::where('title',$value['title'])                    ->where('top_depart_id',$data['top_depart_id'])                    ->where('type',$value['type'])                    ->where('del_time',0)                    ->exists();                $data['data'][$key]['crt_time'] = time();                $data['data'][$key]['crt_id'] = $user['id'];                $data['data'][$key]['depart_id'] = $data['depart_id'];                $data['data'][$key]['top_depart_id'] = $data['top_depart_id'];            }else{                if($this->isEmpty($data,'id')) return [false,'id不能为空!'];                $top_depart_id = BasicType::where('id',$data['id'])->value('top_depart_id');                $bool = BasicType::where('title',$value['title'])                    ->where('top_depart_id',$top_depart_id)                    ->where('type',$value['type'])                    ->where('id','<>',$data['id'])                    ->where('del_time',0)                    ->exists();            }            if($bool) return [false,'名称不能重复'];        }        return [true, $data];    }    /**     * 拼接数据     * @param $data     * @return array     */    public function fillData($data){        if(empty($data['data'])) return $data;        foreach ($data['data'] as $key => $value){            $data['data'][$key]['type_name'] = BasicType::$type[$value['type']] ?? '';        }        return $data;    }    public function basicTypeSearch($data){        $basic = BasicType::where('title', 'LIKE', '%'.$data.'%')            ->select('id')            ->get()->toArray();        return array_column($basic,'id');    }    public function basicTypeSearchId($data){        $title = "";        $basic = BasicType::where('id', $data)            ->select('title')            ->first();        if(! empty($basic)) $title = $basic->title;        $basic = BasicType::where('title', $title)            ->select('id')            ->get()->toArray();        return array_column($basic,'id');    }    //获取当前    public function getMyBasicList($user, $type){        if(! is_array($type)) $type = [$type];        $depart_id = $this->getDepart($user);        $top_depart_id = $user['depart_map'][$depart_id] ?? 0;        return BasicType::where('del_time',0)            ->whereIn('type',$type)            ->where('top_depart_id',$top_depart_id)            ->get()->toArray();    }    public function maked(){        $list = BasicType::where('del_time',0)            ->select('title','type','top_depart_id')            ->get()->toArray();        $basic_type2 = [];        $basic_other_type = [];        foreach ($list as $value){            if($value['top_depart_id'] == 2){                if(! in_array($value['type'], [28,23,24,22,8])){                    $basic_type2[] = $value['title'] . '|' . $value['type'];                }            }else{                $basic_other_type[$value['top_depart_id']][] = $value['title'] . '|' . $value['type'];            }        }dd($basic_type2);        $depart = Depart::where('del_time',0)            ->where('id','<>',2)            ->where('parent_id',0)//            ->whereIn('id',[139,49])            ->select('id')            ->orderBy('id','asc')            ->get()->toArray();        $depart = array_column($depart,'id');        $insert = [];        $time = time();        foreach ($depart as $value){            if(! empty($basic_other_type[$value])){                foreach ($basic_type2 as $val){                    if(! in_array($val,$basic_other_type[$value])) {                        $tmp = explode('|',$val);                        $insert[] = [                            'title' => $tmp[0],                            'type' => $tmp[1],                            'top_depart_id' => $value,                            'crt_time' => $time,                            'crt_id' => 1                        ];                    }                }            }else{                foreach ($basic_type2 as $val){                    $tmp = explode('|',$val);                    $insert[] = [                        'title' => $tmp[0],                        'type' => $tmp[1],                        'top_depart_id' => $value,                        'crt_time' => $time,                        'crt_id' => 1                    ];                }            }        }        if(! empty($insert)) BasicType::insert($insert);        dd('ok');    }}
 |