package com.ssm.controller;

import com.github.pagehelper.PageInfo;
import com.ssm.core.bean.GenericResponse;
import com.ssm.core.bean.PageResponse;
import com.ssm.core.util.SessionHelp;
import com.ssm.manage.entity.SmsException;
import com.ssm.manage.entity.StoreMonthTask;
import com.ssm.manage.query.StoreMonthTaskRequest;
import com.ssm.manage.response.ResponseCode;
import com.ssm.manage.response.StoreMonthTaskResponse;
import com.ssm.manage.service.SsmStoreMonthTaskService;
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;
import java.text.ParseException;

@Api(value = "门店月度任务controller", tags = {"门店月度任务相关接口"})
@RestController
@RequestMapping("/demay/ssm/monthTask")
@Slf4j
public class SsmStoreMonthTaskController {
    @Autowired
    private SsmStoreMonthTaskService ssmStoreMonthTaskService;


    @ApiOperation(value = "分页查询门店月度任务 门店端-门店月度任务-分页查询门店月度任务", httpMethod = "POST", response = PageResponse.class)
    @RequestMapping("/page")
    public PageResponse selectPage(@RequestBody StoreMonthTaskRequest storeMonthTaskRequest) {
        PageResponse<StoreMonthTask> pageResponse = new PageResponse<>();
        PageInfo<StoreMonthTask> pageInfo = new PageInfo<>();
        try {
            pageInfo = ssmStoreMonthTaskService.selectPage(storeMonthTaskRequest);
        } catch (Exception e) {
            log.error("SsmStoreMonthTaskController.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 add(@RequestBody StoreMonthTaskRequest storeMonthTaskRequest) {
        GenericResponse<StoreMonthTask> genericResponse = new GenericResponse<>();

        int i = 0;
        try {
            i = ssmStoreMonthTaskService.addTask(storeMonthTaskRequest);
        }catch (ParseException e) {
            log.error("SsmStoreMonthTaskController.add", e);
            genericResponse.setCodeAndMsg(ResponseCode.UNKNOWN_SYSTEM_EXCEPTION);
            return genericResponse;
        }catch (Exception e) {
            log.error("SsmStoreMonthTaskController.add", e);
            genericResponse.setCodeAndMsg(ResponseCode.FAILED);
            return genericResponse;
        }
        if (i > 0) {
            genericResponse.setCodeAndMsg(ResponseCode.OK);
        }
        return genericResponse;
    }

    @ApiOperation(value = "修改门店月度任务  门店端-门店月度任务-修改门店月度任务", httpMethod = "POST", response = GenericResponse.class)
    @RequestMapping("/update")
    public GenericResponse update(@RequestBody StoreMonthTaskRequest storeMonthTaskRequest) {
        GenericResponse<StoreMonthTask> genericResponse = new GenericResponse<>();

        int i = 0;
        try {
            i = ssmStoreMonthTaskService.updateTask(storeMonthTaskRequest);
        } catch (ParseException e) {
            log.error("SsmStoreMonthTaskController.update", e);
            genericResponse.setCodeAndMsg(ResponseCode.UNKNOWN_SYSTEM_EXCEPTION);
            return genericResponse;
        }catch (SmsException e){
            genericResponse.setCodeAndMsg(e);
            return genericResponse;
        }catch (Exception e) {
            log.error("SsmStoreMonthTaskController.update", e);
            genericResponse.setCodeAndMsg(ResponseCode.FAILED);
            return genericResponse;
        }
        if (i > 0) {
            genericResponse.setCodeAndMsg(ResponseCode.OK);
        }
        return genericResponse;
    }

    @ApiOperation(value = "删除门店月度任务  门店端-门店月度任务-删除门店月度任务", httpMethod = "POST", response = GenericResponse.class)
    @RequestMapping("/delete")
    public GenericResponse delete(@RequestBody StoreMonthTaskRequest storeMonthTaskRequest) {
        GenericResponse<StoreMonthTask> genericResponse = new GenericResponse<>();

        int i = 0;
        try {
            i = ssmStoreMonthTaskService.deleteTask(storeMonthTaskRequest);
        }catch (SmsException e){
            genericResponse.setCodeAndMsg(e);
            return genericResponse;
        }catch (Exception e) {
            log.error("SsmStoreMonthTaskController.delete", e);
            genericResponse.setCodeAndMsg(ResponseCode.FAILED);
            return genericResponse;
        }
        if (i > 0) {
            genericResponse.setCodeAndMsg(ResponseCode.OK);
        }
        return genericResponse;
    }

    @ApiOperation(value = "门店月度任务状态修改  门店端-门店月度任务-门店月度任务状态修改", httpMethod = "POST", response = GenericResponse.class)
    @RequestMapping("/updateStates")
    public GenericResponse updateStates(@RequestBody StoreMonthTaskRequest storeMonthTaskRequest) {
        GenericResponse<StoreMonthTask> genericResponse = new GenericResponse<>();

        int i = 0;
        try {
            i = ssmStoreMonthTaskService.updateState(storeMonthTaskRequest);
        }catch (Exception e) {
            log.error("SsmStoreMonthTaskController.updateStates", e);
            genericResponse.setCodeAndMsg(ResponseCode.FAILED);
            return genericResponse;
        }
        if (i > 0) {
            genericResponse.setCodeAndMsg(ResponseCode.OK);
        }
        return genericResponse;
    }


    @ApiOperation(value = "门店月度任务统计  门店端-门店月度任务-门店月度任务统计", httpMethod = "POST", response = GenericResponse.class)
    @RequestMapping("/statistic")
    public GenericResponse statistic(@RequestBody StoreMonthTaskRequest storeMonthTaskRequest) {
        GenericResponse<StoreMonthTaskResponse> genericResponse = new GenericResponse<>();

        StoreMonthTaskResponse response = new StoreMonthTaskResponse();

        try {
            response = ssmStoreMonthTaskService.statistic(storeMonthTaskRequest);
        }catch (Exception e) {
            log.error("SsmStoreMonthTaskController.statistic", e);
            genericResponse.setCodeAndMsg(ResponseCode.FAILED);
            return genericResponse;
        }

        genericResponse.setCodeAndMsg(ResponseCode.OK);
        genericResponse.setData(response);
        return genericResponse;
    }
}
