package com.gic.haoban.manage.service.service.impl;

import com.gic.commons.util.UniqueIdUtils;
import com.gic.haoban.manage.api.enums.WelcomeSuitTypeEnum;
import com.gic.haoban.manage.service.dao.mapper.TabHaobanWelcomeSuitRangMapper;
import com.gic.haoban.manage.service.entity.TabHaobanWelcomeSuitRang;
import com.gic.haoban.manage.service.pojo.bo.welcome.WelcomeSuitDepartmentBO;
import com.gic.haoban.manage.service.pojo.bo.welcome.WelcomeSuitStaffBO;
import com.gic.haoban.manage.service.service.WelcomeSuitRangService;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * desc：欢迎语适用范围服务接口实现
 *
 * @author: YongEn
 * @date: 2022/2/10
 **/
@Service
public class WelcomeSuitRangServiceImpl implements WelcomeSuitRangService {

    @Autowired
    private TabHaobanWelcomeSuitRangMapper welcomeSuitRangMapper;

    private static final int INSERT_LIMIT = 500;

    @Override
    public void addSuitDepartment(String welcomeId, String wxEnterpriseId, List<WelcomeSuitDepartmentBO> suitDepartmentList) {
        if (CollectionUtils.isEmpty(suitDepartmentList)) {
            return;
        }
        int size = suitDepartmentList.size();
        if (size <= INSERT_LIMIT) {
            welcomeSuitRangMapper.batchInsert(convertWelcomeSuitRang(welcomeId, wxEnterpriseId, suitDepartmentList));
            return;
        }
        int from;
        int to;
        int times = (size / INSERT_LIMIT) + 1;
        for (int i = 1; i <= times; i++) {
            from = (i - 1) * INSERT_LIMIT;
            to = Math.min(i * INSERT_LIMIT, size);
            List<WelcomeSuitDepartmentBO> subTaskIdList = suitDepartmentList.subList(from, to);
            welcomeSuitRangMapper.batchInsert(convertWelcomeSuitRang(welcomeId, wxEnterpriseId, subTaskIdList));
        }
    }

    private List<TabHaobanWelcomeSuitRang> convertWelcomeSuitRang(String welcomeId,
                                                                  String wxEnterpriseId,
                                                                  List<WelcomeSuitDepartmentBO> suitDepartmentList) {
        return suitDepartmentList.stream().map(one -> {
            TabHaobanWelcomeSuitRang welcomeSuitRang = new TabHaobanWelcomeSuitRang();
            welcomeSuitRang.setWelcomeSuitRangId(UniqueIdUtils.uniqueLong());
            welcomeSuitRang.setWxEnterpriseId(wxEnterpriseId);
            welcomeSuitRang.setWelcomeId(welcomeId);
            welcomeSuitRang.setSuitType(WelcomeSuitTypeEnum.DEPARTMENT.getCode());
            welcomeSuitRang.setSuitDepartmentId(one.getDepartmentId());
            welcomeSuitRang.setSuitDepartmentName(one.getDepartmentName());
            welcomeSuitRang.setSuitStaffId("-1");
            welcomeSuitRang.setSuitStaffName("-1");
            welcomeSuitRang.setStaffBelongDepartmentId("-1");
            welcomeSuitRang.setStaffBelongDepartmentName("-1");
            welcomeSuitRang.setSelectType(one.getSelectType());
            welcomeSuitRang.setCreateTime(new Date());
            welcomeSuitRang.setUpdateTime(new Date());
            welcomeSuitRang.setDeleteFlag(0);
            return welcomeSuitRang;
        }).collect(Collectors.toList());
    }

    @Override
    public void addSuitStaff(String welcomeId, String wxEnterpriseId, List<WelcomeSuitStaffBO> suitStaffList) {
        if (CollectionUtils.isEmpty(suitStaffList)) {
            return;
        }
        // 构建实体类
        List<TabHaobanWelcomeSuitRang> welcomeSuitRangList = suitStaffList.stream().map(one -> {
            TabHaobanWelcomeSuitRang welcomeSuitRang = new TabHaobanWelcomeSuitRang();
            welcomeSuitRang.setWelcomeSuitRangId(UniqueIdUtils.uniqueLong());
            welcomeSuitRang.setWxEnterpriseId(wxEnterpriseId);
            welcomeSuitRang.setWelcomeId(welcomeId);
            welcomeSuitRang.setSuitType(WelcomeSuitTypeEnum.STAFF.getCode());
            welcomeSuitRang.setSuitDepartmentId("-1");
            welcomeSuitRang.setSuitDepartmentName("-1");
            welcomeSuitRang.setSuitStaffId(one.getStaffId());
            welcomeSuitRang.setSuitStaffName(one.getStaffName());
            welcomeSuitRang.setStaffBelongDepartmentId(one.getBelongDepartmentId());
            welcomeSuitRang.setStaffBelongDepartmentName(one.getBelongDepartmentName());
            welcomeSuitRang.setSelectType(1);
            welcomeSuitRang.setCreateTime(new Date());
            welcomeSuitRang.setUpdateTime(new Date());
            welcomeSuitRang.setDeleteFlag(0);
            return welcomeSuitRang;
        }).collect(Collectors.toList());
        welcomeSuitRangMapper.batchInsert(welcomeSuitRangList);
    }

    @Override
    public void deleteSuitDepartment(String welcomeId, String wxEnterpriseId, List<WelcomeSuitDepartmentBO> suitDepartmentList) {
        if (CollectionUtils.isEmpty(suitDepartmentList)) {
            return;
        }
        Set<String> welcomeSuitDepartmentIdSet = suitDepartmentList.stream()
                .map(WelcomeSuitDepartmentBO::getDepartmentId).collect(Collectors.toSet());
        welcomeSuitRangMapper.batchDeleteBySuitDepartmentIdSet(welcomeId, wxEnterpriseId, welcomeSuitDepartmentIdSet);
    }

    @Override
    public void deleteSuitStaff(String welcomeId, String wxEnterpriseId, List<WelcomeSuitStaffBO> suitStaffList) {
        if (CollectionUtils.isEmpty(suitStaffList)) {
            return;
        }
        Set<String> welcomeSuitDepartmentIdSet = suitStaffList.stream()
                .map(WelcomeSuitStaffBO::getStaffId).collect(Collectors.toSet());
        welcomeSuitRangMapper.batchDeleteBySuitStaffIdSet(welcomeId, wxEnterpriseId, welcomeSuitDepartmentIdSet);
    }

}
