package com.happy.service.impl;

import com.happy.Model.AdminManager;
import com.happy.Model.Hotel;
import com.happy.Model.HotelCoupon;
import com.happy.Until.DateUtil;
import com.happy.dao.HotelCouponDao;
import com.happy.dao.HotelDao;
import com.happy.dto.IPage;
import com.happy.service.HotelCoupomService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
@Service("hotelCoupomService")
public class HotelCoupomImplService implements HotelCoupomService {

    @Resource
    private HotelCouponDao hotelCouponDao;

    @Resource
    public HotelDao hotelDao;

    @Override
    public int insert(HotelCoupon hotelCoupon) {
        return hotelCouponDao.insert(hotelCoupon);
    }

    @Override
    public int update(HotelCoupon hotelCoupon) {
        return hotelCouponDao.update(hotelCoupon);
    }

    @Override
    public int delete(Integer id) {
        return hotelCouponDao.delete(id);
    }

    @Override
    public HotelCoupon getById(String id) {
        return hotelCouponDao.getById(id);
    }

    @Override
    public IPage<HotelCoupon> queryPage(String sqlx, int page, int rows) {
        IPage<HotelCoupon> iPage = new IPage();
        List<HotelCoupon> hotelCouponList = hotelCouponDao.queryPage(sqlx,page,rows);
        hotelCouponList.forEach(hotelCoupon -> {
            String hotelIds = hotelCoupon.getHotelIds();
            String sql = " and id in ( " + hotelIds + " )";
            List<Hotel> hotels = hotelDao.queryList(sql);
            if (hotels!=null){
                StringBuilder stringBuilder = new StringBuilder();
                hotels.forEach(hotel -> {
                    stringBuilder.append(hotel.getHname()).append(",");
                });
                String hotelNames = stringBuilder.deleteCharAt(stringBuilder.length() - 1).toString();
                hotelCoupon.setHotelNames(hotelNames);
            }
        });
        int total = hotelCouponDao.queryTotal(sqlx);
        iPage.setPageList(hotelCouponList);
        iPage.setPage(page);
        iPage.setTotalPage( (int)Math.ceil((double)total/rows));
        iPage.setRows(rows);
        iPage.setTotal(total);
        return iPage;
    }

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

    @Override
    public  int updateExpire() {
        return hotelCouponDao.updateExpire();
    }

    @Override
    public int updateLapse(List<String> coupomIds) {
        return hotelCouponDao.updateLapse(coupomIds);
    }
}
