StorehouseService.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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 = new Storehouse(['userData' => $user]);
  66. $model = $model->where('del_time',0)
  67. ->select('id','title')
  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);
  72. return [true,$list];
  73. }
  74. public function fillData($data){
  75. if(empty($data['data'])) return $data;
  76. foreach ($data['data'] as $key => $value){
  77. }
  78. return $data;
  79. }
  80. public function storehouseRule($data, $is_check = true){
  81. if($this->isEmpty($data,'data')) return [false,'数据不能为空!'];
  82. $title = array_column($data['data'],'title');
  83. $title = array_map(function($val) {
  84. return $val !== null ? $val : 0;
  85. }, $title);
  86. $title_count = array_count_values($title);
  87. foreach ($title as $value){
  88. if(empty($value)) return [false,'仓库名称不能为空!'];
  89. if($title_count[$value] > 1) return [false,'仓库名称不能重复'];
  90. }
  91. foreach ($data['data'] as $value){
  92. if($is_check){
  93. $bool = Storehouse::where('title',$value['title'])
  94. ->where('del_time',0)
  95. ->exists();
  96. }else{
  97. if($this->isEmpty($data,'id')) return [false,'id不能为空!'];
  98. $bool = Storehouse::where('title',$value['title'])
  99. ->where('id','<>',$data['id'])
  100. ->where('del_time',0)
  101. ->exists();
  102. }
  103. if($bool) return [false,'仓库名称不能重复'];
  104. }
  105. return [true,''];
  106. }
  107. }