package com.ssm.controller;

import com.github.pagehelper.PageInfo;
import com.ssm.manage.response.GenericResponse;
import com.ssm.manage.response.PageResponse;
import com.ssm.manage.entity.Routing;
import com.ssm.manage.entity.SmsException;
import com.ssm.manage.query.RoutingListRequest;
import com.ssm.manage.response.ResponseCode;
import com.ssm.manage.service.SsmRoutingService;
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.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;

@Api(value = "路由controller", tags = {"路由相关接口"})
@RestController
@RequestMapping("/demay/ssm/rou")
@Slf4j
public class SsmRoutingContorller {
    @Autowired
    private SsmRoutingService ssmRoutingService;





    @ApiOperation(value = "查询路由",httpMethod = "POST", response = PageResponse.class)
    @RequestMapping("/page")
    public PageResponse selectPage(@RequestBody RoutingListRequest routingListRequest, HttpSession session) {
        PageResponse<Routing> pageResponse = new PageResponse<>();
        PageInfo<Routing> pageInfo = new PageInfo<>();
        try {
            pageInfo = ssmRoutingService.selectPage(routingListRequest);
        } catch (Exception e) {
            log.error("SsmRoutingController.page",e);
            pageResponse.setCodeAndMsg(ResponseCode.FAILED);
            return pageResponse;
        }
        pageResponse.setPageInfo(pageInfo);
        pageResponse.setCodeAndMsg(ResponseCode.OK);
        return pageResponse;
    }



    @ApiOperation(value = "路由添加", httpMethod = "POST", response = GenericResponse.class)
    @RequestMapping("/add")
    public GenericResponse selectPageNew(@RequestBody RoutingListRequest routingListRequest, HttpSession session) {
        GenericResponse genericResponse = new GenericResponse();
        if(routingListRequest.getStaffPhone()!=null)
            ;
        //测试注释routingListRequest.setStaffPhone(SessionHelp.getLoginAccount(session));
        int i = 0;
        try {
            i = ssmRoutingService.add(routingListRequest);
        } catch (SmsException e){
            genericResponse.setCodeAndMsg(e);
            return genericResponse;
        }catch (Exception e){
            log.error("SsmRoutingController.add:",e);
            genericResponse.setCodeAndMsg(ResponseCode.FAILED);
            return genericResponse;
        }
        if (i > 0) {
            genericResponse.setCodeAndMsg(ResponseCode.OK);
        }
        return genericResponse;
    }


    @ApiOperation(value = "根据返回ID和type删除路由", httpMethod = "POST", response = GenericResponse.class)
    @RequestMapping("/delete")
    public GenericResponse delete(@RequestBody RoutingListRequest routingListRequest ) {
        GenericResponse genericResponse = new GenericResponse();
        int i = 0;
        try {
            i = ssmRoutingService.delete(routingListRequest);
        } catch (Exception e){
            log.error("SsmRoutingController.delete:",e);
            genericResponse.setCodeAndMsg(ResponseCode.FAILED);
            return genericResponse;
        }
        if (i > 0) {
            genericResponse.setCodeAndMsg(ResponseCode.OK);
        }
        return genericResponse;
    }

    @ApiOperation(value = "根据主键ID和type修改", httpMethod = "POST", response = GenericResponse.class)
    @RequestMapping("/update")
    public GenericResponse update(@RequestBody RoutingListRequest routingListRequest ) {
        GenericResponse genericResponse = new GenericResponse();
        //测试注释routingListRequest.setStaffPhone(SessionHelp.getLoginAccount(session));
        int i = 0;
        try {
            i = ssmRoutingService.update(routingListRequest);
        } catch (Exception e){
            log.error("SsmRoutingController.update:",e);
            genericResponse.setCodeAndMsg(ResponseCode.FAILED);
            return genericResponse;
        }
        if (i > 0) {
            genericResponse.setCodeAndMsg(ResponseCode.OK);
        }
        return genericResponse;
    }




}
