package com.flossom.system.service.impl;

import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

import com.flossom.common.core.domain.entity.*;
import com.flossom.common.core.enums.SitePushTypeEnum;
import com.flossom.common.core.mapper.*;
import com.flossom.common.core.utils.DateUtils;
import com.flossom.common.core.utils.StringUtils;
import com.flossom.common.security.utils.SecurityUtils;
import org.apache.commons.compress.utils.Lists;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.flossom.system.service.ISiteInfoService;
import org.springframework.util.CollectionUtils;

/**
 * 站点管理Service业务层处理
 *
 * @author flossom
 * @date 2023-12-20
 */
@Service
public class SiteInfoServiceImpl implements ISiteInfoService
{
    @Autowired
    private SiteInfoMapper siteInfoMapper;
    @Autowired
    private SysTagMapper sysTagMapper;
    @Autowired
    private ActivityChannelInfoMapper activityChannelInfoMapper;
    @Autowired
    private SiteInfoTagMapper siteInfoTagMapper;
    @Autowired
    private SiteInfoChannelMapper siteInfoChannelMapper;
    @Autowired
    private WxInstrumentMapper wxInstrumentMapper;
    @Autowired
    private SiteInfoInstrumentMapper siteInfoInstrumentMapper;
    @Autowired
    private SiteLogMapper siteLogMapper;

