package com.happy.action;

import com.alibaba.fastjson.JSONObject;
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;

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 void queryData() {
        JSONObject objects = houseNumberStatusService.queryData(houseNumberStatusDto);
        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);
        houseNumberStatusService.modifyStatusBatch(postDataObj);
        ResUtil.writeJson(ServletActionContext.getResponse(), new JSONObject() {{
            put("message", "请求成功");
            put("code", 200);
        }}.toString());
    }

    /**
     * 开/关房 房态管理-房态管理
     */
    @SneakyThrows
    public void modifyStatus() {
        houseNumberStatusService.modifyStatus(houseNumberStatusDto);
        ResUtil.writeJson(ServletActionContext.getResponse(), new JSONObject() {{
            put("message", "请求成功");
            put("code", 200);
        }}.toString());
    }
}
