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

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.gic.commons.util.EntityUtil;
import com.gic.commons.util.UniqueIdUtils;
import com.gic.haoban.manage.api.dto.contact.ContactConfigDTO;
import com.gic.haoban.manage.service.dao.mapper.contact.TabContactConfigMapper;
import com.gic.haoban.manage.service.entity.contact.TabContactConfig;
import com.gic.haoban.manage.service.service.contact.ContactConfigService;
import org.apache.commons.lang3.StringUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

@Service("contactConfigService")
public class ContactConfigServiceImpl implements ContactConfigService {

    private static final Logger logger = LogManager.getLogger(ContactConfigService.class);

    @Autowired
    private TabContactConfigMapper contactConfigMapper;

    @Override
    public void saveContactConfig(ContactConfigDTO contactConfig) {
        logger.info("保存联系配置信息:{}", JSON.toJSONString(contactConfig));
        if (contactConfig.getContactAutoList() == null) {
            contactConfig.setContactAutoList(Collections.emptyList());
        }
        if (contactConfig.getRemarkContentList() == null) {
            contactConfig.setRemarkContentList(Collections.emptyList());
        }
        String enterpriseId = contactConfig.getEnterpriseId();
        TabContactConfig config = contactConfigMapper.queryByEnterpriseId(enterpriseId);
        if (config != null) {
            config.setRemarkFlag(contactConfig.getRemarkFlag());
            config.setContactAuto(contactConfig.getContactAutoList().stream().map(String::valueOf).collect(Collectors.joining(",")));
            config.setRemarkContent(JSON.toJSONString(contactConfig.getRemarkContentList()));
            contactConfigMapper.update(config);
        }else {
            config = EntityUtil.changeEntityNew(TabContactConfig.class, contactConfig);
            config.setConfigId(UniqueIdUtils.uniqueLong());
            config.setContactAuto(contactConfig.getContactAutoList().stream().map(String::valueOf).collect(Collectors.joining(",")));
            config.setRemarkContent(JSON.toJSONString(contactConfig.getRemarkContentList()));
            contactConfigMapper.insert(config);
        }
    }

    @Override
    public ContactConfigDTO getContactConfig(String enterpriseId) {
        TabContactConfig config = contactConfigMapper.queryByEnterpriseId(enterpriseId);
        if (config == null) {
            return null;
        }
        ContactConfigDTO contactConfigDTO = EntityUtil.changeEntityNew(ContactConfigDTO.class, config);
        List<Integer> contactAutoList = Optional.ofNullable(config.getContactAuto())
                .filter(StringUtils::isNotBlank)
                .map(s -> Arrays.stream(s.split(",")).map(Integer::valueOf).collect(Collectors.toList()))
                .orElse(Collections.emptyList());
        contactConfigDTO.setContactAutoList(contactAutoList);
        List<String> remarkContentList = Optional.ofNullable(config.getRemarkContent())
                .filter(StringUtils::isNotBlank)
                .map(x -> JSONArray.parseArray(x, String.class))
                .orElse(Collections.emptyList());
        contactConfigDTO.setRemarkContentList(remarkContentList);
        return contactConfigDTO;
    }
}
