package com.limap.demo.service.impl;

import java.util.List;
import com.limap.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.limap.common.utils.StringUtils;
import java.util.Arrays;
import org.springframework.transaction.annotation.Transactional;
import com.limap.demo.domain.DemoStudent;
import com.limap.demo.mapper.DemoStudentMapper;
import com.limap.demo.mapper.DemoClassesMapper;
import com.limap.demo.domain.DemoClasses;
import com.limap.demo.service.IDemoClassesService;

/**
 * 主子表演示Service业务层处理
 * 
 * @author metaee
 * @date 2023-02-28
 */
@Service
public class DemoClassesServiceImpl extends ServiceImpl<DemoClassesMapper, DemoClasses> implements IDemoClassesService {
    @Autowired
    private DemoClassesMapper demoClassesMapper;

    @Autowired
    private DemoStudentMapper demoStudentMapper;
    
    /**
     * 查询主子表演示列表
     * 
     * @param demoClasses 主子表演示
     * @return 主子表演示
     */
    @Override
    public List<DemoClasses> selectDemoClassesList(DemoClasses demoClasses) {
    	LambdaQueryWrapper<DemoClasses> queryWrapper = Wrappers.lambdaQuery();
        if (StringUtils.isNotEmpty(demoClasses.getClassesName())) {
			queryWrapper.like(DemoClasses::getClassesName, demoClasses.getClassesName());
		}
        if (StringUtils.isNotEmpty(demoClasses.getStatus())) {
			queryWrapper.eq(DemoClasses::getStatus, demoClasses.getStatus());
		}
        return demoClassesMapper.selectList(queryWrapper);
    }

    /**
     * 查询主子表演示
     * 
     * @param classesId 主子表演示主键
     * @return 主子表演示
     */
    @Override
    public DemoClasses selectDemoClassesByClassesId(Long classesId) {
        return demoClassesMapper.selectDemoClassesByClassesId(classesId);
    }

    /**
     * 新增主子表演示
     * 
     * @param demoClasses 主子表演示
     * @return 结果
     */
    @Transactional
    @Override
    public int insertDemoClasses(DemoClasses demoClasses) {
        demoClasses.setCreateTime(DateUtils.getNowDate());
        int rows = demoClassesMapper.insert(demoClasses);
        insertDemoStudent(demoClasses);
        return rows;
    }

    /**
     * 修改主子表演示
     * 
     * @param demoClasses 主子表演示
     * @return 结果
     */
    @Transactional
    @Override
    public int updateDemoClasses(DemoClasses demoClasses) {
        demoClasses.setUpdateTime(DateUtils.getNowDate());
        LambdaQueryWrapper<DemoStudent> queryWrapper = Wrappers.lambdaQuery();
     	queryWrapper.eq(DemoStudent::getClassesId, demoClasses.getClassesId());
     	demoStudentMapper.delete(queryWrapper);
        
        insertDemoStudent(demoClasses);
        return demoClassesMapper.updateById(demoClasses);
    }

    /**
     * 批量删除主子表演示
     * 
     * @param classesIds 需要删除的主子表演示主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteDemoClassesByClassesIds(Long[] classesIds) {
        LambdaQueryWrapper<DemoStudent> queryWrapper = Wrappers.lambdaQuery();
     	queryWrapper.in(DemoStudent::getClassesId, Arrays.asList(classesIds));
     	demoStudentMapper.delete(queryWrapper);
        return demoClassesMapper.deleteBatchIds(Arrays.asList(classesIds));
    }

    /**
     * 删除主子表演示信息
     * 
     * @param classesId 主子表演示主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteDemoClassesByClassesId(Long classesId) {
        LambdaQueryWrapper<DemoStudent> queryWrapper = Wrappers.lambdaQuery();
     	queryWrapper.eq(DemoStudent::getClassesId, classesId);
     	demoStudentMapper.delete(queryWrapper);
        return demoClassesMapper.deleteById(classesId);
    }
    
    /**
     * 新增单表演示信息
     * 
     * @param demoClasses 主子表演示对象
     */
    public void insertDemoStudent(DemoClasses demoClasses) {
        List<DemoStudent> demoStudentList = demoClasses.getDemoStudentList();
        Long classesId = demoClasses.getClassesId();
        if (StringUtils.isNotNull(demoStudentList)) {
            for (DemoStudent demoStudent : demoStudentList) {
                demoStudent.setClassesId(classesId);
                demoStudentMapper.insert(demoStudent);
            }
        }
    }

}
