| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159 |
- <?php
- namespace App\Service;
- use App\Model\Dimension;
- use Illuminate\Support\Facades\DB;
- class DimensionService extends Service
- {
- public function dimensionEdit($data,$user){
- list($status,$msg) = $this->dimensionRule($data, $user, false);
- if(!$status) return [$status,$msg];
- try {
- DB::beginTransaction();
- $model = Dimension::where('id',$data['id'])->first();
- $model->title = $data['title'] ?? '';
- $model->code = $data['code'] ?? '';
- $model->type = $data['type'] ?? 0;
- $model->is_use = $data['is_use'] ?? 0;
- $model->sort = $data['sort'] ?? 0;
- $model->save();
- DB::commit();
- }catch (\Exception $exception){
- DB::rollBack();
- return [false,$exception->getMessage()];
- }
- return [true, ''];
- }
- public function dimensionAdd($data,$user){
- list($status,$msg) = $this->dimensionRule($data, $user);
- if(!$status) return [$status,$msg];
- try {
- DB::beginTransaction();
- $model = new Dimension();
- $model->title = $data['title'] ?? '';
- $model->code = $data['code'] ?? '';
- $model->type = $data['type'] ?? 0;
- $model->is_use = $data['is_use'] ?? 0;
- $model->sort = $data['sort'] ?? 0;
- $model->crt_id = $user['id'];
- $model->save();
- DB::commit();
- }catch (\Exception $exception){
- DB::rollBack();
- return [false,$exception->getMessage()];
- }
- return [true, ''];
- }
- public function dimensionDel($data){
- if($this->isEmpty($data,'id')) return [false,'请选择数据!'];
- try {
- DB::beginTransaction();
- $time = time();
- Dimension::where('del_time',0)
- ->whereIn('id',$data['id'])
- ->update(['del_time' => $time]);
- DB::commit();
- }catch (\Exception $exception){
- DB::rollBack();
- return [false,$exception->getMessage()];
- }
- return [true, ''];
- }
- public function dimensionDetail($data,$user){
- if($this->isEmpty($data,'id')) return [false,'请选择数据!'];
- $customer = Dimension::where('del_time',0)
- ->where('id',$data['id'])
- ->first();
- if(empty($customer)) return [false,'运费设置不存在或已被删除'];
- $customer = $customer->toArray();
- $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']): '';
- return [true, $customer];
- }
- public function dimensionCommon($data,$user, $field = []){
- if(empty($field)) $field = Dimension::$field;
- $model = Dimension::where('del_time',0)
- ->select($field)
- ->orderby('sort', 'desc');
- if(! empty($data['code'])) $model->where('code', 'LIKE', '%'.$data['code'].'%');
- if(! empty($data['title'])) $model->where('title', 'LIKE', '%'.$data['title'].'%');
- 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 dimensionList($data,$user){
- $model = $this->dimensionCommon($data, $user);
- $list = $this->limit($model,'',$data);
- $list = $this->fillData($list,$user);
- return [true, $list];
- }
- public function dimensionRule(&$data, $user, $is_add = true){
- if(empty($data['title'])) return [false, '维度选项不能为空'];
- if(empty($data['code'])) return [false, '维度编码不能为空'];
- if(empty($data['type'])) return [false, '类型不能为空'];
- if(! isset(Dimension::$type_name[$data['type']])) return [false, '类型不能不存在'];
- if(isset($data['sort'])){
- $res = $this->checkNumber($data['sort'],0);
- if(! $res['valid']) return [false,'排序:' . $res['error']];
- }
- if($is_add){
- $bool = Dimension::where('code',$data['code'])
- ->where('del_time',0)
- ->exists();
- }else{
- if(empty($data['id'])) return [false,'ID不能为空'];
- $bool = Dimension::where('code',$data['code'])
- ->where('id','<>',$data['id'])
- ->where('del_time',0)
- ->exists();
- }
- if(! $bool) return [false,'维度编码已存在'];
- return [true, $data];
- }
- public function fillData($data, $user){
- if(empty($data['data'])) return $data;
- $emp = (new EmployeeService())->getEmployeeMap(array_unique(array_column($data['data'],'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]['crt_name'] = $emp[$value['crt_id']] ?? '';
- $data['data'][$key]['is_use_title'] = Dimension::$is_use_name[$value['is_use']] ?? '';
- $data['data'][$key]['type_title'] = Dimension::$type_name[$value['type']] ?? '';
- }
- return $data;
- }
- }
|