package com.controller;

im
port java.text.SimpleDateFormat;
im
port java.util.ArrayList;
im
port java.util.Arrays;
im
port java.util.Calendar;
im
port java.util.Map;
im
port java.util.HashMap;
im
port java.util.Iterator;
im
port java.util.Date;
im
port java.util.List;
im
port javax.servlet.http.HttpServletRequest;
im
port com.utils.ValidatorUtils;
im
port org.apache.commons.lang3.StringUtils;
im
port org.springf
ramework.beans.factory.annotation.Autowired;
im
port org.springf
ramework.format.annotation.DateTimeFormat;
im
port org.springf
ramework.web.bind.annotation.PathVariable;
im
port org.springf
ramework.web.bind.annotation.RequestBody;
im
port org.springf
ramework.web.bind.annotation.RequestMapping;
im
port org.springf
ramework.web.bind.annotation.RequestParam;
im
port org.springf
ramework.web.bind.annotation.RestController;
im
port com.baomidou.mybatisplus.mapper.EntityWrapper;
im
port com.baomidou.mybatisplus.mapper.Wrapper;
im
port com.annotation.IgnoreAuth;
im
port com.entity.GongyingshangEntity;
im
port com.entity.view.GongyingshangView;
im
port com.service.GongyingshangService;
im
port com.service.TokenService;
im
port com.utils.PageUtils;
im
port com.utils.R;
im
port com.utils.MD5Util;
im
port com.utils.MPUtil;
im
port com.utils.CommonUtil;
im
port java.io.IOException;
@RestCo
ntroller
@RequestMapping("/gongyingshang")
public class Go
ngyingshangController {
@Autowired
private Go
ngyingshangService gongyingshangService;
@Autowired
private TokenService tokenService;
@IgnoreAuth
@RequestMapping(value = "/login")
public R login(String username, String password, String captcha, HttpServletRequest request) {
Go
ngyingshangEntity user = gongyingshangService.selectOne(new EntityWrapper<GongyingshangEntity>().eq("gongyingbianhao", username));
if(user==null || !user.getMima().equals(password)) {
return R.error("账号或密码不正确");
}
String token = tokenService.generateToken(user.getId(), username,"gongyingshang", "供应商" );
return R.ok().put("token", token);
}
@IgnoreAuth
@RequestMapping("/register")
public R register(@RequestBody Go
ngyingshangEntity gongyingshang){
//ValidatorUtils.validateEntity(gongyingshang);
Go
ngyingshangEntity user = gongyingshangService.selectOne(new EntityWrapper<GongyingshangEntity>().eq("gongyingbianhao", gongyingshang.getGo
ngyingbianhao()));
if(user!=null) {
return R.error("注册用户已存在");
}
Long uId = new Date().getTime();
gongyingshang.setId(uId);
gongyingshangService.insert(gongyingshang);
return R.ok();
}
@RequestMapping("/logout")
public R logout(HttpServletRequest request) {
request.getSession().invalidate();
return R.ok("退出成功");
}
@RequestMapping("/session")
public R getCurrUser(HttpServletRequest request){
Long id = (Long)request.getSession().getAttribute("userId");
Go
ngyingshangEntity user = gongyingshangService.selectById(id);
return R.ok().put("data", user);
}
@IgnoreAuth
@RequestMapping(value = "/resetPass")
public R resetPass(String username, HttpServletRequest request){
Go
ngyingshangEntity user = gongyingshangService.selectOne(new EntityWrapper<GongyingshangEntity>().eq("gongyingbianhao", username));
if(user==null) {
return R.error("账号不存在");
}
user.setMima("123456");
gongyingshangService.updateById(user);
return R.ok("密码已重置为:123456");
}
@RequestMapping("/page")
public R page(@RequestParam Map<String, Object> params,Go
ngyingshangEntity gongyingshang,
HttpServletRequest request){
EntityWrapper<GongyingshangEntity> ew = new EntityWrapper<GongyingshangEntity>();
PageUtils page = gongyingshangService.queryPage(params, MPUtil.sort(MPUtil.between(MPUtil.likeOrEq(ew, gongyingshang), params), params));
request.setAttribute("data", page);
return R.ok().put("data", page);
}
@IgnoreAuth
@RequestMapping("/list")
public R list(@RequestParam Map<String, Object> params,Go
ngyingshangEntity gongyingshang,
HttpServletRequest request){
EntityWrapper<GongyingshangEntity> ew = new EntityWrapper<GongyingshangEntity>();
PageUtils page = gongyingshangService.queryPage(params, MPUtil.sort(MPUtil.between(MPUtil.likeOrEq(ew, gongyingshang), params), params));
request.setAttribute("data", page);
return R.ok().put("data", page);
}
@RequestMapping("/lists")
public R list( Go
ngyingshangEntity gongyingshang){
EntityWrapper<GongyingshangEntity> ew = new EntityWrapper<GongyingshangEntity>();
ew.allEq(MPUtil.allEQMapPre( gongyingshang, "gongyingshang"));
return R.ok().put("data", gongyingshangService.selectListView(ew));
}
@RequestMapping("/query")
public R query(Go
ngyingshangEntity gongyingshang){
EntityWrapper< GongyingshangEntity> ew = new EntityWrapper< GongyingshangEntity>();
ew.allEq(MPUtil.allEQMapPre( gongyingshang, "gongyingshang"));
Go
ngyingshangView go
ngyingshangView = gongyingshangService.selectView(ew);
return R.ok("查询供应商成功").put("data", gongyingshangView);
}
@RequestMapping("/info/{id}")
public R info(@PathVariable("id") Long id){
Go
ngyingshangEntity go
ngyingshang = gongyingshangService.selectById(id);
return R.ok().put("data", gongyingshang);
}
@IgnoreAuth
@RequestMapping("/detail/{id}")
public R detail(@PathVariable("id") Long id){
Go
ngyingshangEntity go
ngyingshang = gongyingshangService.selectById(id);
return R.ok().put("data", gongyingshang);
}
@RequestMapping("/save")
public R save(@RequestBody Go
ngyingshangEntity gongyingshang, HttpServletRequest request){
gongyingshang.setId(new Date().getTime()+new Double(Math.floor(Math.random()*1000)).lo
ngValue());
//ValidatorUtils.validateEntity(gongyingshang);
Go
ngyingshangEntity user = gongyingshangService.selectOne(new EntityWrapper<GongyingshangEntity>().eq("gongyingbianhao", gongyingshang.getGo
ngyingbianhao()));
if(user!=null) {
return R.error("用户已存在");
}
gongyingshang.setId(new Date().getTime());
gongyingshangService.insert(gongyingshang);
return R.ok();
}
@RequestMapping("/add")
public R add(@RequestBody Go
ngyingshangEntity gongyingshang, HttpServletRequest request){
gongyingshang.setId(new Date().getTime()+new Double(Math.floor(Math.random()*1000)).lo
ngValue());
//ValidatorUtils.validateEntity(gongyingshang);
Go
ngyingshangEntity user = gongyingshangService.selectOne(new EntityWrapper<GongyingshangEntity>().eq("gongyingbianhao", gongyingshang.getGo
ngyingbianhao()));
if(user!=null) {
return R.error("用户已存在");
}
gongyingshang.setId(new Date().getTime());
gongyingshangService.insert(gongyingshang);
return R.ok();
}
@RequestMapping("/update")
public R update(@RequestBody Go
ngyingshangEntity gongyingshang, HttpServletRequest request){
//ValidatorUtils.validateEntity(gongyingshang);
gongyingshangService.updateById(gongyingshang);//全部更新
return R.ok();
}