package com.yida.data.system.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yida.data.common.core.entity.system.DeptSchoolInformation;
import com.yida.data.common.core.entity.system.DeptSchoolLabel;
import com.yida.data.system.dto.SchoolInformationDTO;
import com.yida.data.system.dto.SchoolPageDTO;
import com.yida.data.system.mapper.DeptSchoolInformationMapper;
import com.yida.data.system.service.CommonService;
import com.yida.data.system.service.DeptSchoolInformationService;
import com.yida.data.system.service.DeptSchoolLabelService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

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

/**
 * @author ccl
 */
@Slf4j
@Service
@RequiredArgsConstructor
@Transactional(rollbackFor = Exception.class)
public class DeptSchoolInformationServiceImpl extends ServiceImpl<DeptSchoolInformationMapper, DeptSchoolInformation>
        implements DeptSchoolInformationService {

    private final DeptSchoolLabelService labelService;
    private final CommonService commonService;

    @Override
    public Page<SchoolInformationDTO> findSchoolListByDeptId(Long deptId, String schoolName, Integer schoolType,
                                                             Integer natureType, String address, String tag, Page<SchoolInformationDTO> page,
                                                             Integer schoolOwnership) {
        Page<SchoolInformationDTO> schoolListByDeptId = baseMapper.findSchoolListByDeptId(deptId, schoolName, schoolType, natureType, address, tag, page, schoolOwnership);
        for (SchoolInformationDTO record : schoolListByDeptId.getRecords()) {
            List<DeptSchoolLabel> list = labelService.list(Wrappers.query(new DeptSchoolLabel()).lambda().eq(DeptSchoolLabel::getDeptSchoolId, record.getId()));
            List<String> tagList = new ArrayList<>();
            for (DeptSchoolLabel deptSchoolLabel : list) {
                tagList.add(deptSchoolLabel.getLabel());
            }
            record.setTags(tagList);
        }
        return schoolListByDeptId;
    }

    @Override
    public Page<SchoolInformationDTO> findSchoolPageList(String schoolName, List<Integer> schoolType, List<Integer> natureType, Page<SchoolInformationDTO> page,
                                                         String[] tags, Long deptId, List<Integer> schoolOwnership) {
        List<Long> deptSchoolId = new ArrayList<>();
        if (tags != null && tags.length > 0) {
            deptSchoolId = baseMapper.findDeptSchoolId(tags);
            if (CollectionUtil.isEmpty(deptSchoolId)) {
                return new Page<>();
            }
        }
        return baseMapper.findSchoolPageList(schoolName, schoolType, natureType, deptSchoolId, page, deptId, schoolOwnership);
    }

    @Override
    public Page<SchoolInformationDTO> findSchoolByAddress(Page<SchoolInformationDTO> page, String address, Long deptId) {

        return baseMapper.findSchoolByAddress(page, address, deptId);
    }

    @Override
    public IPage<SchoolInformationDTO> listSchoolPage(SchoolPageDTO dto) {
        return baseMapper.listSchoolPage(dto.toPage(), dto);
    }
}
