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.utils.SessionHelp;
import com.ssm.manage.entity.SmsMessages;
import com.ssm.manage.query.SmsMessagesRequest;
import com.ssm.manage.response.ResponseCode;
import com.ssm.manage.response.SmsMessageResponse;
import com.ssm.manage.service.SmsMessagesService;
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/smsMessages")
@Slf4j
public class SmsMessagesController {

    @Autowired
    private SmsMessagesService smsMessagesService;


    @ApiOperation(value = "查询短信列表", httpMethod = "POST", response = GenericResponse.class)
    @RequestMapping("/list")
    public PageResponse add(@RequestBody SmsMessagesRequest messages, HttpSession session) {
        PageResponse<SmsMessages> pageResponse = new PageResponse<>();
        PageInfo<SmsMessages> pageInfo = new PageInfo<>();
        if(messages.getStoreId()==null){
            messages.setStoreId(SessionHelp.getStoreId(session));
        }
        if(messages.getStartTime()==null || messages.getEndTime()==null){
            pageResponse.setCodeAndMsg(ResponseCode.MSG,"请选择短信的发送日期");
            return pageResponse;
        }
        pageInfo = smsMessagesService.list(messages);
        pageResponse.setPageInfo(pageInfo);
        pageResponse.setCodeAndMsg(ResponseCode.OK);
        return pageResponse;
    }

    @ApiOperation(value = "查询短信列表", httpMethod = "POST", response = GenericResponse.class)
    @RequestMapping("/ratio")
    public GenericResponse ratio(@RequestBody SmsMessagesRequest messages, HttpSession session) {
        GenericResponse< SmsMessageResponse> genericResponse = new GenericResponse<>();
        if(messages.getStoreId()==null){
            messages.setStoreId(SessionHelp.getStoreId(session));
        }
        SmsMessageResponse messagesList ;
        try {
            messagesList = smsMessagesService.ratio(messages);

        } catch (Exception e) {
            log.error("SsmProjectController.add", e);
            genericResponse.setCodeAndMsg(ResponseCode.FAILED);
            return genericResponse;
        }
        genericResponse.setData(messagesList);
        genericResponse.setCodeAndMsg(ResponseCode.OK);
        return genericResponse;
    }




}