    /**
     * 查询站点管理
     *
     * @param id 站点管理主键
     * @return 站点管理
     */
    @Override
    public SiteInfo selectSiteInfoById(Long id)
    {
        // 开始处理回显
        SiteInfo siteInfo = siteInfoMapper.selectSiteInfoById(id);
        SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        if(Objects.nonNull(siteInfo.getSiteStartTime())) {
            siteInfo.getStartTime().add(sd.format(siteInfo.getSiteStartTime()));
        }
        if(Objects.nonNull(siteInfo.getSiteEndTime())) {
            siteInfo.getStartTime().add(sd.format(siteInfo.getSiteEndTime()));
        }
        if(Objects.nonNull(siteInfo.getUserRegisterStartTime())) {
            siteInfo.getRegisterTime().add(sd.format(siteInfo.getUserRegisterStartTime()));
        }
        if(Objects.nonNull(siteInfo.getUserRegisterEndTime())) {
            siteInfo.getRegisterTime().add(sd.format(siteInfo.getUserRegisterEndTime()));
        }
        // 处理推送时间
        if (StringUtils.isNotEmpty(siteInfo.getPushTimer())) {
            List<Long> pushTimerList = Arrays.asList(siteInfo.getPushTimer().split(",")).stream().map(Long::parseLong).collect(Collectors.toList());
            siteInfo.setPushTimerArray(pushTimerList);
        }
        // 查询用的标签关联实体
        SiteInfoTag infoTag = new SiteInfoTag();
        infoTag.setSiteId(id);
        // 处理禁用外部标签数组
        if (StringUtils.isNotEmpty(siteInfo.getForbidCompanyTagId())) {
            // 关联表查询数据
            infoTag.setTagType(2);
            infoTag.setStatus(1l);
            List<SiteInfoTag> siteInfoTags = siteInfoTagMapper.selectSiteInfoTagList(infoTag);
            List<Long> forbidCompanyList = siteInfoTags.stream().map(SiteInfoTag::getTagId).collect(Collectors.toList());
            siteInfo.setForbidCompanyTagArray(forbidCompanyList);
        }
        // 处理禁用小程序标签数组
        if (StringUtils.isNotEmpty(siteInfo.getForbidWechatTagId())) {
            // 关联表查询数据
            infoTag.setTagType(1);
            infoTag.setStatus(1l);
            List<SiteInfoTag> siteInfoTags = siteInfoTagMapper.selectSiteInfoTagList(infoTag);
            List<Long> forbidWechatList = siteInfoTags.stream().map(SiteInfoTag::getTagId).collect(Collectors.toList());
            siteInfo.setForbidWechatTagArray(forbidWechatList);
        }
        // 处理可见小程序标签数组
        if (StringUtils.isNotEmpty(siteInfo.getShowWechatTagId())) {
            // 关联表查询数据
            infoTag.setTagType(1);
            infoTag.setStatus(0l);
            List<SiteInfoTag> siteInfoTags = siteInfoTagMapper.selectSiteInfoTagList(infoTag);
            List<Long> showWechatList = siteInfoTags.stream().map(SiteInfoTag::getTagId).collect(Collectors.toList());
            siteInfo.setShowWechatTagArray(showWechatList);
        }
        // 处理可见外部标签数组
        if (StringUtils.isNotEmpty(siteInfo.getShowCompanyTagId())) {
            // 关联表查询数据
            infoTag.setTagType(2);
            infoTag.setStatus(0l);
            List<SiteInfoTag> siteInfoTags = siteInfoTagMapper.selectSiteInfoTagList(infoTag);
            List<Long> showCompanyList = siteInfoTags.stream().map(SiteInfoTag::getTagId).collect(Collectors.toList());
            siteInfo.setShowCompanyTagArray(showCompanyList);
        }
        // 处理弹窗活动小程序标签
        if (StringUtils.isNotEmpty(siteInfo.getHandlerWechatTagId())){
            // 关联表查询数据
            infoTag.setTagType(1);
            infoTag.setStatus(2l);
            List<SiteInfoTag> siteInfoTags = siteInfoTagMapper.selectSiteInfoTagList(infoTag);
            List<Long> handlerWechatList = siteInfoTags.stream().map(SiteInfoTag::getTagId).collect(Collectors.toList());
            siteInfo.setHandlerWechatTagArray(handlerWechatList);
        }
        // 处理弹窗活动外部标签
        if (StringUtils.isNotEmpty(siteInfo.getHandlerCompanyTagId())){
            // 关联表查询数据
            infoTag.setTagType(2);
            infoTag.setStatus(2l);
            List<SiteInfoTag> siteInfoTags = siteInfoTagMapper.selectSiteInfoTagList(infoTag);
            List<Long> handlerCompanyList = siteInfoTags.stream().map(SiteInfoTag::getTagId).collect(Collectors.toList());
            siteInfo.setHandlerCompanyTagArray(handlerCompanyList);
        }
        // 处理渠道展示
        if (StringUtils.isNotEmpty(siteInfo.getChannelId())) {
            List<Long> channelList = Arrays.asList(siteInfo.getChannelId().split(",")).stream().map(Long::parseLong).collect(Collectors.toList());
            siteInfo.setChannelArray(channelList);
        }
        // 处理仪器展示
        if (StringUtils.isNotEmpty(siteInfo.getInstrumentId())) {
            List<Long> instrumentList = Arrays.asList(siteInfo.getInstrumentId().split(",")).stream().map(Long::parseLong).collect(Collectors.toList());
            siteInfo.setMachineArray(instrumentList);
        }
        return siteInfo;
    }

    /**
     * 查询站点管理列表
     *
     * @param siteInfo 站点管理
     * @return 站点管理
     */
    @Override
    public List<SiteInfo> selectSiteInfoList(SiteInfo siteInfo)
    {
        return siteInfoMapper.selectSiteInfoList(siteInfo);
    }

