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.model.TaVisitor;
import com.community.huiju.service.TaVistorServiceI;
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 java.util.List;

/**
 * @author weichaochao
 * @Title: VistorController
 * @Description: 访客相关API
 * @date 2018/11/1
 */
@RestController
@RefreshScope
@RequestMapping("/")
@Api(value = "访客相关的API", description = "访客相关的API")
public class VistorController extends BaseController {

    @Autowired
    private TaVistorServiceI vistorServiceI;

    @RequestMapping(value = "/vistorDetail/{communityId}", method = RequestMethod.POST)
    @ApiOperation(value = "创建访客", notes = "创建访客")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "header",dataType = "String",name = "X-Auth-Token",value = "Token"),
            @ApiImplicitParam(paramType = "path", dataType = "integer", name = "communityId", value = "小区Id"),
            @ApiImplicitParam(paramType = "body", dataType = "String", name = "parameter", value = "carLicense:车牌(数组),drivingStatus:0 是不开车 1是开车," +
                    "visitorName:访问人姓名,visitorNum:访问人数,visitorTel:访问人电话,visitorStartTime:访问人开始时间,visitorEndTime:访问人结束时间,payEntity:支付方式(owmer是由我承担  visitor是访客承担)")})
    public ResponseBean findVistorDetail(@PathVariable("communityId")Integer communityId, @RequestBody String parameter, HttpSession session) {
        ResponseBean responseBean = new ResponseBean();

        if (!check(communityId)) {
            responseBean.addError("小区不存在");
            return responseBean;
        }

        UserElement userElement = getUserElement(session);
        TaVisitor taVisitor = JSONObject.parseObject(parameter,TaVisitor.class);
        responseBean = vistorServiceI.insertTaVistor(taVisitor, userElement);
        return  responseBean;
    }

    @RequestMapping(value = "/invateVistorHis/{communityId}", method = RequestMethod.GET)
    @ApiOperation(value = "邀请历史", notes = "邀请历史")
    @ApiImplicitParams({@ApiImplicitParam(paramType = "header",dataType = "String",name = "X-Auth-Token",value = "Token"),
            @ApiImplicitParam(paramType = "path", dataType = "integer", name = "communityId", value = "小区Id")})
    public ResponseBean invateHis(@PathVariable("communityId")Integer communityId, HttpSession session) {
        ResponseBean responseBean = new ResponseBean();

        if (!check(communityId)) {
            responseBean.addError("小区不存在");
            return responseBean;
        }

        UserElement userElement = (UserElement) session.getAttribute(Constant.APP_USER_SESSION);
        Integer userId = userElement.getId();
        responseBean = vistorServiceI.invateHis(communityId, userId);
        return  responseBean;
    }
}
