package com.huiju.estateagents.property.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
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.center.taUser.service.ITaUserService;
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.TpTicket;
import com.huiju.estateagents.property.model.TpTicketRecordComment;
import com.huiju.estateagents.property.service.IUserService;
import com.huiju.estateagents.property.service.TpTicketService;
import com.huiju.estateagents.service.ITaPersonService;
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;
import java.io.IOException;
import java.util.List;

/**
 * <p>
 * 工单表 前端控制器
 * </p>
 *
 * @author jobob
 * @since 2019-02-14
 */
@RestController
@RequestMapping("/api")
@Api(value = "工单列表API", tags = "工单列表API")
public class TpTicketController extends BaseController {
    @Autowired
    private ITaUserService userService;

    @Autowired
    private ITaPersonService taPersonService;

    @Autowired
    private TpTicketService tpTicketService;
    // @ApiOperation(value = "工单列表", notes = "工单列表")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "body",dataType = "String",name = "parameter",value = "id:工单编号，" +
                    "type:工单类型(1:投诉 2:报修   3:联系物业)," +
                    "ticketTitle:工单标题，createUser:发起人," +
                    "status:流转状态(0:待分配  1:拒绝受理 2:待处理 3:正在处理 4:待评价 5:已评价 6:已终止'）" +
                    "tpUserId:当前处理人" )
    })
    @RequestMapping(value = "/admin/ticketLiset", method = RequestMethod.POST)
    public ResponseBean ticketLiset(@RequestBody String parameter, HttpServletRequest request){
        TaUser userElement = getTaUser(request);
        ResponseBean  responseBean = tpTicketService.ticketLiset(parameter,userElement.getOrgId());
        return responseBean;
    }

    // @ApiOperation(value = "工单详情", notes = "工单详情")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "body",dataType = "String",name = "parameter",value = "id:工单编号")
    })

    @RequestMapping(value = "/admin/ticketDetails", method = RequestMethod.POST)
    public ResponseBean ticketDetails(@RequestBody String parameter, HttpServletRequest request){
        TaUser userElement = getTaUser(request);
        ResponseBean  responseBean = tpTicketService.ticketDetails(parameter,userElement.getUserId(),userElement.getOrgId());
        return responseBean;
    }

     @ApiOperation(value = "工单详情", notes = "工单详情")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "body",dataType = "String",name = "parameter",value = "id:工单编号")
    })

    @RequestMapping(value = "/wx/ticketDetails", method = RequestMethod.POST)
    public ResponseBean wxTicketDetails(@RequestBody String parameter, HttpServletRequest request){
        ResponseBean  responseBean = tpTicketService.ticketDetails(parameter,null,getOrgId(request));
        return responseBean;
    }

    // @ApiOperation(value = "添加工单对话", notes = "添加工单对话")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "body",dataType = "String",name = "parameter",value = "content:对话内容，imageUrl:[]图片,ticketId:工单id，" +
                    "status:工单状态（0:待分配  1:拒绝受理 2:待处理 3:正在处理 4:待评价 5:已评价 6:已终止）")
    })

    @RequestMapping(value = "/admin/addRecordComment", method = RequestMethod.POST)
    public ResponseBean addRecordComment(@RequestBody String parameter, HttpServletRequest request){
        TaUser userElement = getTaUser(request);
        ResponseBean  responseBean = tpTicketService.addRecordComment(parameter,userElement.getUserId(),userElement.getOrgId(),userElement.getUserName());
        return responseBean;
    }
    // @ApiOperation(value = "工单受理分配", notes = "工单受理分配")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "body",dataType = "String",name = "parameter",value = "ticketId:工单id，" +
                    "id:当前处理人id")
    })

    @RequestMapping(value = "/admin/addRecord", method = RequestMethod.POST)
    public ResponseBean addRecord(@RequestBody String parameter, HttpServletRequest request){
        TaUser userElement = getTaUser(request);
        ResponseBean  responseBean = tpTicketService.addRecord(parameter,userElement.getUserId(),userElement.getOrgId());
        return responseBean;
    }

    @ApiOperation(value = "生活管家给维修工分配工单", notes = "生活管家给维修工分配工单")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "body",dataType = "String",name = "parameter",value = "ticketId:工单编号，id：选中的分配人员的id")
    })
    @RequestMapping(value = "/wx/addRecord", method = RequestMethod.POST)
    public ResponseBean wxAddRecord(@RequestBody String parameter, HttpServletRequest request){
        TaUser taUser = getPropUserByPersonId(getPersonId(request));

        ResponseBean  responseBean = tpTicketService.addRecord(parameter,taUser.getUserId(),getOrgId(request));
        return responseBean;
    }

    private TaUser getPropUserByPersonId(String personId) {
        TaPerson person = taPersonService.getById(personId);
        LambdaQueryWrapper<TaUser> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(TaUser::getPhone,person.getPhone());
        List<TaUser> list = userService.list(queryWrapper);
        TaUser taUser = new TaUser();
        if (list.size() > 0){
            taUser = list.get(0);
        }
        return taUser;
    }

    // @ApiOperation(value = "修改工单状态为拒绝处理", notes = "修改工单状态为拒绝处理")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "body",dataType = "String",name = "parameter",value = "ticketId:工单id,textContent:拒绝的内容" )
    })

    @RequestMapping(value = "/admin/updateTicketStatus", method = RequestMethod.POST)
    public ResponseBean updateTicketStatus(@RequestBody String parameter, HttpServletRequest request){
        TaUser userElement = getTaUser(request);
        ResponseBean  responseBean = tpTicketService.updateTicketStatus(parameter,userElement.getUserId(),userElement.getOrgId(),userElement.getUserName(),userElement.getLoginName());
        return responseBean;
    }

    // @ApiOperation(value = "修改当前状态为待评价", notes = "修改当前状态为待评价")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "body",dataType = "String",name = "parameter",value = "content:对话内容，imageUrl:[]图片,ticketId:工单id" )
    })

    @RequestMapping(value = "/admin/updateTicket", method = RequestMethod.POST)
    public ResponseBean updateTicket(@RequestBody String parameter, HttpServletRequest request){
        TaUser userElement = getTaUser(request);
        ResponseBean  responseBean = tpTicketService.updateTicket(parameter,userElement.getUserId(),userElement.getOrgId(),userElement.getUserName(),userElement.getLoginName());
        return responseBean;
    }

    /**
     * 已完成订单
     * @param parameter
     * @param request
     * @return
     */
    @RequestMapping(value = "/admin/endTicket", method = RequestMethod.POST)
    public ResponseBean endTicket(@RequestBody String parameter, HttpServletRequest request){
        TaUser userElement = getTaUser(request);
        ResponseBean  responseBean = tpTicketService.endTicket(parameter,userElement.getUserId(),userElement.getOrgId(),userElement.getUserName(),userElement.getLoginName());
        return responseBean;
    }

    /**
     * 已完成订单
     * @param parameter
     * @param request
     * @return
     */
    @ApiOperation(value = "生活管家结束工单", notes = "生活管家结束工单")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "body",dataType = "String",name = "parameter",value = "ticketId:工单编号")
    })
    @RequestMapping(value = "/wx/endTicket", method = RequestMethod.POST)
    public ResponseBean wxEndTicket(@RequestBody String parameter, HttpServletRequest request){
        TaUser taUser = getPropUserByPersonId(getPersonId(request));
        ResponseBean  responseBean = tpTicketService.endTicket(parameter,taUser.getUserId(),taUser.getOrgId(),taUser.getUserName(),taUser.getLoginName());
        return responseBean;
    }


    @RequestMapping(value = "/admin/updateTicketPrice", method = RequestMethod.POST)
    public ResponseBean updateTicketPrice(@RequestBody String parameter, HttpServletRequest request){
        TaUser userElement = getTaUser(request);
        ResponseBean  responseBean = tpTicketService.updateTicketPrice(parameter,userElement.getUserId(),userElement.getOrgId());
        return responseBean;
    }


    @RequestMapping(value = "/wx/ticketList/{orgId}", method = RequestMethod.GET)
    @ApiOperation(value = "获取 报修/投诉/联系单 各3条数据", notes = "根据 小区编号,第几页,一页多少行")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "path",dataType = "Integer",name = "orgId",value = "小区编号"),
            @ApiImplicitParam(paramType = "query",dataType = "Integer",name = "pageNum",value = "第几页"),
            @ApiImplicitParam(paramType = "query",dataType = "Integer",name = "pageSize",value = "一页多少数据"),
            @ApiImplicitParam(paramType = "header",dataType = "String",name = "X-Auth-Token",value = "Token"),
    })
    @Deprecated
    public ResponseBean getService(@PathVariable(value = "orgId") Integer orgId,
                                   @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                   @RequestParam(value = "pageSize", defaultValue = "3") Integer pageSize,
                                   HttpServletRequest request) {
        ResponseBean response = new ResponseBean();
        TpTicket tpTicket = new TpTicket();
        tpTicket.setOrgId(Integer.valueOf(orgId));
        tpTicket.setPersonId(getPersonId(request));

        response = tpTicketService.getList(tpTicket, pageNum, pageSize);
        return response;
    }

    @ApiOperation(value = "获取报修/投诉/联系单进度详情数据", notes = "获取报修/投诉/联系单进度详情数据")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "path", dataType = "Integer", name = "orgId", value = "小区编号"),
            @ApiImplicitParam(paramType = "query", dataType = "Integer", name = "ticketId", value = "报修id")
    })
    @RequestMapping(value = "/wx/ticket/schedule/{orgId}", method = RequestMethod.GET)
    public ResponseBean getTicketSchedule(@PathVariable(value = "orgId") Integer orgId, @RequestParam Integer ticketId) {
        ResponseBean responseBean = new ResponseBean();
        TpTicket ticket = tpTicketService.getTicketSchedule(String.valueOf(orgId), ticketId);
        if (null != ticket) {
            responseBean.addSuccess(ticket);
        }else {
            responseBean.addError("没有该工单!");
        }

        return responseBean;
    }

    @RequestMapping(value = "/wx/tickets/list/{orgId}", method = RequestMethod.GET)
    @ApiOperation(value = "获取 报修/投诉/联系单 数据列表", notes = "根据 小区编号,工单类型,第几页,一页多少行")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "path",dataType = "Integer",name = "orgId", value = "小区编号"),
            @ApiImplicitParam(paramType = "query",dataType = "Integer",name = "pageNum", value = "第几页"),
            @ApiImplicitParam(paramType = "query",dataType = "Integer",name = "pageSize", value = "一页多少数据"),
            @ApiImplicitParam(paramType = "query",dataType = "String",name = "type", value = "工单类型 (1：投诉   2：报修 3：联系物业)"),
            @ApiImplicitParam(paramType = "header",dataType = "String",name = "X-Auth-Token",value = "Token"),

    })
    public ResponseBean getServiceList(@PathVariable(value = "orgId") Integer orgId,
                                       @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                       @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
                                       @RequestParam(value = "type") String type,
                                       HttpServletRequest request) {
        ResponseBean responseBean = new ResponseBean();
        TpTicket tpTicket = new TpTicket();
        tpTicket.setOrgId(Integer.valueOf(orgId));
        tpTicket.setPersonId(getPersonId(request));
        tpTicket.setType(type);

        responseBean = tpTicketService.getByTypeList(tpTicket, pageNum, pageSize);

        return responseBean;

    }

    @ApiOperation(value = "回复 报修/投诉/联系单 追问接口", notes = "回复 报修/投诉/联系单 追问接口")
    @ApiImplicitParams({@ApiImplicitParam(paramType = "path", dataType = "integer", name = "orgId", value = "小区Id"),
            @ApiImplicitParam(name = "tpTicketRecordComment", value = "回复记录参数(ticketId:工单id,content:回复时间,ticketRecordCommentId:需要回复的id)", paramType = "body"),
            @ApiImplicitParam(paramType = "header",dataType = "String",name = "X-Auth-Token",value = "Token")})
    @RequestMapping(value = "/wx/ticket/reply/{orgId}", method = RequestMethod.POST)
    public ResponseBean updateTicketsReply(@PathVariable(value = "orgId") Integer orgId, @RequestBody TpTicketRecordComment tpTicketRecordComment, HttpServletRequest request){
        ResponseBean responseBean = new ResponseBean();

        tpTicketRecordComment.setOrgId(orgId);
        Integer size = tpTicketService.updateTicketsReply(tpTicketRecordComment, getPersonId(request));
        if (size > 0){
            responseBean.addSuccess(size);
        } else {
            responseBean.addError("更新失败");
        }
        return responseBean;
    }

    @ApiOperation(value = "添加 报修/投诉/联系单", notes = "添加 报修/投诉/联系单")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "body", dataType = "String", name = "paramets", value =
                    "ticketTitle:工单标题,ticketContent:工单内容，type: 1：投诉   2：报修 3：联系物业 , repairType：0:公共区域  1: 房屋质量  2:户内设施, imageUrl:图片地址 "),
            @ApiImplicitParam(paramType = "header",dataType = "String",name = "X-Auth-Token",value = "Token")
    })
    @RequestMapping(value = "/wx/addtpTicket", method = RequestMethod.POST)
    @ResponseBody
    public ResponseBean addTransaction(@RequestBody String paramets, HttpServletRequest request) throws IOException {
        ResponseBean responseBean = new ResponseBean();
        ResponseBean response = tpTicketService.addAiTicketService(getPersonId(request), paramets);
        return response;
    }

    @ApiOperation(value = "修改 报修/投诉/联系单", notes = "修改 报修/投诉/联系单")
    @ApiImplicitParams({@ApiImplicitParam(paramType = "path", dataType = "integer", name = "ticketId", value = "工单Id"),
            @ApiImplicitParam(paramType = "body", dataType = "String", name = "parameter", value =
                    "ticketTitle:工单标题,ticketContent:工单内容，type: 1：投诉   2：报修 3：联系物业 , imageUrl:图片地址 "),
            @ApiImplicitParam(paramType = "header",dataType = "String",name = "X-Auth-Token",value = "Token")
    })
    @RequestMapping(value = "/wx/updateTicket/{ticketId}", method = RequestMethod.POST)
    public ResponseBean updateTicketContent(@PathVariable("ticketId") Integer ticketId,
                                            @RequestBody String parameter,
                                            HttpServletRequest request) {
        ResponseBean responseBean = new ResponseBean();
        responseBean = tpTicketService.updateTicketContent(ticketId, parameter,getPersonId(request));
        return responseBean;
    }

    @ApiOperation(value = "报修进度终止", notes = "报修进度终止")
    @ApiImplicitParams({@ApiImplicitParam(paramType = "path", dataType = "integer", name = "orgId", value = "小区Id"),
            @ApiImplicitParam(paramType = "query", dataType = "integer", name = "ticketId", value = "帖子Id"),
            @ApiImplicitParam(paramType = "header",dataType = "String",name = "X-Auth-Token",value = "Token")})
    @RequestMapping(value = "/wx/stopTicket/{orgId}", method = RequestMethod.GET)
    public ResponseBean stopTicket(@PathVariable("orgId") Integer orgId, @RequestParam("ticketId") Integer ticketId, HttpServletRequest request) {
        ResponseBean responseBean = new ResponseBean();
        responseBean = tpTicketService.stopTpTicketRecord(orgId, ticketId, getPersonId(request));
        return responseBean;
    }

    @ApiOperation(value = "添加工单对话", notes = "添加工单对话")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "body",dataType = "String",name = "parameter",value = "content:对话内容，imageUrl:[]图片,ticketId:工单id，" )
    })

    @RequestMapping(value = "/wx/addWxRecordComment", method = RequestMethod.POST)
    public ResponseBean addWxRecordComment(@RequestBody String parameter, HttpServletRequest request){
        TaPerson taPerson = taPersonService.getById(getPersonId(request));
        ResponseBean  responseBean = tpTicketService.addWxRecordComment(parameter,taPerson.getPersonId(),1,taPerson.getName());
        return responseBean;
    }

    /**
     * 获取维修人员的订单列表
     * @param
     * @param request
     * @return
     */
    @ApiOperation(value = "获取生活管家的工单列表", notes = "获取生活管家的工单列表")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query",dataType = "Integer",name = "pageNum", value = "第几页"),
            @ApiImplicitParam(paramType = "query",dataType = "Integer",name = "pageSize", value = "一页多少数据")
    })
    @RequestMapping(value = "/wx/life/ticket/list", method = RequestMethod.GET)
    public ResponseBean getWorkerList(@RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                      @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
                                      HttpServletRequest request){
        TaUser taUser = getPropUserByPersonId(getPersonId(request));
        ResponseBean  responseBean = tpTicketService.getWorkerList(pageNum,pageSize,taUser.getUserId(),taUser.getOrgId());
        return responseBean;
    }
}
