package com.flossom.system.service.impl;

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

import com.alibaba.nacos.common.utils.CollectionUtils;
import com.flossom.common.core.domain.entity.*;
import com.flossom.common.core.enums.TagTypeStatusEnum;
import com.flossom.common.core.mapper.*;
import com.flossom.common.core.utils.DateUtils;
import com.flossom.common.core.utils.StringUtils;
import org.apache.commons.compress.utils.Lists;
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;
    @Autowired
    private WxModeOpenSourceMapper wxModeOpenSourceMapper;
    @Autowired
    private WxModeServiceInfoMapper wxModeServiceInfoMapper;
    @Autowired
    private WxModeCombineInfoMapper wxModeCombineInfoMapper;
    /**
     * 查询模式列
     *
     * @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);
            }
        }
        WxModeOpenSource openSource = new WxModeOpenSource();
        openSource.setModeId(id);
        List<WxModeOpenSource> wxModeOpenSources = wxModeOpenSourceMapper.selectWxModeOpenSourceList(openSource);
        wxMode.setOpenSourceData(wxModeOpenSources);

        // 获取服务内容
        WxModeServiceInfo serviceInfo = new WxModeServiceInfo();
        serviceInfo.setModeId(id);
        List<WxModeServiceInfo> wxModeServiceInfos = wxModeServiceInfoMapper.selectWxModeServiceInfoList(serviceInfo);
        for (WxModeServiceInfo wxModeServiceInfo: wxModeServiceInfos) {
            List<Date> queryDate = Lists.newArrayList();
            queryDate.add(wxModeServiceInfo.getServiceStartTime());
            queryDate.add(wxModeServiceInfo.getServiceEndTime());
            wxModeServiceInfo.setStartTimeArray(queryDate);
        }
        wxMode.setServiceData(wxModeServiceInfos);

        // 获取组合内容
        WxModeCombineInfo modeCombineInfo = new WxModeCombineInfo();
        modeCombineInfo.setModeId(id);
        List<WxModeCombineInfo> wxModeCombineInfos = wxModeCombineInfoMapper.selectWxModeCombineInfoList(modeCombineInfo);
        for(WxModeCombineInfo wxModeCombineInfo:wxModeCombineInfos) {
            String technologyInfo = wxModeCombineInfo.getTechnologyInfo();
            if (StringUtils.isNotEmpty(technologyInfo)) {
                List<String> technologyList = Arrays.asList(technologyInfo.split(",")).stream().map(String::valueOf).collect(Collectors.toList());
                wxModeCombineInfo.setTechnologyArray(technologyList);
            }
        }
        wxMode.setCombineData(wxModeCombineInfos);
        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);
            }
        }
        // 插入弹出资源数据
        if (CollectionUtils.isNotEmpty(wxMode.getOpenSourceData())) {
            List<WxModeOpenSource> openSourceData = wxMode.getOpenSourceData();
            for(WxModeOpenSource openSource: openSourceData) {
                openSource.setCreateTime(DateUtils.getNowDate());
                openSource.setModeId(wxMode.getId());
                wxModeOpenSourceMapper.insertWxModeOpenSource(openSource);
            }
        }
        // 插入服务时间内容
        if (CollectionUtils.isNotEmpty(wxMode.getServiceData())) {
            List<WxModeServiceInfo> serviceData = wxMode.getServiceData();
            for (WxModeServiceInfo wxModeServiceInfo: serviceData) {
                wxModeServiceInfo.setModeId(wxMode.getId());
                wxModeServiceInfo.setCreateTime(DateUtils.getNowDate());
                List<Date> startTimeArray = wxModeServiceInfo.getStartTimeArray();
                wxModeServiceInfo.setServiceStartTime(startTimeArray.get(0));
                wxModeServiceInfo.setServiceEndTime(startTimeArray.get(1));
                wxModeServiceInfoMapper.insertWxModeServiceInfo(wxModeServiceInfo);
            }
        }
        // 插入组合数据
        if(CollectionUtils.isNotEmpty(wxMode.getCombineData())) {
            List<WxModeCombineInfo> combineData = wxMode.getCombineData();
            for (WxModeCombineInfo wxModeCombineInfo: combineData) {
                wxModeCombineInfo.setModeId(wxMode.getId());
                wxModeCombineInfo.setCreateTime(DateUtils.getNowDate());
                if (CollectionUtils.isNotEmpty(wxModeCombineInfo.getTechnologyArray())) {
                    String keys = wxModeCombineInfo.getTechnologyArray().stream().map(String::valueOf).collect(Collectors.joining(","));
                    wxModeCombineInfo.setTechnologyInfo(keys);
                }
                wxModeCombineInfoMapper.insertWxModeCombineInfo(wxModeCombineInfo);
            }
        }
        return i;
    }

    /**
     * 修改模式列
     *
     * @param wxMode 模式列
     * @return 结果
     */
    @Override
    public int updateWxMode(WxMode wxMode)
    {
        wxMode.setUpdateTime(DateUtils.getNowDate());
        // 先删除关联关系
        wxModeTagMapper.deleteWxModeTagByModeId(wxMode.getId());
        // 删除关联弹窗视频
        wxModeOpenSourceMapper.deleteWxModeOpenSourceByModeId(wxMode.getId());
        // 删除服务关系
        wxModeServiceInfoMapper.deleteWxModeServiceInfoByModeId(wxMode.getId());
        // 删除组合关系
        wxModeCombineInfoMapper.deleteWxModeCombineInfoByModeId(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);
            }
        }
        // 插入弹出资源数据
        if (CollectionUtils.isNotEmpty(wxMode.getOpenSourceData())) {
            List<WxModeOpenSource> openSourceData = wxMode.getOpenSourceData();
            for(WxModeOpenSource openSource: openSourceData) {
                openSource.setCreateTime(DateUtils.getNowDate());
                openSource.setModeId(wxMode.getId());
                wxModeOpenSourceMapper.insertWxModeOpenSource(openSource);
            }
        }
        // 插入服务时间已经内容
        if (CollectionUtils.isNotEmpty(wxMode.getServiceData())) {
            List<WxModeServiceInfo> serviceData = wxMode.getServiceData();
            for (WxModeServiceInfo wxModeServiceInfo: serviceData) {
                wxModeServiceInfo.setModeId(wxMode.getId());
                wxModeServiceInfo.setCreateTime(DateUtils.getNowDate());
                List<Date> startTimeArray = wxModeServiceInfo.getStartTimeArray();
                wxModeServiceInfo.setServiceStartTime(startTimeArray.get(0));
                wxModeServiceInfo.setServiceEndTime(startTimeArray.get(1));
                wxModeServiceInfoMapper.insertWxModeServiceInfo(wxModeServiceInfo);
            }
        }
        // 插入组合数据
        if(CollectionUtils.isNotEmpty(wxMode.getCombineData())) {
            List<WxModeCombineInfo> combineData = wxMode.getCombineData();
            for (WxModeCombineInfo wxModeCombineInfo: combineData) {
                wxModeCombineInfo.setModeId(wxMode.getId());
                wxModeCombineInfo.setCreateTime(DateUtils.getNowDate());
                if (CollectionUtils.isNotEmpty(wxModeCombineInfo.getTechnologyArray())) {
                    String keys = wxModeCombineInfo.getTechnologyArray().stream().map(String::valueOf).collect(Collectors.joining(","));
                    wxModeCombineInfo.setTechnologyInfo(keys);
                }
                wxModeCombineInfoMapper.insertWxModeCombineInfo(wxModeCombineInfo);
            }
        }
        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);
    }
}