    /**
     * 新增站点管理
     *
     * @param siteInfo 站点管理
     * @return 结果
     */
    @Override
    public int insertSiteInfo(SiteInfo siteInfo)
    {
        siteInfo.setCreateTime(DateUtils.getNowDate());
        siteInfo.setCreateBy(SecurityUtils.getUsername());
        List<SysTag> showWechatTagNew = Lists.newArrayList();
        List<SysTag> showCompanyTagNew = Lists.newArrayList();
        List<SysTag> forbidWechatTagNew = Lists.newArrayList();
        List<SysTag> forbidCompanyTagNew = Lists.newArrayList();
        List<SysTag> handlerWechatTagNew = Lists.newArrayList();
        List<SysTag> handlerCompanyTagNew = Lists.newArrayList();
        List<ActivityChannelInfo> channelListNew = Lists.newArrayList();
        List<WxInstrument> wxInstrumentsListNew = Lists.newArrayList();
        this.commonHandler(siteInfo,showWechatTagNew,showCompanyTagNew,forbidWechatTagNew,forbidCompanyTagNew,channelListNew,handlerWechatTagNew,handlerCompanyTagNew,wxInstrumentsListNew);

        // 开始保存关联关系
        int i = siteInfoMapper.insertSiteInfo(siteInfo);

        this.commonHandlerRelation(siteInfo,showWechatTagNew,showCompanyTagNew,forbidWechatTagNew,forbidCompanyTagNew,channelListNew,handlerWechatTagNew,handlerCompanyTagNew,wxInstrumentsListNew);

        return i;
    }

