package com.huiju.estateagents.property.controller;


import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.huiju.estateagents.base.BaseController;
import com.huiju.estateagents.base.ResponseBean;
import com.huiju.estateagents.center.taUser.entity.TaUser;
import com.huiju.estateagents.property.common.UserElement;
import com.huiju.estateagents.property.model.TpCarouselSetting;
import com.huiju.estateagents.property.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.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
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 = "query", dataTypeClass = String.class, name = "parameter", value = "carouselNum数量值")
    })
    @RequestMapping(value = "/carousel/announcement/banner", method = RequestMethod.PUT)
    public ResponseBean updateAnnouncementBanner(@RequestBody String parameter, HttpServletRequest request) {
        ResponseBean responseBean = new ResponseBean();
        TaUser userElement = getTaUser(request);
        QueryWrapper<TpCarouselSetting> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("org_id", userElement.getOrgId());
        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.setOrgId(userElement.getOrgId());
            // 添加
            b = iCarouselSettingService.save(one);

        }

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

    // @ApiOperation(value = "添加/修改活动的Banner数量", notes = "添加/修改活动的Banner数量")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", dataTypeClass = String.class, name = "parameter", value = "carouselNum数量值")
    })
    @RequestMapping(value = "/carousel/activity/banner", method = RequestMethod.PUT)
    public ResponseBean updateActivityBanner(@RequestBody String parameter, HttpServletRequest request) {
        ResponseBean responseBean = new ResponseBean();
        TaUser userElement = getTaUser(request);
        QueryWrapper<TpCarouselSetting> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("org_id", userElement.getOrgId());
        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.setOrgId(userElement.getOrgId());
            one.setCarouselNum(carouselNum);
            b = iCarouselSettingService.save(one);
        }


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

    // @ApiOperation(value = "获取Banner数量", notes = "获取Banner数量")
    @ApiImplicitParams({
            ,
    })
    @RequestMapping(value = "/carousel/getBanner", method = RequestMethod.GET)
    public ResponseBean getBannerNum(HttpServletRequest request) {
        ResponseBean responseBean = new ResponseBean();
        TaUser userElement = getTaUser(request);
        QueryWrapper<TpCarouselSetting> activityQueryWrapper = new QueryWrapper<>();
        activityQueryWrapper.eq("org_id", userElement.getOrgId());
        activityQueryWrapper.eq("carousel_type","activity");
        TpCarouselSetting activityOne = iCarouselSettingService.getOne(activityQueryWrapper);

        QueryWrapper<TpCarouselSetting> announcementQueryWrapper = new QueryWrapper<>();
        announcementQueryWrapper.eq("org_id", userElement.getOrgId());
        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;
    }

}
