package com.happy.action;

import com.happy.Model.BookingComplaint;
import com.happy.Until.Func;
import com.happy.Until.GetHttpParam;
import com.happy.Until.ResUtil;
import com.happy.Until.ResponseUtil;
import com.happy.dto.IPage;
import com.happy.dto.OrderComplaintDto;
import com.happy.dto.OrderRateDto;
import com.happy.service.BookingComplaintProgressService;
import com.happy.service.BookingComplaintService;
import com.happy.vo.ComplaintPageVo;
import com.happy.vo.ProgressDetailsVo;
import com.opensymphony.xwork2.ActionSupport;
import net.sf.json.JSONObject;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.interceptor.ServletRequestAware;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.UUID;

public class AppBookingComplaintAction extends ActionSupport implements ServletRequestAware {

    public HttpServletRequest request;

    public HttpServletResponse response;

    public HttpServletRequest getRequest() {
        return request;
    }

    public void setRequest(HttpServletRequest request) {
        this.request = request;
    }

    @Override
    public void setServletRequest(HttpServletRequest request) {
        this.request = request;
    }

    public HttpServletResponse getResponse() {
        return response;
    }

    public void setResponse(HttpServletResponse response) {
        this.response = response;
    }

    @Resource
    public BookingComplaintService bookingComplaintService;

    @Resource
    public BookingComplaintProgressService bookingComplaintProgressService;

    public String bookingId;
    public String hotelId;
    public String houseId;
    public String title;
    public String content;
    public String createId;
    public String createDate;
    public String modifyDate;
    public List<String> pictureList;

    public String getBookingId() {
        return bookingId;
    }

    public void setBookingId(String bookingId) {
        this.bookingId = bookingId;
    }

    public String getHotelId() {
        return hotelId;
    }

    public void setHotelId(String hotelId) {
        this.hotelId = hotelId;
    }

    public String getHouseId() {
        return houseId;
    }

    public void setHouseId(String houseId) {
        this.houseId = houseId;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }

    public String getCreateId() {
        return createId;
    }

    public void setCreateId(String createId) {
        this.createId = createId;
    }

    public String getCreateDate() {
        return createDate;
    }

    public void setCreateDate(String createDate) {
        this.createDate = createDate;
    }

    public String getModifyDate() {
        return modifyDate;
    }

    public void setModifyDate(String modifyDate) {
        this.modifyDate = modifyDate;
    }

    public List<String> getPictureList() {
        return pictureList;
    }

    public void setPictureList(List<String> pictureList) {
        this.pictureList = pictureList;
    }

    public String usersId;
    public int page; // 页数
    public int rows;// 每页显示数

    public String getUsersId() {
        return usersId;
    }

    public void setUsersId(String usersId) {
        this.usersId = usersId;
    }

    public int getPage() {
        return page;
    }

    public void setPage(int page) {
        this.page = page;
    }

    public int getRows() {
        return rows;
    }

    public void setRows(int rows) {
        this.rows = rows;
    }

    public String complaintId;

    public String getComplaintId() {
        return complaintId;
    }

    public void setComplaintId(String complaintId) {
        this.complaintId = complaintId;
    }




    /**
     * 添加投诉
     *
     * @return
     */
    public JSONObject orderComplaint() {
        JSONObject jsonObject = new JSONObject();

        com.alibaba.fastjson.JSONObject json = GetHttpParam.getHttpParam(request);
        if (json == null) {
            jsonObject.put("code", 400);
            jsonObject.put("success", false);
            jsonObject.put("message", "缺少数据---orderRate");
            ResUtil.writeJson(ServletActionContext.getResponse(), jsonObject.toString());
            return null;
        }

        OrderComplaintDto orderComplaintDto =  com.alibaba.fastjson.JSONObject.parseObject(json.toString(),  OrderComplaintDto.class);

        if (orderComplaintDto==null) {
            jsonObject.put("code", 400);
            jsonObject.put("success", false);
            jsonObject.put("message", "缺少数据---orderRate");
            ResUtil.writeJson(ServletActionContext.getResponse(), jsonObject.toString());
            return null;
        }
        String bookingId = orderComplaintDto.getBookingId();
        String hotelId = orderComplaintDto.getHotelId();
        String houseId = orderComplaintDto.getHouseId();
        String title = orderComplaintDto.getTitle();
        String content = orderComplaintDto.getContent();
        String createId = orderComplaintDto.getCreateId();
        String createDate = orderComplaintDto.getCreateDate();
        String modifyDate = orderComplaintDto.getModifyDate();


        if (Func.checkNull(bookingId) || Func.checkNull(hotelId) || Func.checkNull(houseId) || Func.checkNull(title) || Func.checkNull(content) || Func.checkNull(createId) || Func.checkNull(createDate) || Func.checkNull(modifyDate)) {
            jsonObject.put("code", 400);
            jsonObject.put("success", false);
            jsonObject.put("message", "未传入投诉数据---orderComplaint");
            ResUtil.writeJson(ServletActionContext.getResponse(), jsonObject.toString());
            return null;
        }

        int insert = bookingComplaintService.insert(bookingId, hotelId, houseId, title, content, createId, createDate, modifyDate, orderComplaintDto.getPictureList());

        if (insert > 0) {
            jsonObject.put("code", 200);
            jsonObject.put("message", "成功");
            jsonObject.put("success", true);
            ResUtil.writeJson(ServletActionContext.getResponse(), jsonObject.toString());
            return null;
        }
        jsonObject.put("message", "未知异常");
        jsonObject.put("success", false);
        jsonObject.put("code", 205);
        ResUtil.writeJson(ServletActionContext.getResponse(), jsonObject.toString());
        return null;
    }

