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.RepairNoticeSettingMapper;
import com.repair.model.pojo.RepairNoticeSetting;
import com.repair.model.result.PageUtils;
import com.repair.model.vo.NoticePageVo;
import com.repair.services.RepairNoticeSettingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author ceshi
 * @since 2023-07-11
 */
@Service
public class RepairNoticeSettingServiceImpl extends ServiceImpl<RepairNoticeSettingMapper, RepairNoticeSetting> implements RepairNoticeSettingService {

    @Autowired
    private RepairNoticeSettingMapper repairNoticeSettingMapper;

    @Override
    public int insertRepairNoticeSetting(RepairNoticeSetting rns) {
        int result = repairNoticeSettingMapper.insert(rns);
        return result;
    }

    @Override
    public int updateRepairNoticeSetting(RepairNoticeSetting rns) {
        int result = repairNoticeSettingMapper.updateById(rns);
        return result;
    }

    @Override
    public PageUtils<NoticePageVo> queryPageRepairNoticeSettings(int currentPage, int pageCount, String createTime) throws ParseException {
        Page<NoticePageVo> page = new Page<>();
        page.setCurrent(currentPage);
        page.setSize(pageCount);
        String startTime = null;
        String endTime = null;
        if(createTime != null && createTime != ""){
            Date cd = TimeExchange.StringToDate(createTime, "yyyy-MM-dd");
            startTime = TimeExchange.getStartOfDayStr(cd);
            endTime = TimeExchange.getEndOfDayStr(cd);
        }
        IPage<NoticePageVo> result = repairNoticeSettingMapper.queryNoticePageList(page, startTime, endTime);
        return new PageUtils(result);
    }

    @Override
    public int deleteRepairNoticeSettingById(int id) {
        int result = repairNoticeSettingMapper.deleteById(id);
        return result;
    }

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

    @Override
    public List<RepairNoticeSetting> getRepairNotices() {
        QueryWrapper<RepairNoticeSetting> wrapper = new QueryWrapper<>();
        wrapper.eq("is_top", 1);
        // 通过update_time进行排序
        wrapper.orderByAsc("update_time");
        List<RepairNoticeSetting> result = repairNoticeSettingMapper.selectList(wrapper);
        return result;
    }
}
