package com.happy.action;

import com.alibaba.fastjson.JSONArray;
import com.google.gson.Gson;
import com.alibaba.fastjson.JSONObject;
import com.google.gson.reflect.TypeToken;
import com.happy.Model.*;
import com.happy.Until.*;
import com.happy.common.controller.BaseController;
import com.happy.dto.HouseDto;
import com.happy.dto.HouseNumberStatusDto;
import com.happy.dto.HouseSumEto;
import com.happy.dto.IPage;
import com.happy.service.*;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.interceptor.ServletRequestAware;

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

public class houseAction extends BaseController implements ModelDriven<HouseDto> {

    private final HouseDto houseDto = new HouseDto();

    @Override
    public HouseDto getModel() {
        return houseDto;
    }

    @Resource
    public HouseService houseService;
    @Resource
    public BookService bookService;
    @Resource
    public FileService fileService;
    @Resource
    public HotelDictService hotelDictService;
    @Resource
    public HouseNumberService houseNumberService;

    public String orderTime;//住房时间

    public String fileListJson;
    public String houseNumber; // 房间号

    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 insterHouser(){
        JSONObject resultJson = new JSONObject();
        if (houseDto == null) {
            resultJson.put("message", "请传入参数");
            ResUtil.writeJson(ServletActionContext.getResponse(), resultJson.toString());
            return null;
        }
        House house = new House();
        try {
            house.setManagerId(houseDto.getManagerId());
            house.sethName(houseDto.getHName());
            house.sethAreas(houseDto.getHAreas());
            house.setPrice(houseDto.getPrice());
            house.setNumber(houseDto.getNumber());
            house.sethConfig(houseDto.getHConfig());
            house.setFileInfoList(houseDto.getFileInfoList());
            house.setHouseNumbers(houseDto.getHouseNumbers());
            house.setStatus(houseDto.getStatus());

            if (house == null) {
                resultJson.put("message", "数据为空");
                resultJson.put("code", 500);
                ResUtil.writeJson(ServletActionContext.getResponse(), resultJson.toString());
                return null;
            }

            if (house.getManagerId()==null || house.gethName()==null || house.getPrice()==null || house.getNumber()==null || house.gethAreas()==null){
                resultJson.put("message", "各参数不能为空");
                resultJson.put("code", 500);
                ResUtil.writeJson(ServletActionContext.getResponse(), resultJson.toString());
                return null;
            }
            if(houseNumber == null ){
                resultJson.put("message", "房间号不能为空");
                resultJson.put("code", 500);
                ResUtil.writeJson(ServletActionContext.getResponse(), resultJson.toString());
                return null;
            }

            //2023-09-19 A-jax 房型数量必须和房间数量一致
            List<String> houseNumberList = Arrays.asList(houseNumber.split(","));
            if(house.getNumber().intValue() != houseNumberList.size()){
                resultJson.put("message", "房型数量与房间数量不一致");
                resultJson.put("code", 500);
                ResUtil.writeJson(ServletActionContext.getResponse(), resultJson.toString());
                return null;
            }


            //详细图附件
            List<FileInfo> fileList = JSONArray.parseArray(fileListJson, FileInfo.class);
//            if(fileList==null){
//                resultJson.put("message", "请上传详细图");
//                ResUtil.writeJson(ServletActionContext.getResponse(), resultJson.toString());
//                return null;
//            }
//            List<String> numberList = JSONArray.parseArray(houseNumberList, String.class);

            StringBuilder getOneSqlx = new StringBuilder("");
            getOneSqlx.append(" and h_name = '").append(house.gethName()).append("'")
                    .append(" and manager_id = '").append(house.getManagerId()).append("'");
            House listc = houseService.getOen(getOneSqlx.toString());
            if (listc != null) {
                resultJson.put("message", "房型已存在");
                resultJson.put("code", 500);
                ResUtil.writeJson(ServletActionContext.getResponse(), resultJson.toString());
                return null;
            }
            //房型验证成功添加房号
            String id = Func.newGuid();
            List<HouseNumber> houseNumbers = new ArrayList<HouseNumber>();
            long count = houseNumberList.stream().distinct().count();
            if (count != houseNumberList.size()){
                resultJson.put("message", "房号不可重复");
                resultJson.put("code", 500);
                ResUtil.writeJson(ServletActionContext.getResponse(), resultJson.toString());
                return null;
            }
            for (String s : houseNumberList){
                HouseNumber h1 = new HouseNumber();
                h1.setHouseId(id);
                h1.setNumberName(s);
                h1.setStatus(1);
                houseNumbers.add(h1);
            }
            //执行批量房间号表操作
            int a = houseNumberService.saveBatch(houseNumbers);
            house.setId(id);
            int m = houseService.insertHouse(house);
            if (m > 0) {
                for(FileInfo file : fileList){
                    FileInfo fileInfo = fileService.getById(file.getId());
                    file.setLinkId(String.valueOf(house.getId()));
                    if(fileInfo==null){
                        fileService.insertFile(file);
                    }else{
                        fileService.updateFile(file);
                    }
                }
                resultJson.put("message", "新增成功");
                resultJson.put("code", 200);
                ResUtil.writeJson(ServletActionContext.getResponse(), resultJson.toString());
                return null;
            } else {
                resultJson.put("message", "新增失败");
                resultJson.put("code", 502);
                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;
    }

    /**
     * 描述：修改房型
     * @return
     */
    public String updateHouser(){
        JSONObject resultJson = new JSONObject();
        if (houseDto == null) {
            resultJson.put("message", "请传入参数");
            ResUtil.writeJson(ServletActionContext.getResponse(), resultJson.toString());
            return null;
        }
        House house = new House();
        try {
            house.setId(houseDto.getId());
            house.setManagerId(houseDto.getManagerId());
            house.sethName(houseDto.getHName());
            house.sethAreas(houseDto.getHAreas());
            house.setPrice(houseDto.getPrice());
            house.setNumber(houseDto.getNumber());
            house.sethConfig(houseDto.getHConfig());
            house.setFileInfoList(houseDto.getFileInfoList());
            house.setHouseNumbers(houseDto.getHouseNumbers());

            if (house == null) {
                resultJson.put("message", "数据为空");
                resultJson.put("code", 500);
                ResUtil.writeJson(ServletActionContext.getResponse(), resultJson.toString());
                return null;
            }

            //2023-09-19 A-jax 房型数量必须和房间数量一致
            List<String> houseNumberList = Arrays.asList(houseNumber.split(","));

            long disNum = houseNumberList.stream().distinct().count();
            if(houseNumberList.size() != disNum){
                resultJson.put("message", "房间号存在重复数据");
                resultJson.put("code", 500);
                ResUtil.writeJson(ServletActionContext.getResponse(), resultJson.toString());
                return null;
            }

            if(house.getNumber().intValue() != houseNumberList.size()){
                resultJson.put("message", "房型数量与房间数量不一致");
                resultJson.put("code", 500);
                ResUtil.writeJson(ServletActionContext.getResponse(), resultJson.toString());
                return null;
            }

            if (house.getId() == null) {
                resultJson.put("message", "房型Id为空");
                resultJson.put("code", 500);
                ResUtil.writeJson(ServletActionContext.getResponse(), resultJson.toString());
                return null;
            }
            if (house.getManagerId()==null || house.gethName()==null || house.getPrice()==null || house.getNumber()==null || house.gethAreas()==null){
                resultJson.put("message", "各参数不能为空");
                resultJson.put("code", 500);
                ResUtil.writeJson(ServletActionContext.getResponse(), resultJson.toString());
                return null;
            }
            //详细图附件
            List<FileInfo> fileList = JSONArray.parseArray(fileListJson, FileInfo.class);
//            if(fileList==null){
//                resultJson.put("message", "请上传详细图");
//                ResUtil.writeJson(ServletActionContext.getResponse(), resultJson.toString());
//                return null;
//            }
//            List<String> numberList = JSONArray.parseArray(houseNumberList, String.class);

            StringBuilder getOneSqlx = new StringBuilder("");
            getOneSqlx.append(" and h_name = '").append(house.gethName()).append("'");
            getOneSqlx.append(" and manager_id = '").append(house.getManagerId()).append("'");
            getOneSqlx.append(" and id != '").append(house.getId()).append("'");
            House listc = houseService.getOen(getOneSqlx.toString());
            if (listc != null) {
                resultJson.put("message", "房型已存在");
                resultJson.put("code", 500);
                ResUtil.writeJson(ServletActionContext.getResponse(), resultJson.toString());
                return null;
            }
            //验证成功修改房号表
            List<HouseNumber> houseNumbers = new ArrayList<HouseNumber>();
            for (String s : houseNumberList){
                HouseNumber h1 = new HouseNumber();
                h1.setHouseId(house.getId());
                h1.setNumberName(s);
                h1.setStatus(1);
                houseNumbers.add(h1);
            }
            int a = houseNumberService.updateBatch(houseNumbers);
            int m = houseService.updateHouse(house);
            if (m > 0) {
                if(house.getId()!= null){
                    fileService.delLinkFile(String.valueOf(house.getId()));
                }
                for(FileInfo file : fileList){
                    FileInfo fileInfo = fileService.getById(file.getId());
                    file.setLinkId(String.valueOf(house.getId()));
                    if(fileInfo==null){
                        fileService.insertFile(file);
                    }else{
                        fileService.updateFile(file);
                    }
                }
                resultJson.put("message", "修改成功");
                resultJson.put("code", 200);
                ResUtil.writeJson(ServletActionContext.getResponse(), resultJson.toString());
                return null;
            } else {
                resultJson.put("message", "修改败");
                resultJson.put("code", 502);
                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;
    }

    /**
     * 描述：删除房型
     * @return
     */
    public String delHouser(){
        JSONObject resultJson = new JSONObject();
        if (houseDto == null || houseDto.getId() == null) {
            resultJson.put("message", "请传入id");
            resultJson.put("code", 500);
            ResUtil.writeJson(ServletActionContext.getResponse(), resultJson.toString());
            return null;
        }
        int m = houseService.delHouse(houseDto.getId());
        if (m > 0) {
            resultJson.put("message", "删除成功");
            resultJson.put("code", 200);
            ResUtil.writeJson(ServletActionContext.getResponse(), resultJson.toString());
            return null;
        }
        resultJson.put("message", "未知异常");
        resultJson.put("code", 205);
        ResUtil.writeJson(ServletActionContext.getResponse(), resultJson.toString());
        return null;
    }

    /**
     * 描述：根据Id查询详细
     * @return
     */
    public String getById(){
        JSONObject resultJson = new JSONObject();
        if (houseDto == null || houseDto.getId() == null) {
            resultJson.put("message", "请传入id");
            resultJson.put("code", 500);
            ResUtil.writeJson(ServletActionContext.getResponse(), resultJson.toString());
            return null;
        }
        House house = houseService.getById(houseDto.getId());

        StringBuilder fileSqlx = new StringBuilder("");
        fileSqlx.append("and link_id = '").append(id).append("'");
        List<FileInfo> fileInfoList = fileService.queryList(fileSqlx.toString());
        house.setFileInfoList(fileInfoList);
        if (house != null) {
            //获取有效房型服务
            if(house.gethConfig() != null && !"".equals(house.gethConfig())){
                house.sethConfig(hotelDictService.setConfig(house.gethConfig()));
            }
            resultJson.put("message", "查询成功");
            resultJson.put("code", 200);
            resultJson.put("data", house);
            ResUtil.writeJson(ServletActionContext.getResponse(), resultJson.toString());
            return null;
        }else{
            resultJson.put("message", "未查到信息");
            resultJson.put("code", 500);
            ResUtil.writeJson(ServletActionContext.getResponse(), resultJson.toString());
            return null;
        }
    }

    /**
     * 描述：分页查询
     * @return
     */
    public String queryPage(){
        JSONObject resultJson = new JSONObject();
        StringBuilder s1 = new StringBuilder("");
        if (houseDto != null && houseDto.getHName() !=null){
            s1.append(" and h_name like '%").append(houseDto.getHName()).append("%'");
        }
        if (houseDto != null && houseDto.getManagerId() !=null){
            s1.append(" and manager_id = '").append(houseDto.getManagerId()).append("'");
        }
        IPage<House> houseIPage = houseService.queryPage(s1.toString(),page,rows);
        if(houseIPage!=null && !"".equals(houseIPage)){
            resultJson.put("message", "查询分页成功");
            resultJson.put("code", 200);
            resultJson.put("data", houseIPage);
            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 queryPageHouseSum(){
        JSONObject resultJson = new JSONObject();
        StringBuilder s1 = new StringBuilder("");
        StringBuilder s2 = new StringBuilder("");
        if (orderTime!=null){
            s1.append("  and DATE_FORMAT(order_start_time,'%Y-%m-%d') = '").append(orderTime).append("'");
        }else{
            orderTime = UUIDUtil.getNewDate().substring(0,10);
        }
        if (houseDto != null && houseDto.getHName() !=null){
            s2.append("  and a.h_name like '%").append(houseDto.getHName()).append("%'");
        }
        if (houseDto != null && houseDto.getManagerId() !=null){
            s2.append("  and a.manager_id = '").append(houseDto.getManagerId()).append("'");
        }
        IPage<HouseSumEto> houseIPage = houseService.queryPageHouseSum(orderTime,s2.toString(),page,rows);
        if(houseIPage!=null && !"".equals(houseIPage)){
            resultJson.put("message", "查询分页成功");
            resultJson.put("code", 200);
            resultJson.put("data", houseIPage);
            ResUtil.writeJson(ServletActionContext.getResponse(), resultJson.toString());
        }else{
            resultJson.put("message", "查询分页失败");
            resultJson.put("code", 500);
            ResUtil.writeJson(ServletActionContext.getResponse(), resultJson.toString());
        }
        return null;
    }

}
