package com.xjrsoft.module.banding.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.yulichang.base.MPJBaseServiceImpl;
import com.xjrsoft.module.banding.dto.BandingTaskClassPageDto;
import com.xjrsoft.module.banding.dto.BandingTaskMajorConditionDto;
import com.xjrsoft.module.banding.dto.BandingTaskMajorConditionListDto;
import com.xjrsoft.module.banding.dto.SaveMajorConditionDto;
import com.xjrsoft.module.banding.entity.BandingTaskMajorCondition;
import com.xjrsoft.module.banding.mapper.BandingTaskMajorConditionMapper;
import com.xjrsoft.module.banding.service.IBandingTaskMajorConditionService;
import com.xjrsoft.module.banding.vo.BandingTaskMajorConditionListVo;
import com.xjrsoft.module.student.entity.BaseNewStudent;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
* @title: 新生分班任务
* @Author dzx
* @Date: 2024-07-01
* @Version 1.0
*/
@Service
@AllArgsConstructor
public class BandingTaskMajorConditionServiceImpl extends MPJBaseServiceImpl<BandingTaskMajorConditionMapper, BandingTaskMajorCondition> implements IBandingTaskMajorConditionService {
    @Override
    public Boolean add(BandingTaskMajorCondition majorCondition) {
        this.baseMapper.insert(majorCondition);
        return true;
    }

    @Override
    public Boolean update(BandingTaskMajorCondition majorCondition) {
        this.baseMapper.updateById(majorCondition);
        return true;
    }

    @Override
    public Boolean delete(List<Long> ids) {
        this.baseMapper.deleteBatchIds(ids);
        return true;
    }

    @Override
    public Boolean save(SaveMajorConditionDto dto) {
        this.baseMapper.delete(
                new QueryWrapper<BandingTaskMajorCondition>().lambda()
                        .eq(BandingTaskMajorCondition::getBandingTaskId, dto.getBandingTaskId())
        );
        List<BandingTaskMajorCondition> dataList = new ArrayList<>();
        Date createDate = new Date();
        for (BandingTaskMajorConditionDto condition : dto.getConditionList()) {
            dataList.add(
                    new BandingTaskMajorCondition(){{
                        setBandingTaskId(dto.getBandingTaskId());
                        setHeight(condition.getHeight());
                        setScore(condition.getScore());
                        setMajorSetId(condition.getMajorSetId());
                        setCreateDate(createDate);
                    }}
            );
        }
        if(!dataList.isEmpty()){
            this.saveBatch(dataList);
        }
        return true;
    }

    @Override
    public List<BandingTaskMajorConditionListVo> getList(BandingTaskMajorConditionListDto dto) {
        return this.baseMapper.getList(dto);
    }

    @Override
    public List<BaseNewStudent> getStudentMajorId(BandingTaskClassPageDto dto) {
        return this.baseMapper.getStudentMajorId(dto);
    }
}
