package com.sdut.labex.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sdut.labex.dto.ExperimentDTO;
import com.sdut.labex.entity.SubmitRecord;
import com.sdut.labex.mapper.SubmitRecordsMapper;
import com.sdut.labex.service.SubmitRecordsService;
import com.sdut.labex.utils.ResVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

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

/**
 * @author springforest
 * @description 针对表【submit_records(实验提交记录)】的数据库操作Service实现
 * @createDate 2023-06-21 11:01:21
 */
@Service
@Slf4j
public class SubmitRecordsServiceImpl extends ServiceImpl<SubmitRecordsMapper, SubmitRecord>
        implements SubmitRecordsService {
    @Resource
    private SubmitRecordsMapper submitRecordsMapper;

    @Override
    public ResVo addRecords(SubmitRecord submitRecord) {
        try {
            submitRecordsMapper.insert(submitRecord);
        } catch (Exception e) {
            e.printStackTrace();
            return ResVo.error("保存失败");
        }
        return ResVo.ok("保存成功");
    }

    @Override
    public ResVo deleteRecord(Integer id) {
        try {
            submitRecordsMapper.deleteById(id);
        } catch (Exception e) {
            e.printStackTrace();
            return ResVo.error("删除失败");
        }
        return ResVo.ok("删除成功");
    }

    @Override
    public ResVo updateRecords(SubmitRecord submitRecord) {
        log.info("updateRecords:{}", submitRecord);
        try {
            submitRecordsMapper.updateById(submitRecord);
        } catch (Exception e) {
            e.printStackTrace();
            return ResVo.error("更新失败");
        }
        return ResVo.ok("更新成功");
    }

    @Override
    public ResVo selectRecords(int pageNum, int pageSize, SubmitRecord submitRecord) {
        log.info("selectRecords:{}", submitRecord);
        Page<SubmitRecord> page = new Page<>(pageNum, pageSize);
        submitRecordsMapper.selectPageForCheck(page, submitRecord);

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

    @Override
    public ResVo selectRecords(int pageNum, int pageSize, String className, String studentId, String applyUser) {
        Page<ExperimentDTO> page = new Page<>(pageNum, pageSize);
        submitRecordsMapper.selectPageST(page, className, studentId, applyUser);
        Map<String, Object> map = new HashMap<>();
        map.put("current", page.getCurrent());
        map.put("total", page.getTotal());
        map.put("list", page.getRecords());
        return ResVo.ok(map);
    }
}




