package com.amms.service.impl;

import java.util.List;
import java.util.Date;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.amms.mapper.ReservationMapper;
import com.amms.domain.Reservation;
import com.amms.service.IReservationService;
import com.amms.service.IReservationVisitorService;
import com.amms.domain.ReservationVisitor;
import com.amms.security.SecurityUtils;

/**
 * 预约Service业务层处理
 */
@Service
public class ReservationServiceImpl implements IReservationService {

    @Autowired
    private ReservationMapper reservationMapper;

    @Autowired
    private IReservationVisitorService reservationVisitorService;

    /**
     * 查询预约列表
     *
     * @param reservation 预约
     * @return 预约
     */
    @Override
    public List<Reservation> selectReservationList(Reservation reservation) {
        return reservationMapper.selectReservationList(reservation);
    }

    /**
     * 查询预约
     *
     * @param id 预约主键
     * @return 预约
     */
    @Override
    public Reservation selectReservationById(Long id) {
        return reservationMapper.selectReservationById(id);
    }

    /**
     * 新增预约
     *
     * 1. 写入预约主表（设置 createTime）
     * 2. 若携带 reservationVisitors，则同步逐一写入游客明细，并对齐 totalVisitors
     * 3. 使用事务保证主表与明细写入的原子性
     *
     * @param reservation 预约
     * @return 结果
     */
    @Override
    @Transactional
    public int insertReservation(Reservation reservation) {
        reservation.setCreateTime(new Date());
        // 根据游客列表数量自动对齐整单人数
        if (reservation.getReservationVisitors() != null) {
            reservation.setTotalVisitors(reservation.getReservationVisitors().size());
        }
        int rows = reservationMapper.insertReservation(reservation);
        if (rows > 0 && reservation.getReservationVisitors() != null) {
            Long reservationId = reservation.getId();
            // 逐一插入游客明细，绑定预约ID
            for (ReservationVisitor v : reservation.getReservationVisitors()) {
                v.setReservationId(reservationId);
                reservationVisitorService.insertReservationVisitor(v);
            }
        }
        return rows;
    }

    /**
     * 修改预约
     *
     * @param reservation 预约
     * @return 结果
     */
    @Override
    public int updateReservation(Reservation reservation) {
        reservation.setUpdateTime(new Date());
        reservation.setUpdater(SecurityUtils.getCurrentUser().getId());
        return reservationMapper.updateReservation(reservation);
    }

    /**
     * 删除预约信息
     *
     * @param id 预约主键
     * @return 结果
     */
    @Override
    public int deleteReservationById(Long id) {
        return reservationMapper.deleteReservationById(id);
    }

    /**
     * 审核预约（通过/驳回）
     *
     * 审核通过时：
     * 1）生成整单核验二维码链接写入 Reservation.qrCode
     * 2）为每位游客生成个人核验二维码链接写入 ReservationVisitor.visitorQrCode
     */
    @Transactional
    public int auditReservation(Reservation reservation) {
        if (reservation == null || reservation.getId() == null || reservation.getStatus() == null) {
            return 0;
        }
        Long reservationId = reservation.getId();

        // 审核通过：生成整单核验二维码链接
        if (Long.valueOf(1).equals(reservation.getStatus())) {
            String orderQr = "/reservation/verifyAll/" + reservationId;
            reservation.setQrCode(orderQr);

            // 为每位游客生成个人核验二维码链接
            ReservationVisitor query = new ReservationVisitor();
            query.setReservationId(reservationId);
            List<ReservationVisitor> visitors = reservationVisitorService.selectReservationVisitorList(query);
            for (ReservationVisitor v : visitors) {
                String visitorQr = "/reservation/verifyVisitor/" + v.getId();
                v.setVisitorQrCode(visitorQr);
                reservationVisitorService.updateReservationVisitor(v);
            }
        }

        reservation.setUpdateTime(new Date());
        reservation.setUpdater(SecurityUtils.getCurrentUser().getId());
        return reservationMapper.updateReservation(reservation);
    }

    /**
     * 整单核验：将该预约下所有游客核验状态置为1
     */
    @Transactional
    public int verifyAllVisitors(Long reservationId) {
        ReservationVisitor query = new ReservationVisitor();
        query.setReservationId(reservationId);
        List<ReservationVisitor> visitors = reservationVisitorService.selectReservationVisitorList(query);
        Date now = new Date();
        int cnt = 0;
        for (ReservationVisitor v : visitors) {
            v.setVerifyStatus(1L);
            v.setVerifyTime(now);
            cnt += reservationVisitorService.updateReservationVisitor(v);
        }
        return cnt;
    }

    /**
     * 游客个人核验：将该游客核验状态置为1
     */
    @Transactional
    public int verifyVisitor(Long visitorId) {
        ReservationVisitor v = reservationVisitorService.selectReservationVisitorById(visitorId);
        if (v == null) return 0;
        v.setVerifyStatus(1L);
        v.setVerifyTime(new Date());
        return reservationVisitorService.updateReservationVisitor(v);
    }

}
