package com.happy.action;

import com.alibaba.fastjson.JSONObject;
import com.happy.Model.Holiday;
import com.happy.Until.GetHttpParam;
import com.happy.Until.ResUtil;
import com.happy.common.controller.BaseController;
import com.happy.dto.HouseNumberStatusDto;
import com.happy.service.HouseNumberStatusService;
import com.opensymphony.xwork2.ModelDriven;
import lombok.SneakyThrows;
import org.apache.struts2.ServletActionContext;

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

import static com.happy.Until.HolidayUtil.getYearHoliday;

public class HouseNumberStatusAction extends BaseController implements ModelDriven<HouseNumberStatusDto> {
    private final HouseNumberStatusDto houseNumberStatusDto = new HouseNumberStatusDto();

    @Override
    public HouseNumberStatusDto getModel() {
        return houseNumberStatusDto;
    }

    @Resource(name = "HouseNumberStatusService")
    private HouseNumberStatusService houseNumberStatusService;

    /**
     * 根据年份获取节假日数据
     */
    public String queryHoliday() {
        String year = houseNumberStatusDto.getYear();

        if (year.length() > 4) {
            ResUtil.writeJson(ServletActionContext.getResponse(), new JSONObject() {{
                put("message", "请输入年份");
                put("code", 500);
            }}.toString());
            return null;
        }

        if (year == null) {
            ResUtil.writeJson(ServletActionContext.getResponse(), new JSONObject() {{
                put("message", "请传入参数");
                put("code", 500);
            }}.toString());
            return null;
        }


        List<Holiday> days = getYearHoliday("2023");

        if (days == null) {
            ResUtil.writeJson(ServletActionContext.getResponse(), new JSONObject() {{
                put("message", "第三方接口调用失败");
                put("code", 500);
            }}.toString());
            return null;
        }

        if (days.size() <= 0) {
            ResUtil.writeJson(ServletActionContext.getResponse(), new JSONObject() {{
                put("message", "第三方接口调用失败");
                put("code", 500);
            }}.toString());
            return null;
        }

        houseNumberStatusService.deleteHolidayByYear(year);

        int lenegth = houseNumberStatusService.saveHolidayBatch(days);
        if (lenegth <= 0) {
            ResUtil.writeJson(ServletActionContext.getResponse(), new JSONObject() {{
                put("message", "节假日数据存储失败");
                put("code", 500);
            }}.toString());
            return null;
        }

        ResUtil.writeJson(ServletActionContext.getResponse(), new JSONObject() {{
            put("message", "节假日数据存储成功");
            put("code", 200);
        }}.toString());
        return null;
    }

    /**
     * 表格数据 房态管理-房态管理
     */
    public void queryData() {
        JSONObject objects = houseNumberStatusService.queryData(houseNumberStatusDto,page,rows);
        if (objects != null) {
            ResUtil.writeJson(ServletActionContext.getResponse(), new JSONObject() {{
                put("message", "请求成功");
                put("code", 200);
                put("data", objects);
            }}.toString());
        } else {
            ResUtil.writeJson(ServletActionContext.getResponse(), new JSONObject() {{
                put("message", "该房型未新增房间号");
                put("code", 200);
            }}.toString());
        }
    }

    /**
     * 查询可置脏/净、可开/关的房型及房间号 房态管理-房态管理
     */
    public void queryListWithHouse() {
        ResUtil.writeJson(ServletActionContext.getResponse(), new JSONObject() {{
            put("message", "请求成功");
            put("code", 200);
            put("data", houseNumberStatusService.queryListWithHouse(houseNumberStatusDto));
        }}.toString());

    }

    /**
     * 批量开/关房 房态管理-房态管理
     */
    @SneakyThrows
    public void modifyStatusBatch() {
        String postDataStr = GetHttpParam.getRequestPostData(request);
        HouseNumberStatusDto postDataObj = JSONObject.parseObject(postDataStr, HouseNumberStatusDto.class);
        //2023-09-20 A-jax 参数判断
        if(postDataObj.getHouseNumberIds() == null){
            ResUtil.writeJson(ServletActionContext.getResponse(), new JSONObject() {{
                put("message", "房间不能为空");
                put("code", 500);
            }}.toString());
        }
        if(postDataObj.getStatus() == 2 && postDataObj.getCreateId() == null){
            ResUtil.writeJson(ServletActionContext.getResponse(), new JSONObject() {{
                put("message", "关房类型不能为空");
                put("code", 500);
            }}.toString());
        }
        if(postDataObj.getSetDate() == null){
            ResUtil.writeJson(ServletActionContext.getResponse(), new JSONObject() {{
                put("message", "日期不能为空");
                put("code", 500);
            }}.toString());
        }

        houseNumberStatusService.modifyStatusBatch(postDataObj);
        ResUtil.writeJson(ServletActionContext.getResponse(), new JSONObject() {{
            put("message", "请求成功");
            put("code", 200);
        }}.toString());
    }

    /**
     * 开/关房 房态管理-房态管理
     */
    @SneakyThrows
    public void modifyStatus() {
        if(houseNumberStatusDto.getCloseType() == null){
            ResUtil.writeJson(ServletActionContext.getResponse(), new JSONObject() {{
                put("message", "关房类型不能为空");
                put("code", 500);
            }}.toString());
        }

        houseNumberStatusService.modifyStatus(houseNumberStatusDto);
        ResUtil.writeJson(ServletActionContext.getResponse(), new JSONObject() {{
            put("message", "请求成功");
            put("code", 200);
        }}.toString());
    }
}
