LsStationRouteController.java 9.34 KB
package com.bsth.controller;

import com.bsth.common.ResponseCode;
import com.bsth.entity.LsSectionRoute;
import com.bsth.entity.LsStationRoute;
import com.bsth.service.LsStationRouteService;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

import java.util.*;

/**
 * @author Hill
 */
@RestController
@RequestMapping("/api/lsstationroute")
public class LsStationRouteController extends BaseController<LsStationRoute, Integer> {

    private static final Logger log = LoggerFactory.getLogger(LsStationRouteController.class);

    @Autowired
    private LsStationRouteService lsStationRouteService;

    @Autowired
    private ObjectMapper mapper;

    @RequestMapping(value = "/findAllByParams", method = RequestMethod.GET)
    public Iterable<LsStationRoute> findAllByParams(@RequestParam Map<String, Object> params) {
        return lsStationRouteService.findAllByParams(params);
    }

    @RequestMapping(value = "/findPageByParams", method = RequestMethod.GET)
    public Page<LsStationRoute> findPageByParams(@RequestParam Map<String, Object> params) {
        int page = params.get("page") == null ? 0 : Integer.parseInt(params.get("page").toString());
        int size = params.get("size") == null ? 10 : Integer.parseInt(params.get("size").toString());
        String order = params.get("order").toString(), direction = params.get("direction").toString();

        return super.list(params, page, size, order, direction);
    }

    @RequestMapping(value = "/findRoutes" , method = RequestMethod.GET)
    public  Map<String, Object> findRoutes(@RequestParam Map<String, Object> map) {
        map.put("destroy_eq", 0);
        if (map.get("line.id_eq") == null || map.get("versions_eq") == null) {
            throw new IllegalArgumentException("需正确传入线路、版本参数");
        }

        return lsStationRouteService.findRoutes(map);
    }

    @RequestMapping(value = "/findStationRoutes" , method = RequestMethod.GET)
    public  List<LsStationRoute> findStationRoutes(@RequestParam Map<String, Object> map) {
        map.put("destroy_eq", 0);
        if (map.get("line.id_eq") == null || map.get("versions_eq") == null) {
            throw new IllegalArgumentException("需正确传入线路、版本参数");
        }

        return lsStationRouteService.findStationRoutes(map);
    }

    /**
     * @Description :TODO(查询站点的下一个缓存站点)
     */
    @RequestMapping(value = "/findCurrentAndNext" , method = RequestMethod.GET)
    public List<LsStationRoute> findCurrentAndNext(Integer id) {
        return lsStationRouteService.findCurrentAndNext(id);
    }

    /**
     * @param id
     * @throws
     * @Description: TODO(批量撤销站点)
     */
    @RequestMapping(value = "/destroy", method = RequestMethod.POST)
    public Map<String, Object> destroy(Integer id) {
        Map<String, Object> result = new HashMap<>();
        try {
            lsStationRouteService.batchDestroy(Arrays.asList(id));
            result.put("status", ResponseCode.SUCCESS);
        } catch (Exception e) {
            result.put("status", ResponseCode.ERROR);
            result.put("msg", e.getMessage());
            log.error("", e);
        }

        return result;
    }

    /**
     * @param ids
     * @throws
     * @Description: TODO(批量撤销站点)
     */
    @RequestMapping(value = "/batchDestroy", method = RequestMethod.POST)
    public Map<String, Object> batchDestroy(@RequestParam(value = "ids[]") List<Integer> ids) {
        Map<String, Object> result = new HashMap<>();
        try {
            lsStationRouteService.batchDestroy(ids);
            result.put("status", ResponseCode.SUCCESS);
        } catch (Exception e) {
            result.put("status", ResponseCode.ERROR);
            result.put("msg", e.getMessage());
            log.error("", e);
        }

        return result;
    }

    /**
     * @param ids
     * @throws
     * @Description: TODO(批量恢复撤销站点)
     */
    @RequestMapping(value = "/batchRecover", method = RequestMethod.POST)
    public Map<String, Object> batchRecover(@RequestParam(value = "ids[]") List<Integer> ids) {
        Map<String, Object> result = new HashMap<>();
        try {
            lsStationRouteService.batchRecover(ids);
            result.put("status", ResponseCode.SUCCESS);
        } catch (Exception e) {
            result.put("status", ResponseCode.ERROR);
            result.put("msg", e.getMessage());
            log.error("", e);
        }

        return result;
    }

