package com.happy.service.impl;

import com.happy.Model.Booking;
import com.happy.Model.Hotel;
import com.happy.Model.House;
import com.happy.Model.weixin.Users;
import com.happy.Until.Enum.OrderEnum;
import com.happy.Until.Enum.TempEnum;
import com.happy.Until.Enum.VisitsEnum;
import com.happy.Until.Func;
import com.happy.dao.HotelDao;
import com.happy.dao.UserDao;
import com.happy.dto.IPage;
import com.happy.service.HotelService;
import com.happy.service.UserService;
import com.happy.vo.UnevaluatedOrder;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.namedparam.MapSqlParameterSource;
import org.springframework.stereotype.Service;

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

@Service("UserService")
public class UserServiceImpl implements UserService {

    @Resource
    public UserDao userDao;
    @Resource
    public HotelDao hotelDao;

    public int insert(Users users){
        return userDao.insert(users);
    }

    // 修改信息
    public int update(Users users){
        return userDao.update(users);
    }

    public int del(int id){
        return userDao.del(id);
    }

    public Users queryByNum(String card_number){
        return userDao.queryByNum(card_number);
    }

    public int updateOpenid(String openid,String card_number){
        return userDao.updateOpenid(openid, card_number);
    }

    public Users queryByOpenid(String openid){
        return userDao.queryByOpenid(openid);
    }

    public Users queryByUserId(String userId){
        return userDao.queryByUserId(userId);
    }

    // 查询用户表中的总记录数
    public int queryUserTotal(String sqlStr){
        return userDao.queryUserTotal(sqlStr);
    }

    public IPage<Users> queryUserPage(String sqlx, int page, int rows){
        IPage<Users> iPage = new IPage();
        List<Users> usersList = userDao.queryUserPage(sqlx,page,rows);
        int total = userDao.queryUserTotal(sqlx);
        iPage.setPageList(usersList);
        iPage.setPage(page);
        iPage.setTotalPage( (int)Math.ceil((double)total/rows));
        iPage.setRows(rows);
        iPage.setTotal(total);
        return iPage;
    }

    @Override
    public Users addhotelAndUsers(String hotelId, String userId, TempEnum type)
    {
        if (Func.checkNull(hotelId) || Func.checkNull(userId))
            throw new RuntimeException("未传入数据hotelId、userId、type");

        Users users = userDao.queryByUserId(userId);
        Hotel hotel = hotelDao.getById(Func.parseInt(hotelId));
        if (users == null || hotel == null)
            throw new RuntimeException("无法查询到数据users-hotel");

        String live = users.getLive_hotel();
        if (live==null) {
            live="";
        }
        String collect = users.getCollect_hotel();
        if (collect==null) {
            collect="";
        }
        switch (type){
            case 住过:
                if (Func.checkNull(live) || !live.contains(hotelId))
                    users.setLive_hotel(live + hotelId+ ",");
                break;
            case 收藏:
                if (Func.checkNull(collect) || !collect.contains(hotelId))
                    users.setCollect_hotel(collect + hotelId+ ",");
                break;
        }
        userDao.update(users);
        return users;
    }
    @Override
    public Users delhotelAndUsers(String hotelId, String userId, TempEnum type)
    {
        if (Func.checkNull(hotelId) || Func.checkNull(userId))
            throw new RuntimeException("未传入数据hotelId、userId、type");

        Users users = userDao.queryByUserId(userId);
        Hotel hotel = hotelDao.getById(Func.parseInt(hotelId));
        if (users == null || hotel == null)
            throw new RuntimeException("无法查询到数据users-hotel");

        String live = users.getLive_hotel();
        String collect = users.getCollect_hotel();
        switch (type){
            case 住过:
                if (Func.checkNull(live))
                    break;
                if (live.contains(hotelId+ ","))
                    users.setLive_hotel(live.replace(hotelId+ ",",""));

                break;
            case 收藏:
                if (Func.checkNull(collect))
                    break;

                if (collect.contains(hotelId+ ","))
                    users.setCollect_hotel(collect.replace(hotelId+ ",",""));
                break;
        }

        userDao.update(users);
        return users;
    }

    @Override
    public UnevaluatedOrder collect(String hotelId1, Integer createId1) {
        UnevaluatedOrder vo=userDao.collect(hotelId1,createId1);
        Integer count = vo.getCount();
        if (count<=0) {
            vo.setIsCollect(false);
        }else {
            vo.setIsCollect(true);
        }
        return vo;
    }


}
