package com.flossom.system.service.impl;

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

import com.alibaba.nacos.common.utils.CollectionUtils;
import com.flossom.common.core.domain.entity.WxMode;
import com.flossom.common.core.domain.entity.WxModeTag;
import com.flossom.common.core.enums.TagTypeStatusEnum;
import com.flossom.common.core.mapper.WxModeMapper;
import com.flossom.common.core.mapper.WxModeTagMapper;
import com.flossom.common.core.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.flossom.system.service.IWxModeService;

/**
 * 模式列Service业务层处理
 *
 * @author flossom
 * @date 2024-01-17
 */
@Service
public class WxModeServiceImpl implements IWxModeService
{
    @Autowired
    private WxModeMapper wxModeMapper;
    @Autowired
    private WxModeTagMapper wxModeTagMapper;
    /**
     * 查询模式列
     *
     * @param id 模式列主键
     * @return 模式列
     */
    @Override
    public WxMode selectWxModeById(Long id)
    {
        WxMode wxMode = wxModeMapper.selectWxModeById(id);
        // 处理标签回显
        WxModeTag wxModeTag = new WxModeTag();
        wxModeTag.setModeId(id);
        List<WxModeTag> wxModeTags = wxModeTagMapper.selectWxModeTagList(wxModeTag);
        if (CollectionUtils.isNotEmpty(wxModeTags)) {
            List<WxModeTag> wechatList = wxModeTags.stream().filter(x -> String.valueOf(TagTypeStatusEnum.MINI_PROGRAM.getCode()).equals(String.valueOf(x.getTagType()))).collect(Collectors.toList());
            List<WxModeTag> companyList = wxModeTags.stream().filter(x -> String.valueOf(TagTypeStatusEnum.ENTERPRISE_WECHAT.getCode()).equals(String.valueOf(x.getTagType()))).collect(Collectors.toList());
            if (CollectionUtils.isNotEmpty(wechatList)) {
                List<Long> wechatIds = wechatList.stream().map(WxModeTag::getTagId).collect(Collectors.toList());
                wxMode.setLockWechatTagArray(wechatIds);
            }
            if (CollectionUtils.isNotEmpty(companyList)) {
                List<Long> companyIds = companyList.stream().map(WxModeTag::getTagId).collect(Collectors.toList());
                wxMode.setLockCompanyTagArray(companyIds);
            }
        }
        return wxMode;
    }

    /**
     * 查询模式列列表
     *
     * @param wxMode 模式列
     * @return 模式列
     */
    @Override
    public List<WxMode> selectWxModeList(WxMode wxMode)
    {
        return wxModeMapper.selectWxModeList(wxMode);
    }

    /**
     * 新增模式列
     *
     * @param wxMode 模式列
     * @return 结果
     */
    @Override
    public int insertWxMode(WxMode wxMode)
    {
        wxMode.setCreateTime(DateUtils.getNowDate());
        int i = wxModeMapper.insertWxMode(wxMode);
        // 插入企微关联关系
        if (CollectionUtils.isNotEmpty(wxMode.getLockCompanyTagArray())) {
            List<Long> lockCompanyTagArray = wxMode.getLockCompanyTagArray();
            for (Long tagId : lockCompanyTagArray) {
                WxModeTag modeTag = new WxModeTag();
                modeTag.setModeId(wxMode.getId());
                modeTag.setTagId(tagId);
                modeTag.setTagType(2l);
                modeTag.setCreateTime(DateUtils.getNowDate());
                wxModeTagMapper.insertWxModeTag(modeTag);
            }
        }
        // 插入小程序关联关系
        if (CollectionUtils.isNotEmpty(wxMode.getLockWechatTagArray())) {
            List<Long> lockWechatTagArray = wxMode.getLockWechatTagArray();
            for (Long tagId : lockWechatTagArray) {
                WxModeTag modeTag = new WxModeTag();
                modeTag.setModeId(wxMode.getId());
                modeTag.setTagId(tagId);
                modeTag.setTagType(1l);
                modeTag.setCreateTime(DateUtils.getNowDate());
                wxModeTagMapper.insertWxModeTag(modeTag);
            }
        }
        return i;
    }

    /**
     * 修改模式列
     *
     * @param wxMode 模式列
     * @return 结果
     */
    @Override
    public int updateWxMode(WxMode wxMode)
    {
        wxMode.setUpdateTime(DateUtils.getNowDate());
        // 先删除关联关系
        wxModeTagMapper.deleteWxModeTagByModeId(wxMode.getId());

        // 插入企微关联关系
        if (CollectionUtils.isNotEmpty(wxMode.getLockCompanyTagArray())) {
            List<Long> lockCompanyTagArray = wxMode.getLockCompanyTagArray();
            for (Long tagId : lockCompanyTagArray) {
                WxModeTag modeTag = new WxModeTag();
                modeTag.setModeId(wxMode.getId());
                modeTag.setTagId(tagId);
                modeTag.setTagType(2l);
                modeTag.setCreateTime(DateUtils.getNowDate());
                wxModeTagMapper.insertWxModeTag(modeTag);
            }
        }
        // 插入小程序关联关系
        if (CollectionUtils.isNotEmpty(wxMode.getLockWechatTagArray())) {
            List<Long> lockWechatTagArray = wxMode.getLockWechatTagArray();
            for (Long tagId : lockWechatTagArray) {
                WxModeTag modeTag = new WxModeTag();
                modeTag.setModeId(wxMode.getId());
                modeTag.setTagId(tagId);
                modeTag.setTagType(1l);
                modeTag.setCreateTime(DateUtils.getNowDate());
                wxModeTagMapper.insertWxModeTag(modeTag);
            }
        }
        return wxModeMapper.updateWxMode(wxMode);
    }

    /**
     * 批量删除模式列
     *
     * @param ids 需要删除的模式列主键
     * @return 结果
     */
    @Override
    public int deleteWxModeByIds(Long[] ids)
    {
        return wxModeMapper.deleteWxModeByIds(ids);
    }

    /**
     * 删除模式列信息
     *
     * @param id 模式列主键
     * @return 结果
     */
    @Override
    public int deleteWxModeById(Long id)
    {
        return wxModeMapper.deleteWxModeById(id);
    }
}