    /**
     * 保存线路某个版本下单行的站点和路段路由
     * 常规使用在根据百度地图生成数据或者模板导入的批量保存
     * @param map
     * @return
     */
    @RequestMapping(value="addRoutes" , method = RequestMethod.POST)
    public Map<String, Object> addRoutes(@RequestBody Map<String, Object> map) {
        Map<String, Object> result = new HashMap<>();
        try {
            if (map.get("lineId") == null || map.get("versions") == null || map.get("directions") == null) {
                throw new IllegalArgumentException("需正确传入线路、方向、版本参数");
            }
            Integer versions = Integer.parseInt(map.get("versions").toString()), directions = Integer.parseInt(map.get("directions").toString()), lineId = Integer.parseInt(map.get("lineId").toString());
            List<LsStationRoute> stationRoutes = mapper.convertValue(map.get("stationRoutes"), mapper.constructType(mapper.getTypeFactory().constructParametricType(List.class, LsStationRoute.class)));
            List<LsSectionRoute> sectionRoutes = mapper.convertValue(map.get("sectionRoutes"), mapper.constructType(mapper.getTypeFactory().constructParametricType(List.class, LsSectionRoute.class)));

            result.putAll(lsStationRouteService.addRoutes(lineId, versions, directions, stationRoutes, sectionRoutes));
            result.put("status", ResponseCode.SUCCESS);
        } catch (Exception e) {
            result.put("status", ResponseCode.ERROR);
            log.error("", e);
        }

        return result;
    }

    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public Map<String, Object> add(LsStationRoute stationRoute) {
        Map<String, Object> result = new HashMap<>();
        try {
            lsStationRouteService.add(stationRoute);
            result.put("status", ResponseCode.SUCCESS);
        } catch (Exception e) {
            result.put("status", ResponseCode.ERROR);
            result.put("msg", e.getMessage());
            log.error("", e);
        }

        return result;
    }

    @RequestMapping(value = "/modify", method = RequestMethod.POST)
    public Map<String, Object> modify(LsStationRoute stationRoute) {
        Map<String, Object> result = new HashMap<>();
        try {
            lsStationRouteService.modify(stationRoute);
            result.put("status", ResponseCode.SUCCESS);
        } catch (Exception e) {
            result.put("status", ResponseCode.ERROR);
            result.put("msg", e.getMessage());
            log.error("", e);
        }

        return result;
    }

    @RequestMapping(value = "/exchangeDirection", method = RequestMethod.POST)
    public Map<String, Object> exchangeDirection(int lineId, int version) {
        Map<String, Object> result = new HashMap<>();
        try {
            lsStationRouteService.exchangeDirection(lineId, version);
            result.put("status", ResponseCode.SUCCESS);
        } catch (Exception e) {
            result.put("status", ResponseCode.ERROR);
            result.put("msg", e.getMessage());
            log.error("", e);
        }

        return result;
    }

    @RequestMapping(value = "/modifyDistance", method = RequestMethod.POST)
    public Map<String, Object> modifyDistance(@RequestParam Map<String, Object> params) {
        Map<String, Object> result = new HashMap<>();
        try {
            List<Integer> ids = new ArrayList<>();
            List<Double> distances = new ArrayList<>();
            for (Map.Entry<String, Object> entry : params.entrySet()) {
                String key = entry.getKey(), value = String.valueOf(entry.getValue());
                ids.add(Integer.parseInt(key.split("_")[1]));
                distances.add(Double.parseDouble(value) / 1000);
            }
            if (ids.size() == 0) {
                throw new IllegalArgumentException("不合法的参数");
            }
            lsStationRouteService.modifyDistance(ids, distances);
            result.put("status", ResponseCode.SUCCESS);
        } catch (Exception e) {
            result.put("status", ResponseCode.ERROR);
            result.put("msg", e.getMessage());
            log.error("", e);
        }

        return result;
    }

    @RequestMapping(value = "/circularRouteHandle", method = RequestMethod.POST)
    public Map<String, Object> circularRouteHandle(Integer lineId, Integer version) {
        Map<String, Object> result = new HashMap<>();
        try {
            lsStationRouteService.circularRouteHandle(lineId, version);
            result.put("status", ResponseCode.SUCCESS);
        } catch (Exception e) {
            result.put("status", ResponseCode.ERROR);
            result.put("msg", e.getMessage());
            log.error("", e);
        }

        return result;
    }
}