package top.continew.admin.business.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import top.continew.admin.business.mapper.HomeworkMapper;
import top.continew.admin.business.model.entity.HomeworkDO;
import top.continew.admin.business.model.query.HomeworkQuery;
import top.continew.admin.business.model.req.HomeworkAddReq;
import top.continew.admin.business.model.req.HomeworkEditReq;
import top.continew.admin.business.model.resp.HomeworkResp;
import top.continew.admin.business.service.HomeworkService;
import top.continew.starter.data.mp.util.QueryWrapperHelper;
import top.continew.starter.extension.crud.model.query.PageQuery;
import top.continew.starter.extension.crud.model.resp.PageResp;

import java.util.Collections;
import java.util.List;

/**
 * business业务实现
 *
 * @author jiay
 * @since 2025/06/26 10:24
 */
@Service
@RequiredArgsConstructor
public class HomeworkServiceImpl extends ServiceImpl<HomeworkMapper, HomeworkDO> implements HomeworkService {

    private final HomeworkMapper homeworkMapper;

    @Override
    public List<HomeworkResp> getHomeworkList(HomeworkQuery query) {
        QueryWrapper queryWrapper = QueryWrapperHelper.build(query);
        List list = this.list(queryWrapper);

        if (CollectionUtil.isNotEmpty(list)) {
            return BeanUtil.copyToList(list, HomeworkResp.class);
        }
        return Collections.emptyList();
    }

    @Override
    public boolean addHomework(HomeworkAddReq req) {
        //插入记录到数据库
        HomeworkDO entity = BeanUtil.copyProperties(req, HomeworkDO.class);
        return homeworkMapper.insert(entity) > 0;

    }

    @Override
    public boolean editHomework(HomeworkEditReq req) {
        HomeworkDO HomeworkDO = BeanUtil.copyProperties(req, HomeworkDO.class);
        return homeworkMapper.updateById(HomeworkDO) > 0;

    }

    @Override
    public boolean deleteHomework(Long id) {
        if (id != null) {
            return homeworkMapper.deleteById(id) > 0;
        }
        return false;
    }

    @Override
    public HomeworkResp getHomework(Long id) {
        HomeworkDO HomeworkDO = homeworkMapper.selectById(id);
        HomeworkResp HomeworkResp = new HomeworkResp();
        if (HomeworkDO != null) {
            HomeworkResp = BeanUtil.copyProperties(HomeworkDO, HomeworkResp.class);
        }
        return HomeworkResp;
    }

    @Override
    public PageResp<HomeworkResp> getHomeworkPage(HomeworkQuery query, PageQuery pageQuery) {
        QueryWrapper queryWrapper = QueryWrapperHelper.build(query);
        Page page = homeworkMapper.selectPage(new Page<>(pageQuery.getPage(), pageQuery.getSize()), queryWrapper);
        return PageResp.build(page);
    }

}