1.

package selleck.web.cart;

import java.io.InputStream;
import java.math.BigDecimal;
import java.sql.Timestamp;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Set; import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession; import lombok.Getter;
import lombok.Setter;
import org.apache.log4j.Logger;
import org.apache.struts2.ServletActionContext; import selleck.Core.util.Constants;
import selleck.Core.util.MD5Maker;
import selleck.Core.web.SelleckActionSupport;
import selleck.model.attachment.dao.imp.AttachmentDAOImp;
import selleck.model.cart.dto.CartDTO;
import selleck.model.cart.service.CartService;
import selleck.model.country.dto.CountryDTO;
import selleck.model.country.service.CountryService;
import selleck.model.product.dto.ProductDTO;
import selleck.model.product.dto.SizeDTO;
import selleck.model.product.service.ProductService;
import selleck.model.product.service.SizeService;
import selleck.model.user.dto.UserDTO; import com.opensymphony.xwork2.ActionContext;
@Setter @Getter
public class AddCartAction extends SelleckActionSupport {
private static Logger logger = Logger.getLogger(AddCartAction.class);
private Double shipFee;
private Double shipFeeJudge;
private CartService cartService;
private CountryService countryService;
private ProductService productService;
private SizeService sizeService;
private List<CartDTO> cartDTOs;
private CountryDTO countryDTO;
private InputStream is; public String addCart(){
HttpServletRequest request = ServletActionContext.getRequest();
HttpServletResponse response = ServletActionContext.getResponse();
HttpSession httpSession = request.getSession();
Map<String, Object> mapSession = ActionContext.getContext().getSession();
shipFee = new Double(request.getServletContext().getInitParameter(Constants.FEE));
shipFeeJudge = new Double(request.getServletContext().getInitParameter(Constants.FEEJUDGE));
UserDTO user = (UserDTO)mapSession.get(Constants.SESSIONID); //1.判断有没有cookie存在
Cookie[] cookies = request.getCookies();
int j = 0;
String sessionId = null;
String countryCode = null;
if ( null != cookies ) {
for (int i = 0, len = cookies.length; i < len; i++) {
Cookie c = cookies[i];
if (c.getName().equals(cookieName)) { //cookieCartId
sessionId = c.getValue();
j++;
}
if (Constants.COUNTRYCODE.equals(c.getName())) {
countryCode = c.getValue();
j++;
}
if ( j >=2 && null != sessionId && null != countryCode) {
break;
}
}
if ( j == 0) {
Cookie cookie = new Cookie(cookieName, httpSession.getId());
cookie.setMaxAge(360 *24*60 *60 *1000);
cookie.setPath("/");
response.addCookie(cookie);
}
} //2. 取国别
if (countryCode == null) countryCode = getDefaultCountryCode();
countryDTO = countryService.findByCode(countryCode);
for (CartDTO cart : cartDTOs) {
if ( null != user) cart.setUserId(user.getId());
else {
if ( null != sessionId){
cart.setSessionId(sessionId);
} else {
cart.setSessionId(httpSession.getId());
}
}
try{
if ( null != cart.getQuantity() && cart.getQuantity() > 0) {
cart.setSizeId(cart.getSizeId());
cart.setCreateTime(new Timestamp(System.currentTimeMillis()));
SizeDTO sizeDTO = sizeService.findById(cart.getSizeId());
Double temprice = sizeDTO.getPrice(); //单价
if (temprice == 0) {logger.error("Product price is zero exception"); return FAIL;}
cart.setUnitPrice(temprice);
cart.setSubTotal(temprice * cart.getQuantity().intValue());
cartService.create(cart);
}
} catch (Exception e) {
logger.error(e.toString());
}
}
if ( null != user) {
cartDTOs = cartService.list(user.getId());
} else {
cartDTOs = cartService.list(httpSession.getId());
}
return SUCCESS;
}
}
05-08 07:50