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.RepairEvaluateMapper;
import com.repair.model.pojo.RepairEvaluate;
import com.repair.model.result.PageUtils;
import com.repair.model.vo.EvaluateVo;
import com.repair.model.vo.RecordEvaluateVo;
import com.repair.services.RepairEvaluateService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author ceshi
 * @since 2023-07-10
 */
@Service
public class RepairEvaluateServiceImpl extends ServiceImpl<RepairEvaluateMapper, RepairEvaluate> implements RepairEvaluateService {

    @Autowired
    private RepairEvaluateMapper repairEvaluateMapper;

    @Override
    public int insertRepairEvaluate(RepairEvaluate ra) {
        int result = repairEvaluateMapper.insert(ra);
        return result;
    }

    @Override
    public int deleteRepairEvaluateById(int id) {
        int result = repairEvaluateMapper.deleteById(id);
        return result;
    }

    @Override
    public PageUtils<RepairEvaluate> queryPageList(int currentPage, int pageCount, String keyWord, String startTime, String endTime) {
        Page<EvaluateVo> page = new Page<>();
        page.setCurrent(currentPage);
        page.setSize(pageCount);
        try{
            startTime = startTime == null ? startTime : TimeExchange.getStartOfDayStr(TimeExchange.StringToDate(startTime, "yyyy-MM-dd"));
            endTime = endTime == null ? endTime : TimeExchange.getEndOfDayStr(TimeExchange.StringToDate(endTime, "yyyy-MM-dd"));
        }catch (Exception e){

        }
        IPage<EvaluateVo> result = repairEvaluateMapper.queryEvaluatePageList(page, keyWord, startTime, endTime);
        return new PageUtils(result);
    }

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

    @Override
    public RepairEvaluate getRepairByRecordId(int recordId) {
        QueryWrapper<RepairEvaluate> query = new QueryWrapper();
        query.eq("record_id", recordId);
        RepairEvaluate result = repairEvaluateMapper.selectOne(query);
        return result;
    }

    @Override
    public List<EvaluateVo> queryEvaluateList(String keyWord, String startTime, String endTime) {
        List<EvaluateVo> datas = repairEvaluateMapper.queryEvaluatePageList(keyWord,startTime,endTime);
        return datas;
    }

    @Override
    public List<RecordEvaluateVo> queryEvaluateList(String dateStr) {
        List<RecordEvaluateVo> result = repairEvaluateMapper.queryEvaluateList(dateStr);
        return result;
    }

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

    @Override
    public List<RepairEvaluate> getRepairByRecordIds(List<Integer> recordIds) {
        QueryWrapper<RepairEvaluate> query = new QueryWrapper();
        query.in("record_id", recordIds);
        List<RepairEvaluate> result = repairEvaluateMapper.selectList(query);
        return result;
    }
}
