package com.flossom.system.service.impl;

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

import com.flossom.common.core.domain.entity.SysTag;
import com.flossom.common.core.domain.entity.WxScriptTag;
import com.flossom.common.core.domain.entity.WxScriptTemplate;
import com.flossom.common.core.mapper.SysTagMapper;
import com.flossom.common.core.mapper.WxScriptTagMapper;
import com.flossom.common.core.mapper.WxScriptTemplateMapper;
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.IWxScriptTemplateService;
import org.springframework.util.CollectionUtils;

/**
 * 话术模板Service业务层处理
 *
 * @author flossom
 * @date 2023-12-18
 */
@Service
public class WxScriptTemplateServiceImpl implements IWxScriptTemplateService
{
    @Autowired
    private WxScriptTemplateMapper wxScriptTemplateMapper;
    @Autowired
    private WxScriptTagMapper wxScriptTagMapper;
    @Autowired
    private SysTagMapper sysTagMapper;
    /**
     * 查询话术模板
     *
     * @param id 话术模板主键
     * @return 话术模板
     */
    @Override
    public WxScriptTemplate selectWxScriptTemplateById(Long id)
    {
        return wxScriptTemplateMapper.selectWxScriptTemplateById(id);
    }

    /**
     * 查询话术模板列表
     *
     * @param wxScriptTemplate 话术模板
     * @return 话术模板
     */
    @Override
    public List<WxScriptTemplate> selectWxScriptTemplateList(WxScriptTemplate wxScriptTemplate)
    {
        return wxScriptTemplateMapper.selectWxScriptTemplateList(wxScriptTemplate);
    }

    /**
     * 新增话术模板
     *
     * @param wxScriptTemplate 话术模板
     * @return 结果
     */
    @Override
    public int insertWxScriptTemplate(WxScriptTemplate wxScriptTemplate)
    {
        wxScriptTemplate.setCreateTime(DateUtils.getNowDate());
        wxScriptTemplate.setCreateBy(SecurityUtils.getLoginUser().getUsername());
        List<SysTag> sysTags = Lists.newArrayList();
        // 处理标签在字段的展示
        List<Long> tagIds = wxScriptTemplate.getTagIdArray();
        if(!CollectionUtils.isEmpty(tagIds)) {
            for (long tagId:tagIds) {
                SysTag sysTag = sysTagMapper.selectDeptById(tagId);
                if (Objects.nonNull(sysTag)) {
                    sysTags.add(sysTag);
                }
            }
        }
        if (!CollectionUtils.isEmpty(sysTags)) {
            String tagNames = sysTags.stream().map(SysTag::getTagName).collect(Collectors.joining(","));
            wxScriptTemplate.setTagNames(tagNames);
            List<Long> resultList = sysTags.stream().map(SysTag::getId).collect(Collectors.toList());
            String ids = StringUtils.join(resultList, ",");
            wxScriptTemplate.setTagIds(ids);
        }

        int i = wxScriptTemplateMapper.insertWxScriptTemplate(wxScriptTemplate);

        for(SysTag tag : sysTags) {
            // 增加关联关系
            WxScriptTag scriptTag = new WxScriptTag();
            scriptTag.setScriptId(wxScriptTemplate.getId());
            scriptTag.setTagId(tag.getId());
            scriptTag.setStatus(0l);
            scriptTag.setCreateBy(SecurityUtils.getLoginUser().getUsername());
            scriptTag.setCreateTime(DateUtils.getNowDate());
            wxScriptTagMapper.insertWxScriptTag(scriptTag);
        }
        return i;
    }

    /**
     * 修改话术模板
     *
     * @param wxScriptTemplate 话术模板
     * @return 结果
     */
    @Override
    public int updateWxScriptTemplate(WxScriptTemplate wxScriptTemplate)
    {
        wxScriptTemplate.setUpdateTime(DateUtils.getNowDate());
        wxScriptTemplate.setUpdateBy(SecurityUtils.getLoginUser().getUsername());
        wxScriptTemplate.setTagIds("");
        wxScriptTemplate.setTagNames("");
        List<SysTag> sysTags = Lists.newArrayList();
        // 处理标签在字段的展示
        List<Long> tagIds = wxScriptTemplate.getTagIdArray();
        if(!CollectionUtils.isEmpty(tagIds)) {
            for (long tagId:tagIds) {
                SysTag sysTag = sysTagMapper.selectDeptById(tagId);
                if (Objects.nonNull(sysTag)) {
                    sysTags.add(sysTag);
                }
            }
        }
        if (!CollectionUtils.isEmpty(sysTags)) {
            String tagNames = sysTags.stream().map(SysTag::getTagName).collect(Collectors.joining(","));
            wxScriptTemplate.setTagNames(tagNames);
            List<Long> resultList = sysTags.stream().map(SysTag::getId).collect(Collectors.toList());
            String ids = StringUtils.join(resultList, ",");
            wxScriptTemplate.setTagIds(ids);
        }

        // 先删除原本的关系
        wxScriptTagMapper.deleteWxScriptByScriptId(wxScriptTemplate.getId());

        for(SysTag tag : sysTags) {
            // 增加关联关系
            WxScriptTag scriptTag = new WxScriptTag();
            scriptTag.setScriptId(wxScriptTemplate.getId());
            scriptTag.setTagId(tag.getId());
            scriptTag.setStatus(0l);
            scriptTag.setCreateBy(SecurityUtils.getLoginUser().getUsername());
            scriptTag.setCreateTime(DateUtils.getNowDate());
            wxScriptTagMapper.insertWxScriptTag(scriptTag);
        }
        return wxScriptTemplateMapper.updateWxScriptTemplate(wxScriptTemplate);
    }

    /**
     * 批量删除话术模板
     *
     * @param ids 需要删除的话术模板主键
     * @return 结果
     */
    @Override
    public int deleteWxScriptTemplateByIds(Long[] ids)
    {
        return wxScriptTemplateMapper.deleteWxScriptTemplateByIds(ids);
    }

    /**
     * 删除话术模板信息
     *
     * @param id 话术模板主键
     * @return 结果
     */
    @Override
    public int deleteWxScriptTemplateById(Long id)
    {
        // 先删除原本的关系
        int i = wxScriptTagMapper.deleteWxScriptByScriptId(id);
        return wxScriptTemplateMapper.deleteWxScriptTemplateById(id);
    }

    @Override
    public boolean hasChildByScriptId(Long id) {
        int result = wxScriptTemplateMapper.hasChildByScriptId(id);
        return result > 0;
    }
}
