1 店铺注册
1.1 js 文件
$ ( function ( ) {
var initUrl = '/o2o/shopadmin/getshopinitinfo' ;
var registerShopUrl = 'o2o/shopadmin/registershop' ;
alert ( initUrl) ;
getShopInitInfo ( ) ;
function getShopInitInfo ( ) {
$. getJSON ( initUrl, function ( data) {
if ( data. success) {
var tempHtml = '' ;
var tempAreaHtml = '' ;
data. shopCategoryList. map ( function ( item, index) {
tempHtml += '+ item. shopCategoryId + '">'
+ item. shopCategoryName + '' ;
} ) ;
data. areaList. map ( function ( item, index) {
tempAreaHtml += '+ item. areaId + '">'
+ item. areaName + '' ;
}
) ;
$ ( '#shop-category' ) . html ( tempHtml) ;
$ ( '#area' ) . html ( tempAreaHtml) ;
}
} ) ;
$ ( '#submit' ) . click ( function ( ) {
var shop = {
} ;
shop. shopNamme = $ ( '#shop-name' ) . val ( ) ;
shop. shopAddr = $ ( '#shop-addr' ) . val ( ) ;
shop. phone = $ ( '#shop-phone' ) . val ( ) ;
shop. desc = $ ( '#shop-desc' ) . val ( ) ;
shop. shopCategory = {
shopCategoryId: $ ( '#shop-category' ) . find ( 'option' ) . not ( function ( ) {
return ! this . selected;
} ) . data ( 'id' )
} ;
shop. area = {
areaId: $ ( '#area' ) . find ( 'option' ) . not ( function ( ) {
return ! this . selected;
} ) . data ( 'id' )
} ;
var shopImg = $ ( '#shop-img' ) [ 0 ] . files[ 0 ] ;
var formData = new formData ( ) ;
formData. append ( 'shopImg' , shopImg) ;
formData. append ( 'shopStr' , JSON . stringify ( shop) ) ;
$. ajax ( {
url: registerShopUrl,
type: 'POST' ,
data: formData,
contentType: false ,
processData: false ,
cache: false ,
success: function ( data) {
if ( data. success ( ) ) {
$. toast ( '提交成功!' ) ;
} else {
$. toast ( '提交失败!' + data. errMsg) ;
}
}
} ) ;
} ) ;
}
} )
1.2 引入 js
2 店铺类别-ShopCategoryDao
package com. tzb. o2o. dao;
import com. tzb. o2o. entity. ShopCategory;
import org. apache. ibatis. annotations. Param;
import java. util. List;
public interface ShopCategoryDao {
List< ShopCategory> queryShopCategory (
@Param ( "shopCategoryCondition" ) ShopCategory shopCategoryCondition) ;
}
< mapper namespace = " com.tzb.o2o.dao.ShopCategoryDao" >
< select id = " queryShopCategory" resultType = " com.tzb.o2o.entity.ShopCategory" >
SELECT
shop_category_id,
shop_category_name,
shop_category_desc,
shop_category_img,
priority,
create_time,
last_edit_time,
parent_id
FROM
tb_shop_category
< where>
< if test = " shopCategoryCondition.parent!=null" >
and parent_id = #{shopCategoryCondition.parent.shopCategoryId}
if>
where>
ORDER BY
priority DESC
select>
mapper>
2.1 店铺类别dao 测试
package com. tzb. o2o. dao;
import com. tzb. o2o. BaseTest;
import com. tzb. o2o. entity. ShopCategory;
import org. junit. Test;
import org. springframework. beans. factory. annotation. Autowired;
import java. util. List;
import static org. junit. Assert. assertEquals;
public class ShopCategoryDaoTest extends BaseTest {
@Autowired
private ShopCategoryDao shopCategoryDao;
@Test
public void testQueryShopCategory ( ) {
List< ShopCategory> shopCategoryList = shopCategoryDao. queryShopCategory ( new ShopCategory ( ) ) ;
assertEquals ( 2 , shopCategoryList. size ( ) ) ;
ShopCategory testCategory = new ShopCategory ( ) ;
ShopCategory parentCategory = new ShopCategory ( ) ;
parentCategory. setShopCategoryId ( 1 L) ;
testCategory. setParent ( parentCategory) ;
shopCategoryList = shopCategoryDao. queryShopCategory ( testCategory) ;
System. out. println ( shopCategoryList) ;
}
}
3 店铺类别 Service
package com. tzb. o2o. service;
import com. tzb. o2o. entity. ShopCategory;
import java. util. List;
public interface IShopCategoryService {
List< ShopCategory> getShopCategoryList ( ShopCategory shopCategoryCondition) ;
}
package com. tzb. o2o. service. impl;
import com. tzb. o2o. dao. ShopCategoryDao;
import com. tzb. o2o. entity. ShopCategory;
import com. tzb. o2o. service. IShopCategoryService;
import org. springframework. beans. factory. annotation. Autowired;
import org. springframework. stereotype. Service;
import java. util. List;
@Service
public class ShopCatergoryServiceImpl implements IShopCategoryService {
@Autowired
private ShopCategoryDao shopCategoryDao;
@Override
public List< ShopCategory> getShopCategoryList ( ShopCategory shopCategoryCondition) {
return shopCategoryDao. queryShopCategory ( shopCategoryCondition) ;
}
}
4 修改店铺管理 Controller
@Controller
@RequestMapping ( "shopadmin" )
public class ShopManagementController {
@Autowired
private ShopService shopService;
@Autowired
private IShopCategoryService shopCategoryService;
@Autowired
private AreaService areaService;
@RequestMapping ( value = "/getshopinitinfo" , method = RequestMethod. GET)
@ResponseBody
private Map< String, Object> getShopInitInfo ( ) {
Map< String, Object> modelMap = new HashMap < > ( ) ;
List< ShopCategory> shopCategoryList = new ArrayList < > ( ) ;
List< Area> areaList = new ArrayList < > ( ) ;
try {
shopCategoryList = shopCategoryService. getShopCategoryList ( new ShopCategory ( ) ) ;
areaList = areaService. getAreaList ( ) ;
modelMap. put ( "shopCategoryList" , shopCategoryList) ;
modelMap. put ( "areaList" , areaList) ;
modelMap. put ( "success" , true ) ;
} catch ( Exception e) {
modelMap. put ( "success" , false ) ;
modelMap. put ( "errMsg" , e. getMessage ( ) ) ;
}
return modelMap;
}
@RequestMapping ( value = "registershop" , method = RequestMethod. POST)
@ResponseBody
private Map< String, Object> registerShop ( HttpServletRequest request) {
Map< String, Object> modelMap = new HashMap < > ( ) ;
String shopStr = HttpServletRequestUtil. getString ( request, "shopStr" ) ;
ObjectMapper mapper = new ObjectMapper ( ) ;
Shop shop = null;
try {
shop = mapper. readValue ( shopStr, Shop. class ) ;
} catch ( Exception e) {
modelMap. put ( "success" , false ) ;
modelMap. put ( "errMsg" , e. getMessage ( ) ) ;
return modelMap;
}
CommonsMultipartFile shopImg = null;
CommonsMultipartResolver commonsMultipartResolver = new CommonsMultipartResolver (
request. getSession ( ) . getServletContext ( )
) ;
if ( commonsMultipartResolver. isMultipart ( request) ) {
MultipartHttpServletRequest multipartHttpServletRequest = ( MultipartHttpServletRequest) request;
shopImg = ( CommonsMultipartFile) multipartHttpServletRequest. getFile ( "shopImg" ) ;
} else {
modelMap. put ( "success" , false ) ;
modelMap. put ( "errMsg" , "上传图片不能为空" ) ;
return modelMap;
}
if ( null != shop && null != shopImg) {
PersonInfo owner = new PersonInfo ( ) ;
owner. setUserId ( 1 L) ;
shop. setOwner ( owner) ;
ShopExecution se = null;
try {
se = shopService. addShop ( shop, shopImg. getInputStream ( ) , shopImg. getOriginalFilename ( ) ) ;
if ( se. getState ( ) == ShopStateEnum. CHECK. getState ( ) ) {
modelMap. put ( "success" , true ) ;
} else {
modelMap. put ( "success" , false ) ;
modelMap. put ( "errMsg" , se. getStateInfo ( ) ) ;
}
} catch ( IOException e) {
modelMap. put ( "success" , false ) ;
modelMap. put ( "errMsg" , e. getMessage ( ) ) ;
return modelMap;
}
return modelMap;
} else {
modelMap. put ( "success" , false ) ;
modelMap. put ( "errMsg" , "请输入店铺信息" ) ;
return modelMap;
}
}
}
5 测试
http://localhost:8080/o2o/shopadmin/shopoperation