package com.happy.action;

import com.happy.Model.UserVisits;
import com.happy.Until.DateUtil;
import com.happy.Until.Enum.VisitsEnum;
import com.happy.Until.Func;
import com.happy.Until.ResUtil;
import com.happy.dao.UserVisitsDao;
import com.happy.dto.*;
import com.happy.service.IDCService;
import com.opensymphony.xwork2.ActionSupport;
import net.sf.json.JSONObject;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.interceptor.ServletRequestAware;
import org.omg.CORBA.PUBLIC_MEMBER;

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

import static com.happy.Until.Enum.VisitsEnum.今日;

public class IDCAction extends ActionSupport implements ServletRequestAware {

    private HttpServletRequest request;
    public HttpServletResponse response;

    public Integer type;//1.今日 2.本月 3.本年 4.自定义时间
    public String startTime; // 开始时间
    public String endTime;  // 结束时间

    @Resource
    private IDCService idcService;
    @Resource
    private UserVisitsDao userVisitsDao;

    public HttpServletRequest getRequest() {
        return request;
    }

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

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

    public HttpServletResponse getResponse() {
        return response;
    }

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

    /**
     * 描述：获取订单状态占比数据
     * @return
     */
    public String getBookStatusData(){
        JSONObject resultJson = new JSONObject();
        List<IDCBookStatusEto> list = idcService.getBookStatusData();
//        if (list == null) {
//            resultJson.put("message", "数据为空");
//            resultJson.put("code", 500);
//            ResUtil.writeJson(ServletActionContext.getResponse(), resultJson.toString());
//            return null;
//        }
        resultJson.put("message", "查询成功");
        resultJson.put("code", 200);
        resultJson.put("data", list);
        ResUtil.writeJson(ServletActionContext.getResponse(), resultJson.toString());
        return null;
    }

    /**
     * 描述：获取各县民宿数量
     * @return
     */
    public String getHotelData() {
        JSONObject resultJson = new JSONObject();
        List<IDCHotelEto> list = idcService.getHotelData();
//        if (list == null) {
//            resultJson.put("message", "数据为空");
//            resultJson.put("code", 500);
//            ResUtil.writeJson(ServletActionContext.getResponse(), resultJson.toString());
//            return null;
//        }
        resultJson.put("message", "查询成功");
        resultJson.put("code", 200);
        resultJson.put("data", list);
        ResUtil.writeJson(ServletActionContext.getResponse(), resultJson.toString());
        return null;
    }

    /**
     * 描述：获取订单量排行
     * @return
     */
    public String getRankBookNumData() {
        JSONObject resultJson = new JSONObject();
        StringBuilder sqlx = new StringBuilder("");
        if(type==1){
            sqlx.append("and DATE_FORMAT(pay_time,'%Y-%m-%d') = DATE_FORMAT(NOW(),'%Y-%m-%d')");
        }
        if(type==2){
            sqlx.append("and DATE_FORMAT(pay_time,'%Y-%m') = DATE_FORMAT(NOW(),'%Y-%m')");
        }
        if(type==3){
            sqlx.append("and DATE_FORMAT(pay_time,'%Y') = DATE_FORMAT(NOW(),'%Y')");
        }
        List<IDCRankEto> list = idcService.getRankBookNumData(sqlx.toString());

        if(list != null){
            int i = 1;
            for (IDCRankEto data:list) {
                data.setRank(i);
                i++;
            }
        }

        resultJson.put("message", "查询成功");
        resultJson.put("code", 200);
        resultJson.put("data", list == null ? new ArrayList<>() : list);
        ResUtil.writeJson(ServletActionContext.getResponse(), resultJson.toString());
        return null;
    }

    /**
     * 描述：获取销售额排行
     * @return
     */
    public String getRankSalesAmountData(){
        JSONObject resultJson = new JSONObject();
        StringBuilder sqlx = new StringBuilder("");
        if(type==1){
            sqlx.append("AND DATE_FORMAT(pay_time,'%Y-%m-%d') = DATE_FORMAT(NOW(),'%Y-%m-%d')");
        }
        if(type==2){
            sqlx.append("AND DATE_FORMAT(pay_time,'%Y-%m') = DATE_FORMAT(NOW(),'%Y-%m')");
        }
        if(type==3){
            sqlx.append("AND DATE_FORMAT(pay_time,'%Y') = DATE_FORMAT(NOW(),'%Y')");
        }
        List<IDCSaleEto> list = idcService.getRankSalesAmountData(sqlx.toString());

        if(list != null){
            int i = 1;
            for (IDCSaleEto data:list) {
                data.setRank(i);
                i++;
            }
        }

        resultJson.put("message", "查询成功");
        resultJson.put("code", 200);
        resultJson.put("data", list == null ? new ArrayList<>() : list);
        ResUtil.writeJson(ServletActionContext.getResponse(), resultJson.toString());
        return null;
    }