    // 新增、更新统一处理关联关系
    private void commonHandlerRelation(SiteInfo siteInfo,List<SysTag> showWechatTagNew,List<SysTag> showCompanyTagNew,List<SysTag> forbidWechatTagNew,List<SysTag> forbidCompanyTagNew,List<ActivityChannelInfo> channelListNew,List<SysTag> handlerWechatTagNew,List<SysTag> handlerCompanyTagNew, List<WxInstrument> wxInstrumentsListNew){
        // 保存可见小程序标签关联关系
        for (SysTag tag: showWechatTagNew) {
            SiteInfoTag infoTag = new SiteInfoTag();
            infoTag.setSiteId(siteInfo.getId());
            infoTag.setTagId(tag.getId());
            infoTag.setTagType(1);
            infoTag.setStatus(0l);
            infoTag.setCreateBy(SecurityUtils.getUsername());
            infoTag.setCreateTime(DateUtils.getNowDate());
            siteInfoTagMapper.insertSiteInfoTag(infoTag);
        }
        // 保存可见外部标签关联关系
        for (SysTag tag: showCompanyTagNew) {
            SiteInfoTag infoTag = new SiteInfoTag();
            infoTag.setSiteId(siteInfo.getId());
            infoTag.setTagId(tag.getId());
            infoTag.setTagType(2);
            infoTag.setStatus(0l);
            infoTag.setCreateBy(SecurityUtils.getUsername());
            infoTag.setCreateTime(DateUtils.getNowDate());
            siteInfoTagMapper.insertSiteInfoTag(infoTag);
        }
        // 保存禁用小程序关联关系
        for (SysTag tag: forbidWechatTagNew) {
            SiteInfoTag infoTag = new SiteInfoTag();
            infoTag.setSiteId(siteInfo.getId());
            infoTag.setTagId(tag.getId());
            infoTag.setTagType(1);
            infoTag.setStatus(1l);
            infoTag.setCreateBy(SecurityUtils.getUsername());
            infoTag.setCreateTime(DateUtils.getNowDate());
            siteInfoTagMapper.insertSiteInfoTag(infoTag);
        }
        // 保存禁用外部标签关联关系
        for (SysTag tag: forbidCompanyTagNew) {
            SiteInfoTag infoTag = new SiteInfoTag();
            infoTag.setSiteId(siteInfo.getId());
            infoTag.setTagId(tag.getId());
            infoTag.setTagType(2);
            infoTag.setStatus(1l);
            infoTag.setCreateBy(SecurityUtils.getUsername());
            infoTag.setCreateTime(DateUtils.getNowDate());
            siteInfoTagMapper.insertSiteInfoTag(infoTag);
        }
        // 保存渠道关联关系
        for (ActivityChannelInfo channel: channelListNew) {
            SiteInfoChannel channelInfo = new SiteInfoChannel();
            channelInfo.setSiteId(siteInfo.getId());
            channelInfo.setChannelId(channel.getId());
            channelInfo.setStatus(0l);
            channelInfo.setCreateBy(SecurityUtils.getUsername());
            channelInfo.setCreateTime(DateUtils.getNowDate());
            siteInfoChannelMapper.insertSiteInfoChannel(channelInfo);
        }
        // 保存获得小程序关联关系
        for (SysTag tag: handlerWechatTagNew) {
            SiteInfoTag infoTag = new SiteInfoTag();
            infoTag.setSiteId(siteInfo.getId());
            infoTag.setTagId(tag.getId());
            infoTag.setTagType(1);
            infoTag.setStatus(2l);
            infoTag.setCreateBy(SecurityUtils.getUsername());
            infoTag.setCreateTime(DateUtils.getNowDate());
            siteInfoTagMapper.insertSiteInfoTag(infoTag);
        }
        // 保存获得外部标签关联关系
        for (SysTag tag: handlerCompanyTagNew) {
            SiteInfoTag infoTag = new SiteInfoTag();
            infoTag.setSiteId(siteInfo.getId());
            infoTag.setTagId(tag.getId());
            infoTag.setTagType(2);
            infoTag.setStatus(2l);
            infoTag.setCreateBy(SecurityUtils.getUsername());
            infoTag.setCreateTime(DateUtils.getNowDate());
            siteInfoTagMapper.insertSiteInfoTag(infoTag);
        }
        // 保存仪器关联信息
        for (WxInstrument wxInstrument: wxInstrumentsListNew) {
            SiteInfoInstrument infoInstrument = new SiteInfoInstrument();
            infoInstrument.setInstrumentId(wxInstrument.getId());
            infoInstrument.setSiteId(siteInfo.getId());
            infoInstrument.setCreateBy(SecurityUtils.getUsername());
            infoInstrument.setCreateTime(DateUtils.getNowDate());
            siteInfoInstrumentMapper.insertSiteInfoInstrument(infoInstrument);
        }
    }
    // 新增、更新统一调用汇总
    private void commonHandler(SiteInfo siteInfo,List<SysTag> showWechatTagNew,List<SysTag> showCompanyTagNew,List<SysTag> forbidWechatTagNew,List<SysTag> forbidCompanyTagNew,List<ActivityChannelInfo> channelListNew,List<SysTag> handlerWechatTagNew,List<SysTag> handlerCompanyTagNew,List<WxInstrument> wxInstrumentsListNew){
        // 如果是自定义频次，则需要保存推送时间 周一到周日
        if (!CollectionUtils.isEmpty(siteInfo.getPushTimerArray())) {
            siteInfo.setPushTimer(StringUtils.join(siteInfo.getPushTimerArray(),","));
        }
        // 保存可见小程序标签
        if (!CollectionUtils.isEmpty(siteInfo.getShowWechatTagArray())) {
            List<Long> showWechatTagArray = siteInfo.getShowWechatTagArray();
            for(long tagId : showWechatTagArray) {
                SysTag sysTag = sysTagMapper.selectDeptById(tagId);
                if (Objects.nonNull(sysTag)) {
                    showWechatTagNew.add(sysTag);
                }
            }
        }
        if (!CollectionUtils.isEmpty(showWechatTagNew)) {
            String tagNames = showWechatTagNew.stream().map(SysTag::getTagName).collect(Collectors.joining(","));
            siteInfo.setShowWechatTag(tagNames);
            List<Long> resultList = showWechatTagNew.stream().map(SysTag::getId).collect(Collectors.toList());
            String ids = StringUtils.join(resultList, ",");
            siteInfo.setShowWechatTagId(ids);
        }
        // 保存可见外部标签
        if (!CollectionUtils.isEmpty(siteInfo.getShowCompanyTagArray())) {
            List<Long> showCompanyTagArray = siteInfo.getShowCompanyTagArray();
            for(long tagId : showCompanyTagArray) {
                SysTag sysTag = sysTagMapper.selectDeptById(tagId);
                if (Objects.nonNull(sysTag)) {
                    showCompanyTagNew.add(sysTag);
                }
            }
        }
        if (!CollectionUtils.isEmpty(showCompanyTagNew)) {
            String tagNames = showCompanyTagNew.stream().map(SysTag::getTagName).collect(Collectors.joining(","));
            siteInfo.setShowCompanyTag(tagNames);
            List<Long> resultList = showCompanyTagNew.stream().map(SysTag::getId).collect(Collectors.toList());
            String ids = StringUtils.join(resultList, ",");
            siteInfo.setShowCompanyTagId(ids);
        }
        // 保存禁用小程序标签
        if (!CollectionUtils.isEmpty(siteInfo.getForbidWechatTagArray())) {
            List<Long> forbidWechatTagArray = siteInfo.getForbidWechatTagArray();
            for(long tagId : forbidWechatTagArray) {
                SysTag sysTag = sysTagMapper.selectDeptById(tagId);
                if (Objects.nonNull(sysTag)) {
                    forbidWechatTagNew.add(sysTag);
                }
            }
        }
        if (!CollectionUtils.isEmpty(forbidWechatTagNew)) {
            String tagNames = forbidWechatTagNew.stream().map(SysTag::getTagName).collect(Collectors.joining(","));
            siteInfo.setForbidWechatTag(tagNames);
            List<Long> resultList = forbidWechatTagNew.stream().map(SysTag::getId).collect(Collectors.toList());
            String ids = StringUtils.join(resultList, ",");
            siteInfo.setForbidWechatTagId(ids);
        }
        // 保存禁用外部标签
        if (!CollectionUtils.isEmpty(siteInfo.getForbidCompanyTagArray())) {
            List<Long> forbidCompanyTagArray = siteInfo.getForbidCompanyTagArray();
            for(long tagId : forbidCompanyTagArray) {
                SysTag sysTag = sysTagMapper.selectDeptById(tagId);
                if (Objects.nonNull(sysTag)) {
                    forbidCompanyTagNew.add(sysTag);
                }
            }
        }
        if (!CollectionUtils.isEmpty(forbidCompanyTagNew)) {
            String tagNames = forbidCompanyTagNew.stream().map(SysTag::getTagName).collect(Collectors.joining(","));
            siteInfo.setForbidCompanyTag(tagNames);
            List<Long> resultList = forbidCompanyTagNew.stream().map(SysTag::getId).collect(Collectors.toList());
            String ids = StringUtils.join(resultList, ",");
            siteInfo.setForbidCompanyTagId(ids);
        }

        // 保存获得外部标签
        if (!CollectionUtils.isEmpty(siteInfo.getHandlerCompanyTagArray())) {
            List<Long> handlerCompanyTagArray = siteInfo.getHandlerCompanyTagArray();
            for(long tagId : handlerCompanyTagArray) {
                SysTag sysTag = sysTagMapper.selectDeptById(tagId);
                if (Objects.nonNull(sysTag)) {
                    handlerCompanyTagNew.add(sysTag);
                }
            }
        }
        if (!CollectionUtils.isEmpty(handlerCompanyTagNew)) {
            String tagNames = handlerCompanyTagNew.stream().map(SysTag::getTagName).collect(Collectors.joining(","));
            siteInfo.setHandlerCompanyTag(tagNames);
            List<Long> resultList = handlerCompanyTagNew.stream().map(SysTag::getId).collect(Collectors.toList());
            String ids = StringUtils.join(resultList, ",");
            siteInfo.setHandlerCompanyTagId(ids);
        }

        // 保存获得小程序标签
        if (!CollectionUtils.isEmpty(siteInfo.getHandlerWechatTagArray())) {
            List<Long> handlerWechatTagArray = siteInfo.getHandlerWechatTagArray();
            for(long tagId : handlerWechatTagArray) {
                SysTag sysTag = sysTagMapper.selectDeptById(tagId);
                if (Objects.nonNull(sysTag)) {
                    handlerWechatTagNew.add(sysTag);
                }
            }
        }
        if (!CollectionUtils.isEmpty(handlerWechatTagNew)) {
            String tagNames = handlerWechatTagNew.stream().map(SysTag::getTagName).collect(Collectors.joining(","));
            siteInfo.setHandlerWechatTag(tagNames);
            List<Long> resultList = handlerWechatTagNew.stream().map(SysTag::getId).collect(Collectors.toList());
            String ids = StringUtils.join(resultList, ",");
            siteInfo.setHandlerWechatTagId(ids);
        }


        // 保存渠道信息
        if (!CollectionUtils.isEmpty(siteInfo.getChannelArray())) {
            List<Long> channelArray = siteInfo.getChannelArray();
            for(long channelId : channelArray) {
                ActivityChannelInfo channelInfo = activityChannelInfoMapper.selectActivityChannelInfoById(channelId);
                if (Objects.nonNull(channelInfo)) {
                    channelListNew.add(channelInfo);
                }
            }
        }
        if (!CollectionUtils.isEmpty(channelListNew)) {
            String channelNames = channelListNew.stream().map(ActivityChannelInfo::getChannelName).collect(Collectors.joining(","));
            siteInfo.setChannelName(channelNames);
            List<Long> resultList = channelListNew.stream().map(ActivityChannelInfo::getId).collect(Collectors.toList());
            String ids = StringUtils.join(resultList, ",");
            siteInfo.setChannelId(ids);
        }
        // 保存仪器信息
        if (!CollectionUtils.isEmpty(siteInfo.getMachineArray())) {
            List<Long> machineArray = siteInfo.getMachineArray();
            for(long instrumentId : machineArray) {
                WxInstrument wxInstrument = wxInstrumentMapper.selectWxInstrumentById(instrumentId);
                if (Objects.nonNull(wxInstrument)) {
                    wxInstrumentsListNew.add(wxInstrument);
                }
            }
        }
        if (!CollectionUtils.isEmpty(wxInstrumentsListNew)) {
            String channelNames = wxInstrumentsListNew.stream().map(WxInstrument::getName).collect(Collectors.joining(","));
            siteInfo.setInstrumentName(channelNames);
            List<Long> resultList = wxInstrumentsListNew.stream().map(WxInstrument::getId).collect(Collectors.toList());
            String ids = StringUtils.join(resultList, ",");
            siteInfo.setInstrumentId(ids);
        }
    }

