package com.community.huiju.controller;

import com.alibaba.fastjson.JSONObject;
import com.community.commom.constant.Constant;
import com.community.commom.mode.ResponseBean;
import com.community.commom.session.UserElement;
import com.community.huiju.common.base.BaseController;
import com.community.huiju.model.ToBanner;
import com.community.huiju.service.IToBannerService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import javax.validation.Valid;
import java.util.List;

/**
 * @author weiximei
 */
@RestController
@RefreshScope
@RequestMapping("/")
@Api(value = "Banner 控制器")
public class BannerController extends BaseController {

    @Autowired
    private IToBannerService iToBannerService;

    @ApiOperation(value = "添加Banner",notes = "添加Banner")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "body",dataType = "ToBanner",name = "banner",value = "title(标题)," +
                    "sort(权重),bannerCover(图片),bannerPosition(Banner位),communityId(小区)," +
                    "effTime(生效时间),expTime(失效时间),bannerType(Banner类型),externalLink(Banner的URL)," +
                    ",bannerTitle(内容标题),bannerContent(Banner内容),bannerDescription(跳转描述)"),
            @ApiImplicitParam(paramType = "header",dataType = "String",name = "X-Auth-Token",value = "Token"),
    })
    @RequestMapping(value = "/banner",method = RequestMethod.POST)
    public ResponseBean addBanner(@RequestBody @Valid ToBanner banner,
                                  HttpSession session){

        UserElement userElement = getUserElement(session);
        ResponseBean response = new ResponseBean();
        response = iToBannerService.addBanner(banner,userElement.getId());
        return response;
    }

    @ApiOperation(value = "修改Banner",notes = "修改Banner")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "body",dataType = "ToBanner",name = "banner",value = "bannerTitle(标题)," +
                    "sort(权重),bannerCover(图片),bannerPosition(Banner位),communityId(小区)," +
                    "effTime(生效时间),expTime(失效时间),bannerType(Banner类型),externalLink(Banner的URL)," +
                    "bannerContent(Banner内容)"),
            @ApiImplicitParam(paramType = "header",dataType = "String",name = "X-Auth-Token",value = "Token"),
    })
    @RequestMapping(value = "/banner",method = RequestMethod.PUT)
    public ResponseBean updateBanner(@RequestBody @Valid ToBanner banner,
                                  HttpSession session){

        UserElement userElement = getUserElement(session);
        ResponseBean response = new ResponseBean();
        response = iToBannerService.modifyBanner(banner,userElement.getId());
        return response;
    }

    @ApiOperation(value = "获取Banner详情",notes = "获取Banner详情")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "path",dataType = "Integer",name = "bannerId",value = "bannerId(编号),")
    })
    @RequestMapping(value = "/banner/{bannerId}",method = RequestMethod.GET)
    public ResponseBean getByIdBanner(@PathVariable(value = "bannerId") Integer bannerId){

        ResponseBean response = new ResponseBean();
        response = iToBannerService.getById(bannerId);
        return response;
    }

    @ApiOperation(value = "根据条件查询Banner",notes = "根据条件查询Banner")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query",dataType = "String",name = "title",value = "title(标题)"),
            @ApiImplicitParam(paramType = "query",dataType = "Integer",name = "bannerPosition",value = "bannerPosition(Banner位)"),
            @ApiImplicitParam(paramType = "query",dataType = "String",name = "bannerDescription",value = "bannerDescription(跳转概述)"),
            @ApiImplicitParam(paramType = "query",dataType = "String",name = "communityId",value = "communityId(小区)"),
            @ApiImplicitParam(paramType = "query", dataType = "Integer", name = "pageNum", value = "分页第几页"),
            @ApiImplicitParam(paramType = "query", dataType = "Integer", name = "pageSize", value = "分页每页长度")
    })
    @RequestMapping(value = "/banner/list",method = RequestMethod.GET)
    public ResponseBean getBanner(@RequestParam(value = "title", required = false) String title,
                                  @RequestParam(value = "bannerPosition", required = false) Integer bannerPosition,
                                  @RequestParam(value = "bannerDescription", required = false) String bannerDescription,
                                  @RequestParam(value = "communityId", required = false) Integer communityId,
                                  @RequestParam(value ="pageNum",defaultValue = "1") Integer pageNum,
                                  @RequestParam(value ="pageSize",defaultValue = "10") Integer pageSize){


        ResponseBean response = new ResponseBean();
        ToBanner banner = new ToBanner();
        banner.setTitle(title);
        banner.setBannerPosition(bannerPosition);
        banner.setBannerDescription(bannerDescription);
        banner.setCommunityId(communityId);
        response = iToBannerService.getBanner(banner,pageNum,pageSize);
        return response;
    }

    @ApiOperation(value = "删除banner",notes = "根据bannerId删除banner")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "body",dataType = "List<Integer>",name = "list",value = "banner的id")
    })
    @RequestMapping(value = "/banner",method = RequestMethod.DELETE)
    public ResponseBean deleteBanner(@RequestBody List<Integer> list){
        ResponseBean response = new ResponseBean();
        response = iToBannerService.deleteBanner(list);
        return response;
    }

}
