123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231 |
- <?php
- namespace App\Service;
- use App\Model\BasicType;
- use App\Model\Employee;
- use App\Model\Product;
- use App\Model\ProductActivity;
- use App\Model\ProductActivityPrice;
- use Illuminate\Support\Facades\DB;
- class ProductActivityService extends Service
- {
- public function productEdit($data,$user){
- list($status,$msg) = $this->productRule($data, $user, false);
- if(!$status) return [$status,$msg];
- try {
- DB::beginTransaction();
- $model = ProductActivity::where('id',$data['id'])->first();
- $model->title = $data['title'];
- $model->start_time = $data['start_time'] ?? 0;
- $model->end_time = $data['end_time'] ?? 0;
- $model->mark = $data['mark'] ?? '';
- $model->save();
- $time = time();
- ProductActivityPrice::where('product_activity_id',$data['id'])
- ->where('del_time',0)
- ->update(['del_time' => $time]);
- if(! empty($data['product'])){
- $insert = [];
- foreach ($data['product'] as $value){
- $insert[] = [
- 'product_activity_id' => $model->id,
- 'product_id' => $model->id,
- 'basic_type_id' => $value['basic_type_id'],
- 'price' => $value['price'],
- 'crt_time' => $time,
- 'start_time' => $data['start_time'] ?? 0,
- 'end_time' => $data['end_time'] ?? 0,
- ];
- }
- ProductActivityPrice::insert($insert);
- }
- DB::commit();
- }catch (\Exception $exception){
- DB::rollBack();
- return [false,$exception->getMessage()];
- }
- return [true,''];
- }
- public function productAdd($data,$user){
- list($status,$msg) = $this->productRule($data, $user);
- if(!$status) return [$status,$msg];
- try {
- DB::beginTransaction();
- $model = new ProductActivity();
- $model->product_category_id = $data['product_category_id'] ?? 0;
- $model->title = $data['title'];
- $model->start_time = $data['start_time'] ?? 0;
- $model->end_time = $data['end_time'] ?? 0;
- $model->mark = $data['mark'] ?? '';
- $model->crt_id = $user['id'];
- $model->depart_id = $data['depart_id'] ?? 0;
- $model->top_depart_id = $data['top_depart_id'] ?? 0;
- $model->save();
- $time = time();
- if(! empty($data['product'])){
- $insert = [];
- foreach ($data['product'] as $value){
- $insert[] = [
- 'product_activity_id' => $model->id,
- 'product_id' => $model->id,
- 'basic_type_id' => $value['basic_type_id'],
- 'price' => $value['price'],
- 'crt_time' => $time,
- 'start_time' => $data['start_time'] ?? 0,
- 'end_time' => $data['end_time'] ?? 0,
- ];
- }
- ProductActivityPrice::insert($insert);
- }
- DB::commit();
- }catch (\Exception $exception){
- DB::rollBack();
- return [false,$exception->getMessage()];
- }
- return [true,''];
- }
- public function productDel($data){
- if($this->isEmpty($data,'id')) return [false,'请选择数据!'];
- try {
- DB::beginTransaction();
- $time = time();
- ProductActivity::where('del_time',0)->where('id',$data['id'])->update(['del_time' => $time]);
- ProductActivityPrice::where('product_activity_id',$data['id'])
- ->where('del_time',0)
- ->update(['del_time' => $time]);
- DB::commit();
- }catch (\Exception $exception){
- DB::rollBack();
- return [false,$exception->getMessage()];
- }
- return [true,''];
- }
- public function productDetail($data,$user){
- if($this->isEmpty($data,'id')) return [false,'请选择数据!'];
- $customer = ProductActivity::where('del_time',0)
- ->where('id',$data['id'])
- ->first();
- if(empty($customer)) return [false,'活动不存在或已被删除'];
- $customer = $customer->toArray();
- $detail = ProductActivityPrice::where('del_time',0)
- ->where('product_activity_id',$data['id'])
- ->get()->toArray();
- $title_map = BasicType::whereIn('id',array_column($detail,'basic_type_id'))
- ->pluck('title','id')
- ->toArray();
- $customer['product'] = [];
- foreach ($detail as $value){
- $customer['product'][] = [
- 'basic_type_id' => $value['basic_type_id'],
- 'basic_type_title' => $title_map[$value['basic_type_id']] ?? '',
- 'price' => $value['price'],
- ];
- }
- $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 productList($data,$user){
- $model = new ProductActivity(['userData' => $user, 'search' => $data]);
- $model = $model->where('del_time',0)
- ->orderby('id', 'desc');
- if(! empty($data['title'])) $model->where('title', 'LIKE', '%'.$data['title'].'%');
- $list = $this->limit($model,'',$data);
- $list = $this->fillData($list,$user);
- return [true, $list];
- }
- public function productRule(&$data, $user, $is_add = true){
- if(empty($data['title'])) return [false,'活动名称名称不能为空'];
- if(empty($data['activity_time'][0]) || empty($data['activity_time'][1])) return [false,'请填写活动时间范围'];
- $data['start_time'] = $this->changeDateToDateMin($data['activity_time'][0]);
- $data['end_time'] = $this->changeDateToDateMin($data['activity_time'][1]);
- if($is_add){
- $product = ProductActivityPrice::where('del_time',0)
- ->where('start', '<=', $data['end_time'])
- ->where('end', '>=', $data['start_time'])
- ->select('product_id')
- ->get()->toArray();
- $product = array_column($product,'product_id');
- }else{
- if(empty($data['id'])) return [false,'ID不能为空'];
- $product = ProductActivityPrice::where('del_time',0)
- ->where('id','<>',$data['id'])
- ->where('start', '<=', $data['end_time'])
- ->where('end', '>=', $data['start_time'])
- ->select('product_id')
- ->get()->toArray();
- $product = array_column($product,'product_id');
- }
- if(! empty($data['product'])){
- $start_time = date("Y-m-d H:i",$data['start_time']);
- $end_time = date("Y-m-d H:i",$data['end_time']);
- $map = BasicType::whereIn('id',array_column($data['product'],'basic_type_id'))
- ->pluck('title','id')
- ->toArray();
- $map2 = Product::whereIn('id',array_column($data['product'],'product_id'))
- ->pluck('title','id')
- ->toArray();
- foreach ($data['product'] as $value){
- $pro = $map2[$value['product_id']] ?? "";
- if(in_array($value['product_id'], $product)) return [false,'产品:' . $pro . '在' . $start_time . '——' . $end_time . '已设置活动价格'];
- if(! empty($value['price'])) {
- $tmp = $map[$value['basic_type_id']] ?? '';
- $res = $this->checkNumber($value['price']);
- if(! $res) return [false, $tmp . '请输入不超过两位小数并且大于0的数值'];
- }
- }
- }
- //所属部门 以及 顶级部门
- if(empty($data['depart_id'])) $data['depart_id'] = $this->getDepart($user);
- $data['top_depart_id'] = $user['depart_map'][$data['depart_id']] ?? 0;
- return [true, $data];
- }
- public function fillData($data, $user){
- if(empty($data['data'])) return $data;
- $emp = Employee::whereIn('id',array_unique(array_column($data['data'],'crt_id')))
- ->pluck('emp_name','id')
- ->toArray();
- 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']] ?? '';
- }
- return $data;
- }
- }
|