package com.huiju.estateagents.property.controller;

import com.alibaba.fastjson.JSONObject;
import com.huiju.estateagents.base.BaseController;
import com.huiju.estateagents.base.ResponseBean;
import com.huiju.estateagents.center.taUser.entity.TaUser;
import com.huiju.estateagents.entity.TaPerson;
import com.huiju.estateagents.property.common.Constant;
import com.huiju.estateagents.property.common.UserElement;
import com.huiju.estateagents.property.model.TpActivity;
import com.huiju.estateagents.property.model.TpAnnouncement;
import com.huiju.estateagents.property.model.TpTicket;
import com.huiju.estateagents.property.model.TpTransaction;
import com.huiju.estateagents.property.service.SocialServiceI;
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.HttpServletRequest;
import javax.servlet.http.HttpSession;

/**
 * @author weichaochao11
 * @Title: SocialController
 * @date 2018/10/23
 */
@RestController
@RequestMapping("/api")
@Api(value = "公告相关的API", tags = "公告相关的API")
public class SocialController extends BaseController {

    @Autowired
    private SocialServiceI socialServiceI;

    @RequestMapping(value = "/wx/announcement/{orgId}", method = RequestMethod.GET)
    @ApiOperation(value = "论坛公告帖子详情", notes = "获取论坛公告帖子详情")
    @ApiImplicitParams({@ApiImplicitParam(paramType = "path", dataType = "integer", name = "orgId", value = "小区Id"),
            @ApiImplicitParam(paramType = "query", dataType = "integer", name = "id", value = "公告帖子id")})
    public ResponseBean findAnnouncementDetail(@PathVariable(value = "orgId") Integer orgId, @RequestParam("id") Integer id, HttpServletRequest request) {
//        TaUser userElement = getTaUser(request);
//        Integer userId = userElement.getUserId();
        ResponseBean responseBean = new ResponseBean();

        TpAnnouncement tpAnnouncement = socialServiceI.findAnnouncementDetail(id, orgId, getPersonId(request));
        responseBean.addSuccess(tpAnnouncement);
        return responseBean;
    }

    @ApiOperation(value = "按小区获取分页公告列表", notes = "按小区获取分页公告列表,公告标题模糊查询")
    @ApiImplicitParams({@ApiImplicitParam(paramType = "path", dataType = "String", name = "orgId", value = "小区Id"),
            @ApiImplicitParam(paramType = "query", dataType = "String", name = "title", value = "公告标题"),
            @ApiImplicitParam(paramType = "query", dataType = "String", name = "annType", value = "公告类型"),
            @ApiImplicitParam(paramType = "query", dataType = "integer", name = "pageNum", value = "分页第几页"),
            @ApiImplicitParam(paramType = "query", dataType = "integer", name = "pageSize", value = "分页每页长度")})
    @RequestMapping(value = "/wx/announcements/{orgId}", method = RequestMethod.GET)
    public ResponseBean getAnnouncements(@PathVariable Integer orgId,
                                         @RequestParam(required = false) String title,
                                         @RequestParam(required = false) String annType,
                                         @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                         @RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize) {
        ResponseBean responseBean = new ResponseBean();

        ResponseBean announcementList = socialServiceI.getAnnouncements(orgId, title, annType, pageNum, pageSize);
        return announcementList;
    }


    @ApiOperation(value = "评价工单内容以及评分", notes = "评价工单内容以及评分")
    @ApiImplicitParams({@ApiImplicitParam(paramType = "path", dataType = "integer", name = "orgId", value = "小区Id"),
            @ApiImplicitParam(name = "parameter", value = "score 评分, comment 评语 ", required = true, dataType = "String"),
            @ApiImplicitParam(paramType = "query", dataType = "integer", name = "ticketId", value = "工单Id")})
    @RequestMapping(value = "/wx/accessTicket/{orgId}", method = RequestMethod.POST)
    public ResponseBean accessTicket(@PathVariable("orgId") Integer orgId, @RequestBody String parameter, @RequestParam("ticketId") String ticketId, HttpServletRequest request) {
        ResponseBean responseBean = new ResponseBean();
        TpTicket tpTicket = JSONObject.parseObject(parameter,TpTicket.class);
        socialServiceI.accessTicket(orgId, tpTicket, ticketId, getPersonId(request));
        return responseBean;
    }
}
