package com.happy.action;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.happy.Model.BookingComment;
import com.happy.Model.House;
import com.happy.Until.GetHttpParam;
import com.happy.Until.ResUtil;
import com.happy.common.controller.BaseController;
import com.happy.dto.BookCommentDto;
import com.happy.dto.IPage;
import com.happy.service.BookingCommentService;
import com.happy.service.BookingComplaintService;
import net.sf.json.JSONObject;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.interceptor.ServletRequestAware;

import javax.annotation.Resource;
import java.util.List;

public class bookCommentAction extends BaseController implements ServletRequestAware {
    public String keywords;//关键字搜索

    public String score; //评分

    public Integer bookId; //关联订单id

    public String commentStartTime;//起始时间

    public String commentEndTime;//结束时间

    public String checkoutStartTime;//退房起始时间
    public String checkoutEndTime;//退房结束时间

    public String managerId;//商家id

    @Resource(name = "BookingCommentService")
    private BookingCommentService bookingCommentService;

    public String queryPage(){
        JSONObject resultJson = new JSONObject();
        StringBuilder s1 = new StringBuilder("");
        StringBuilder s2 = new StringBuilder("");
        if (managerId != null){
            s1.append(" and b.hotel_manager_id = ").append(managerId).append(" ");
            s2.append(" and a.hotel_manager_id = ").append(managerId).append(" ");
        }else {
            resultJson.put("message", "查询失败！商户id不能为空！");
            resultJson.put("code", 500);
            ResUtil.writeJson(ServletActionContext.getResponse(), resultJson.toString());
        }
        if (keywords != null){
            s1.append(" and (a.booking_id like '%").append(keywords).append("%'");
//            s1.append(" or a.comment_name like '%").append(keywords).append("%'");
            s1.append(" or c.user_name like '%").append(keywords).append("%')");
            s2.append(" and (a.booking_id like '%").append(keywords).append("%'");
            s2.append(" or a.createName like '%").append(keywords).append("%')");
        }
        if (score != null){
            s1.append(" and a.score = ").append(score).append(" ");
            s2.append(" and a.score = ").append(score).append(" ");
        }
        if (commentStartTime != null){
            s1.append(" and DATE_FORMAT(a.create_date, '%Y-%m-%d') >= '").append(commentStartTime).append("' ");
            s2.append(" and DATE_FORMAT(a.create_date, '%Y-%m-%d') >= '").append(commentStartTime).append("' ");
        }
        if (commentEndTime != null){
            s1.append(" and DATE_FORMAT(a.create_date, '%Y-%m-%d') <= '").append(commentEndTime).append("' ");
            s2.append(" and DATE_FORMAT(a.create_date, '%Y-%m-%d') <= '").append(commentEndTime).append("' ");
        }
        if (checkoutStartTime != null){
            s1.append(" and DATE_FORMAT(b.check_out_time, '%Y-%m-%d') >= '").append(checkoutStartTime).append("' ");
            s2.append(" and DATE_FORMAT(a.checkOutTime, '%Y-%m-%d') >= '").append(checkoutStartTime).append("' ");
        }
        if (checkoutEndTime != null){
            s1.append(" and DATE_FORMAT(b.check_out_time, '%Y-%m-%d') <= '").append(checkoutEndTime).append("' ");
            s2.append(" and DATE_FORMAT(a.checkOutTime, '%Y-%m-%d') <= '").append(checkoutEndTime).append("' ");
        }
        if (status != null){
            s1.append(" and a.comment_status = ").append(status).append(" ");
            s2.append(" and a.comment_status = ").append(status).append(" ");
        }

        IPage<BookingComment> list = bookingCommentService.queryPage(s1.toString(),s2.toString(), page, rows);
        if(list != null && !"".equals(list)){
            resultJson.put("message", "查询分页成功");
            resultJson.put("code", 200);
            resultJson.put("data", list);
            ResUtil.writeJson(ServletActionContext.getResponse(), resultJson.toString());
        }else{
            resultJson.put("message", "查询分页失败");
            resultJson.put("code", 500);
            ResUtil.writeJson(ServletActionContext.getResponse(), resultJson.toString());
        }
        return null;
    }

    public String getByBookId(){
        JSONObject resultJson = new JSONObject();
        if (bookId == null) {
            resultJson.put("message", "请传入订单id");
            resultJson.put("code", 500);
            ResUtil.writeJson(ServletActionContext.getResponse(), resultJson.toString());
            return null;
        }
        BookCommentDto book = bookingCommentService.getByBookingId(bookId);
        if (book != null) {
            resultJson.put("message", "查询成功");
            resultJson.put("code", 200);
            resultJson.put("data", book);
            ResUtil.writeJson(ServletActionContext.getResponse(), resultJson.toString());
            return null;
        }else{
            resultJson.put("message", "未查到评价信息");
            resultJson.put("code", 500);
            ResUtil.writeJson(ServletActionContext.getResponse(), resultJson.toString());
            return null;
        }
    }

    public String getById(){
        JSONObject resultJson = new JSONObject();
        if (id == null) {
            resultJson.put("message", "请传入订单id");
            resultJson.put("code", 500);
            ResUtil.writeJson(ServletActionContext.getResponse(), resultJson.toString());
            return null;
        }
        BookCommentDto list = bookingCommentService.getById(id);
        if (list != null) {
            resultJson.put("message", "查询成功");
            resultJson.put("code", 200);
            resultJson.put("data", list);
            ResUtil.writeJson(ServletActionContext.getResponse(), resultJson.toString());
            return null;
        }else{
            resultJson.put("message", "未查到评价信息");
            resultJson.put("code", 500);
            ResUtil.writeJson(ServletActionContext.getResponse(), resultJson.toString());
            return null;
        }
    }

    public String insterCommpent(){
        JSONObject resultJson = new JSONObject();
        Gson gson = new Gson();
        com.alibaba.fastjson.JSONObject json = GetHttpParam.getRequestParameters(request);
        if (json == null) {
            resultJson.put("message", "请传入参数");
            ResUtil.writeJson(ServletActionContext.getResponse(), resultJson.toString());
            return null;
        }
        BookingComment bookingComment = null;
        try{
            bookingComment = gson.fromJson(json.toString(), new TypeToken<BookingComment>() {}.getType());
            if (bookingComment.getCommentId()==null || bookingComment.getContent()==null || bookingComment.getCommentParentId()==null || bookingComment.getCommentName()==null){
                resultJson.put("message", "各参数不能为空");
                resultJson.put("code", 500);
                ResUtil.writeJson(ServletActionContext.getResponse(), resultJson.toString());
                return null;
            }
            bookingComment.setCreateUsername("商家");//商家回复
            int m = bookingCommentService.insterCommpent(bookingComment);
            if (m > 0){
                resultJson.put("message", "回复成功");
                resultJson.put("code", 200);
                ResUtil.writeJson(ServletActionContext.getResponse(), resultJson.toString());
            }else {
                resultJson.put("message", "回复失败");
                resultJson.put("code", 500);
                ResUtil.writeJson(ServletActionContext.getResponse(), resultJson.toString());
                return null;
            }
        }catch (Exception e) {
            e.printStackTrace();
        }
        resultJson.put("message", "未知异常");
        resultJson.put("code", 205);
        ResUtil.writeJson(ServletActionContext.getResponse(), resultJson.toString());
        return null;
    }
}
