WareHouse.php 13 KB

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