詹子聪 %!s(int64=5) %!d(string=hai) anos
pai
achega
2f0c4b935e

+ 1 - 1
src/main/java/com/miekir/shibei/bean/CouponBean.java

@@ -19,7 +19,7 @@ public class CouponBean {
     @Id
     @GeneratedValue(strategy= GenerationType.AUTO)
     @Column(name = "id", nullable = false, insertable = true, updatable = false)
-    public long couponId;
+    public long id;
 
     /**
      * 模板类型

+ 124 - 0
src/main/java/com/miekir/shibei/controller/api/CouponController.java

@@ -0,0 +1,124 @@
+package com.miekir.shibei.controller.api;
+
+import com.alibaba.fastjson.JSON;
+import com.miekir.shibei.bean.CouponBean;
+import com.miekir.shibei.bean.GoodsBean;
+import com.miekir.shibei.bean.ResponseResult;
+import com.miekir.shibei.bean.ResultCode;
+import com.miekir.shibei.repository.CouponRepository;
+import com.miekir.shibei.repository.UserRepository;
+import com.miekir.shibei.tool.RequestTool;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.data.domain.PageRequest;
+import org.springframework.data.domain.Sort;
+import org.springframework.http.HttpHeaders;
+import org.springframework.stereotype.Controller;
+import org.springframework.web.bind.annotation.*;
+
+import java.util.List;
+
+@Controller
+public class CouponController {
+    // 自动装配
+    @Autowired
+    private CouponRepository couponRepository;
+
+    @Autowired
+    private UserRepository userRepository;
+
+    /**
+     * 新增和更新优惠券
+     */
+    @RequestMapping(value = "/api/addCoupon", method = RequestMethod.POST, produces = "application/json; charset=utf-8")
+    @ResponseBody
+    public String addCoupon(@RequestHeader HttpHeaders header, @RequestBody CouponBean couponBean) {
+        ResponseResult<String> responseResult = new ResponseResult<String>();
+        responseResult.setMessage("操作失败");
+
+        if (couponBean == null || !RequestTool.isRequestAdminValid(header, userRepository)) {
+            return JSON.toJSONString(responseResult);
+        }
+
+        try {
+            couponRepository.save(couponBean);
+        } catch (Exception e) {
+            e.printStackTrace();
+            return JSON.toJSONString(responseResult);
+        }
+
+        responseResult.setContent("操作成功");
+        responseResult.setCode(ResultCode.SUCCESS);
+        responseResult.setMessage("操作成功");
+        return JSON.toJSONString(responseResult);
+    }
+
+    /**
+     * 删除优惠券
+     */
+    @RequestMapping(value = "/api/deleteCouponById", method = RequestMethod.GET, produces = "application/json; charset=utf-8")
+    @ResponseBody
+    public String deleteCoupon(@RequestHeader HttpHeaders header, long couponId) {
+        ResponseResult<String> responseResult = new ResponseResult<String>();
+        responseResult.setMessage("删除失败");
+
+        if (!RequestTool.isRequestAdminValid(header, userRepository)) {
+            return JSON.toJSONString(responseResult);
+        }
+
+        // 查找对应的商品
+        CouponBean dbCouponBean;
+        try {
+            dbCouponBean = couponRepository.findCouponById(couponId);
+        } catch (Exception e) {
+            e.printStackTrace();
+            return JSON.toJSONString(responseResult);
+        }
+
+        // 找不到对应商品
+        if (dbCouponBean == null) {
+            return JSON.toJSONString(responseResult);
+        }
+
+        try {
+            couponRepository.delete(dbCouponBean);
+        } catch (Exception e) {
+            e.printStackTrace();
+            return JSON.toJSONString(responseResult);
+        }
+
+        responseResult.setCode(ResultCode.SUCCESS);
+        responseResult.setMessage("删除成功");
+        return JSON.toJSONString(responseResult);
+    }
+
+    /**
+     * 查询优惠券
+     */
+    @RequestMapping(value = "/api/getCouponList", method = RequestMethod.GET, produces = "application/json; charset=utf-8")
+    @ResponseBody
+    public String getCouponList(int pageNum, int pageSize) {
+        ResponseResult<String> responseResult = new ResponseResult<String>();
+        responseResult.setMessage("获取失败");
+
+        // 自带的分页查询
+        List<CouponBean> couponBeanList;
+        try {
+            Sort sort = new Sort(Sort.Direction.DESC,"id");
+            couponBeanList = couponRepository.findAll(new PageRequest(pageNum, pageSize, sort)).getContent();
+        } catch (Exception e) {
+            e.printStackTrace();
+            return JSON.toJSONString(responseResult);
+        }
+
+        try {
+            responseResult.setContent(JSON.toJSONString(couponBeanList));
+        } catch (Exception e) {
+            e.printStackTrace();
+            return JSON.toJSONString(responseResult);
+        }
+
+        responseResult.setCode(ResultCode.SUCCESS);
+        responseResult.setMessage("获取成功");
+        return JSON.toJSONString(responseResult);
+    }
+}

