package com.happy.service.impl;

import com.happy.Model.Booking;
import com.happy.Model.HotelDict;
import com.happy.Model.HouseNumber;
import com.happy.Model.HouseNumberStatus;
import com.happy.Model.app.News;
import com.happy.Model.app.Tongji;
import com.happy.Until.*;
import com.happy.Until.Enum.PayEnum;
import com.happy.dao.BookDao;
import com.happy.dao.HotelDictDao;
import com.happy.dto.BookTypeEto;
import com.happy.dto.IPage;
import com.happy.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.namedparam.MapSqlParameterSource;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.text.ParseException;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.*;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicReference;

@Service("BookService")
public class BookImplService implements BookService {

    @Resource
    public BookDao bookDao;

    @Resource
    public HotelDictDao hotelDictDao;
    @Resource
    public BookinglogService bookinglogService;

    @Resource
    public AdminManagerService adminManagerService;

    @Resource
    public HotelDictService hotelDictService;

    @Resource
    public HouseNumberService houseNumberService;

    @Resource
    public HouseNumberStatusService houseNumberStatusService;

    @Autowired
    private HttpServletRequest request;

    @Override
    public int insertBooking(Booking book) {
        return bookDao.insertBooking(book);
    }

    @Override
    public int updateBooking(Booking book) {
        return bookDao.updateBooking(book);
    }

    @Override
    public int delBooking(int id) {
        return bookDao.delBooking(id);
    }

    @Override
    public Booking getById(int id) {
        return bookDao.getById(id);
    }

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

    // 订单金额统计
    public Tongji queryOrderT(String sqlx){
        return bookDao.queryOrderT(sqlx);
    }

    // 支付金额统计
    public Tongji queryPayT(String sqlx){
        return bookDao.queryPayT(sqlx);
    }

    // 退款金额统计
    public Tongji queryRefundT(String sqlx){
        return bookDao.queryRefundT(sqlx);
    }

    // 入账金额统计
    public Tongji queryInT(String sqlx){
        return bookDao.queryInT(sqlx);
    }

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

    // 清账操作
    public int updateSaccount(Booking booking){
        return bookDao.updateSaccount(booking);
    }

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

    @Override
    public Double sumAccount(String sqlx){
        return bookDao.sumAccount(sqlx);
    }

    @Override
    public BookTypeEto getBookStatusSum(String sqlx){
        return bookDao.getBookStatusSum(sqlx);
    }

//    @Override
//    public String cancelBooking(String bookingId) throws Exception {
//        if (Func.checkNull(bookingId))
//            return "未传入订单id";
//
//        Booking book = bookDao.getById(Func.parseInt(bookingId));
//        //是否能取消订单（1是 2否）
//        if (Func.parseInt(book.getHotelIsCanorder()) == 2)
//            return "商家设置不可取消订单，请联系商家";
//
//        AdminManager adminManager = adminManagerService.getById(Func.parseInt(book.getHotelManagerId()));
//
//        //1待支付,2已支付,3待入住,4已入住,5已消费,6支付超时,7已取消,8已退单,9已退款,10退款中
//        int status = Func.parseInt(book.getOrderStatus());
//        if (status == 1)
//        {
//            book.setOrderStatus(Func.parseStr(PayEnum.SEVEN.getNum())); // 如果在1待支付情况，用户点击取消订单按钮，后台直接修改取消订单操作
//            book.setUpdateTime(DateUtil.getFormatPaternDate(new Date()));
//            book.setRemake(Func.parseStr(PayEnum.FIVE.getPayStatus()));
//            bookDao.updateBooking(book);
//
//            return null;
//        }else if (status == 2 || status == 3)
//        {
//            book.setOrderStatus(Func.parseStr(PayEnum.TEN.getNum())); // 如果在2已支付,3待入住 情况，用户点击取消订单按钮，后台先修改状态为退款中，然后走退款流程
//            book.setUpdateTime(DateUtil.getFormatPaternDate(new Date()));
//            book.setRemake(Func.parseStr(PayEnum.FIVE.getPayStatus()));
//            if (book.getPayAccount() > 0)
//            {
//                // 注明如果支付金额大于0，则需要退款: （标识走退款操作）
//                book.setRefundWay(OrderEnum.微信退款.toString());
//            }
//            bookDao.updateBooking(book);
//
//            return null;
//        }
//        return "当前状态下，不可取消订单";
//    }

    @Override
    public String delBooking(String bookingId) {
        if (Func.checkNull(bookingId))
            return "未传入订单id";

        // 是否假删除：0删除，1正常
        Booking book = bookDao.getById(Func.parseInt(bookingId));
        if (book == null)
            return "无此订单，请重新查询";

        book.setStatus_del(0);
        book.setUpdateTime(DateUtil.getFormatPaternDate(new Date()));

        int status = Func.parseInt(book.getOrderStatus());
        if (status == PayEnum.待支付.getNum()) {
            book.setOrderStatus(Func.parseStr(PayEnum.已取消.getNum())); // 如果在1待支付情况，用户点击删除订单按钮，后台直接修改取消订单操作,(应当释放房间)
            book.setUpdateTime(DateUtil.getFormatPaternDate(new Date()));
            book.setRemake(Func.parseStr(PayEnum.已取消.getPayStatus()));
        }
        bookDao.updateBooking(book);

        // 退款成功后，写入数据操作
        bookinglogService.addBooklog("用户删除了这条订单的显示数据"  ,book);

        return null;
    }

    @Override
    public Booking queryByOrder(String orderNum) {
        List<Booking> list = bookDao.queryList("and order_num = '"+orderNum+"'");
        if(list!=null && list.size()>0) return list.get(0);
        return null;
    }




}
