package cn.mldn.dibmp.web.action.back;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import cn.mldn.dibmp.ccc.service.IGoodsService;
import cn.mldn.dibmp.ccc.service.ISubtypeService;
import cn.mldn.dibmp.ccc.service.IWitemService;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.shiro.SecurityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;
import cn.mldn.dibmp.service.ISaleService;
import cn.mldn.dibmp.vo.Distribution;
import cn.mldn.dibmp.vo.DistributionDetails;
import cn.mldn.dibmp.vo.Goods;
import cn.mldn.dibmp.vo.GoodsNum;
import cn.mldn.util.action.abs.AbstractAction;
import cn.mldn.util.exception.CacheException;
import cn.mldn.util.fastdfs.FastDFS;
import cn.mldn.web.helper.SplitPageHelper;
import cn.mldn.web.util.CastUtil;
import cn.mldn.web.util.RedisUtil;
import cn.mldn.web.util.StringUtil;
import cn.mldn.web.util.WebUtil;
@Controller
@RequestMapping("/pages/back/admin/goods/*")
public class GoodsActionBack extends AbstractAction {
private Logger logger = LoggerFactory.getLogger(GoodsActionBack.class);
private static final String TITLE = "商品出库申请单";
@Resource
private ISaleService saleServicess;
@Resource
private IWitemService witemService;
@Resource
private ISubtypeService subtypeService;
@Resource
private IGoodsService goodsService;
@RequestMapping("add_preGoods")
public ModelAndView addPreGoods() {
ModelAndView mav = new ModelAndView(super.getPage("goods.add.page"));
mav.addObject("allWitem",witemService.list());
return mav;
}
@RequestMapping("listSubtype")
@ResponseBody
public Object findByWiid(long wiid) {
return subtypeService.findByWiid(wiid);
}
@RequestMapping("findByName")
@ResponseBody
public Object findByName(String name) {
return goodsService.findByName(name)!=null;
}
@RequestMapping("addGoods")
public ModelAndView addGoods(Goods goods, MultipartFile pic) {
System.err.println(pic.getSize());
ModelAndView mav = new ModelAndView(super.getPage("forward.page"));
if(pic.getSize()==0) {
goods.setPhoto("group1/M00/00/00/wKgclVoDyeOAJIEHAAA21Ria8C4574.jpg");
}else {
String fileName = FastDFS.upload(pic);
goods.setPhoto(fileName);
}
if(goodsService.add(goods, (String)SecurityUtils.getSubject().getSession().getAttribute("mid"))) {
super.setMsgAndUrl(mav, "goods.add.action", "vo.add.success", TITLE);
}else {
super.setMsgAndUrl(mav, "goods.add.action", "vo.add.failure", TITLE);
}
return mav;
}
@RequestMapping("edit_pre")
public ModelAndView editPre(long gid) {
ModelAndView mav = new ModelAndView(super.getPage("goods.edit.page"));
mav.addObject("Goods", goodsService.editPre(gid));
mav.addObject("allWitem", witemService.list());
mav.addObject("allSubtype",subtypeService.findByWiid(goodsService.editPre(gid).getWiid()));
return mav;
}
@RequestMapping("edit")
public ModelAndView edit(Goods goods,MultipartFile pic) {
ModelAndView mav = new ModelAndView(super.getPage("forward.page"));
if(pic.getSize()>0) {
String fileName = FastDFS.upload(pic);
goods.setPhoto(fileName);
}
if(goodsService.update(goods, (String)SecurityUtils.getSubject().getSession().getAttribute("mid"))) {
super.setMsgAndUrl(mav, "goods.list.action", "vo.edit.success", TITLE);
}else {
super.setMsgAndUrl(mav, "goods.list.action", "vo.edit.failure", TITLE);
}
return mav;
}
@RequestMapping("list")
public ModelAndView list(HttpServletRequest request) {
ModelAndView mav = new ModelAndView(super.getPage("goods.list.page"));
String defaultColumn = "商品名称:name|商品价格:price";
SplitPageHelper sph = new SplitPageHelper(request, super.getPage("goods.list.action"),defaultColumn);
Map<String, Object> map = this.saleServicess.list(sph.getColumn(), sph.getKeyWord(), sph.getCurrentPage(), sph.getLineSize());
mav.addAllObjects(map);
return mav;
}
@RequestMapping("show")
public ModelAndView show(Long gid) {
ModelAndView mav = new ModelAndView(super.getPage("goods.show.page"));
mav.addAllObjects(this.saleServicess.findGoodsDetailsById(gid));
return mav;
}
@RequestMapping("detailSstorage")
public void Details(HttpServletResponse response,Long gid) {
WebUtil.writeJSON(response, this.saleServicess.findAllGoodsStorageDetails(gid));
}
@RequestMapping("storageDetails")
public void storageDetails(HttpServletResponse response,Long gid) {
WebUtil.writeJSON(response, this.saleServicess.findGoodsStorageDetails(gid));
}
@RequestMapping("unStorage")
public void unStorage(HttpServletRequest request,HttpServletResponse response,GoodsNum goodsNum) {
String mid = (String) SecurityUtils.getSubject().getSession().getAttribute("mid");
try {
RedisUtil.put(mid,goodsNum,goodsNum.getGid());
WebUtil.writeJSON(response, "true");
}catch(Exception e) {
logger.error("缓存出现错误了", e);
WebUtil.writeJSON(response, "false");
}
}
/**
* 从Redis中读取商品数据和所对应的联系人
* @param request
* @return
*/
@RequestMapping("unStorageGoods")
public ModelAndView unStorageGoods(HttpServletRequest request) {
ModelAndView mav = new ModelAndView(super.getPage("distribution.goods.list.page"));
String mid = (String) SecurityUtils.getSubject().getSession().getAttribute("mid");
mav.addAllObjects(this.saleServicess.findAllUnStorageGoods(mid));
return mav;
}
/**
* 修改代购商品(修改redis的数据)
* @param response
*/
@RequestMapping("updateUnStorageGoods")
public void updateUnStorageGoods(HttpServletResponse response,GoodsNum goodsNum) {
String mid = (String) SecurityUtils.getSubject().getSession().getAttribute("mid");
try {
if(RedisUtil.get(mid) == null) {
RedisUtil.put(mid, goodsNum,goodsNum.getGid());
WebUtil.writeJSON(response, "true");
}else {
RedisUtil.update(mid, goodsNum,goodsNum.getGid());
WebUtil.writeJSON(response, "true");
}
}catch(CacheException e) {
logger.error("修改代购商品数据",e);
WebUtil.writeJSON(response, "false");
}
}
@RequestMapping("deleteGoods")
public void deleteGoods(HttpServletResponse response,String ids) {
String mid = (String) SecurityUtils.getSubject().getSession().getAttribute("mid");
try {
RedisUtil.del(mid, ids.substring(0, ids.length()-1));
WebUtil.writeJSON(response, "true");
}catch(Exception e) {
logger.error("删除代购商品失败",e);
WebUtil.writeJSON(response, "false");
}
}
@RequestMapping("distributionGoods")
public void distributionGoods(HttpServletResponse response,Distribution distribution) {
String mid = (String) SecurityUtils.getSubject().getSession().getAttribute("mid");
try {
RedisUtil.set(mid, distribution, distribution.getPid());
WebUtil.writeJSON(response, "true");
}catch(Exception e) {
logger.error("创建出库商品失败",e);
WebUtil.writeJSON(response, "false");
}
}
@Requ