    /**
     * 描述：获取销售额排行
     * @return
     */
    public String getSumData(){
        JSONObject resultJson = new JSONObject();

        IDCSum idcSum = idcService.getSumData();
//        if (idcSum == null) {
//            resultJson.put("message", "数据为空");
//            resultJson.put("code", 500);
//            ResUtil.writeJson(ServletActionContext.getResponse(), resultJson.toString());
//            return null;
//        }
        resultJson.put("message", "查询成功");
        resultJson.put("code", 200);
        resultJson.put("data", idcSum);
        ResUtil.writeJson(ServletActionContext.getResponse(), resultJson.toString());
        return null;
    }

    /**
     * 访问量,今日访问量
     * 返回数据
     * todayCount,  今日访问量
     * yesterdayCount,  昨日访问量
     *  compare  相对比
     * @return
     */
    public String visitsToday()
    {
        JSONObject resultJson = new JSONObject();
        UserVisits info = userVisitsDao.queryTodayUserVisits();
//        if (info == null) {
//            resultJson.put("message", "数据为空");
//            resultJson.put("code", 500);
//            ResUtil.writeJson(ServletActionContext.getResponse(), resultJson.toString());
//            return null;
//        }

        resultJson.put("message", "查询成功");
        resultJson.put("code", 200);
        resultJson.put("data", info);
        ResUtil.writeJson(ServletActionContext.getResponse(), resultJson.toString());
        return null;
    }

    /**
     * 访问量趋势 今日、本周、本月
     * @return
     */
    public String visitsDiyQuery(){
        JSONObject resultJson = new JSONObject();
        List list = new ArrayList();
        try {
            switch (VisitsEnum.getEunmBykey(type)){
                case 今日:
                    list = getTodayInfo();
                    break;
                case 本周:
                    list = getWeekInfo();
                    break;
                case 本月:
                    list = getMonthInfo();
                    break;
                case 自定义:
                    list = getDiyInfo(startTime, endTime);
                    break;
                case 状态异常:
                    break;
            }
            resultJson.put("message", "查询成功");
            resultJson.put("code", 200);
            resultJson.put("data", list);
            ResUtil.writeJson(ServletActionContext.getResponse(), resultJson.toString());
            return null;
        } catch (Exception e) {
            resultJson.put("message", e.getMessage());
            resultJson.put("code", 400);
            resultJson.put("data", list);
            ResUtil.writeJson(ServletActionContext.getResponse(), resultJson.toString());
            return null;
        }
    }

    // 自定义时间查询
    private List getDiyInfo(String startTime,String endTime) throws Exception
    {
        if (!(DateUtil.isDateStr(startTime,true) && DateUtil.isDateStr(endTime,true)))
            throw new RuntimeException("请输入正确的时间");

//        if (DateUtil.getDayDiff(startTime,endTime) > 31)
//            throw new RuntimeException("查询天数仅31天之内");


        return userVisitsDao.queryVisitsByDiyTime(startTime,endTime);
    }

    // 今日
    private List getTodayInfo()
    {
        String sql = " DATE_ADD(DATE_FORMAT(NOW(),'%Y-%m-%d'), INTERVAL seq DAY) = DATE_FORMAT(NOW(),'%Y-%m-%d') ";
        return userVisitsDao.queryVisitsBySql(sql);
    }
    // 本周
    private List getWeekInfo()
    {
        String sql = "DATE_ADD(DATE_FORMAT(NOW(),'%Y-%m-%d'), INTERVAL seq DAY) >= DATE_SUB(CURDATE(), INTERVAL WEEKDAY(CURDATE()) DAY) \n" +
                "AND DATE_ADD(DATE_FORMAT(NOW(),'%Y-%m-%d'), INTERVAL seq DAY) <= DATE_SUB(CURDATE(), INTERVAL WEEKDAY(CURDATE())-6 DAY)";
        return userVisitsDao.queryVisitsBySql(sql);
    }
    // 本月
    private List getMonthInfo()
    {
        String sql = "DATE_ADD(DATE_FORMAT(NOW(),'%Y-%m-%d'), INTERVAL seq DAY) >= DATE_ADD(CURDATE(),interval -day(CURDATE())+1 day)\n" +
                "AND DATE_ADD(DATE_FORMAT(NOW(),'%Y-%m-%d'), INTERVAL seq DAY) <= LAST_DAY(CURDATE())";
        return userVisitsDao.queryVisitsBySql(sql);
    }


}
