WareHouse.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. <?php
  2. namespace app\admin\controller;
  3. use app\BaseController;
  4. use think\App;
  5. use think\facade\Db;
  6. class WareHouse extends BaseController
  7. {
  8. public function __construct(App $app)
  9. {
  10. parent::__construct($app);
  11. $post =$this->request->post();
  12. $token = isset($post['token']) ? trim($post['token']) : "";
  13. // if($token==""){
  14. // return error_show(101,'token不能为空');
  15. //
  16. // }
  17. // $effetc = VerifyTokens($token);
  18. // if(!empty($effetc) && $effetc['code']!=0){
  19. // return error_show($effetc['code'],$effetc['message']);
  20. // }
  21. }
  22. public function add(){
  23. $post =$this->request->post();
  24. $name = isset($post['name'])&& $post['name']!='' ?trim($post['name']) :'';
  25. if($name==''){
  26. return error_show(1004,'参数name 不能为空');
  27. }
  28. $supplier = isset($post['supplierNo'])&& $post['supplierNo']!='' ?trim($post['supplierNo']) :'';
  29. if($supplier==''){
  30. return error_show(1004,'参数supplierNo 不能为空');
  31. }
  32. $supplierinfo = Db::name("supplier")->where(["code"=>$supplier])->find();
  33. if(empty($supplierinfo)){
  34. return error_show(1004,'未找到供应商数据');
  35. }
  36. $wsmtype = isset($post['wsmtype'])&& $post['wsmtype']!='' ?intval($post['wsmtype']) :'';
  37. if($wsmtype==''){
  38. return error_show(1004,'参数wsmtype 不能为空');
  39. }
  40. $isT = Db::name('warehouse_info')->where([['name',"=",$name],["is_del","=",0]])->find();
  41. if(!empty($isT)){
  42. return error_show(1004,'仓库名称已存在');
  43. }
  44. $addr = isset($post['addr'])&& $post['addr']!='' ?trim($post['addr']) :'';
  45. if($addr==''){
  46. return error_show(1004,'参数addr 不能为空');
  47. }
  48. $contactor = isset($post['contactor'])&& $post['contactor']!='' ?trim($post['contactor']) :'';
  49. if($contactor==''){
  50. return error_show(1004,'参数contactor 不能为空');
  51. }
  52. $mobile = isset($post['mobile'])&& $post['mobile']!='' ?trim($post['mobile']) :'';
  53. if($mobile==''){
  54. return error_show(1004,'参数mobile 不能为空');
  55. }
  56. $position = isset($post['position'])&& $post['position']!='' ?trim($post['position']) :'';
  57. $wsm_type = isset($post['wsm_type']) && $post['wsm_type']!=="" ? intval($post['wsm_type']):"1";
  58. $wsm_name = isset($post['wsm_name']) && $post['wsm_name']!=="" ? trim($post['wsm_name']):"";
  59. $wsm_mobile = isset($post['wsm_mobile']) && $post['wsm_mobile']!=="" ? trim($post['wsm_mobile']):"";
  60. $addr_code = isset($post['addr_code']) && $post['addr_code']!=="" ? trim($post['addr_code']):"";
  61. $wsm_addr = isset($post['wsm_addr']) && $post['wsm_addr']!=="" ? trim($post['wsm_addr']):"";
  62. Db::startTrans();
  63. try{
  64. $wsm_code = makeNo("WSM");
  65. $data =[
  66. 'wsm_code'=>$wsm_code,
  67. 'name'=>$name,
  68. 'wsm_type'=>$wsmtype,
  69. 'supplierNo'=>$supplier,
  70. 'addr'=>$addr,
  71. 'contactor'=>$contactor,
  72. 'mobile'=>$mobile,
  73. 'position'=>$position,
  74. 'status'=>1,
  75. 'addtime'=>date("Y-m-d H:i:s"),
  76. 'updatetime'=>date("Y-m-d H:i:s")
  77. ];
  78. $add = Db::name('warehouse_info')->insert($data,true);
  79. if($add>0){
  80. $addrinfo = [
  81. "wsm_code"=>$wsm_code,
  82. "wsm_type"=> $wsm_type,
  83. "wsm_name"=>$wsm_name,
  84. "wsm_mobile"=>$wsm_mobile,
  85. "wsm_addr"=>$wsm_addr,
  86. "addr_code"=>$addr_code,
  87. "status"=>1,
  88. "addtime"=>date("Y-m-d H:i:s"),
  89. "updatetime"=>date("Y-m-d H:i:s"),
  90. ];
  91. $addri = DB::name("warehouse_addr")->insert($addrinfo);
  92. if($addri){
  93. Db::commit();
  94. return app_show(0,'仓库创建成功');
  95. }
  96. }
  97. Db::rollback();
  98. return error_show(1005,'仓库创建失败');
  99. }catch (\Exception $e){
  100. Db::rollback();
  101. return error_show(1005,$e->getMessage());
  102. }
  103. }
  104. public function list(){
  105. $post =$this->request->post();
  106. $condition= [['a.is_del',"=",0],["b.is_del","=",0]];
  107. $supplier = isset($post['supplierNo'])&&$post['supplierNo']!="" ? trim($post['supplierNo']) :"";
  108. if($supplier!=""){
  109. $condition[]=["supplierNo","=",$supplier];
  110. }
  111. $wsm_code = isset($post['wsm_code'])&&$post['wsm_code']!="" ? trim($post['wsm_code']) :"";
  112. if($wsm_code!=""){
  113. $condition[]=["a.wsm_code","=",$wsm_code];
  114. }
  115. $start = isset($post['start'])&&$post['start']!="" ? $post['start'] :"";
  116. if($start!=""){
  117. $condition[]=["a.addtime",">=",$start];
  118. }
  119. $end = isset($post['end'])&&$post['end']!="" ? $post['end'] :"";
  120. if($end!=""){
  121. $condition[]=["a.addtime","<=",$end];
  122. }
  123. $mobile = isset($post['mobile'])&&$post['mobile']!="" ? trim($post['mobile']) :"";
  124. if($mobile!=""){
  125. $condition[]=["a.mobile","like","%{$mobile}%"];
  126. }
  127. $contactor = isset($post['contactor'])&&$post['contactor']!="" ? trim($post['contactor']) :"";
  128. if($contactor!=""){
  129. $condition[]=["a.contactor","like","%{$contactor}%"];
  130. }
  131. $page = isset($post['page']) &&$post['page']!=='' ?intval($post['page']) :1;
  132. $size = isset($post['size']) &&$post['size']!=='' ?intval($post['size']) :10;
  133. $count = Db::name("warehouse_info")->alias("a")->join("warehouse_addr as b ","a.wsm_code=b.wsm_code","left")
  134. ->where($condition)->count();
  135. $page>=ceil($count/$size) ? $page = ceil($count/$size): '';
  136. $list = Db::name("warehouse_info")->alias("a")->join("warehouse_addr as b ","a.wsm_code=b.wsm_code","left")
  137. ->where($condition)->field("a.id.a.wsm_code,a.name,a.wsm_type,a.supplierNo,a.addr,a.contactor,a.mobile,a.position,a.status,a.addtime,
  138. a.updatetime,b.wsm_type,b.wsm_name,b.wsm_mobile,b.wsm_addr,b.addr_code")->page($page,$size)->order("a.addtime desc")
  139. ->select();
  140. foreach ($list as $key=>$value){
  141. if($value['supplierNo']!=""){
  142. $supplierinfo = Db::name("supplier")->where(["code"=>$value['supplierNo']])->find();
  143. $value['supplier_name'] = isset($supplierinfo['name']) ? $supplierinfo['name'] :"";
  144. }else{
  145. $value['supplier_name']="";
  146. }
  147. if($value['wsm_type']!=""){
  148. $type = Db::name("warehouse_type")->where(["id"=>$value['wsm_type']])->find();
  149. $value['type_name'] = isset($type['name']) ? $type['name'] :"";
  150. }else{
  151. $value['type_name']="";
  152. }
  153. $list[$key]=$value;
  154. }
  155. return app_show(0,"获取成功",['list'=>$list,"count"=>$count]);
  156. }
  157. public function info(){
  158. $post =$this->request->post();
  159. $id = isset($post['id']) && $post['id']!==''?intval($post['id']): "";
  160. if($id===""){
  161. return error_show(1004,"参数id不能为空");
  162. };
  163. $warse = Db::name("warehouse_info")->alias("a")->join("warehouse_addr as b ","a.wsm_code=b.wsm_code","left")
  164. ->where(["a.id"=>$id,"a.is_del"=>0])->field("a.id.a.wsm_code,a.name,a.wsm_type,a.supplierNo,a.addr,a.contactor,a.mobile,a.position,a.status,a.addtime,
  165. a.updatetime,b.wsm_type,b.wsm_name,b.wsm_mobile,b.wsm_addr,b.addr_code")->find();
  166. if(empty($warse)){
  167. return error_show(1004,"未找到数据");
  168. }
  169. if($warse['supplierNo']!=""){
  170. $supplierinfo = Db::name("supplier")->where(["code"=>$warse['supplierNo']])->find();
  171. $warse['supplier_name'] = isset($supplierinfo['name']) ? $supplierinfo['name'] :"";
  172. }else{
  173. $warse['supplier_name']="";
  174. }
  175. if($warse['wsm_type']!=""){
  176. $type = Db::name("warehouse_type")->where(["id"=>$warse['wsm_type']])->find();
  177. $warse['type_name'] = isset($type['name']) ? $type['name'] :"";
  178. }else{
  179. $warse['type_name']="";
  180. }
  181. return app_show(0,"获取成功",$warse);
  182. }
  183. public function save(){
  184. $post =$this->request->post();
  185. $id = isset($post['id']) && $post['id']!==''?intval($post['id']): "";
  186. if($id===""){
  187. return error_show(1004,"参数id不能为空");
  188. };
  189. $warse = Db::name("warehouse_info")->where(["id"=>$id,"is_del"=>0])->find();
  190. if(empty($warse)){
  191. return error_show(1004,"未找到数据");
  192. }
  193. $name = isset($post['name'])&& $post['name']!='' ?trim($post['name']) :'';
  194. if($name==''){
  195. return error_show(1004,'参数name 不能为空');
  196. }
  197. $isT = Db::name('warehouse_info')->where([['name',"=",$name],["is_del","=",0],["id","<>",$id]])->find();
  198. if(!empty($isT)){
  199. return error_show(1004,'仓库名称已存在');
  200. }
  201. $addr = isset($post['addr'])&& $post['addr']!='' ?trim($post['addr']) :'';
  202. if($addr==''){
  203. return error_show(1004,'参数addr 不能为空');
  204. }
  205. $contactor = isset($post['contactor'])&& $post['contactor']!='' ?trim($post['contactor']) :'';
  206. if($contactor==''){
  207. return error_show(1004,'参数contactor 不能为空');
  208. }
  209. $mobile = isset($post['mobile'])&& $post['mobile']!='' ?trim($post['mobile']) :'';
  210. if($mobile==''){
  211. return error_show(1004,'参数mobile 不能为空');
  212. }
  213. $position = isset($post['position'])&& $post['position']!='' ?trim($post['position']) :'';
  214. $wsm_type = isset($post['wsm_type']) && $post['wsm_type']!=="" ? intval($post['wsm_type']):"1";
  215. $wsm_name = isset($post['wsm_name']) && $post['wsm_name']!=="" ? trim($post['wsm_name']):"";
  216. $wsm_mobile = isset($post['wsm_mobile']) && $post['wsm_mobile']!=="" ? trim($post['wsm_mobile']):"";
  217. $addr_code = isset($post['addr_code']) && $post['addr_code']!=="" ? $post['addr_code']:"";
  218. $wsm_addr = isset($post['wsm_addr']) && $post['wsm_addr']!=="" ? trim($post['wsm_addr']):"";
  219. Db::startTrans();
  220. try{
  221. $data =[
  222. 'name'=>$name,
  223. 'addr'=>$addr,
  224. 'contactor'=>$contactor,
  225. 'mobile'=>$mobile,
  226. 'position'=>$position,
  227. 'updatetime'=>date("Y-m-d H:i:s")
  228. ];
  229. $add = Db::name('warehouse_info')->where($warse)->update($data);
  230. if($add){
  231. $addrinfo = [
  232. "wsm_type"=> $wsm_type,
  233. "wsm_name"=>$wsm_name,
  234. "wsm_mobile"=>$wsm_mobile,
  235. "wsm_addr"=>$wsm_addr,
  236. "addr_code"=>$addr_code,
  237. "updatetime"=>date("Y-m-d H:i:s"),
  238. ];
  239. $addri = DB::name("warehouse_addr")->where(["wsm_code"=>$warse['wsm_code'],"is_del"=>0])->update($addrinfo);
  240. if($addri){
  241. Db::commit();
  242. return app_show(0,'仓库更新成功');
  243. }
  244. }
  245. Db::rollback();
  246. return error_show(1005,'仓库更新失败');
  247. }catch (\Exception $e){
  248. Db::rollback();
  249. return error_show(1005,$e->getMessage());
  250. }
  251. }
  252. public function delete(){
  253. $post =$this->request->post();
  254. $id = isset($post['id']) && $post['id']!==''?intval($post['id']): "";
  255. if($id===""){
  256. return error_show(1004,"参数id不能为空");
  257. };
  258. $warse = Db::name("warehouse_info")->where(["id"=>$id,"is_del"=>0])->find();
  259. if(empty($warse)){
  260. return error_show(1004,"未找到数据");
  261. }
  262. $warse['is_del'] = 1;
  263. $warse['updatetime'] = date("Y-m-d H:i:s");
  264. $Del= Db::name("warehouse_info")->save($warse);
  265. return $Del? app_show(0,'仓库删除成功'): error_show(1005,'仓库删除失败');
  266. }
  267. public function status(){
  268. $post =$this->request->post();
  269. $id = isset($post['id']) && $post['id']!==''?intval($post['id']): "";
  270. if($id===""){
  271. return error_show(1004,"参数id不能为空");
  272. };
  273. $warse = Db::name("warehouse_info")->where(["id"=>$id,"is_del"=>0])->find();
  274. if(empty($warse)){
  275. return error_show(1004,"未找到数据");
  276. }
  277. $status = isset($post['status']) && $post['status']!=="" ? intval($post['status']) : "";
  278. if($status===""){
  279. return error_show(1005,"参数status 不能为空");
  280. }
  281. $warse['status'] = $status;
  282. $warse['updatetime'] = date("Y-m-d H:i:s");
  283. $Del= Db::name("warehouse_info")->save($warse);
  284. return $Del? app_show(0,'仓库状态修改成功'): error_show(1005,'仓库状态修改失败');
  285. }
  286. }