StorehouseService.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. <?php
  2. namespace App\Service;
  3. use App\Model\Storehouse;
  4. use App\Model\StorehouseEmployee;
  5. use Illuminate\Support\Facades\DB;
  6. class StorehouseService extends Service
  7. {
  8. public function edit($data){
  9. list($status,$msg) = $this->storehouseRule($data,false);
  10. if(!$status) return [$status,$msg];
  11. $res = $data['data'][0];
  12. try{
  13. DB::beginTransaction();
  14. Storehouse::where('id',$data['id'])->update([
  15. 'title' => $res['title']
  16. ]);
  17. DB::commit();
  18. }catch (\Exception $e){
  19. DB::rollBack();
  20. return [false,$e->getMessage()];
  21. }
  22. return [true,'保存成功!'];
  23. }
  24. public function add($data){
  25. list($status,$msg) = $this->storehouseRule($data);
  26. if(!$status) return [$status,$msg];
  27. try{
  28. DB::beginTransaction();
  29. $insert = [];
  30. $time = time();
  31. foreach ($data['data'] as $value){
  32. $insert = [
  33. 'title' => $value['title'],
  34. 'crt_time' => $time
  35. ];
  36. }
  37. Storehouse::insert($insert);
  38. DB::commit();
  39. }catch (\Exception $e){
  40. DB::rollBack();
  41. return [false,$e->getMessage()];
  42. }
  43. return [true,'保存成功!'];
  44. }
  45. public function del($data){
  46. if($this->isEmpty($data,'id')) return [false,'请选择数据'];
  47. try {
  48. DB::beginTransaction();
  49. Storehouse::whereIn('id',$data['id'])->update([
  50. 'del_time'=>time()
  51. ]);
  52. DB::commit();
  53. }catch (\Exception $e){
  54. DB::rollBack();
  55. return [false,$e->getMessage()];
  56. }
  57. return [true,'删除成功'];
  58. }
  59. public function storehouseDetail($data){
  60. if($this->isEmpty($data,'id')) return [false,'请选择数据'];
  61. $detail = Storehouse::where('id',$data['id'])->first()->toArray();
  62. return [true,$detail];
  63. }
  64. public function storehouseList($data, $user){
  65. $model = Storehouse::TopClear($user,$data);
  66. $model = $model->where('del_time',0)
  67. ->select('id','title','depart_id','top_depart_id')
  68. ->orderBy('id','asc');
  69. if(! empty($data['title'])) $model->where('title', 'LIKE', '%'.$data['title'].'%');
  70. $list = $this->limit($model,'',$data);
  71. $list = $this->fillData($list, $user);
  72. return [true,$list];
  73. }
  74. public function fillData($data, $user){
  75. if(empty($data['data'])) return $data;
  76. //总社id
  77. $top_depart_id = $user['head'] ?? [];
  78. $top_depart_id = $top_depart_id['id'] ?? 0;
  79. foreach ($data['data'] as $key => $value){
  80. $is_top = 0;
  81. if($value['depart_id'] == $top_depart_id) $is_top = 1;
  82. $data['data'][$key]['is_top'] = $is_top;
  83. }
  84. return $data;
  85. }
  86. public function storehouseRule($data, $is_check = true){
  87. if($this->isEmpty($data,'data')) return [false,'数据不能为空!'];
  88. $title = array_column($data['data'],'title');
  89. $title = array_map(function($val) {
  90. return $val !== null ? $val : 0;
  91. }, $title);
  92. $title_count = array_count_values($title);
  93. foreach ($title as $value){
  94. if(empty($value)) return [false,'仓库名称不能为空!'];
  95. if($title_count[$value] > 1) return [false,'仓库名称不能重复'];
  96. }
  97. foreach ($data['data'] as $value){
  98. if($is_check){
  99. $bool = Storehouse::where('title',$value['title'])
  100. ->where('del_time',0)
  101. ->exists();
  102. }else{
  103. if($this->isEmpty($data,'id')) return [false,'id不能为空!'];
  104. $bool = Storehouse::where('title',$value['title'])
  105. ->where('id','<>',$data['id'])
  106. ->where('del_time',0)
  107. ->exists();
  108. }
  109. if($bool) return [false,'仓库名称不能重复'];
  110. }
  111. return [true,''];
  112. }
  113. }