0
点赞
收藏
分享

微信扫一扫

配置环境问题

删除收货地址功能及展示商品热销排行功能的实现

删除收货地址

(一)删除收货地址(持久层)

1.规划sql

delete from t_address where aid=?
select * from t_address where uid=? order by modified_time DESC limit 0,1

其中limit 0,1表示查询到的第一条数据(limit (n-1),pageSize),这样查询后就只会获得第一条数据

2.设计接口和抽象方法

//删除地址
    Integer deleteByAid(Integer aid);
//查找最后修改的一条收货地址
  Address findLastModifedTime(Integer uid);
    <delete id="deleteByAid">
delete from t_address where aid=#{aid}
    </delete>
    <select id="findLastModifedTime" resultMap="AddressEntityMap">

select * from t_address where uid=#{uid} order by modified_time desc limit 0,1
    </select>

(二)删除收货地址(业务层)

1.规划异常

/**删除数据时产生的异常*/
public class DeleteException extends ServiceException{
    /**重写ServiceException的所有构造方法*/
}

2.设计接口和抽象方法及实现

根据分析可得,该抽象方法的实现依赖于持久层的以下方法:
1.findByAid:查询该条地址数据是否存在,参数是aid
2.判断地址数据归属是否正确
3.deleteByAid:删除地址数据,参数是aid
4.判断删除的是否是默认地址
5.countByUid:统计用户地址数量,参数是uid
6.findLastModified:查询得到最后修改的一条地址,参数是uid
7.updateDefaultByAid:设置默认收货地址,参数是aid,modifiedUser,modifiedTime

稍加分析可以得出接下来定义的抽象方法的参数是:aid,uid,username

/**
* 删除用户选中的收货地址数据
* @param aid 收货地址id
* @param uid 用户id
* @param username 用户名
*/
void delete(Integer aid,Integer uid,String username);

    @Override
    public void deleteAddress(Integer aid, Integer uid, String username) {
        Address aid1 = addressMapper.findByAid(aid);
        if (aid1==null){
            throw new AddressNotFoundException("收货地址不存在");
        }
        if (!aid1.getUid().equals(uid)){
            throw new AccessDeniedException("非法数据访问");
        }
        //判断是否为默认地址
        Integer deleteByAid = addressMapper.deleteByAid(aid);
        if (deleteByAid!=1){
            throw new DeleteException("删除收货地址时出现错误");
        }
        if (addressMapper.countByUid(uid)==0){
            return;
        }
if (aid1.getIsDefault()==1){

    Address address = addressMapper.findLastModifedTime(uid);
    Integer integer = addressMapper.updateDefaultByAid(address.getAid(), username, new Date());
    if (integer!=1){
        throw new UpdateException("设置默认地址时产生异常");
    }
}
    }

(三)删除收货地址(控制层)

1.处理异常

else if (e instanceof DeleteException) {
    result.setState(5002);
    result.setMessage("删除数据时产生未知的异常");
}

2.设计请求

3.处理请求

@PostMapping("{aid}/delete")
    public JsonResult<Void> deleteAddress(@PathVariable("aid") Integer aid,HttpSession session){
    String getusernamesession = getusernamesession(session);
    Integer getuidfromsession = getuidfromsession(session);
    addressService.deleteAddress(aid,getuidfromsession,getusernamesession);
    return new JsonResult<>(ok);
}

(四)删除收货地址(前端页面)

<td><a onclick="deleteByAid(#{aid})" class="btn btn-xs add-del btn-info"><span class="fa fa-trash-o"></span> 删除</a></td>

 function deleteByAid(aid){
				  $.ajax({
					  url: "/addresses/"+aid+"/delete",
					  type: "POST",
					  dataType: "JSON",
					  success: function (json) {
						  if (json.state == 200) {
							  //重新加载收货地址列表页面
							  showAddressList();
						  } else {
							  alert("删除收货地址失败")
						  }
					  },
					  error: function (xhr) {
						  alert("删除收货地址时产生未知的异常!"+xhr.message);
					  }
				  });
			  }

商品热销排行

(一)创建数据表

