package com.yida.data.school.secondClass.service.impl;


import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yida.data.common.core.entity.school.secondClass.EduSecondClassVenueExhibit;
import com.yida.data.school.dto.secondClass.ListVenueExhibitDTO;
import com.yida.data.school.secondClass.mapper.EduSecondClassVenueExhibitMapper;
import com.yida.data.school.secondClass.service.EduSecondClassVenueExhibitService;
import com.yida.data.school.vo.secondClass.VenueExhibitVO;
import java.util.Arrays;
import java.util.List;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.annotation.Propagation;
import lombok.RequiredArgsConstructor;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

/**
 * Service实现
 *
 * @author zhanghaijun
 * @date 2022-04-18 09:39:53
 */
@Service
@RequiredArgsConstructor
@Transactional(rollbackFor = Exception.class)
public class EduSecondClassVenueExhibitServiceImpl extends ServiceImpl
    <EduSecondClassVenueExhibitMapper, EduSecondClassVenueExhibit> implements EduSecondClassVenueExhibitService {

    @Transactional(readOnly = true)
    @Override
    public IPage<VenueExhibitVO> listExhibitPage(ListVenueExhibitDTO dto) {
        Page page = dto.toPage();
        page.setRecords(baseMapper.listExhibit(page, dto));
        return page;
    }

    @Override
    public void saveExhibit(EduSecondClassVenueExhibit exhibit) {
        if (exhibit.getId() == null) {
            exhibit.setSort((int) count(
                Wrappers.<EduSecondClassVenueExhibit>lambdaQuery()
                    .eq(EduSecondClassVenueExhibit::getVenueId, exhibit.getVenueId())));
        }
        saveOrUpdate(exhibit);
    }

    @Override
    public void changeGroup(Long[] exhibitIds, Long groupId) {
        update(Wrappers.<EduSecondClassVenueExhibit>lambdaUpdate().in(EduSecondClassVenueExhibit::getId, exhibitIds)
            .set(EduSecondClassVenueExhibit::getGroupId, groupId));
    }

    @Override
    public void delExhibit(List<Long> ids) {
        EduSecondClassVenueExhibit one = getById(ids.get(0));
        removeByIds(ids);
        // 重排序其它展品
        List<EduSecondClassVenueExhibit> list = list(
            Wrappers.<EduSecondClassVenueExhibit>lambdaQuery().eq(EduSecondClassVenueExhibit::getVenueId, one.getVenueId())
                .orderByAsc(EduSecondClassVenueExhibit::getSort));
        if (CollUtil.isNotEmpty(list)) {
            for (int i = 0; i < list.size(); i++) {
                list.get(i).setSort(i);
            }
            updateBatchById(list);
        }
    }

    @Override
    public void changeSort(Long id, Long anotherId) {
        EduSecondClassVenueExhibit one = getById(id);
        EduSecondClassVenueExhibit anotherOne = getById(anotherId);
        int sum = anotherOne.getSort() + one.getSort();
        one.setSort(anotherOne.getSort());
        anotherOne.setSort(sum - one.getSort());
        updateBatchById(Arrays.asList(one, anotherOne));
    }

    @Override
    public VenueExhibitVO getInfo(Long id) {
        EduSecondClassVenueExhibit exhibit = getById(id);
        VenueExhibitVO vo = new VenueExhibitVO();
        BeanUtils.copyProperties(exhibit, vo);
        return vo;
    }

    @Override
    public Integer countExhibit(Long venueId, Long groupId) {
        LambdaQueryWrapper<EduSecondClassVenueExhibit> condition = Wrappers.<EduSecondClassVenueExhibit>lambdaQuery()
            .eq(EduSecondClassVenueExhibit::getVenueId, venueId);
        if (groupId != null) {
            condition.eq(EduSecondClassVenueExhibit::getGroupId, groupId);
        }
        return (int) count(condition);
    }
}
