package com.controller;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Map;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import com.utils.ValidatorUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.annotation.IgnoreAuth;
import com.entity.ShangpinxinxiEntity;
import com.entity.view.ShangpinxinxiView;
import com.service.ShangpinxinxiService;
import com.service.TokenService;
import com.utils.PageUtils;
import com.utils.R;
import com.utils.MD5Util;
import com.utils.MPUtil;
import com.utils.CommonUtil;
import java.io.IOException;
import com.service.StoreupService;
import com.entity.StoreupEntity;
@RestController
@RequestMapping("/shangpinxinxi")
public class ShangpinxinxiController {
@Autowired
private ShangpinxinxiService shangpinxinxiService;
@Autowired
private StoreupService storeupService;
@RequestMapping("/page")
public R page(@RequestParam Map<String, Object> params, ShangpinxinxiEntity shangpinxinxi,
@RequestParam(required = false) Double pricestart,
@RequestParam(required = false) Double priceend, HttpServletRequest request) {
String tableName = request.getSession().getAttribute("tableName").toString();
if (tableName.equals("shangjia")) {
shangpinxinxi.setShangpubianhao((String) request.getSession().getAttribute("username"));
}
EntityWrapper<ShangpinxinxiEntity> ew = new EntityWrapper<ShangpinxinxiEntity>();
if (pricestart != null) ew.ge("price", pricestart);
if (priceend != null) ew.le("price", priceend);
PageUtils page = shangpinxinxiService.queryPage(params, MPUtil.sort(MPUtil.between(MPUtil.likeOrEq(ew, shangpinxinxi), params), params));
return R.ok().put("data", page);
}
@IgnoreAuth
@RequestMapping("/list")
public R list(@RequestParam Map<String, Object> params, ShangpinxinxiEntity shangpinxinxi,
@RequestParam(required = false) Double pricestart,
@RequestParam(required = false) Double priceend, HttpServletRequest request) {
EntityWrapper<ShangpinxinxiEntity> ew = new EntityWrapper<ShangpinxinxiEntity>();
if (pricestart != null) ew.ge("price", pricestart);
if (priceend != null) ew.le("price", priceend);
PageUtils page = shangpinxinxiService.queryPage(params, MPUtil.sort(MPUtil.between(MPUtil.likeOrEq(ew, shangpinxinxi), params), params));
return R.ok().put("data", page);
}
@RequestMapping("/lists")
public R list(ShangpinxinxiEntity shangpinxinxi) {
EntityWrapper<ShangpinxinxiEntity> ew = new EntityWrapper<ShangpinxinxiEntity>();
ew.allEq(MPUtil.allEQMapPre(shangpinxinxi, "shangpinxinxi"));
return R.ok().put("data", shangpinxinxiService.selectListView(ew));
}
@RequestMapping("/query")
public R query(ShangpinxinxiEntity shangpinxinxi) {
EntityWrapper<ShangpinxinxiEntity> ew = new EntityWrapper<ShangpinxinxiEntity>();
ew.allEq(MPUtil.allEQMapPre(shangpinxinxi, "shangpinxinxi"));
ShangpinxinxiView shangpinxinxiView = shangpinxinxiService.selectView(ew);
return R.ok("查询商品信息成功").put("data", shangpinxinxiView);
}
@RequestMapping("/info/{id}")
public R info(@PathVariable("id") Long id) {
ShangpinxinxiEntity shangpinxinxi = shangpinxinxiService.selectById(id);
shangpinxinxi.setClicknum(shangpinxinxi.getClicknum() + 1);
shangpinxinxi.setClicktime(new Date());
shangpinxinxiService.updateById(shangpinxinxi);
return R.ok().put("data", shangpinxinxi);
}
@IgnoreAuth
@RequestMapping("/detail/{id}")
public R detail(@PathVariable("id") Long id) {
ShangpinxinxiEntity shangpinxinxi = shangpinxinxiService.selectById(id);
shangpinxinxi.setClicknum(shangpinxinxi.getClicknum() + 1);
shangpinxinxi.setClicktime(new Date());
shangpinxinxiService.updateById(shangpinxinxi);
return R.ok().put("data", shangpinxinxi);
}
@RequestMapping("/thumbsup/{id}")
public R vote(@PathVariable("id") String id, String type) {
ShangpinxinxiEntity shangpinxinxi = shangpinxinxiService.selectById(id);
if (type.equals("1")) {
shangpinxinxi.setThumbsupnum(shangpinxinxi.getThumbsupnum() + 1);
} else {
shangpinxinxi.setCrazilynum(shangpinxinxi.getCrazilynum() + 1);
}
shangpinxinxiService.updateById(shangpinxinxi);
return R.ok("投票成功");
}
@RequestMapping("/save")
public R save(@RequestBody ShangpinxinxiEntity shangpinxinxi, HttpServletRequest request) {
shangpinxinxi.setId(new Date().getTime() + new Double(Math.floor(Math.random() * 1000)).longValue());
shangpinxinxiService.insert(shangpinxinxi);
return R.ok();
}
@RequestMapping("/add")
public R add(@RequestBody ShangpinxinxiEntity shangpinxinxi, HttpServletRequest request) {
shangpinxinxi.setId(new Date().getTime() + new Double(Math.floor(Math.random() * 1000)).longValue());
shangpinxinxiService.insert(shangpinxinxi);
return R.ok();
}
@RequestMapping("/update")
@Transactional
public R update(@RequestBody ShangpinxinxiEntity shangpinxinxi, HttpServletRequest request) {
shangpinxinxiService.updateById(shangpinxinxi);
return R.ok();
}
@RequestMapping("/delete")
public R delete(@RequestBody Long[] ids) {
shangpinxinxiService.deleteBatchIds(Arrays.asList(ids));
return R.ok();
}
@IgnoreAuth
@RequestMapping("/autoSort")
public R autoSort(@RequestParam Map<String, Object> params, ShangpinxinxiEntity shangpinxinxi,
HttpServletRequest request, String pre) {
EntityWrapper<ShangpinxinxiEntity> ew = new EntityWrapper<ShangpinxinxiEntity>();
Map<String, Object> newMap = new HashMap<String, Object>();
Map<String, Object> param = new HashMap<String, Object>();
Iterator<Map.Entry<String, Object>> it = param.entrySet().iterator();
while (it.hasNext()) {
Map.Entry<String, Object> entry = it.next();
String key = entry.getKey();
String newKey = entry.getKey();
if (pre.endsWith(".")) {
newMap.put(pre + newKey, entry.getValue());
} else if (StringUtils.isEmpty(pre)) {
newMap.put(newKey, entry.getValue());
} else {
newMap.put(pre + "." + newKey, entry.getValue());
}
}
params.put("sort", "clicknum");
params.put("order", "desc");
PageUtils page = shangpinxinxiService.queryPage(params, MPUtil.sort(MPUtil.between(MPUtil.likeOrEq(ew, shangpinxinxi), params), params));
return R.ok().put("data", page);
}
}