TTInfoDetailController.java 5.08 KB
package com.bsth.controller.schedule;

import com.bsth.common.ResponseCode;
import com.bsth.controller.BaseController;
import com.bsth.entity.CarPark;
import com.bsth.entity.LineInformation;
import com.bsth.entity.schedule.TTInfoDetail;
import com.bsth.repository.schedule.TTInfoDetailRepository;
import com.bsth.service.CarParkService;
import com.bsth.service.LineInformationService;
import com.bsth.service.schedule.TTInfoDetailServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

/**
 * Created by xu on 16/7/2.
 */
@RestController
@RequestMapping("tidc")
public class TTInfoDetailController extends BaseController<TTInfoDetail, Long> {
    @Autowired
    private TTInfoDetailServiceImpl ttInfoDetailService;
    @Autowired
    private CarParkService carParkService;
    @Autowired
    private LineInformationService lineInformationService;
    @Autowired
    private TTInfoDetailRepository ttInfoDetailRepository;

    @RequestMapping(value = "/dataImportExtend", method = RequestMethod.POST)
    public Map<String, Object> uploadDataAndImport(
            MultipartFile file, String xlmc, String ttinfoname) throws Exception {
        Map<String, Object> resultMap = new HashMap<>();

        try {
            // 查找lineinformation对象,没有报错
            Map<String, Object> param = new HashMap<>();
            param.put("line.name_eq", xlmc);
            Iterator<LineInformation> lineInformationIterator = lineInformationService.list(param).iterator();
            if (!lineInformationIterator.hasNext()) {
                // 没有lineinformation,报错
                resultMap.put("status", ResponseCode.ERROR);
                resultMap.put("msg", "没有lineinfomation,线路名称=" + xlmc);
            } else {
                String tcccode = lineInformationIterator.next().getCarPark();
                if (StringUtils.isEmpty(tcccode)) {
                    // 没有停车场code,报错
                    resultMap.put("status", ResponseCode.ERROR);
                    resultMap.put("msg", "线路lineinfomation没有停车场code信息,线路名称=" + xlmc);
                } else {
                    // 使用停车场code查找停车场
                    param.clear();;
                    param.put("parkCode_eq", tcccode);
                    Iterator<CarPark> carParkIterator = carParkService.list(param).iterator();
                    if (!carParkIterator.hasNext()) {
                        // 指定的停车场code没有找到停车场信息,报错
                        resultMap.put("status", ResponseCode.ERROR);
                        resultMap.put("msg", "没有找到停车场信息,停车场code=" + tcccode);
                    } else {
                        String tccname = carParkIterator.next().getParkName();
                        if (StringUtils.isEmpty(tccname)) {
                            // 没有停车场名字,报错
                            resultMap.put("status", ResponseCode.ERROR);
                            resultMap.put("msg", "停车场信息没有停车场名字,停车场code=" + tcccode);
                        } else {
                            ttInfoDetailService.fileDataImport(file, xlmc, ttinfoname, tccname);
                            resultMap.put("status", ResponseCode.SUCCESS);
                            resultMap.put("msg", "导入成功");
                        }
                    }
                }
            }
        } catch (Exception exp) {
            exp.printStackTrace();
            throw exp;
        }

        return resultMap;
    }

    @RequestMapping(value = "/edit/{xlid}/{ttid}", method = RequestMethod.GET)
    public Object getEditInfo(
            @PathVariable("xlid") Integer xlid,
            @PathVariable("ttid") Long ttid) throws Exception {
        // TODO:返回类型需要修正
        return ttInfoDetailService.getEditInfo(xlid, ttid);
    }

    /**
     * 覆写方法,因为form提交的方式参数不全,改用 json形式提交 @RequestBody
     * @Title: save
     * @Description: TODO(持久化对象)
     * @param @param t
     * @param @return    设定文件
     * @return Map<String,Object>  {status: 1(成功),-1(失败)}
     * @throws
     */
    @RequestMapping(method = RequestMethod.POST)
    public Map<String, Object> save(@RequestBody TTInfoDetail t){

        return baseService.save(t);
    }

    @Override
    public TTInfoDetail findById(@PathVariable("id") Long aLong) {
        return ttInfoDetailRepository.findOneExtend(aLong);
    }

    @RequestMapping(value = "/bcdetail", method = RequestMethod.GET)
    public List<TTInfoDetail> findBcdetails(Integer xlId, Long ttinfoId, Long lpId) {
        return ttInfoDetailRepository.findBcdetails(xlId, ttinfoId, lpId);
    }
}