package com.repair.services.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.repair.common.utils.TimeExchange;
import com.repair.mapper.RepairSystemMessagesMapper;
import com.repair.model.pojo.RepairSystemMessages;
import com.repair.model.result.PageUtils;
import com.repair.model.vo.SystemMessagePageVo;
import com.repair.services.RepairSystemMessagesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author ceshi
 * @since 2023-07-20
 */
@Service
public class RepairSystemMessagesServiceImpl extends ServiceImpl<RepairSystemMessagesMapper, RepairSystemMessages> implements RepairSystemMessagesService {

    @Autowired
    private RepairSystemMessagesMapper repairSystemMessagesMapper;

    @Override
    public int insertRepairSystemMessages(RepairSystemMessages rsm) {
        int result = repairSystemMessagesMapper.insert(rsm);
        return result;
    }

    @Override
    public int updateRepairSystemMessages(RepairSystemMessages rsm) {
        int result = repairSystemMessagesMapper.updateById(rsm);
        return result;
    }

    @Override
    public PageUtils<RepairSystemMessages> queryPageRepairSystemMessagess(int currentPage, int pageCount, String name) {
        Page<RepairSystemMessages> page = new Page<>(currentPage, pageCount);
        QueryWrapper<RepairSystemMessages> queryWrapper = new QueryWrapper<>();
        IPage<RepairSystemMessages> result = repairSystemMessagesMapper.selectPage(page, queryWrapper);
        return new PageUtils(result);
    }

    @Override
    public int deleteRepairSystemMessagesById(int id) {
        int result = repairSystemMessagesMapper.deleteById(id);
        return result;
    }

    @Override
    public RepairSystemMessages getRepairById(int id) {
        RepairSystemMessages result = repairSystemMessagesMapper.selectById(id);
        return result;
    }

    @Override
    public List<RepairSystemMessages> getRepairs() {
        List<RepairSystemMessages> result = repairSystemMessagesMapper.selectList(null);
        return result;
    }

    @Override
    public boolean inserBatchSystemMessage(List<RepairSystemMessages> datas) {
        boolean result = this.saveBatch(datas, datas.size());
        return result;
    }

    @Override
    public PageUtils<SystemMessagePageVo> querySystemMessagePage(int currentPage, int pageCount, Integer userId) {
        Page<SystemMessagePageVo> page = new Page<>();
        page.setCurrent(currentPage);
        page.setSize(pageCount);
        IPage<SystemMessagePageVo> result = repairSystemMessagesMapper.querySystemMessagePage(page, userId);
        return new PageUtils(result);
    }


    @Override
    public PageUtils<SystemMessagePageVo> queryPCSystemMessagePage(int currentPage, int pageCount, Integer userId, String createTime, int statu) {
        Page<SystemMessagePageVo> page = new Page<>();
        page.setCurrent(currentPage);
        page.setSize(pageCount);
        //获取开始时间和结束时间
        String startTime = createTime == null ? createTime : TimeExchange.getStartOfDayStr(TimeExchange.StringToDate(createTime, "yyyy-MM-dd"));
        String endTime = createTime == null ? createTime : TimeExchange.getEndOfDayStr(TimeExchange.StringToDate(createTime, "yyyy-MM-dd"));
        IPage<SystemMessagePageVo> result = repairSystemMessagesMapper.queryPCSystemMessagePage(page, userId, statu, startTime, endTime);
        return new PageUtils(result);
    }

    @Override
    public int readAllManageMessage(int userId) {
        QueryWrapper<RepairSystemMessages> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("recipient_id", userId);
        RepairSystemMessages rsm = new RepairSystemMessages();
        rsm.setIsRead(1);
        int result = repairSystemMessagesMapper.update(rsm, queryWrapper);
        return result;
    }

    @Override
    public int queryMessageUnreadCount(int userId) {
        QueryWrapper<RepairSystemMessages> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("recipient_id", userId);
        queryWrapper.eq("is_read", 0);
        int result = repairSystemMessagesMapper.selectCount(queryWrapper);
        return result;
    }
}
