package com.happy.service.impl;

import com.happy.Model.Hotel;
import com.happy.Model.House;
import com.happy.Model.House;
import com.happy.Model.HouseNumber;
import com.happy.dao.HouseDao;
import com.happy.dao.HouseNumberDao;
import com.happy.dto.HouseSumEto;
import com.happy.dto.IPage;
import com.happy.service.HouseService;
import com.happy.vo.PriceHotelDataVo;
import org.springframework.stereotype.Service;

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

@Service("HouseService")
public class HouseImplService implements HouseService {

    @Resource
    public HouseDao houseDao;

    @Resource
    public HouseNumberDao houseNumberDao;

    @Override
    public int insertHouse(House house) {
        return houseDao.insertHouse(house);
    }

    @Override
    public int updateHouse(House house) {
        return houseDao.updateHouse(house);
    }

    @Override
    public int delHouse(int id) {
        return houseDao.delHouse(id);
    }

    @Override
    public int delHouse(String id) {
        return houseDao.delHouse(id);
    }

    @Override
    public House getById(String id) {
        House house = houseDao.getById(id);
        house.setHouseNumbers(houseNumberDao.queryByHouseId(String.valueOf(id)));
        return house;
    }

    @Override
    public IPage<House> queryPage(String sqlx, int page, int rows) {
        IPage<House> iPage = new IPage();
        List<House> houseList = houseDao.queryPage(sqlx,page,rows);
        if(houseList != null){
            houseList.forEach(house -> {
                house.setHouseNumbers(houseNumberDao.queryByHouseId(String.valueOf(house.getId())));
            });
        }

        int total = houseDao.queryTotal(sqlx);
        iPage.setPageList(houseList);
        iPage.setPage(page);
        iPage.setTotalPage( (int)Math.ceil((double)total/rows));
        iPage.setRows(rows);
        iPage.setTotal(total);
        return iPage;
    }

    @Override
    public List<House> queryList(String sqlx) {
        return houseDao.queryList(sqlx);
    }


    @Override
    public House getOen(String sqlx){
        List<House> list = houseDao.queryList(sqlx);
        if(list!=null && list.size()>0) return list.get(0);
        return null;
    }

    @Override
    public IPage<HouseSumEto> queryPageHouseSum(String sqlx1,String sqlx2, int page, int rows){
        IPage<HouseSumEto> iPage = new IPage();
        List<HouseSumEto> houseList = houseDao.queryPageHouseSum(sqlx1,sqlx2,page,rows);
        int total = houseDao.queryPageHouseSumTotal(sqlx1,sqlx2);
        iPage.setPageList(houseList);
        iPage.setPage(page);
        iPage.setTotalPage( (int)Math.ceil((double)total/rows));
        iPage.setRows(rows);
        iPage.setTotal(total);
        return iPage;
    }

    @Override
    public int getHouseSum(int managerId) {
        return houseDao.getHouseSum(managerId);
    }

    @Override
    public Double getHouseMinPrice(int managerId) {
        return houseDao.getHouseMinPrice(managerId);
    }

    @Override
    public List<PriceHotelDataVo> gethotelMinPrice(String managerIds) {
        List<PriceHotelDataVo> result =  houseDao.gethotelMinPrice(managerIds);
        return result;
    }


    @Override
    public Double queryPriceByHouseId(String houseId) {
        return houseDao.queryPriceByHouseId(houseId);
    }
}