    /**
     * 修改站点管理
     *
     * @param siteInfo 站点管理
     * @return 结果
     */
    @Override
    public int updateSiteInfo(SiteInfo siteInfo)
    {
        siteInfo.setUpdateTime(DateUtils.getNowDate());
        siteInfo.setUpdateBy(SecurityUtils.getUsername());
        siteInfo.setForbidCompanyTag("");
        siteInfo.setForbidCompanyTagId("");
        siteInfo.setForbidWechatTag("");
        siteInfo.setForbidWechatTagId("");
        siteInfo.setChannelId("");
        siteInfo.setChannelName("");
        siteInfo.setShowWechatTagId("");
        siteInfo.setShowWechatTag("");
        siteInfo.setShowCompanyTagId("");
        siteInfo.setShowCompanyTag("");
        siteInfo.setInstrumentId("");
        siteInfo.setInstrumentName("");
        List<SysTag> showWechatTagNew = Lists.newArrayList();
        List<SysTag> showCompanyTagNew = Lists.newArrayList();
        List<SysTag> forbidWechatTagNew = Lists.newArrayList();
        List<SysTag> forbidCompanyTagNew = Lists.newArrayList();
        List<ActivityChannelInfo> channelListNew = Lists.newArrayList();
        List<SysTag> handlerWechatTagNew = Lists.newArrayList();
        List<SysTag> handlerCompanyTagNew = Lists.newArrayList();
        List<WxInstrument> wxInstrumentList = Lists.newArrayList();

        SiteInfo oldSiteInfo = siteInfoMapper.selectSiteInfoById(siteInfo.getId());
        // 修改 开启时间，清理弹窗推送记录
        if (oldSiteInfo.getSiteStartTime() != siteInfo.getSiteStartTime()
                || oldSiteInfo.getSiteEndTime() != siteInfo.getSiteEndTime() ) {
            siteLogMapper.deleteBySiteId(siteInfo.getId());
        }
        // 修改 推送频率，清理弹窗推送记录
        if(oldSiteInfo.getPushType() != siteInfo.getPushType()) {
            siteLogMapper.deleteBySiteId(siteInfo.getId());
        } else {
            // 推送频率未改动，但是在自定义推送时，还需判断推送次数和推送日期是否修改
            if(SitePushTypeEnum.CUSTOM.getCode() == siteInfo.getPushType()) {
                if(oldSiteInfo.getPushNumber() != siteInfo.getPushNumber()) {
                    siteLogMapper.deleteBySiteId(siteInfo.getId());
                }
                if(StringUtils.isNotBlank(oldSiteInfo.getPushTimer())) {
                    List<Integer> oldPushTimer = Arrays.stream(oldSiteInfo.getPushTimer().split(","))
                            .map(Integer::valueOf)
                            .sorted()
                            .collect(Collectors.toList());
                    List<Integer> newPushTimer = Arrays.stream(siteInfo.getPushTimer().split(","))
                            .map(Integer::valueOf)
                            .sorted()
                            .collect(Collectors.toList());
                    if(!oldPushTimer.containsAll(newPushTimer) || !newPushTimer.containsAll(oldPushTimer)) {
                        siteLogMapper.deleteBySiteId(siteInfo.getId());
                    }
                } else {
                    siteLogMapper.deleteBySiteId(siteInfo.getId());
                }
            }
        }

        this.commonHandler(siteInfo,showWechatTagNew,showCompanyTagNew,forbidWechatTagNew,forbidCompanyTagNew,channelListNew,handlerWechatTagNew,handlerCompanyTagNew,wxInstrumentList);
        // 删除关联关系
        siteInfoTagMapper.deleteSiteInfoTagBySiteId(siteInfo.getId());

        // 保存新的关联关系
        this.commonHandlerRelation(siteInfo,showWechatTagNew,showCompanyTagNew,forbidWechatTagNew,forbidCompanyTagNew,channelListNew,handlerWechatTagNew,handlerCompanyTagNew,wxInstrumentList);
        if(SitePushTypeEnum.CUSTOM.getCode() != siteInfo.getPushType()) {
            siteInfo.setPushNumber(null);
            siteInfo.setPushTimer(null);
            siteInfo.setPushTimerArray(null);
        }
        return siteInfoMapper.updateSiteInfo(siteInfo);
    }

    /**
     * 批量删除站点管理
     *
     * @param ids 需要删除的站点管理主键
     * @return 结果
     */
    @Override
    public int deleteSiteInfoByIds(Long[] ids)
    {
        return siteInfoMapper.deleteSiteInfoByIds(ids);
    }

    /**
     * 删除站点管理信息
     *
     * @param id 站点管理主键
     * @return 结果
     */
    @Override
    public int deleteSiteInfoById(Long id)
    {
        return siteInfoMapper.deleteSiteInfoById(id);
    }

    @Override
    public int deleteSiteInfoByTagId(Long tagId) {

        return siteInfoTagMapper.deleteSiteInfoTagByTagId(tagId);
    }
}
