package com.controller;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Map;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import com.utils.ValidatorUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.annotation.IgnoreAuth;
import com.entity.YonghuEntity;
import com.entity.view.YonghuView;
import com.service.YonghuService;
import com.service.TokenService;
import com.utils.PageUtils;
import com.utils.R;
import com.utils.MD5Util;
import com.utils.MPUtil;
import com.utils.CommonUtil;
/**
* dụng hộ
* hậu đoan tiếp khẩu
* @author
* @email
* @date 2021-03-13 15:51:47
*/
@RestController
@RequestMapping( "/yonghu" )
public class YonghuController {
@Autowired
private YonghuService yonghuService;
@Autowired
private TokenService tokenService;
/**
* đăng lục
*/
@IgnoreAuth
@RequestMapping(value = "/login" )
public R login(String username, String password, String captcha, HttpServletRequest request) {
YonghuEntity user = yonghuService.selectOne(new EntityWrapper<YonghuEntity>().eq( "gerenzhanghao", username));
if(user==null ||!user.getMima().equals(password)) {
return R.error( "Trướng hào hoặc mật mã bất chính xác" );
}
String token = tokenService.generateToken(user.getId(), username, "yonghu", "Dụng hộ" );
return R.ok().put( "token", token);
}
/**
* chú sách
*/
@IgnoreAuth
@RequestMapping( "/register" )
public R register(@RequestBody YonghuEntity yonghu){
//ValidatorUtils.validateEntity(yonghu);
YonghuEntity user = yonghuService.selectOne(new EntityWrapper<YonghuEntity>().eq( "gerenzhanghao", yonghu.getGerenzhanghao()));
if(user!=null) {
return R.error( "Chú sách dụng hộ dĩ tồn tại" );
}
Long uId = new Date().getTime();
yonghu.setId(uId);
yonghuService.insert(yonghu);
return R.ok();
}
/**
* thối xuất
*/
@RequestMapping( "/logout" )
public R logout(HttpServletRequest request) {
request.getSession().invalidate();
return R.ok( "Thối xuất thành công" );
}
/**
* hoạch thủ dụng hộ đích session dụng hộ tín tức
*/
@RequestMapping( "/session" )
public R getCurrUser(HttpServletRequest request){
Long id = (Long)request.getSession().getAttribute( "userId" );
YonghuEntity user = yonghuService.selectById(id);
return R.ok().put( "data", user);
}
/**
* mật mã trọng trí
*/
@IgnoreAuth
@RequestMapping(value = "/resetPass" )
public R resetPass(String username, HttpServletRequest request){
YonghuEntity user = yonghuService.selectOne(new EntityWrapper<YonghuEntity>().eq( "gerenzhanghao", username));
if(user==null) {
return R.error( "Trướng hào bất tồn tại" );
}
user.setMima( "123456" );
yonghuService.updateById(user);
return R.ok( "Mật mã dĩ trọng trí vi: 123456" );
}
/**
* hậu đoan liệt biểu
*/
@RequestMapping( "/page" )
public R page(@RequestParam Map<String, Object> params,YonghuEntity yonghu,
HttpServletRequest request){
EntityWrapper<YonghuEntity> ew = new EntityWrapper<YonghuEntity>();
PageUtils page = yonghuService.queryPage(params, MPUtil.sort(MPUtil.between(MPUtil.likeOrEq(ew, yonghu), params), params));
request.setAttribute( "data", page);
return R.ok().put( "data", page);
}
/**
* tiền đoan liệt biểu
*/
@RequestMapping( "/list" )
public R list(@RequestParam Map<String, Object> params,YonghuEntity yonghu, HttpServletRequest request){
EntityWrapper<YonghuEntity> ew = new EntityWrapper<YonghuEntity>();
PageUtils page = yonghuService.queryPage(params, MPUtil.sort(MPUtil.between(MPUtil.likeOrEq(ew, yonghu), params), params));
request.setAttribute( "data", page);
return R.ok().put( "data", page);
}
/**
* liệt biểu
*/
@RequestMapping( "/lists" )
public R list( YonghuEntity yonghu){
EntityWrapper<YonghuEntity> ew = new EntityWrapper<YonghuEntity>();
ew.allEq(MPUtil.allEQMapPre( yonghu, "yonghu" ));
return R.ok().put( "data", yonghuService.selectListView(ew));
}
/**
* tra tuân
*/
@RequestMapping( "/query" )
public R query(YonghuEntity yonghu){
EntityWrapper< YonghuEntity> ew = new EntityWrapper< YonghuEntity>();
ew.allEq(MPUtil.allEQMapPre( yonghu, "yonghu" ));
YonghuView yonghuView = yonghuService.selectView(ew);
return R.ok( "Tra tuân dụng hộ thành công" ).put( "data", yonghuView);
}
/**
* hậu đoan tường tình
*/
@RequestMapping( "/info/{id}" )
public R info(@PathVariable( "id" ) Long id){
YonghuEntity yonghu = yonghuService.selectById(id);
return R.ok().put( "data", yonghu);
}
/**
* tiền đoan tường tình
*/
@RequestMapping( "/detail/{id}" )
public R detail(@PathVariable( "id" ) Long id){
YonghuEntity yonghu = yonghuService.selectById(id);
return R.ok().put( "data", yonghu);
}
/**
* hậu đoan bảo tồn
*/
@RequestMapping( "/save" )
public R save(@RequestBody YonghuEntity yonghu, HttpServletRequest request){
yonghu.setId(new Date().getTime()+new Double(Math.floor(Math.random()*1000)).longValue());
//ValidatorUtils.validateEntity(yonghu);
YonghuEntity user = yonghuService.selectOne(new EntityWrapper<YonghuEntity>().eq( "gerenzhanghao", yonghu.getGerenzhanghao()));
if(user!=null) {
return R.error( "Dụng hộ dĩ tồn tại" );
}
yonghu.setId(new Date().getTime());
yonghuService.insert(yonghu);
return R.ok();
}
/**
* tiền đoan bảo tồn
*/
@RequestMapping( "/add" )
public R add(@RequestBody YonghuEntity yonghu, HttpServletRequest request){
yonghu.setId(new Date().getTime()+new Double(Math.floor(Math.random()*1000)).longValue());
//ValidatorUtils.validateEntity(yonghu);
YonghuEntity user = yonghuService.selectOne(new EntityWrapper<YonghuEntity>().eq( "gerenzhanghao", yonghu.getGerenzhanghao()));
if(user!=null) {
return R.error( "Dụng hộ dĩ tồn tại" );
}
yonghu.setId(new Date().getTime());
yonghuService.insert(yonghu);
return R.ok();
}
/**
* tu cải
*/
@RequestMapping( "/update" )
public R update(@RequestBody YonghuEntity yonghu, HttpServletRequest request){
//ValidatorUtils.validateEntity(yonghu);
yonghuService.updateById(yonghu);// toàn bộ canh tân
return R.ok();
}
/**
* san trừ
*/
@RequestMapping( "/delete" )
public R delete(@RequestBody Long[] ids){
yonghuService.deleteBatchIds(Arrays.asList(ids));
return R.ok();
}
/**
* đề tỉnh tiếp khẩu
*/
@RequestMapping( "/remind/{columnName}/{type}" )
public R remindCount(@PathVariable( "columnName" ) String columnName, HttpServletRequest request,
@PathVariable( "type" ) String type,@RequestParam Map<String, Object> map) {
map.put( "column", columnName);
map.put( "type", type);
if(type.equals( "2" )) {
SimpleDateFormat sdf = new SimpleDateFormat( "yyyy-MM-dd" );
Calendar c = Calendar.getInstance();
Date remindStartDate = null;
Date remindEndDate = null;
if(map.get( "remindstart" )!=null