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.PlatformApi;
import com.ssm.manage.query.PlatformApiRequest;
import com.ssm.manage.query.StoreRequest;
import com.ssm.manage.response.ResponseCode;
import com.ssm.manage.service.SsmBaseStoreService;
import com.ssm.manage.service.SsmPlatformApiService;
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.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;

@Api(value="开通平台信息",tags={"开通平台信息相关接口"})
@RestController
@RequestMapping("/demay/ssm/PlatformApi")
@Slf4j
public class SsmPlatformApiController {

    @Autowired
    private SsmPlatformApiService ssmPlatformApiService;

    @Autowired
    private SsmBaseStoreService ssmBaseStoreService;

    @ApiOperation(value = "分页查询总店信息  平台端-门店管理-门店审核-分页查询总店信息",httpMethod = "POST",notes = "",response = PageResponse.class)
    @RequestMapping("/page")
    public PageResponse<StoreRequest> selectPage(@RequestBody StoreRequest storeRequest){
        PageResponse<StoreRequest> pageResponse = new PageResponse<>();
        PageInfo<StoreRequest> pageInfo = new PageInfo<>();
        try {
            storeRequest.setType(1);
            pageInfo = ssmBaseStoreService.selectPage(storeRequest);
        }catch (Exception e){
            log.error("SsmBaseStoreController.selectPage", e);
            pageResponse.setCodeAndMsg(ResponseCode.FAILED);
            return pageResponse;
        }
        pageResponse.setPageInfo(pageInfo);
        pageResponse.setCodeAndMsg(ResponseCode.OK);
        return pageResponse;
    }

    @ApiOperation(value = "分页查询总店信息  平台端-门店管理-门店审核-分页查询总店信息",httpMethod = "POST",notes = "",response = PageResponse.class)
    @RequestMapping("/selectPage")
    public PageResponse<PlatformApi> selectPage(@RequestBody PlatformApiRequest platformApiRequest){
        PageResponse<PlatformApi> pageResponse = new PageResponse<>();
        PageInfo<PlatformApi> pageInfo = new PageInfo<>();
        try {
            pageInfo=ssmPlatformApiService.selectPage(platformApiRequest);
        }catch (Exception e){
            log.error("SsmBaseStoreController.selectPage", e);
            pageResponse.setCodeAndMsg(ResponseCode.FAILED);
            return pageResponse;
        }
        pageResponse.setPageInfo(pageInfo);
        pageResponse.setCodeAndMsg(ResponseCode.OK);
        return pageResponse;
    }

    @ApiOperation(value = "门店开通平台对接添加", notes = "门店管理--互联网平台开通--开通平台--添加", response = GenericResponse.class)
    @RequestMapping(path="/add", method= RequestMethod.POST)
    public GenericResponse add(@RequestBody PlatformApiRequest platformApiRequest, HttpSession session) {
        GenericResponse<PlatformApi> genericResponse = new GenericResponse<>();
        int i=0;
        try{
            i=ssmPlatformApiService.add(platformApiRequest);
        }catch (Exception e){
            log.error("SsmPlatformApiController.add:", e);
            genericResponse.setCodeAndMsg(ResponseCode.FAILED);
            return genericResponse;
        }
        if(i>0)
            genericResponse.setCodeAndMsg(ResponseCode.OK);
        else{
            genericResponse.setCodeAndMsg(ResponseCode.MSG,"开通平台失败");
        }
        return genericResponse;
    }

    @ApiOperation(value = "门店开通平台对接修改", notes = "门店管理--互联网平台开通--开通平台--修改", response = GenericResponse.class)
    @RequestMapping(path="/update", method= RequestMethod.POST)
    public GenericResponse update(@RequestBody PlatformApiRequest platformApiRequest, HttpSession session) {
        GenericResponse<PlatformApi> genericResponse = new GenericResponse<>();
        int i=0;
        try{
            i=ssmPlatformApiService.update(platformApiRequest);
        }catch (Exception e){
            log.error("SsmPlatformApiController.add:", e);
            genericResponse.setCodeAndMsg(ResponseCode.FAILED);
            return genericResponse;
        }
        if(i>0)
            genericResponse.setCodeAndMsg(ResponseCode.OK);
        else{
            genericResponse.setCodeAndMsg(ResponseCode.MSG,"修改失败");
        }
        return genericResponse;
    }
}
