WXMineService.java
package com.drefore.secondhand.wechat.service;
import com.alibaba.fastjson.JSONObject;
import com.drefore.secondhand.core.utils.Constants;
import com.drefore.secondhand.core.utils.MailOperation;
import com.drefore.secondhand.core.utils.ServiceUtil;
import com.drefore.secondhand.core.utils.UploadUtil;
import com.drefore.secondhand.domain.*;
import com.drefore.secondhand.domain.Collection;
import com.drefore.secondhand.mapper.*;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.*;
@Service
public class WXMineService {
@Autowired
ProductMapperproductMapper;
@Autowired
UserMapperuserMapper;
@Autowired
CollectionMappercollectionMapper;
@Autowired
ProvincesMapperprovincesMapper;
@Autowired
CitiesMappercitiesMapper;
@Autowired
SchoolMapperschoolMapper;
@Autowired
PaymentRulesMapperpaymentRulesMapper;
@Autowired
RegisterMapperregisterMapper;
@Autowired
NoticeLogMappernoticeLogMapper;
@Autowired
NoticeMappernoticeMapper;
@Autowired
RegisterSchoolMapperregisterSchoolMapper;
@Autowired
AdconfigMapperadconfigMapper;
//我的信息
public Map getMine(Integer userID) {
return null;
}
//两个日期比较相等
public boolean sameDate(Date d1, Date d2) {
SimpleDateFormat fmt =new SimpleDateFormat("yyyyMMdd");
//fmt.setTimeZone(new TimeZone()); // 如果需要设置时间区域,可以在这里设置
return fmt.format(d1).equals(fmt.format(d2));
}
//获取收藏
public Map getCollectionList(Integer userID, Integer pageNum, Integer pageSize) {
if (userID !=null && pageNum !=null && pageSize !=null) {
RowBounds rowBounds =new RowBounds(pageNum, pageSize);
Map dataMap =new HashMap<>();
CollectionCriteria cc =new CollectionCriteria();
cc.createCriteria().andUserIdEqualTo(userID);
cc.setOrderByClause("id DESC ");
List collectList =collectionMapper.selectByExampleWithRowbounds(cc, rowBounds);
List prod =new ArrayList<>();
for (Collection coll : collectList) {
Product product =productMapper.selectByPrimaryKey(coll.getProductId());
if(product.getDeleteFlag() ==true){
continue;
}
Map map =new HashMap<>();
map.put("productID", product.getId());
String[] images= product.getProductBanner().split(",");
if(images.length>0){
map.put("image",Constants.PRODUCT_URL +images[0]);
}
map.put("title", product.getProductTitle());
map.put("currentPrice", product.getProductPrice());
if (product.getProductState() ==0){
map.put("proState","已下架");
}
prod.add(map);
}
dataMap.put("products", prod);
return ServiceUtil.generateResponseMap(dataMap, Constants.STATE_SUCCESS);
}else {
return ServiceUtil.generateResponseMap(null, Constants.STATE_FAILED);
}
}
//添加和删除收藏
public Map addCollection(Integer userID, Integer productID) {
CollectionCriteria collectionCriteria =new CollectionCriteria();
collectionCriteria.createCriteria().andUserIdEqualTo(userID).andProductIdEqualTo(productID);
if (collectionMapper.countByExample(collectionCriteria) >0) {
collectionMapper.deleteByExample(collectionCriteria);
return ServiceUtil.generateResponseMap(null, Constants.STATE_SUCCESS);
}else {
Collection collection =new Collection();
collection.setUserId(userID);
collection.setProductId(productID);
Date date =new Date();
collection.setCollectTime(date);
int i =collectionMapper.insertSelective(collection);
return ServiceUtil.generateResponseMap(null, i >0 ? Constants.STATE_SUCCESS : Constants.STATE_FAILED);
}
}
//获取省
public Map getProvinces() {
List provincesList =provincesMapper.selectByExample(new ProvincesCriteria());
Map map =new HashMap<>();
map.put("provincesList", provincesList);
return ServiceUtil.generateResponseMap(map, Constants.STATE_SUCCESS);
}
//获取城市
public Map getCity(String provinceId) {
CitiesCriteria citiesCriteria =new CitiesCriteria();
citiesCriteria.createCriteria().andProvinceidEqualTo(provinceId);
List citiesList =citiesMapper.selectByExample(citiesCriteria);
Map map =new HashMap<>();
map.put("citiesList", citiesList);
return ServiceUtil.generateResponseMap(map, Constants.STATE_SUCCESS);
}
//获取学校
public Map getSchool(String citiesId) {
SchoolCriteria schoolCriteria =new SchoolCriteria();
schoolCriteria.createCriteria().andCityIdEqualTo(citiesId);
List schoolList =schoolMapper.selectByExample(schoolCriteria);
Map map =new HashMap<>();
map.put("schoolList", schoolList);
return ServiceUtil.generateResponseMap(map, Constants.STATE_SUCCESS);
}
//获取缴费规则
public Map getPaymentRules() {
PaymentRulesCriteria paymentRulesCriteria =new PaymentRulesCriteria();
paymentRulesCriteria.createCriteria().andStateEqualTo(0);
List paymentRules =paymentRulesMapper.selectByExample(paymentRulesCriteria);
Map map =new HashMap<>();
map.put("paymentRules", paymentRules);
return ServiceUtil.generateResponseMap(map, Constants.STATE_SUCCESS);
}
//注册
public Map insertRegister(Integer paymentRulesId, String provincesId, String citiesId, Integer schoolId, Integer userId, String phone,String code, MultipartFile[] files)throws IOException {
RegisterCriteria registerCriteria =new RegisterCriteria();
registerCriteria.createCriteria().andUserIdEqualTo(userId);
List registerList =registerMapper.selectByExample(registerCriteria);
if (registerList.size() >0) {
if (code.equals(registerList.get(0).getEmailCode())){
if (registerList.get(0).getValidTime().getTime() >= (new Date()).getTime()) {
String Url = UploadUtil.getUrlFromFiles(files, Constants.SHOP_WA_QR_PATH);
Register register =new Register();
register.setProvinceId(provincesId);
register.setPaymentRulesId(paymentRulesId);
register.setImgUrl(Url);
register.setCityId(citiesId);
register.setSchoolId(schoolId);
register.setPhone(phone);
register.setShopTime(new Date());
register.setRegisterState(Constants.REGISTER_STATE_WAITING);
int i =registerMapper.updateByExampleSelective(register, registerCriteria);
Map map =new HashMap<>();
map.put("registerState", Constants.REGISTER_STATE_WAITING);
return ServiceUtil.generateResponseMap(map, i >0 ? Constants.STATE_SUCCESS : Constants.STATE_FAILED);
}else {
return ServiceUtil.generateResponseMap(null, Constants.STATE_FAILED,"验证码超时");
}
}else {
return ServiceUtil.generateResponseMap(null, Constants.STATE_FAILED,"验证码错误!");
}
}
return ServiceUtil.generateResponseMap(null, Constants.STATE_FAILED,"验证码错误!");
}
//查询注册
public Map selectRegister(Integer userId){
if (userId !=null) {
Map map =new HashMap<>();
RegisterCriteria registerCriteria =new RegisterCriteria();
registerCriteria.createCriteria().andUserIdEqualTo(userId);
List registerList =registerMapper.selectByExample(registerCriteria);
if (registerList.size()>0){
Register register = registerList.get(0);
String str[] =new String[1];
str[0] = Constants.SHOP_WA_QR_URL + register.getImgUrl();
map.put("register",register);
map.put("img",str);
List provincesList =provincesMapper.selectByExample(new ProvincesCriteria());
map.put("provincesList", provincesList);
Map citiesList = (Map) getCity(register.getProvinceId()).get("data");
Map schoolList = (Map) getSchool(register.getCityId()).get("data");
map.put("citiesList", citiesList.get("citiesList"));
map.put("schoolList", schoolList.get("schoolList"));
}
return ServiceUtil.generateResponseMap(map, Constants.STATE_SUCCESS);
}
return ServiceUtil.generateResponseMap(null, Constants.STATE_FAILED);
}
//修改注册
public Map updRegister(Integer Id, String provincesId, String citiesId, Integer schoolId, Integer userId, String phone,String email, MultipartFile[] files)throws IOException {
if (Id !=null) {
String Url = UploadUtil.getUrlFromFiles(files, Constants.SHOP_WA_QR_PATH);
Register register =new Register();
register.setId(Id);
register.setProvinceId(provincesId);
register.setEmail(email);
register.setImgUrl(Url);
register.setCityId(citiesId);
register.setSchoolId(schoolId);
register.setPhone(phone);
int i =registerMapper.updateByPrimaryKeySelective(register);
return ServiceUtil.generateResponseMap(null, i >0 ? Constants.STATE_SUCCESS : Constants.STATE_FAILED);
}
return ServiceUtil.generateResponseMap(null, Constants.STATE_FAILED);
}
//获取用户权限
public Map getRule(Integer userId) {
RegisterCriteria registerCriteria =new RegisterCriteria();
System.out.println(userId);
registerCriteria.createCriteria().andUserIdEqualTo(userId);
List registerList =registerMapper.selectByExample(registerCriteria);
int state = Constants.REGISTER_STATE_NO;
System.out.println(registerList);
System.out.println(registerList.get(0).getRegisterState());
if (registerList.size() >0) {
if (registerList.get(0).getRegisterState() == Constants.REGISTER_STATE_WAITING) {
state = Constants.REGISTER_STATE_WAITING;
}else if (registerList.get(0).getRegisterState() == Constants.REGISTER_STATE_SUCCESS) {
state = Constants.REGISTER_STATE_SUCCESS;
}else {
state = Constants.REGISTER_STATE_REFUND;
}
}
Map map =new HashMap<>();
map.put("registerState", state);
return ServiceUtil.generateResponseMap(map, Constants.STATE_SUCCESS);
}
public Map getCode(String email, Integer userID) {
if (email !=null) {
boolean flag =false;
RegisterCriteria registerCriteria =new RegisterCriteria();
registerCriteria.createCriteria().andEmailEqualTo(email);
List registerList =registerMapper.selectByExample(registerCriteria);
if (registerList.size() >1) {
return ServiceUtil.generateResponseMap(null, Constants.STATE_FAILED,"邮箱已经被占用!");
}else if (registerList.size() ==1) {
Register register = registerList.get(0);
if (register.getUserId() == userID) {
flag =true;
}else {
return ServiceUtil.generateResponseMap(null, Constants.STATE_FAILED,"邮箱已经被占用!");
}
}
MailOperation operation =new MailOperation();
String from ="13470091370@163.com";
String to = email;// 收件人
String subject ="卓丰科技";
//邮箱内容
StringBuffer sb =new StringBuffer();
Integer yzm = (int)(Math.random()*(9999-1000+1))+1000;//产生1000-9999的随机数;
sb.append("<!DOCTYPE>"+"<div bgcolor='#f1fcfa' style='border:1px solid #d9f4ee; font-size:14px; line-height:22px; color:#005aa0;padding-left:1px;padding-top:5px; padding-bottom:5px;'><span style='font-weight:bold;'>温馨提示:"
+"<div style='width:950px;font-family:arial;'>欢迎使用本系统,您的验证码为:<br/><h2 style='color:green'>"+yzm+"</h2><br/>本邮件由系统自动发出,请勿回复。<br/>感谢您的使用。<br/>卓丰科技"
+"</div>");
try {
String res = operation.sendMail(from, to,
subject, sb.toString());
int i =0;
if ("success".equals(res)) {
RegisterCriteria registerCriteria1 =new RegisterCriteria();
registerCriteria1.createCriteria().andUserIdEqualTo(userID).andEmailEqualTo(email);
Register register =new Register();
register.setEmail(email);
register.setUserId(userID);
register.setEmailCode(yzm.toString());
Calendar calendar = Calendar.getInstance();
calendar.add(Calendar.MINUTE,5);
register.setValidTime(calendar.getTime());
if (flag) {
i =registerMapper.updateByExampleSelective(register, registerCriteria1);
}else {
i =registerMapper.insertSelective(register);
}
Map map =new HashMap<>();
map.put("code",yzm);
return i >0 ? ServiceUtil.generateResponseMap(map, Constants.STATE_SUCCESS) : ServiceUtil.generateResponseMap(null, Constants.STATE_FAILED);
}else if ("failure".equals(res)) {
return ServiceUtil.generateResponseMap(null, Constants.STATE_FAILED);
}
}catch (Exception e) {
e.printStackTrace();
return ServiceUtil.generateResponseMap(null, Constants.STATE_FAILED);
}
}
return ServiceUtil.generateResponseMap(null, Constants.STATE_FAILED,"邮箱不能为空!");
}
public Map getNotice(Integer userID,Integer offset,Integer limit){
User user =userMapper.selectByPrimaryKey(userID);
RowBounds rowBounds =new RowBounds(offset,limit);
Map maps =new HashMap<>();
List listNotice =new ArrayList<>();
Map params =new HashMap<>();
//userID == "普通用户"
if (user.getAccessLevel() ==0){
// 查出所有等于0(并且状态是已发送的)的和 指定发的
params.put("userID",userID);
System.out.println(user.getAccessLevel());
params.put("access",user.getAccessLevel());
listNotice=noticeMapper.selectByAccess(params,rowBounds);
}else if(user.getAccessLevel() ==1) {//userID == "商家"
// 查出所有等于0(并且状态是已发送的)和 等于1的 和指定发的
params.put("userID",userID);
params.put("access",user.getAccessLevel());
listNotice=noticeMapper.selectByAccess(params,rowBounds);
}
maps.put("dataMap", listNotice);
return ServiceUtil.generateResponseMap(maps, Constants.STATE_SUCCESS);
}
public Map registerShool(String provinceID,String cityID,String schoolName){
RegisterSchoolCriteria registerSchoolCriteria =new RegisterSchoolCriteria();
registerSchoolCriteria.createCriteria().andCityIdEqualTo(cityID).andSchoolNameEqualTo(schoolName);
List registerSchools =registerSchoolMapper.selectByExample(registerSchoolCriteria);
if (registerSchools.size()>0){
return ServiceUtil.generateResponseMap(null, Constants.STATE_FAILED,"该学校申请已存在,正在审核中");
}
SchoolCriteria schoolCriteria=new SchoolCriteria();
schoolCriteria.createCriteria().andCityIdEqualTo(cityID).andSchoolNameEqualTo(schoolName);
List schoolList=schoolMapper.selectByExample(schoolCriteria);
if (schoolList.size()>0){
return ServiceUtil.generateResponseMap(null,Constants.STATE_FAILED,"该学校已经存在");
}
RegisterSchool registerSchool =new RegisterSchool();
registerSchool.setProvinceId(provinceID);
registerSchool.setCityId(cityID);
registerSchool.setSchoolName(schoolName);
int i =registerSchoolMapper.insertSelective(registerSchool);
return ServiceUtil.generateResponseMap(null, i>0 ? Constants.STATE_SUCCESS:Constants.STATE_FAILED);
}
public Map getInstructions(){
List list =adconfigMapper.selectTotal();
Map map =new HashMap<>();
map.put("dataMap",list);
return ServiceUtil.generateResponseMap(map,Constants.STATE_SUCCESS);
}
}
WXMineController.java
package com.drefore.secondhand.wechat.controller;
import com.alibaba.fastjson.JSONObject;
import com.drefore.secondhand.wechat.service.WXMineService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
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.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.text.ParseException;
import java.util.Map;
@Controller
@RequestMapping("/wechat/user")
public class WXMineController {
@Autowired
private WXMineServicewxMineService;
//我的
@ResponseBody
@RequestMapping("/getMine")
public Map getMine(@RequestBody String parameters){
JSONObject jsonObject = JSONObject.parseObject(parameters);
Integer userID = jsonObject.getInteger("userID");
return wxMineService.getMine(userID);
}
//获取我的收藏
@ResponseBody
@RequestMapping("/getCollectionList")
public Map getCollectionList(@RequestBody String parameters){
JSONObject jsonObject = JSONObject.parseObject(parameters);
Integer userID = jsonObject.getInteger("userID");
Integer offset = jsonObject.getInteger("offset");
Integer limit = jsonObject.getInteger("limit");
return wxMineService.getCollectionList(userID,offset,limit);
}
//添加收藏
@ResponseBody
@RequestMapping("/addCollection")
public Map addCollection(@RequestBody String parameters){
JSONObject jsonObject = JSONObject.parseObject(parameters);
Integer userID = jsonObject.getInteger("userID");
Integer productID =jsonObject.getInteger("productID");
return wxMineService.addCollection(userID,productID);
}
//获取省
@ResponseBody
@RequestMapping("/getProvinces")
public Map getProvinces(){
return wxMineService.getProvinces();
}
//获取城市
@ResponseBody
@RequestMapping("/getCity")
public Map getCity(@RequestBody String parameters){
JSONObject jsonObject = JSONObject.parseObject(parameters);
String provinceId = jsonObject.getString("provinceId");
return wxMineService.getCity(provinceId);
}
//获取学校
@ResponseBody
@RequestMapping("/getSchool")
public Map getSchool(@RequestBody String parameters){
JSONObject jsonObject = JSONObject.parseObject(parameters);
String citiesId = jsonObject.getString("citiesId");
return wxMineService.getSchool(citiesId);
}
//获取缴费规则
@ResponseBody
@RequestMapping("/getPaymentRules")
public Map getPaymentRules(){
return wxMineService.getPaymentRules();
}
//注册
@ResponseBody
@RequestMapping("/insertRegister")
public Map insertRegister(HttpServletRequest request,@RequestParam(value ="file") MultipartFile[] file)throws IOException {
JSONObject jsonObject = JSONObject.parseObject(request.getParameter("parameters"));
Integer paymentRulesId = jsonObject.getInteger("paymentRulesId");
String provincesId = jsonObject.getString("provincesId");
String citiesId = jsonObject.getString("citiesId");
Integer schoolId = jsonObject.getInteger("schoolId");
Integer userId = jsonObject.getInteger("userId");
String phone = jsonObject.getString("phone");
String code = jsonObject.getString("code");
return wxMineService.insertRegister(paymentRulesId,provincesId,citiesId,schoolId,userId,phone,code,file);
}
//查询注册
@ResponseBody
@RequestMapping("/selectRegister")
public Map selectRegister(@RequestBody String parameters){
JSONObject jsonObject = JSONObject.parseObject(parameters);
Integer userId = jsonObject.getInteger("userId");
System.out.println(userId);
return wxMineService.selectRegister(userId);
}
//修改注册
@ResponseBody
@RequestMapping("/updRegister")
public Map updRegister(HttpServletRequest request,@RequestParam(value ="file") MultipartFile[] file)throws IOException {
JSONObject jsonObject = JSONObject.parseObject(request.getParameter("parameters"));
Integer Id = jsonObject.getInteger("Id");
String provincesId = jsonObject.getString("provincesId");
String citiesId = jsonObject.getString("citiesId");
Integer schoolId = jsonObject.getInteger("schoolId");
Integer userId = jsonObject.getInteger("userId");
String phone = jsonObject.getString("phone");
String email = jsonObject.getString("email");
return wxMineService.updRegister(Id,provincesId,citiesId,schoolId,userId,phone,email,file);
}
//获取用户权限
@ResponseBody
@RequestMapping("/getRule")
public Map getRule(@RequestBody String parameters){
JSONObject jsonObject = JSONObject.parseObject(parameters);
Integer userId = jsonObject.getInteger("userId");
return wxMineService.getRule(userId);
}
//获取验证码
@ResponseBody
@RequestMapping("/getCode")
public Map getCode (@RequestBody String parameters) {
JSONObject jsonObject = JSONObject.parseObject(parameters);
String email = jsonObject.getString("email");
Integer userID = jsonObject.getInteger("userID");
return wxMineService.getCode(email,userID);
}
//获取通知
@ResponseBody
@RequestMapping("/getNotice")
public Map getNotice (@RequestBody String parameters){
JSONObject jsonObject = JSONObject.parseObject(parameters);
Integer userID = jsonObject.getInteger("userID");
Integer offset = jsonObject.getInteger("offset");
Integer limit = jsonObject.getInteger("limit");
return wxMineService.getNotice(userID,offset,limit);
}
//学校申请
@ResponseBody
@RequestMapping("/registerShool")
public Map registerShool (@RequestBody String parameters){
JSONObject jsonObject = JSONObject.parseObject(parameters);
String provinceID = jsonObject.getString("provinceID");
String cityID = jsonObject.getString("cityID");
String schoolName = jsonObject.getString("schoolName");
return wxMineService.registerShool(provinceID,cityID,schoolName);
}
@ResponseBody
@RequestMapping("/getInstructions")
public Map getInstructions(){
return wxMineService.getInstructions();
}
}
网友评论