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

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yida.data.common.core.common.ResultBean;
import com.yida.data.common.core.entity.DeptSchoolRelate;
import com.yida.data.common.core.entity.agent.EduAgentSchoolRelate;
import com.yida.data.common.core.utils.FebsUtil;
import com.yida.data.system.dto.AgentSchoolRelateSaveDTO;
import com.yida.data.system.mapper.DeptSchoolRelateMapper;
import com.yida.data.system.mapper.EduAgentSchoolRelateMapper;
import com.yida.data.system.service.EduAgentSchoolRelateService;
import com.yida.data.system.vo.AgentSchoolRelateVO;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 代理商-学校关联表 Service实现
 *
 * @author ZYJ
 * @date 2021-09-26 15:41:06
 */
@Service
@RequiredArgsConstructor
@Transactional(rollbackFor = Exception.class)
public class EduAgentSchoolRelateServiceImpl extends ServiceImpl
        <EduAgentSchoolRelateMapper, EduAgentSchoolRelate> implements EduAgentSchoolRelateService {

    private final DeptSchoolRelateMapper deptSchoolRelateMapper;

    @Override
    public List<AgentSchoolRelateVO> listAgentSchoolRelates() {
        Long topDeptId = FebsUtil.getTopDeptId();
        // 查询代理商和学校关联关系
        List<DeptSchoolRelate> list = deptSchoolRelateMapper.selectList(
                Wrappers.lambdaQuery(new DeptSchoolRelate())
                        .eq(DeptSchoolRelate::getDeptId, topDeptId));
        // 转换返回数据
        List<AgentSchoolRelateVO> returnList = new ArrayList<>();
        if (!CollectionUtils.isEmpty(list)) {
            returnList = list.stream().map(deptSchoolRelate -> {
                AgentSchoolRelateVO agentSchoolRelateVO = new AgentSchoolRelateVO();
                BeanUtils.copyProperties(deptSchoolRelate, agentSchoolRelateVO);
                return agentSchoolRelateVO;
            }).collect(Collectors.toList());
        }
        return returnList;
    }

    /**
     * 保存代理商和学校关联关系
     *
     * @param agentSchoolRelateSaveDTO 需要保存的关联关系集合数据
     * @return com.yida.data.common.core.common.ResultBean<java.lang.String>
     * @author ZYJ
     * @date 2021/9/26 16:59
     */
    @Override
    public ResultBean<String> saveAgentSchoolRelate(AgentSchoolRelateSaveDTO agentSchoolRelateSaveDTO) {
        Long topDeptId = FebsUtil.getTopDeptId();
        // 删除以前的关联关系
        deptSchoolRelateMapper.delete(Wrappers.lambdaQuery(new DeptSchoolRelate())
                .eq(DeptSchoolRelate::getDeptId, topDeptId));
        // 添加新的关联关系
        agentSchoolRelateSaveDTO.getSchoolIds().forEach(schoolId -> {
            DeptSchoolRelate deptSchoolRelate = new DeptSchoolRelate();
            // 判断关联关系是否存在
            List<DeptSchoolRelate> list = deptSchoolRelateMapper.selectList(Wrappers.lambdaQuery(new DeptSchoolRelate())
                    .eq(DeptSchoolRelate::getDeptId, topDeptId)
                    .eq(DeptSchoolRelate::getSchoolId, schoolId));
            if (CollectionUtils.isEmpty(list)) {
                // 保存代理商和学校关联关系
                deptSchoolRelate.setDeptId(topDeptId);
                deptSchoolRelate.setSchoolId(schoolId);
                deptSchoolRelate.setCreateDeptId(FebsUtil.getDeptId());
                this.deptSchoolRelateMapper.insert(deptSchoolRelate);
            }
        });
        return ResultBean.buildSuccess();
    }
}
