package com.happy.service.impl;

import com.happy.Model.Hotel;
import com.happy.Model.HotelDict;
import com.happy.Until.Func;
import com.happy.dao.HotelDao;
import com.happy.dto.IPage;
import com.happy.service.HotelDictService;
import com.happy.service.HotelService;
import org.springframework.stereotype.Service;

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

@Service("HotelService")
public class HotelImplService implements HotelService {

    @Resource
    public HotelDao hotelDao;
    @Resource
    public HotelDictService hotelDictService;

    @Override
    public int insertHotel(Hotel hotel) {
        return hotelDao.insertHotel(hotel);
    }

    @Override
    public int updateHotel(Hotel hotel) {
        return hotelDao.updateHotel(hotel);
    }

    @Override
    public int delHotel(int id) {
        return hotelDao.delHotel(id);
    }

    @Override
    public Hotel getById(int id) {
        return hotelDao.getById(id);
    }

    @Override
    public Hotel getByManagerId(int id) {
        return hotelDao.getByManagerId(id);
    }

    @Override
    public IPage<Hotel> queryPage(String sqlx, int page, int rows) {
        IPage<Hotel> iPage = new IPage();
        List<Hotel> hotelList = hotelDao.queryPage(sqlx,page,rows);
        int total = hotelDao.queryTotal(sqlx);
        iPage.setPageList(hotelList);
        iPage.setPage(page);
        iPage.setTotalPage( (int)Math.ceil((double)total/rows));
        iPage.setRows(rows);
        iPage.setTotal(total);
        return iPage;
    }

    @Override
    public IPage<Hotel> queryPagePrice(String sqlx, int page, int rows) {
        IPage<Hotel> iPage = new IPage();
        List<Hotel> hotelList = hotelDao.queryPagePrice(sqlx,page,rows);
        int total = hotelDao.queryTotalPrice(sqlx);
        iPage.setPageList(hotelList);
        iPage.setPage(page);
        iPage.setTotalPage( (int)Math.ceil((double)total/rows));
        iPage.setRows(rows);
        iPage.setTotal(total);
        return iPage;
    }

    @Override
    public IPage<Hotel> queryPreferredPagePrice(String sqlx, int page, int rows) {
        IPage<Hotel> iPage = new IPage();
        List<Hotel> hotelList = hotelDao.queryPreferredPagePrice(sqlx,page,rows);
        int total = hotelDao.queryTotalPrice(sqlx);
        iPage.setPageList(hotelList);
        iPage.setPage(page);
        iPage.setTotalPage( (int)Math.ceil((double)total/rows));
        iPage.setRows(rows);
        iPage.setTotal(total);
        return iPage;
    }

    @Override
    public IPage<Hotel> querySelectedPagePrice(String sqlx, int page, int rows) {
        IPage<Hotel> iPage = new IPage();
        List<Hotel> hotelList = hotelDao.querySelectedPagePrice(sqlx,page,rows);
        int total = hotelDao.queryTotalPrice(sqlx);
        iPage.setPageList(hotelList);
        iPage.setPage(page);
        iPage.setTotalPage( (int)Math.ceil((double)total/rows));
        iPage.setRows(rows);
        iPage.setTotal(total);
        return iPage;
    }

    public Map queryHotelDicMap()
    {
        Map map = new HashMap<>();
        List<HotelDict> list = hotelDictService.queryList("and code in (1,2,3,4) ");
        for (HotelDict h:list) {
            if (h == null || Func.checkNull(Func.parseStr(h.getId())))
                continue;

                map.put(h.getId(),h);
        }
        return map;
    }

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

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