    /**
     * 投诉完成
     *
     * @return
     */
    public JSONObject resolutionComplaint() {
        JSONObject jsonObject = new JSONObject();

        if (Func.checkNull(complaintId) || Func.checkNull(createId) || Func.checkNull(createDate) || Func.checkNull(modifyDate)) {
            jsonObject.put("code", 400);
            jsonObject.put("success", false);
            jsonObject.put("message", "未传入complaintId数据---serviceHotline");
            ResUtil.writeJson(ServletActionContext.getResponse(), jsonObject.toString());
            return null;
        }

        int serviceHotlineInsert = bookingComplaintProgressService.resolutionComplaint(complaintId, createId, createDate, modifyDate);

        if (serviceHotlineInsert > 0) {
            jsonObject.put("code", 200);
            jsonObject.put("message", "成功");
            jsonObject.put("success", true);
            ResUtil.writeJson(ServletActionContext.getResponse(), jsonObject.toString());
            return null;
        }

        jsonObject.put("message", "未知异常");
        jsonObject.put("success", false);
        jsonObject.put("code", 205);
        ResUtil.writeJson(ServletActionContext.getResponse(), jsonObject.toString());
        return null;
    }


    /**
     * 投诉进度
     *
     * @return
     */
    public JSONObject complaintPage() {
        JSONObject jsonObject = new JSONObject();

        if (Func.checkNull(usersId)) {
            jsonObject.put("code", 400);
            jsonObject.put("success", false);
            jsonObject.put("message", "未传入usersId数据---complaintPage");
            ResUtil.writeJson(ServletActionContext.getResponse(), jsonObject.toString());
            return null;
        }

        if (page <= 0 || rows <= 0) {
            jsonObject.put("code", 400);
            jsonObject.put("success", false);
            jsonObject.put("message", "未传入page,rows数据---evaluatePage");
            ResUtil.writeJson(ServletActionContext.getResponse(), jsonObject.toString());
            return null;
        }

        IPage<ComplaintPageVo> iPage = bookingComplaintService.complaintPage(usersId, page, rows);

        jsonObject.put("code", 200);
        jsonObject.put("message", "成功");
        jsonObject.put("success", true);
        jsonObject.put("page", iPage);
        ResponseUtil.writeJson(ServletActionContext.getResponse(), jsonObject.toString());

        return null;


    }

    /**
     * 投诉进度详情
     * 根据订单ID查询订单进度数据
     * @return
     */
    public JSONObject progressDetails() {
        JSONObject jsonObject = new JSONObject();
        if (Func.checkNull(complaintId)) {
            jsonObject.put("code", 400);
            jsonObject.put("success", false);
            jsonObject.put("message", "未传入complaintId数据---progressDetails");
            ResUtil.writeJson(ServletActionContext.getResponse(), jsonObject.toString());
            return null;
        }

        ProgressDetailsVo vo=bookingComplaintService.progressNewDetail(complaintId);
        jsonObject.put("code", 200);
        jsonObject.put("message", "成功");
        jsonObject.put("success", true);
        jsonObject.put("page", vo);
        ResponseUtil.writeJson(ServletActionContext.getResponse(), jsonObject.toString());


        return null;
    }


}
