ProductActivityService.php 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. <?php
  2. namespace App\Service;
  3. use App\Model\BasicType;
  4. use App\Model\Employee;
  5. use App\Model\Product;
  6. use App\Model\ProductActivity;
  7. use App\Model\ProductActivityPrice;
  8. use Illuminate\Support\Facades\DB;
  9. class ProductActivityService extends Service
  10. {
  11. public function productEdit($data,$user){
  12. list($status,$msg) = $this->productRule($data, $user, false);
  13. if(!$status) return [$status,$msg];
  14. try {
  15. DB::beginTransaction();
  16. $model = ProductActivity::where('id',$data['id'])->first();
  17. $model->title = $data['title'];
  18. $model->start_time = $data['start_time'] ?? 0;
  19. $model->end_time = $data['end_time'] ?? 0;
  20. $model->mark = $data['mark'] ?? '';
  21. $model->save();
  22. $time = time();
  23. ProductActivityPrice::where('product_activity_id',$data['id'])
  24. ->where('del_time',0)
  25. ->update(['del_time' => $time]);
  26. if(! empty($data['product'])){
  27. $insert = [];
  28. foreach ($data['product'] as $value){
  29. $insert[] = [
  30. 'product_activity_id' => $model->id,
  31. 'product_id' => $model->id,
  32. 'basic_type_id' => $value['basic_type_id'],
  33. 'price' => $value['price'],
  34. 'crt_time' => $time,
  35. 'start_time' => $data['start_time'] ?? 0,
  36. 'end_time' => $data['end_time'] ?? 0,
  37. ];
  38. }
  39. ProductActivityPrice::insert($insert);
  40. }
  41. DB::commit();
  42. }catch (\Exception $exception){
  43. DB::rollBack();
  44. return [false,$exception->getMessage()];
  45. }
  46. return [true,''];
  47. }
  48. public function productAdd($data,$user){
  49. list($status,$msg) = $this->productRule($data, $user);
  50. if(!$status) return [$status,$msg];
  51. try {
  52. DB::beginTransaction();
  53. $model = new ProductActivity();
  54. $model->product_category_id = $data['product_category_id'] ?? 0;
  55. $model->title = $data['title'];
  56. $model->start_time = $data['start_time'] ?? 0;
  57. $model->end_time = $data['end_time'] ?? 0;
  58. $model->mark = $data['mark'] ?? '';
  59. $model->crt_id = $user['id'];
  60. $model->depart_id = $data['depart_id'] ?? 0;
  61. $model->top_depart_id = $data['top_depart_id'] ?? 0;
  62. $model->save();
  63. $time = time();
  64. if(! empty($data['product'])){
  65. $insert = [];
  66. foreach ($data['product'] as $value){
  67. $insert[] = [
  68. 'product_activity_id' => $model->id,
  69. 'product_id' => $model->id,
  70. 'basic_type_id' => $value['basic_type_id'],
  71. 'price' => $value['price'],
  72. 'crt_time' => $time,
  73. 'start_time' => $data['start_time'] ?? 0,
  74. 'end_time' => $data['end_time'] ?? 0,
  75. ];
  76. }
  77. ProductActivityPrice::insert($insert);
  78. }
  79. DB::commit();
  80. }catch (\Exception $exception){
  81. DB::rollBack();
  82. return [false,$exception->getMessage()];
  83. }
  84. return [true,''];
  85. }
  86. public function productDel($data){
  87. if($this->isEmpty($data,'id')) return [false,'请选择数据!'];
  88. try {
  89. DB::beginTransaction();
  90. $time = time();
  91. ProductActivity::where('del_time',0)->where('id',$data['id'])->update(['del_time' => $time]);
  92. ProductActivityPrice::where('product_activity_id',$data['id'])
  93. ->where('del_time',0)
  94. ->update(['del_time' => $time]);
  95. DB::commit();
  96. }catch (\Exception $exception){
  97. DB::rollBack();
  98. return [false,$exception->getMessage()];
  99. }
  100. return [true,''];
  101. }
  102. public function productDetail($data,$user){
  103. if($this->isEmpty($data,'id')) return [false,'请选择数据!'];
  104. $customer = ProductActivity::where('del_time',0)
  105. ->where('id',$data['id'])
  106. ->first();
  107. if(empty($customer)) return [false,'活动不存在或已被删除'];
  108. $customer = $customer->toArray();
  109. $detail = ProductActivityPrice::where('del_time',0)
  110. ->where('product_activity_id',$data['id'])
  111. ->get()->toArray();
  112. $title_map = BasicType::whereIn('id',array_column($detail,'basic_type_id'))
  113. ->pluck('title','id')
  114. ->toArray();
  115. $customer['product'] = [];
  116. foreach ($detail as $value){
  117. $customer['product'][] = [
  118. 'basic_type_id' => $value['basic_type_id'],
  119. 'basic_type_title' => $title_map[$value['basic_type_id']] ?? '',
  120. 'price' => $value['price'],
  121. ];
  122. }
  123. $customer['crt_name'] = Employee::where('id',$customer['crt_id'])->value('emp_name');
  124. $customer['crt_time'] = $customer['crt_time'] ? date("Y-m-d H:i:s",$customer['crt_time']): '';
  125. return [true, $customer];
  126. }
  127. public function productList($data,$user){
  128. $model = new ProductActivity(['userData' => $user, 'search' => $data]);
  129. $model = $model->where('del_time',0)
  130. ->orderby('id', 'desc');
  131. if(! empty($data['title'])) $model->where('title', 'LIKE', '%'.$data['title'].'%');
  132. $list = $this->limit($model,'',$data);
  133. $list = $this->fillData($list,$user);
  134. return [true, $list];
  135. }
  136. public function productRule(&$data, $user, $is_add = true){
  137. if(empty($data['title'])) return [false,'活动名称名称不能为空'];
  138. if(empty($data['activity_time'][0]) || empty($data['activity_time'][1])) return [false,'请填写活动时间范围'];
  139. $data['start_time'] = $this->changeDateToDateMin($data['activity_time'][0]);
  140. $data['end_time'] = $this->changeDateToDateMin($data['activity_time'][1]);
  141. if($is_add){
  142. $product = ProductActivityPrice::where('del_time',0)
  143. ->where('start', '<=', $data['end_time'])
  144. ->where('end', '>=', $data['start_time'])
  145. ->select('product_id')
  146. ->get()->toArray();
  147. $product = array_column($product,'product_id');
  148. }else{
  149. if(empty($data['id'])) return [false,'ID不能为空'];
  150. $product = ProductActivityPrice::where('del_time',0)
  151. ->where('id','<>',$data['id'])
  152. ->where('start', '<=', $data['end_time'])
  153. ->where('end', '>=', $data['start_time'])
  154. ->select('product_id')
  155. ->get()->toArray();
  156. $product = array_column($product,'product_id');
  157. }
  158. if(! empty($data['product'])){
  159. $start_time = date("Y-m-d H:i",$data['start_time']);
  160. $end_time = date("Y-m-d H:i",$data['end_time']);
  161. $map = BasicType::whereIn('id',array_column($data['product'],'basic_type_id'))
  162. ->pluck('title','id')
  163. ->toArray();
  164. $map2 = Product::whereIn('id',array_column($data['product'],'product_id'))
  165. ->pluck('title','id')
  166. ->toArray();
  167. foreach ($data['product'] as $value){
  168. $pro = $map2[$value['product_id']] ?? "";
  169. if(in_array($value['product_id'], $product)) return [false,'产品:' . $pro . '在' . $start_time . '——' . $end_time . '已设置活动价格'];
  170. if(! empty($value['price'])) {
  171. $tmp = $map[$value['basic_type_id']] ?? '';
  172. $res = $this->checkNumber($value['price']);
  173. if(! $res) return [false, $tmp . '请输入不超过两位小数并且大于0的数值'];
  174. }
  175. }
  176. }
  177. //所属部门 以及 顶级部门
  178. if(empty($data['depart_id'])) $data['depart_id'] = $this->getDepart($user);
  179. $data['top_depart_id'] = $user['depart_map'][$data['depart_id']] ?? 0;
  180. return [true, $data];
  181. }
  182. public function fillData($data, $user){
  183. if(empty($data['data'])) return $data;
  184. $emp = Employee::whereIn('id',array_unique(array_column($data['data'],'crt_id')))
  185. ->pluck('emp_name','id')
  186. ->toArray();
  187. foreach ($data['data'] as $key => $value){
  188. $data['data'][$key]['crt_time'] = $value['crt_time'] ? date('Y-m-d H:i:s',$value['crt_time']) : '';
  189. $data['data'][$key]['crt_name'] = $emp[$value['crt_id']] ?? '';
  190. }
  191. return $data;
  192. }
  193. }