package cn.exrick.xboot.your.controller;
import cn.exrick.xboot.core.common.utils.PageUtil;
import cn.exrick.xboot.core.common.utils.ResultUtil;
import cn.exrick.xboot.core.common.vo.PageVo;
import cn.exrick.xboot.core.common.vo.Result;
import cn.exrick.xboot.your.entity.Area;
import cn.exrick.xboot.your.entity.Car;
import cn.exrick.xboot.your.service.IAreaService;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.transaction.annotation.Transactional;
import java.util.ArrayList;
import java.util.List;
/**
* @author zhangzeli
*/
@Slf4j
@RestController
@Api(tags = "配送片区管理接口")
@RequestMapping("/xboot/area")
@Transactional
public class AreaController {
@Autowired
private IAreaService iAreaService;
@RequestMapping(value = "/get/{id}", method = RequestMethod.GET)
@ApiOperation(value = "通过id获取")
public Result get(@PathVariable String id) {
Area area = iAreaService.getById(id);
return new ResultUtil().setData(area);
}
@RequestMapping(value = "/getAll", method = RequestMethod.GET)
@ApiOperation(value = "获取全部数据")
public Result> getAll() {
QueryWrapper wrapper = new QueryWrapper<>();
wrapper.orderByAsc("name");
List list = iAreaService.list();
return new ResultUtil>().setData(list);
}
@RequestMapping(value = "/getAllAreas", method = RequestMethod.GET)
@ApiOperation(value = "获取所有区域数据")
public Result> getAllAreas(){
QueryWrapper wrapper = new QueryWrapper<>();
wrapper.orderByAsc("name");
List names = new ArrayList<>();
List list = iAreaService.list();
for (Area area:list ) {
String name = area.getName();
/* int wz = name.indexOf("0");
if(wz>0){
name = name.substring(0,wz);
}*/
name = name
.replace("01车","")
.replace("02车","")
.replace("03车","")
.replace("04车","")
.replace("05车","")
.replace("06车","")
.replace("07车","")
.replace("08车","")
.replace("09车","")
.replace("10车","")
.replace("11车","")
.replace("12车","")
.replace("13车","")
.replace("14车","")
.replace("15车","");
name = name
.replace("01","")
.replace("02","")
.replace("03","")
.replace("04","")
.replace("05","")
.replace("06","")
.replace("07","")
.replace("08","")
.replace("09","")
.replace("10","")
.replace("11","")
.replace("12","")
.replace("13","")
.replace("14","")
.replace("15","");
if(!names.contains(name)){
names.add(name);
}
}
return new ResultUtil>().setData(names);
}
@RequestMapping(value = "/getByPage", method = RequestMethod.GET)
@ApiOperation(value = "分页获取")
public Result> getByPage(String name,PageVo page) {
QueryWrapper wrapper = new QueryWrapper<>();
if (!StrUtil.isEmpty(name))
wrapper.like("a.name",name);
IPage data = iAreaService.page2(PageUtil.initMpPage(page),wrapper);
return new ResultUtil>().setData(data);
}
@RequestMapping(value = "/insertOrUpdate", method = RequestMethod.POST)
@ApiOperation(value = "编辑或更新数据")
public Result saveOrUpdate(Area area) {
QueryWrapper wrapper = new QueryWrapper<>();
if (StrUtil.isNotEmpty(area.getCarId())){
wrapper.eq("car_id",area.getCarId());
Area area1 = iAreaService.getOne(wrapper);
if (area1 != null && !area.getId().equals(area1.getId())){
return new ResultUtil().setErrorMsg("该车辆已被其它片区绑定,请先解绑");
}
}
if (iAreaService.saveOrUpdate(area)) {
return new ResultUtil().setData(area);
}
return new ResultUtil().setErrorMsg("操作失败");
}
@RequestMapping(value = "/delByIds", method = RequestMethod.POST)
@ApiOperation(value = "批量通过id删除")
public Result