package org.dromara.ygpassport.service.impl;

import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.mybatis.core.page.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.dromara.ygpassport.domain.bo.PhysiqueTestBo;
import org.dromara.ygpassport.domain.vo.PhysiqueTestVo;
import org.dromara.ygpassport.domain.PhysiqueTest;
import org.dromara.ygpassport.mapper.PhysiqueTestMapper;
import org.dromara.ygpassport.service.IPhysiqueTestService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 学生体质健康测试数据Service业务层处理
 *
 * @author Lion Li
 * @date 2024-03-16
 */
@RequiredArgsConstructor
@Service
public class PhysiqueTestServiceImpl implements IPhysiqueTestService {

    private final PhysiqueTestMapper baseMapper;

    /**
     * 查询学生体质健康测试数据
     */
    @Override
    public PhysiqueTestVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询学生体质健康测试数据列表
     */
    @Override
    public TableDataInfo<PhysiqueTestVo> queryPageList(PhysiqueTestBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<PhysiqueTest> lqw = buildQueryWrapper(bo);
        Page<PhysiqueTestVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询学生体质健康测试数据列表
     */
    @Override
    public List<PhysiqueTestVo> queryList(PhysiqueTestBo bo) {
        LambdaQueryWrapper<PhysiqueTest> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    /**
     * 根据学号查询学生体质健康测试数据列表
     */
    @Override
    public List<PhysiqueTestVo> queryListByStuNo(String stuNo){
        return baseMapper.selectVoListByStuNo(stuNo);
    }

    private LambdaQueryWrapper<PhysiqueTest> buildQueryWrapper(PhysiqueTestBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<PhysiqueTest> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getStuNo()), PhysiqueTest::getStuNo, bo.getStuNo());
        lqw.eq(bo.getDeleted() != null, PhysiqueTest::getDeleted, bo.getDeleted());
        return lqw;
    }

    /**
     * 新增学生体质健康测试数据
     */
    @Override
    public Boolean insertByBo(PhysiqueTestBo bo) {
        PhysiqueTest add = MapstructUtils.convert(bo, PhysiqueTest.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改学生体质健康测试数据
     */
    @Override
    public Boolean updateByBo(PhysiqueTestBo bo) {
        PhysiqueTest update = MapstructUtils.convert(bo, PhysiqueTest.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(PhysiqueTest entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除学生体质健康测试数据
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
