package com.community.huiju.controller;


import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.community.commom.mode.ResponseBean;
import com.community.commom.session.UserElement;
import com.community.huiju.common.base.BaseController;
import com.community.huiju.model.TpCarouselSetting;
import com.community.huiju.service.ICarouselSettingService;
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.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 * 轮播数设置表 前端控制器
 * </p>
 *
 * @author weiximei
 * @since 2019-03-06
 */
@RestController
@RequestMapping("/")
@Api(value = "轮播数设置表 API", description = "轮播数设置表 API")
public class CarouselSettingController extends BaseController {

    @Autowired
    private ICarouselSettingService iCarouselSettingService;

    @ApiOperation(value = "添加/修改公告的Banner数量", notes = "添加/修改公告的Banner数量")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "header", dataTypeClass = String.class, name = "X-Auth-Token", value = "Token"),
            @ApiImplicitParam(paramType = "query", dataTypeClass = String.class, name = "parameter", value = "carouselNum数量值")
    })
    @RequestMapping(value = "/carousel/announcement/banner", method = RequestMethod.PUT)
    public ResponseBean updateAnnouncementBanner(@RequestBody String parameter, HttpSession session) {
        ResponseBean responseBean = new ResponseBean();
        UserElement userElement = getUserElement(session);
        QueryWrapper<TpCarouselSetting> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("community_id", userElement.getCommunityId());
        queryWrapper.eq("carousel_type","announcement");
        TpCarouselSetting one = iCarouselSettingService.getOne(queryWrapper);
        Integer carouselNum = JSONObject.parseObject(parameter).getInteger("carouselNum");
        boolean b = false;
        // 修改
        if (null != one) {
            one.setCarouselNum(carouselNum);
            b = iCarouselSettingService.updateById(one);
        } else {
            one = new TpCarouselSetting();
            one.setCarouselNum(carouselNum);
            one.setCarouselType("announcement");
            one.setCommunityId(userElement.getCommunityId());
            // 添加
            b = iCarouselSettingService.save(one);

        }

        if (b) {
            responseBean.addSuccess("操作成功!");
            return responseBean;
        }
        responseBean.addError("操作失败!");
        return responseBean;
    }

    @ApiOperation(value = "添加/修改活动的Banner数量", notes = "添加/修改活动的Banner数量")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "header", dataTypeClass = String.class, name = "X-Auth-Token", value = "Token"),
            @ApiImplicitParam(paramType = "query", dataTypeClass = String.class, name = "parameter", value = "carouselNum数量值")
    })
    @RequestMapping(value = "/carousel/activity/banner", method = RequestMethod.PUT)
    public ResponseBean updateActivityBanner(@RequestBody String parameter, HttpSession session) {
        ResponseBean responseBean = new ResponseBean();
        UserElement userElement = getUserElement(session);
        QueryWrapper<TpCarouselSetting> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("community_id", userElement.getCommunityId());
        queryWrapper.eq("carousel_type","activity");
        TpCarouselSetting one = iCarouselSettingService.getOne(queryWrapper);
        Integer carouselNum = JSONObject.parseObject(parameter).getInteger("carouselNum");
        boolean b = false;
        if (null != one) {
            one.setCarouselNum(carouselNum);
            b = iCarouselSettingService.updateById(one);
        } else {
            one = new TpCarouselSetting();
            one.setCarouselType("activity");
            one.setCommunityId(userElement.getCommunityId());
            one.setCarouselNum(carouselNum);
            b = iCarouselSettingService.save(one);
        }


        if (b) {
            responseBean.addSuccess("操作成功!");
            return responseBean;
        }
        responseBean.addError("操作失败!");
        return responseBean;
    }

    @ApiOperation(value = "获取Banner数量", notes = "获取Banner数量")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "header", dataTypeClass = String.class, name = "X-Auth-Token", value = "Token"),
    })
    @RequestMapping(value = "/carousel/getBanner", method = RequestMethod.GET)
    public ResponseBean getBannerNum(HttpSession session) {
        ResponseBean responseBean = new ResponseBean();
        UserElement userElement = getUserElement(session);
        QueryWrapper<TpCarouselSetting> activityQueryWrapper = new QueryWrapper<>();
        activityQueryWrapper.eq("community_id", userElement.getCommunityId());
        activityQueryWrapper.eq("carousel_type","activity");
        TpCarouselSetting activityOne = iCarouselSettingService.getOne(activityQueryWrapper);

        QueryWrapper<TpCarouselSetting> announcementQueryWrapper = new QueryWrapper<>();
        announcementQueryWrapper.eq("community_id", userElement.getCommunityId());
        announcementQueryWrapper.eq("carousel_type","announcement");
        TpCarouselSetting announcementOne = iCarouselSettingService.getOne(announcementQueryWrapper);

        Map<String, Object> map = new HashMap<>();
        map.put("activity", activityOne);
        map.put("announcement", announcementOne);
        responseBean.addSuccess(map);
        return responseBean;
    }

}
