package com.happy.action;

import com.happy.Model.Booking;
import com.happy.Model.BookingComplaint;
import com.happy.Model.Hotel;
import com.happy.Model.House;
import com.happy.Model.weixin.WeiXinUtil;
import com.happy.Until.Excel.toExcel;
import com.happy.Until.ResUtil;
import com.happy.Until.ResponseUtil;
import com.happy.Until.TimeExchange;
import com.happy.Until.UUIDUtil;
import com.happy.common.controller.BaseController;
import com.happy.dto.IPage;
import com.happy.service.*;
import com.happy.vo.BookingComplaintVo;
import net.sf.json.JSONObject;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.interceptor.ServletRequestAware;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Vector;
import java.util.logging.SimpleFormatter;

public class bookComplainAction extends BaseController implements ServletRequestAware {

    public String keywords;//关键字搜索

    public String remark;//备注

    public String complainStartTime;//起始时间

    public String complainEndTime;//结束时间

    @Resource(name = "BookingComplaintService")
    private BookingComplaintService bookingComplaintService;

    @Resource(name = "BookingComplaintProgressService")
    private BookingComplaintProgressService bookingComplaintProgressService;
    /**
     * 列表查询
     * @return
     */
    public String queryPage(){
        JSONObject resultJson = new JSONObject();
        StringBuilder s1 = new StringBuilder("");
        StringBuilder s2 = new StringBuilder("");
        if (keywords != null){
            s1.append(" and (t.id like '%").append(keywords).append("%'");
            s2.append(" and (a.id like '%").append(keywords).append("%'");
            s1.append(" or t.title like '%").append(keywords).append("%'");
            s2.append(" or a.title like '%").append(keywords).append("%'");
            s1.append(" or t.content like '%").append(keywords).append("%'");
            s2.append(" or a.content like '%").append(keywords).append("%'");
            s1.append(" or t.booking_id = '").append(keywords).append("' ");
            s2.append(" or a.booking_id = '").append(keywords).append("' ");
            s1.append(" or t.hotel_name like '%").append(keywords).append("%')");
            s2.append(" or a.hotel_name like '%").append(keywords).append("%')");
        }
        if (status != null){
            s1.append(" and t.status = ").append(status).append(" ");
            s2.append(" and a.status = ").append(status).append(" ");
        }
        if (complainStartTime != null){
            s1.append(" and DATE_FORMAT(t.create_date, '%Y-%m-%d') >= '").append(complainStartTime).append("' ");
            s2.append(" and DATE_FORMAT(create_date, '%Y-%m-%d') >= '").append(complainStartTime).append("' ");
        }
        if (complainStartTime != null){
            s1.append(" and DATE_FORMAT(t.create_date, '%Y-%m-%d') <= '").append(complainEndTime).append("' ");
            s2.append(" and DATE_FORMAT(create_date, '%Y-%m-%d') <= '").append(complainEndTime).append("' ");
        }
        IPage<BookingComplaintVo> bookingComplaintIPage = bookingComplaintService.queryPage(s1.toString(),s2.toString(), page, rows);
        if(bookingComplaintIPage!=null && !"".equals(bookingComplaintIPage)){
            resultJson.put("message", "查询分页成功");
            resultJson.put("code", 200);
            resultJson.put("data", bookingComplaintIPage);
            ResUtil.writeJson(ServletActionContext.getResponse(), resultJson.toString());
        }else{
            resultJson.put("message", "查询分页失败");
            resultJson.put("code", 500);
            ResUtil.writeJson(ServletActionContext.getResponse(), resultJson.toString());
        }
        return null;

    }


    /**
     * 确认受理
     * @return
     */
    public String acceptance(){
        JSONObject resultJson = new JSONObject();
        if(id == null){
            resultJson.put("code", 500);
            resultJson.put("message", "数据ID或备注不能为空");
            ResponseUtil.writeJson(ServletActionContext.getResponse(), resultJson.toString());
            return null;
        }

        BookingComplaint bookingComplaint = bookingComplaintService.getById(id);
        if (bookingComplaint == null) {
            resultJson.put("code", 500);
            resultJson.put("message", "投诉数据已失效，受理失败");
            ResponseUtil.writeJson(ServletActionContext.getResponse(), resultJson.toString());
            return null;
        }

        int updateStatus = bookingComplaintService.updateComplain(id,"status = 2" +(remark == null ? "" : ",remark = '"+remark+"'"));
        if(updateStatus <= 0){
            resultJson.put("code", 500);
            resultJson.put("message", "受理失败");
            ResponseUtil.writeJson(ServletActionContext.getResponse(), resultJson.toString());
            return null;
        }

        int cpInsert = bookingComplaintProgressService.insert(id,bookingComplaint.getCreateId(), TimeExchange.getTime(),TimeExchange.getTime(), "2");
        if(cpInsert <= 0){
            resultJson.put("code", 500);
            resultJson.put("message", "受理失败");
            ResponseUtil.writeJson(ServletActionContext.getResponse(), resultJson.toString());
            return null;
        }

        resultJson.put("message", "受理成功");
        resultJson.put("code", 200);
        resultJson.put("data", bookingComplaint);
        ResUtil.writeJson(ServletActionContext.getResponse(), resultJson.toString());
        return null;
    }