CREATE TABLE t_product (
  id int(20) NOT NULL COMMENT '商品id',
  category_id int(20) DEFAULT NULL COMMENT '分类id',
  item_type varchar(100) DEFAULT NULL COMMENT '商品系列',
  title varchar(100) DEFAULT NULL COMMENT '商品标题',
  sell_point varchar(150) DEFAULT NULL COMMENT '商品卖点',
  price bigint(20) DEFAULT NULL COMMENT '商品单价',
  num int(10) DEFAULT NULL COMMENT '库存数量',
  image varchar(500) DEFAULT NULL COMMENT '图片路径',
  `status` int(1) DEFAULT '1' COMMENT '商品状态  1:上架   2:下架   3:删除',
  priority int(10) DEFAULT NULL COMMENT '显示优先级',
  created_time datetime DEFAULT NULL COMMENT '创建时间',
  modified_time datetime DEFAULT NULL COMMENT '最后修改时间',
  created_user varchar(50) DEFAULT NULL COMMENT '创建人',
  modified_user varchar(50) DEFAULT NULL COMMENT '最后修改人',
  PRIMARY KEY (id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

LOCK TABLES t_product WRITE;
INSERT INTO t_product VALUES (10000001,238,'牛皮纸记事本','广博(GuangBo)10本装40张A5牛皮纸记事本子日记本办公软抄本GBR0731','经典回顾!超值特惠!',23,99999,'/images/portal/00GuangBo1040A5GBR0731/',1,62,'2017-10-25 15:08:55','2017-10-25 15:08:55','admin','admin'),等等等等;
UNLOCK TABLES;

(三)创建商品的实体类

@Data
@AllArgsConstructor
@NoArgsConstructor
public class Product extends baseEntity implements Serializable {
    private Integer id;
    private Integer categoryId;
    private String itemType;
    private String title;
    private String sellPoint;
    private Long price;
    private Integer num;
    private String image;
    private Integer status;
    private Integer priority;

}

(四)商品热销排行(持久层)

1.规划sql

SELECT * FROM t_product WHERE status=1 ORDER BY priority DESC LIMIT 0,4

2.设计接口和抽象方法

public interface ProductMapper {
    List<Product> findHotList();
}
<mapper namespace="com.cy.store.mapper.ProductMapper">
    <resultMap id="ProductEntityMap" type="com.cy.store.entity.Product">
        <id column="id" property="id"/>
        <result column="category_id" property="categoryId"/>
        <result column="item_type" property="itemType"/>
        <result column="sell_point" property="sellPoint"/>
        <result column="created_user" property="createdUser"/>
        <result column="created_time" property="createdTime"/>
        <result column="modified_user" property="modifiedUser"/>
        <result column="modified_time" property="modifiedTime"/>
    </resultMap>

<select id="findHotList" resultMap="ProductEntityMap">
    select *
    from t_product where status=1 order by priority desc limit 0,4;
</select>

(五)商品热销排行(业务层)

1.规划异常

2.设计接口和抽象方法及实现

public interface IProductService {
List<Product> findHotList();
}

@Service
public class ProductServiceImpl implements IProductService {
    @Resource
    private ProductMapper productMapper;

    @Override
    public List<Product> findHotList() {
        List<Product> hotList = productMapper.findHotList();
        for (Product product : hotList) {
            product.setPriority(null);
            product.setCreatedUser(null);
            product.setCreatedTime(null);
            product.setModifiedUser(null);
            product.setModifiedTime(null);
        }
        return hotList;

    }
}

(六)商品热销排行(控制层)

1.处理异常

2.设计请求

3.处理请求

@RequestMapping("products")
@RestController
public class ProductController extends BaseController{
@Autowired
private IProductService productService;

@GetMapping("hot_list")
    public JsonResult<List<Product>> findHotList(){
    List<Product> hotList = productService.findHotList();
    return new JsonResult<>(ok,hotList);
}
patterns.add("/products/**");

(七)商品热销排行(前端页面)

<div id="hot-list" class="panel-body panel-item">
	<!-- ... -->
</div>

<script type="text/javascript">
    $(document).ready(function() {
    showHotList();
});

function showHotList() {
    $("#hot-list").empty();
    $.ajax({
        url: "/products/hot_list",
        type: "GET",
        dataType: "JSON",
        success: function(json) {
            var list = json.data;
            for (var i = 0; i < list.length; i++) {
                console.log(list[i].title);//调试用
                var html = '<div class="col-md-12">'
                + '<div class="col-md-7 text-row-2"><a href="product.html?id=#{id}">#{title}</a></div>'
                + '<div class="col-md-2">¥#{price}</div>'
                + '<div class="col-md-3"><img src="..#{image}collect.png" class="img-responsive" /></div>'
                + '</div>';

                html = html.replace(/#{id}/g, list[i].id);
                html = html.replace(/#{title}/g, list[i].title);
                html = html.replace(/#{price}/g, list[i].price);
                html = html.replace(/#{image}/g, list[i].image);

                $("#hot-list").append(html);
            }
        }
    });
}
</script>

后记
👉👉💕💕美好的一天,到此结束,下次继续努力!欲知后续,请看下回分解,写作不易,感谢大家的支持!! 🌹🌹🌹

举报

相关推荐

0 条评论