package com.ssm.controller;


import com.ssm.manage.query.ProjectRequest;
import com.ssm.manage.response.CooperateResponse;
import com.ssm.manage.response.GenericResponse;
import com.ssm.manage.response.PageResponse;
import com.ssm.manage.entity.*;
import com.ssm.manage.query.CooperateListRequest;
import com.ssm.manage.response.ResponseCode;
import com.ssm.manage.service.CooperateListService;
import com.ssm.manage.utils.SessionHelp;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
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;
import java.util.List;

@Api(value="合作商",tags={"合作商"})
@RestController
@RequestMapping("/demay/ssm/cooperate")
@Slf4j
public class CooperateListController {

    @Autowired
    private CooperateListService cooperateListService;

    @ApiOperation(value = "新增", httpMethod = "POST", response = GenericResponse.class)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "brandNum",value = "合作商编码",required = true),
            @ApiImplicitParam(name = "brandName",value = "合作商名字",required = true),
            @ApiImplicitParam(name = "phone",value = "合作商联系电话",required = true)
    })
    @RequestMapping("/add")
    public GenericResponse add(@RequestBody CooperateListRequest cooperateListRequest, HttpSession session) {
        GenericResponse<CooperateList> genericResponse = new GenericResponse<>();
        int i;
            i = cooperateListService.insert(cooperateListRequest,session);
        if (i > 0) {
            genericResponse.setCodeAndMsg(ResponseCode.OK);
        }
        return genericResponse;
    }


    @ApiOperation(value = "修改", httpMethod = "POST", response = GenericResponse.class)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id",value = "id",required = true)
    })
    @RequestMapping("/update")
    public GenericResponse update(@RequestBody CooperateListRequest cooperateListRequest, HttpSession session) {
        GenericResponse<CooperateList> genericResponse = new GenericResponse<>();
        int i;
        i = cooperateListService.update(cooperateListRequest,session);
        if (i > 0) {
            genericResponse.setCodeAndMsg(ResponseCode.OK);
        }
        return genericResponse;
    }



    @ApiOperation(value = "删除", httpMethod = "POST", response = GenericResponse.class)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id",value = "id",required = true)
    })
    @RequestMapping("/delete")
    public GenericResponse delete(@RequestBody CooperateListRequest cooperateListRequest, HttpSession session) {
        GenericResponse<CooperateList> genericResponse = new GenericResponse<>();
        int i;
        i = cooperateListService.delete(cooperateListRequest.getId());
        if (i > 0) {
            genericResponse.setCodeAndMsg(ResponseCode.OK);
        }
        return genericResponse;
    }


    @ApiOperation(value = "分页查询", httpMethod = "POST", response = GenericResponse.class)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum",required = true),
            @ApiImplicitParam(name = "pageSize",required = true)
    })
    @RequestMapping("/page")
    public PageResponse<CooperateList> page(@RequestBody CooperateListRequest cooperateListRequest, HttpSession session) {
        cooperateListRequest.setStoreId(SessionHelp.getStoreId(session));
        return cooperateListService.page(cooperateListRequest);
    }

    @ApiOperation(value = "关联合作商", httpMethod = "POST", response = GenericResponse.class)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id",required = true),
            @ApiImplicitParam(name = "projectId",required = true)
    })
    @RequestMapping("/bind")
    public PageResponse<CooperateList> bind(@RequestBody CooperateListRequest cooperateListRequest, HttpSession session) {
        return cooperateListService.bind(cooperateListRequest);
    }

    @ApiOperation(value = "合作商项目", httpMethod = "POST", response = GenericResponse.class)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum",required = true),
            @ApiImplicitParam(name = "pageSize",required = true)
    })
    @RequestMapping("/cooperateProjectPage")
    public PageResponse<BaseProject> cooperateProjectPage(@RequestBody ProjectRequest baseProject, HttpSession session) {
        return cooperateListService.cooperateProjectPage(baseProject);
    }

    @ApiOperation(value = "合作商报表", httpMethod = "POST", response = GenericResponse.class)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "startTime",required = true),
            @ApiImplicitParam(name = "endTime",required = true),
            @ApiImplicitParam(name = "storeId",required = true)
    })
    @RequestMapping("/cooper")
    public GenericResponse<List<CooperateResponse>> cooper(@RequestBody ProjectRequest baseProject, HttpSession session) throws ParseException, SmsException {
        return cooperateListService.cooperByDate(baseProject);
    }

}
