NoticeController.java 4.42 KB
package com.bsth.controller.realcontrol;


import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.bsth.common.ResponseCode;
import com.bsth.controller.BaseController;
import com.bsth.data.BasicData;
import com.bsth.data.notice.NoticeService;
import com.bsth.data.notice.entity.Notice;
import com.bsth.util.SignUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.context.request.WebRequest;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * 公告
 */
@RestController
@RequestMapping("/notice")
public class NoticeController extends BaseController<Notice, Long>{

    Logger log = LoggerFactory.getLogger(this.getClass());


    @Autowired
    NoticeService noticeService;

    @InitBinder
    public void initBinder(WebDataBinder binder, WebRequest request) {
        //转换日期 注意这里的转化要和传进来的字符串的格式一直 如2015-9-9 就应该为yyyy-MM-dd
        DateFormat dateFormat=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));// CustomDateEditor为自定义日期编辑器
    }


    @RequestMapping(value = "/findList", method = RequestMethod.GET)
    public Map<String, Object> findList(@RequestParam Map<String, String> map) {
        return noticeService.findList(map);
    }

    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    public Notice findById(@PathVariable("id") Long id) {
        Notice notice = noticeService.findById(id);
        return notice;
    }

    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    public Map<String, Object> deleteInfo(Notice t) {
        Map<String, Object> map = new HashMap<>();
        try{
            noticeService.deleteInfo(t);
            map.put("status", ResponseCode.SUCCESS);
        } catch (Exception e) {
            log.error(e.toString(), e);
            map.put("status", ResponseCode.ERROR);
        }
        return map;
    }


    @RequestMapping(method = RequestMethod.POST)
    public Map<String, Object> save(Notice t) {
        Map<String, Object> map = new HashMap<>();
        try{
            t.setNOTICE_XLNAME(BasicData.lineCode2NameMap.get(t.getNOTICE_XL()));
            t.setNOTICE_STATIONNAME(BasicData.stationCode2NameMap.get(t.getNOTICE_XL()+"_"+t.getNOTICE_XSFX()+"_"+t.getNOTICE_STATION()));
            t.setCREATE_BY(t.getNOTICE_BBR());
            t.setUPDATE_BY(t.getNOTICE_BBR());
            noticeService.save(t);
            map.put("status", ResponseCode.SUCCESS);
        } catch (Exception e) {
            log.error(e.toString(), e);
            map.put("status", ResponseCode.ERROR);
        }
        return map;
    }

    @RequestMapping(value = "/getNotice", method = RequestMethod.POST)
    public Map<String, Object> getNotice(@RequestBody JSONObject jsonObject) {
        Map<String, Object> map = new HashMap<>();
        Map<String, String> params = new HashMap<>();
        try{
            if(!SignUtils.validation(Long.parseLong(jsonObject.getString("timestamp")),jsonObject.getString("sign"))){
                map.put("status", "验证失败");
                return map;
            }
            StringBuffer stations=new StringBuffer();
            StringBuffer lineCodes=new StringBuffer();
            JSONArray jsonArray = jsonObject.getJSONArray("stations");
            for (int i = 0; i < jsonArray.size(); i++) {
                JSONObject line=jsonArray.getJSONObject(i);
                String lineCode = line.get("lineCode").toString();
                String stationCode = line.get("stationCode").toString();
                stations.append(stationCode+",");
                lineCodes.append(lineCode+",");
            }
            params.put("stations",stations.toString().substring(0,stations.toString().length()-1));
            params.put("lineCodes",lineCodes.toString().substring(0,lineCodes.toString().length()-1));
            return noticeService.getNotice(params);
        } catch (Exception e) {
            log.error(e.toString(), e);
            map.put("status", ResponseCode.ERROR);
            return map;
        }
    }

}