package com.sdut.labex.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sdut.labex.dto.BorrowInfoDTO;
import com.sdut.labex.entity.BorrowInfo;
import com.sdut.labex.entity.Room;
import com.sdut.labex.mapper.BorrowInfoMapper;
import com.sdut.labex.service.BorrowInfoService;
import com.sdut.labex.utils.FormatDate;
import com.sdut.labex.utils.ResVo;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

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

/**
 * @author springforest
 * @description 针对表【borrow_info(借用记录)】的数据库操作Service实现
 * @createDate 2023-06-12 08:07:15
 */
@Service
public class BorrowInfoServiceImpl extends ServiceImpl<BorrowInfoMapper, BorrowInfo>
        implements BorrowInfoService {

    @Resource
    private BorrowInfoMapper borrowInfoMapper;

    @Resource
    private RedisTemplate<String, Object> redisTemplate;

    @Override
    public ResVo borrow(List<String> timeList, BorrowInfo borrowInfo) {
        boolean isAnyRoomBorrowed = false;

        for (String item : timeList) {
            BorrowInfo newBorrowInfo = new BorrowInfo();
            BeanUtil.copyProperties(borrowInfo, newBorrowInfo); // 使用BeanUtil进行属性拷贝
            newBorrowInfo.setTime(item.toString());
            int code = this.isBorrowed(newBorrowInfo);
            if (code == 1) {
                return ResVo.error("借用失败，所选时段教室被占用");
            } else if (code == 0) {
                isAnyRoomBorrowed = true;
            }
        }

        if (isAnyRoomBorrowed) {
            for (String item : timeList) {
                BorrowInfo newBorrowInfo = new BorrowInfo();
                BeanUtil.copyProperties(borrowInfo, newBorrowInfo);
                newBorrowInfo.setTime(item.toString());
                newBorrowInfo.setIsAdmit(1);
                borrowInfoMapper.insert(newBorrowInfo);
            }
        }
        return ResVo.ok("借用成功");
    }


    @Override
    public ResVo deleteBorrowInfoById(Integer id) {
        if (borrowInfoMapper.selectById(id) == null) {
            return ResVo.error("该借用记录不存在");
        }

        try {
            borrowInfoMapper.deleteById(id);
        } catch (Exception e) {
            e.printStackTrace();
            return ResVo.error("删除失败");
        }
        return ResVo.ok("删除成功");
    }

    @Override
    public ResVo getBorrowInfo(BorrowInfo borrowInfo, int pageNum, int pageSize) {
        Page<BorrowInfo> page = new Page<>(pageNum, pageSize);
        borrowInfoMapper.queryBorrowInfoByOptions(page, borrowInfo);

        List<BorrowInfo> list = page.getRecords();
        List<BorrowInfoDTO> resList = new ArrayList<>();
        for (BorrowInfo item : list) {
            BorrowInfoDTO m = new BorrowInfoDTO();
            BeanUtil.copyProperties(item, m);
            m.setDate(FormatDate.formatDate(item.getDate()));
            m.setApplyDate(FormatDate.formatDate(item.getApplyDate()));
            resList.add(m);
        }

        Map<String, Object> res = new HashMap<>();
        res.put("total", page.getTotal());
        res.put("current", page.getCurrent());
        res.put("list", resList);
        return ResVo.ok(res);
    }

    @Override
    public ResVo notBorrowedYet(List<String> timeList, String date) {
        List<Room> RBI = borrowInfoMapper.notBorrowedYet(timeList, date);

        Map<String, Object> res = new HashMap<>();
        res.put("list", RBI);
        return ResVo.ok(res);
    }

    @Override
    public int isBorrowed(BorrowInfo borrowInfo) {
        QueryWrapper<BorrowInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("id").eq("room_name", borrowInfo.getRoomName())
                .eq("time", borrowInfo.getTime()).eq("date", borrowInfo.getDate());

        if (borrowInfoMapper.selectOne(queryWrapper) != null) {
            return 1;
        } else {
            return 0;
        }
    }
}




