package com.yinlihupo.enlish.service.service.unit;

import com.yinlihupo.enlish.service.domain.dataobject.GradeUnitDO;
import com.yinlihupo.enlish.service.domain.dataobject.UnitDO;
import com.yinlihupo.enlish.service.domain.mapper.GradeUnitDOMapper;
import com.yinlihupo.enlish.service.domain.mapper.UnitDOMapper;
import com.yinlihupo.enlish.service.model.vo.unit.AddUnitReqVO;
import com.yinlihupo.enlish.service.service.UnitService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class UnitServiceImpl implements UnitService {

    @Resource
    private UnitDOMapper unitDOMapper;
    @Resource
    private GradeUnitDOMapper gradeUnitDOMapper;

    @Override
    public List<UnitDO> findUnitDOList(Integer page, Integer size) {
        return unitDOMapper.selectUnitDOList((page - 1) * size, size);
    }

    @Override
    public Integer findUnitDOListCount() {
        return unitDOMapper.selectUnitDOListCount();
    }

    @Override
    public void add(AddUnitReqVO addUnitReqVO) {

        UnitDO unitDO = UnitDO.builder()
                .title(addUnitReqVO.getTitle())
                .createAt(LocalDateTime.now())
                .build();
        unitDOMapper.insertSelective(unitDO);

        Integer gradeId = addUnitReqVO.getGradeId();
        gradeUnitDOMapper.insert(GradeUnitDO.builder().gradeId(gradeId).unitId(unitDO.getId()).build());
    }

    @Override
    public void delete(Integer id) {
        unitDOMapper.deleteByPrimaryKey(id);
        gradeUnitDOMapper.deleteByUnitId(id);
    }
}