+ 34 - 67
src/main/java/com/miekir/shibei/controller/api/GoodsController.java

@@ -7,6 +7,8 @@ import com.miekir.shibei.repository.UserRepository;
 import com.miekir.shibei.tool.RequestTool;
 import com.miekir.shibei.tool.TextUtils;
 import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.data.domain.PageRequest;
+import org.springframework.data.domain.Sort;
 import org.springframework.http.HttpHeaders;
 import org.springframework.stereotype.Controller;
 import org.springframework.web.bind.annotation.*;
@@ -16,7 +18,6 @@ import java.util.Map;
 
 @Controller
 public class GoodsController {
-
     // 自动装配
     @Autowired
     private GoodsRepository goodsRepository;
@@ -25,7 +26,7 @@ public class GoodsController {
     private UserRepository userRepository;
 
     /**
-     * 新增和修改京东商品
+     * 新增和更新京东商品
      */
     @RequestMapping(value = "/api/addGoods", method = RequestMethod.POST, produces = "application/json; charset=utf-8")
     @ResponseBody
@@ -88,69 +89,35 @@ public class GoodsController {
         responseResult.setMessage("删除成功");
         return JSON.toJSONString(responseResult);
     }
-//
-//    /**
-//     * 查询密码记事
-//     */
-//    @RequestMapping(value = "/api/getNote", method = RequestMethod.POST, produces = "application/json; charset=utf-8")
-//    @ResponseBody
-//    public String getNote(String email, String token) {
-//        ResponseResult<String> responseResult = new ResponseResult<String>();
-//        responseResult.setMessage("获取失败");
-//
-//        List<NoteBean> noteBeanList;
-//        try {
-//            noteBeanList = goodsRepository.findNotesByEmail(email);
-//        } catch (Exception e) {
-//            e.printStackTrace();
-//            return JSON.toJSONString(responseResult);
-//        }
-//
-//        try {
-//            responseResult.setContent(JSON.toJSONString(noteBeanList));
-//        } catch (Exception e) {
-//            e.printStackTrace();
-//            return JSON.toJSONString(responseResult);
-//        }
-//
-//        responseResult.setCode(ResultCode.SUCCESS);
-//        responseResult.setMessage("获取成功");
-//        return JSON.toJSONString(responseResult);
-//    }
-//
-//    /**
-//     * 更新密码记事
-//     */
-//    @RequestMapping(value = "/api/updateNote", method = RequestMethod.POST, produces = "application/json; charset=utf-8")
-//    @ResponseBody
-//    public String updateNote(String email, String token, String note_bean) {
-//        ResponseResult<String> responseResult = new ResponseResult<String>();
-//        responseResult.setMessage("更新失败");
-//
-//        NoteBean clientBean;
-//        try {
-//            clientBean = JSON.parseObject(note_bean, NoteBean.class);
-//        } catch (Exception e) {
-//            e.printStackTrace();
-//            return JSON.toJSONString(responseResult);
-//        }
-//
-//        // 发送方的用户和该记事的所有者不是同一个人,没有权限
-//        NoteBean oldBean = goodsRepository.findNoteBeanById(clientBean.getId());
-//        if (!TextUtils.equals(email, oldBean.getUserId())) {
-//            return JSON.toJSONString(responseResult);
-//        }
-//
-//
-//        try {
-//            goodsRepository.updateNoteBean(clientBean.getContent(), clientBean.getId(), System.currentTimeMillis());
-//        } catch (Exception e) {
-//            e.printStackTrace();
-//            return JSON.toJSONString(responseResult);
-//        }
-//
-//        responseResult.setCode(ResultCode.SUCCESS);
-//        responseResult.setMessage("更新成功");
-//        return JSON.toJSONString(responseResult);
-//    }
+
+    /**
+     * 查询京东商品
+     */
+    @RequestMapping(value = "/api/getGoodsList", method = RequestMethod.GET, produces = "application/json; charset=utf-8")
+    @ResponseBody
+    public String getGoodsList(int pageNum, int pageSize) {
+        ResponseResult<String> responseResult = new ResponseResult<String>();
+        responseResult.setMessage("获取失败");
+
+        // 自带的分页查询
+        List<GoodsBean> goodsBeanList;
+        try {
+            Sort sort = new Sort(Sort.Direction.DESC,"id");
+            goodsBeanList = goodsRepository.findAll(new PageRequest(pageNum, pageSize, sort)).getContent();
+        } catch (Exception e) {
+            e.printStackTrace();
+            return JSON.toJSONString(responseResult);
+        }
+
+        try {
+            responseResult.setContent(JSON.toJSONString(goodsBeanList));
+        } catch (Exception e) {
+            e.printStackTrace();
+            return JSON.toJSONString(responseResult);
+        }
+
+        responseResult.setCode(ResultCode.SUCCESS);
+        responseResult.setMessage("获取成功");
+        return JSON.toJSONString(responseResult);
+    }
 }

+ 0 - 174
src/main/java/com/miekir/shibei/controller/api/PasswordController.java

@@ -1,174 +0,0 @@
-package com.miekir.shibei.controller.api;
-
-import com.alibaba.fastjson.JSON;
-import com.miekir.shibei.bean.PasswordBean;
-import com.miekir.shibei.bean.ResponseResult;
-import com.miekir.shibei.bean.ResultCode;
-import com.miekir.shibei.repository.PasswordRepository;
-import com.miekir.shibei.repository.UserRepository;
-import com.miekir.shibei.tool.TextUtils;
-import org.springframework.beans.factory.annotation.Autowired;
-import org.springframework.stereotype.Controller;
-import org.springframework.web.bind.annotation.RequestMapping;
-import org.springframework.web.bind.annotation.RequestMethod;
-import org.springframework.web.bind.annotation.ResponseBody;
-
-import java.util.List;
-
-@Controller
-public class PasswordController {
-
-    // 自动装配
-    @Autowired
-    private PasswordRepository passwordRepository;
-
-    @Autowired
-    private UserRepository userRepository;
-
-    /**
-     * 新增密码记事,接收的是纯JSON数据  严格来说,也要传递id和token过来校验的
-     */
-    @RequestMapping(value = "/api/addPassword", method = RequestMethod.POST, produces = "application/json; charset=utf-8")
-    @ResponseBody
-    public String addPassword(String email, String token, String password_bean) {
-        ResponseResult<String> responseResult = new ResponseResult<String>();
-        responseResult.setMessage("保存失败");
-
-        PasswordBean bean;
-        try {
-            //bean = JSON.parseObject(password_bean, PasswordBean.class);
-            bean = JSON.parseObject(password_bean, PasswordBean.class);
-            bean.setUpdateTime(System.currentTimeMillis());
-        } catch (Exception e) {
-            e.printStackTrace();
-            return JSON.toJSONString(responseResult);
-        }
-
-        PasswordBean dbPasswordBean;
-        try {
-            dbPasswordBean = passwordRepository.save(bean);
-
-        } catch (Exception e) {
-            e.printStackTrace();
-            return JSON.toJSONString(responseResult);
-        }
-
-        try {
-            responseResult.setContent(JSON.toJSONString(dbPasswordBean));
-        } catch (Exception e) {
-            e.printStackTrace();
-        }
-
-        responseResult.setCode(ResultCode.SUCCESS);
-        responseResult.setMessage("保存成功");
-        return JSON.toJSONString(responseResult);
-    }
-
-    /**
-     * 删除密码记事
-     */
-    @RequestMapping(value = "/api/delPassword", method = RequestMethod.POST, produces = "application/json; charset=utf-8")
-    @ResponseBody
-    public String delPassword(String email, String token, int passwordBeanId) {
-        ResponseResult<String> responseResult = new ResponseResult<String>();
-        responseResult.setMessage("操作失败");
-
-        PasswordBean dbPasswordBean;
-        try {
-            dbPasswordBean = passwordRepository.findPasswordBeanById(passwordBeanId);
-        } catch (Exception e) {
-            e.printStackTrace();
-            return JSON.toJSONString(responseResult);
-        }
-
-        // 密码不属于该用户
-        if (dbPasswordBean == null || !TextUtils.equals(email, dbPasswordBean.getUserId())) {
-            return JSON.toJSONString(responseResult);
-        }
-
-
-        try {
-            passwordRepository.delete(passwordBeanId);
-        } catch (Exception e) {
-            e.printStackTrace();
-            return JSON.toJSONString(responseResult);
-        }
-
-        responseResult.setCode(ResultCode.SUCCESS);
-        responseResult.setMessage("删除成功");
-        return JSON.toJSONString(responseResult);
-    }
-
-    /**
-     * 查询密码记事
-     */
-    @RequestMapping(value = "/api/getPassword", method = RequestMethod.POST, produces = "application/json; charset=utf-8")
-    @ResponseBody
-    public String getPassword(String email, String token) {
-        ResponseResult<String> responseResult = new ResponseResult<String>();
-        responseResult.setMessage("获取失败");
-
-        List<PasswordBean> passwordBeanList;
-        try {
-            passwordBeanList = passwordRepository.findPasswordNotesByEmail(email);
-        } catch (Exception e) {
-            e.printStackTrace();
-            return JSON.toJSONString(responseResult);
-        }
-
-        try {
-            responseResult.setContent(JSON.toJSONString(passwordBeanList));
-        } catch (Exception e) {
-            e.printStackTrace();
-            return JSON.toJSONString(responseResult);
-        }
-
-        responseResult.setCode(ResultCode.SUCCESS);
-        responseResult.setMessage("获取成功");
-        return JSON.toJSONString(responseResult);
-    }
-
-    /**
-     * 更新密码记事
-     */
-    @RequestMapping(value = "/api/updatePassword", method = RequestMethod.POST, produces = "application/json; charset=utf-8")
-    @ResponseBody
-    public String updatePassword(String email, String token, String mixedPasswordBean) {
-        ResponseResult<String> responseResult = new ResponseResult<String>();
-        responseResult.setMessage("更新失败");
-
-        PasswordBean clientBean;
-        try {
-            //bean = JSON.parseObject(password_bean, PasswordBean.class);
-            clientBean = JSON.parseObject(mixedPasswordBean, PasswordBean.class);
-        } catch (Exception e) {
-            e.printStackTrace();
-            return JSON.toJSONString(responseResult);
-        }
-
-        // 发送方的用户和该记事的所有者不是同一个人,没有权限
-        PasswordBean oldBean = passwordRepository.findPasswordBeanById(clientBean.getId());
-        if (!TextUtils.equals(email, oldBean.getUserId())) {
-            return JSON.toJSONString(responseResult);
-        }
-
-
-        try {
-            passwordRepository.updatePasswordBean(clientBean.getPlatform(), clientBean.getUrl(), clientBean.getAccount(),
-                    clientBean.getPassword(), clientBean.getDescription(), clientBean.getId(), System.currentTimeMillis());
-        } catch (Exception e) {
-            e.printStackTrace();
-            return JSON.toJSONString(responseResult);
-        }
-
-        /*try {
-            responseResult.setResultObj(SecureManager.getInstance().encrypt(JSON.toJSONString(dbPasswordBean)));
-        } catch (Exception e) {
-            e.printStackTrace();
-        }*/
-
-        responseResult.setCode(ResultCode.SUCCESS);
-        responseResult.setMessage("更新成功");
-        return JSON.toJSONString(responseResult);
-    }
-}

+ 42 - 0
src/main/java/com/miekir/shibei/repository/CouponRepository.java

@@ -0,0 +1,42 @@
+package com.miekir.shibei.repository;
+
+import com.miekir.shibei.bean.CouponBean;
+import com.miekir.shibei.bean.GoodsBean;
+import org.springframework.data.jpa.repository.JpaRepository;
+import org.springframework.data.jpa.repository.Query;
+import org.springframework.data.repository.query.Param;
+import org.springframework.stereotype.Repository;
+import org.springframework.transaction.annotation.Transactional;
+
+/**
+ * Created by sjj on 2015/10/24 0024.
+ */
+// 添加注解
+@Repository
+public interface CouponRepository extends JpaRepository<CouponBean, Integer> {
+    // 根据密码Id查询密码记事
+    // 说明该方法是事务性操作
+    @Transactional
+    // @Param注解用于提取参数
+    @Query(value="select couponBean from CouponBean couponBean where couponBean.id=:id")
+    public CouponBean findCouponById(@Param("id") Long id);
+
+
+
+//    @Transactional
+//    // 根据邮件查询用户所有密码记事
+//    // @Param注解用于提取参数
+//    @Query(value="select note from NoteBean note where note.userId=:email order by note.updateTime desc")
+//    public List<NoteBean> findNotesByEmail(@Param("email") String email);
+//
+
+
+
+//    // 修改密码记事
+//    @Modifying
+//    // 说明该方法是事务性操作
+//    @Transactional
+//    // @Param注解用于提取参数
+//    @Query("update NoteBean note set note.content=:content, note.updateTime=:updateTime where note.id=:noteId")
+//    public int updateNoteBean(@Param("content") String content, @Param("noteId") Integer noteId, @Param("updateTime") Long updateTime);
+}

+ 13 - 7
src/main/java/com/miekir/shibei/repository/GoodsRepository.java

@@ -7,26 +7,32 @@ import org.springframework.data.repository.query.Param;
 import org.springframework.stereotype.Repository;
 import org.springframework.transaction.annotation.Transactional;
 
+import java.util.List;
+
 /**
  * Created by sjj on 2015/10/24 0024.
  */
 // 添加注解
 @Repository
 public interface GoodsRepository extends JpaRepository<GoodsBean, Integer> {
-    // 说明该方法是事务性操作
-//    @Transactional
-//    // 根据邮件查询用户所有密码记事
-//    // @Param注解用于提取参数
-//    @Query(value="select note from NoteBean note where note.userId=:email order by note.updateTime desc")
-//    public List<NoteBean> findNotesByEmail(@Param("email") String email);
-//
     // 根据密码Id查询密码记事
     // 说明该方法是事务性操作
     @Transactional
     // @Param注解用于提取参数
     @Query(value="select goodsBean from GoodsBean goodsBean where goodsBean.id=:id")
     public GoodsBean findGoodsById(@Param("id") Long id);
+
+
+
+//    @Transactional
+//    // 根据邮件查询用户所有密码记事
+//    // @Param注解用于提取参数
+//    @Query(value="select note from NoteBean note where note.userId=:email order by note.updateTime desc")
+//    public List<NoteBean> findNotesByEmail(@Param("email") String email);
 //
+
+
+
 //    // 修改密码记事
 //    @Modifying
 //    // 说明该方法是事务性操作

+ 0 - 42
src/main/java/com/miekir/shibei/repository/PasswordRepository.java

@@ -1,42 +0,0 @@
-package com.miekir.shibei.repository;
-
-import com.miekir.shibei.bean.PasswordBean;
-import org.springframework.data.jpa.repository.JpaRepository;
-import org.springframework.data.jpa.repository.Modifying;
-import org.springframework.data.jpa.repository.Query;
-import org.springframework.data.repository.query.Param;
-import org.springframework.stereotype.Repository;
-import org.springframework.transaction.annotation.Transactional;
-
-import java.util.List;
-
-/**
- * Created by sjj on 2015/10/24 0024.
- */
-// 添加注解
-@Repository
-public interface PasswordRepository extends JpaRepository<PasswordBean, Integer> {
-    // 说明该方法是事务性操作
-    @Transactional
-    // 根据邮件查询用户所有密码记事
-    // @Param注解用于提取参数
-    @Query(value="select password from PasswordBean password where password.userId=:email order by password.updateTime desc")
-    public List<PasswordBean> findPasswordNotesByEmail(@Param("email") String email);
-
-    // 根据密码Id查询密码记事
-    // 说明该方法是事务性操作
-    @Transactional
-    // @Param注解用于提取参数
-    @Query(value="select password from PasswordBean password where password.id=:id")
-    public PasswordBean findPasswordBeanById(@Param("id") Integer id);
-
-    // 修改密码记事
-    @Modifying
-    // 说明该方法是事务性操作
-    @Transactional
-    // @Param注解用于提取参数
-    @Query("update PasswordBean pass set pass.platform=:platform, pass.url=:url, pass.account=:account, pass.password=:password, pass.description=:description, pass.updateTime=:updateTime where pass.id=:passId")
-    public int updatePasswordBean(@Param("platform") String platform, @Param("url") String url, @Param("account") String account,
-                           @Param("password") String password, @Param("description") String description, @Param("passId") Integer passId,
-                           @Param("updateTime") Long updateTime);
-}