    /**
     * 获取备注
     */
    public String getRemarkById(){
        JSONObject resultJson = new JSONObject();
        BookingComplaint bookingComplaint = bookingComplaintService.getById(id);
        if (bookingComplaint != null) {
            resultJson.put("message", "查询成功");
            resultJson.put("code", 200);
            resultJson.put("data", bookingComplaint.getContent());
            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 updateRemarkById(){
        JSONObject resultJson = new JSONObject();

        if(id == null || remark == null){
            resultJson.put("code", 500);
            resultJson.put("message", "参数不能为空");
            ResponseUtil.writeJson(ServletActionContext.getResponse(), resultJson.toString());
            return null;
        }

        BookingComplaint bookingComplaint = bookingComplaintService.getById(id);
        if (bookingComplaint != null) {
            int updateStatus = bookingComplaintService.updateComplain(id,"remark = '"+remark+"'");
            if(updateStatus <= 0){
                resultJson.put("code", 500);
                resultJson.put("message", "添加失败");
                ResponseUtil.writeJson(ServletActionContext.getResponse(), resultJson.toString());
                return null;
            }

            resultJson.put("message", "添加成功");
            resultJson.put("code", 200);
            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();
        BookingComplaint bookingComplaint = bookingComplaintService.getDetailById(id);
        if (bookingComplaint != null) {
            resultJson.put("message", "查询成功");
            resultJson.put("code", 200);
            resultJson.put("data", bookingComplaint);
            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 toComplainExcel(){
        JSONObject resultJson = new JSONObject();
        StringBuilder s1 = new StringBuilder("");
        if (keywords != null){
            s1.append(" and a.id like '%").append(keywords).append("%'");
            s1.append(" or title like '%").append(keywords).append("%'");
            s1.append(" or content like '%").append(keywords).append("%'");
            s1.append(" or booking_id = '").append(keywords).append("' ");
            s1.append(" or h2.hname like '%").append(keywords).append("%'");
        }
        if (status != null){
            s1.append(" and a.status = ").append(status).append(" ");
        }
        if (complainStartTime != null) {
            s1.append(" and DATE_FORMAT(a.create_date, '%Y-%m-%d') >= '").append(complainStartTime).append("' ");
        }
        if (complainStartTime != null){
            s1.append(" and DATE_FORMAT(a.create_date, '%Y-%m-%d') <= '").append(complainEndTime).append("' ");
        }
        List<BookingComplaint> list = bookingComplaintService.toComplainExcel(s1.toString());
        if(list==null){
            resultJson.put("code", 500);
            resultJson.put("message", "无数据无法导出");
            ResponseUtil.writeJson(ServletActionContext.getResponse(),
                    resultJson.toString());
            return null;
        }
        List<Vector> vectorsList = new ArrayList<>();
        for(BookingComplaint bookingComplaint : list){
            Vector columnValue = new Vector();
            columnValue.add(bookingComplaint.getId());
            columnValue.add(bookingComplaint.getTitle());
            columnValue.add(bookingComplaint.getContent());
            columnValue.add(bookingComplaint.getBookingId());
            columnValue.add(bookingComplaint.getHotelName());
            columnValue.add(bookingComplaint.getHouseName());
            columnValue.add(bookingComplaint.getCreateDate());
            vectorsList.add(columnValue);
        }
        // 项目路径地址
        String path = request.getSession().getServletContext()
                .getRealPath("/download/");;// File.separator

        try{
            Vector columnName = new Vector();
            columnName.add("投诉id");
            columnName.add("投诉标题");
            columnName.add("投诉内容");
            columnName.add("订单号");
            columnName.add("酒店名称");
            columnName.add("房型");
            columnName.add("投诉时间");

            String filePathName ="投诉信息" + UUIDUtil.generateID() +".xls";
            // 导出文件
            new toExcel().book(vectorsList, path + filePathName,
                    "sheet1", columnName);
            String url = WeiXinUtil.ip_h;//"https://chtech.ncjti.edu.cn/hotelReservation/mhotel";
            resultJson.put("downurl", url + "/download/" + filePathName);
            resultJson.put("code", 200);
            resultJson.put("message", "导出成功");
            ResponseUtil.writeJson(ServletActionContext.getResponse(),
                    resultJson.toString());
        } catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }

}
