package com.xueyi.system.digitalmans.manager.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.xueyi.common.core.constant.basic.OperateConstants;
import com.xueyi.common.core.utils.core.ArrayUtil;
import com.xueyi.common.web.entity.domain.SlaveRelation;
import com.xueyi.system.digitalmans.domain.merge.DmReceptionVisitorMerge;
import com.xueyi.system.digitalmans.domain.po.DmReceptionPo;
import com.xueyi.system.digitalmans.domain.dto.DmReceptionDto;
import com.xueyi.system.digitalmans.domain.query.DmReceptionQuery;
import com.xueyi.system.digitalmans.domain.model.DmReceptionConverter;
import com.xueyi.system.digitalmans.mapper.DmReceptionMapper;
import com.xueyi.common.web.entity.manager.impl.BaseManagerImpl;
import com.xueyi.system.digitalmans.manager.IDmReceptionManager;
import com.xueyi.system.digitalmans.mapper.merge.DmReceptionVisitorMergeMapper;
import com.xueyi.system.staff.manager.impl.DmStaffManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

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

import static com.xueyi.system.api.digitalmans.domain.merge.MergeGroup.Reception_DmReceptionVisitorMerge_GROUP;
import static com.xueyi.system.api.digitalmans.domain.merge.MergeGroup.Reception_Staff_GROUP;



/**
 * 接待模式管理管理 数据封装层处理
 *
 * @author yinruoxi
 */
@Component
public class DmReceptionManager extends BaseManagerImpl<DmReceptionQuery, DmReceptionDto, DmReceptionPo, DmReceptionMapper, DmReceptionConverter> implements IDmReceptionManager {


    @Autowired
    private DmReceptionVisitorMergeMapper dmReceptionVisitorMergeMapper;

    @Override
    protected List<SlaveRelation> subRelationInit() {
        List result = new ArrayList<SlaveRelation>();
        result.add(new SlaveRelation(Reception_Staff_GROUP, DmStaffManager.class, OperateConstants.SubOperateLimit.ONLY_SEL));
        result.add(new SlaveRelation(Reception_DmReceptionVisitorMerge_GROUP, DmReceptionVisitorMergeMapper.class, DmReceptionVisitorMerge.class, OperateConstants.SubOperateLimit.ONLY_DEL));
        return result;
    }


    @Override
    public int insert(DmReceptionDto dto) {
        // 判断priorities的长度，如果长度为0,返回失败
        if (dto.getPriorities() == null || dto.getPriorities().length == 0) {
            return 0;
        } else {
            int row = super.insert(dto);
            if (row > 0) {
                if (dto.getPriorities().length > 1) {
                    this.addDeceptionVisitorMerge(dto.getId(),dto.getPriorities());
                }
            }
            return row;
        }

    }

    @Override
    public void addDeceptionVisitorMerge(Long receptionId, Long[][] visitorIds) {
        if (ArrayUtil.isNotEmpty(visitorIds)) {
            List<DmReceptionVisitorMerge> receptionVisitorMerges = new ArrayList<DmReceptionVisitorMerge>();
            if (visitorIds == null) {
                return;
            }
            for (int p = 1; p < visitorIds.length; p++) {
                int priority = p;
                List<Long> visitorList = new ArrayList<>(Arrays.asList(visitorIds[p]));
                // 1.存储接待模式与访客的关联数据
                receptionVisitorMerges.addAll(visitorList.stream().filter(visitorId->visitorId!=null).map(visitorId -> new DmReceptionVisitorMerge(receptionId, visitorId, priority))
                        .collect(Collectors.toList()));
            }
            // 2.在数据库插入接待模式与访客的关联数据
            if (receptionVisitorMerges.size() > 0) {
                dmReceptionVisitorMergeMapper.insertBatch(receptionVisitorMerges);
            }

        }
    }

    @Override
    public List<DmReceptionVisitorMerge> selectDeceptionVisitorMerge(Long receptionId) {

        return dmReceptionVisitorMergeMapper.selectList(Wrappers. <DmReceptionVisitorMerge>query().lambda()
                .eq(DmReceptionVisitorMerge::getReceptionId,receptionId));
    }

    @Override
    public List<DmReceptionDto> selectReceptionTaskOpenStatus() {
        return baseConverter.mapperDto(baseMapper.selectList(Wrappers.<DmReceptionPo>query().lambda()
                .eq(DmReceptionPo::getStatus,1)));
    }

    @Override
    public int update(DmReceptionDto dto) {
        int row = super.update(dto);
        if (row > 0) {
            this.editReceptionVisitorMerge(dto.getId(),dto.getPriorities());
        }
        return row;
    }

    @Override
    public void editReceptionVisitorMerge(Long receptionId, Long[][] visitorIds) {
        // 1.校验organizeIds是否为空 ? 删除不存在的,增加新增的 : 删除所有
        if (ArrayUtil.isNotEmpty(visitorIds)) {
            // 1.计算所有visitorIds的总和
            List<Long> visitorIdList = new ArrayList<>();
            for (int p = 1; p < visitorIds.length; p++) {
                visitorIdList.addAll(Arrays.asList(visitorIds[p]));
            }
            // 2.删除所有任务相关数据
            dmReceptionVisitorMergeMapper.delete(Wrappers.<DmReceptionVisitorMerge>query().lambda()
                    .eq(DmReceptionVisitorMerge::getReceptionId, receptionId));
            // 3.新增所有任务相关数据
            List<DmReceptionVisitorMerge> receptionVisitorMerges = new ArrayList<DmReceptionVisitorMerge>();
            for (int p = 1; p < visitorIds.length; p++) {
                int priority = p;
                List<Long> visitorList = new ArrayList<>(Arrays.asList(visitorIds[p]));
                // 1.存储接待模式与访客的关联数据
                receptionVisitorMerges.addAll(visitorList.stream().filter(visitorId->visitorId!=null).map(visitorId -> new DmReceptionVisitorMerge(receptionId, visitorId, priority))
                        .collect(Collectors.toList()));
            }
            // 2.在数据库插入接待模式与访客的关联数据
            if (receptionVisitorMerges.size() > 0) {
                dmReceptionVisitorMergeMapper.insertBatch(receptionVisitorMerges);
            }
        }
    }
}