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

import com.alibaba.fastjson.JSONObject;
import com.gic.api.base.commons.BasePageInfo;
import com.gic.api.base.commons.Page;
import com.gic.api.base.commons.ServiceResponse;
import com.gic.clerk.api.dto.ClerkDTO;
import com.gic.clerk.api.service.ClerkService;
import com.gic.enterprise.api.service.StoreService;
import com.gic.haoban.manage.api.dto.contact.ContactFollowDTO;
import com.gic.haoban.manage.api.qdto.contact.ContactFollowBatchQDTO;
import com.gic.haoban.manage.api.qdto.contact.ContactFollowQDTO;
import com.gic.haoban.manage.api.qdto.contact.ContactFollowSearchQDTO;
import com.gic.haoban.manage.api.service.contact.ContactFollowApiService;
import com.gic.haoban.manage.service.service.contact.ContactFollowService;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * Created by wangzubo on 2022/11/1.
 */
@Service("contactFollowApiService")
public class ContactFollowApiServiceImpl implements ContactFollowApiService {

    @Autowired
    private ContactFollowService contactFollowService;
    @Autowired
    private ClerkService clerkService;

    @Override
    public ServiceResponse<Void> saveFollow(ContactFollowQDTO qdto) {
        contactFollowService.saveFollow(qdto);
        return ServiceResponse.success();
    }

    @Override
    public ServiceResponse<Void> saveBatchFollow(List<ContactFollowQDTO> list) {
        contactFollowService.saveBatchFollow(list);
        return ServiceResponse.success();
    }

    @Override
    public ServiceResponse<Void> saveBatchFollowForMQ(String message) {
        if (StringUtils.isBlank(message)) {
            return ServiceResponse.success();
        }
        ContactFollowBatchQDTO qdto = JSONObject.parseObject(message, ContactFollowBatchQDTO.class);
        List<ContactFollowBatchQDTO.MemberFollow> memberList = qdto.getMemberList();
        if (CollectionUtils.isEmpty(memberList)) {
            return ServiceResponse.success();
        }
        String clerkId = qdto.getClerkId();
        if (StringUtils.isBlank(clerkId)) {
            return ServiceResponse.success();
        }
        ClerkDTO clerkDTO = clerkService.getclerkById(clerkId);
        if (clerkDTO == null) {
            return ServiceResponse.success();
        }
        List<ContactFollowQDTO> list = memberList.stream().map(x -> {
            ContactFollowQDTO followQDTO = new ContactFollowQDTO();
            followQDTO.setMemberId(x.getMemberId());
            followQDTO.setClerkId(clerkId);
            followQDTO.setClerkCode(clerkDTO.getClerkCode());
            followQDTO.setFollowRemark(clerkDTO.getClerkName() + qdto.getFollowRemark());
            followQDTO.setFollowTime(x.getFollowTime());
            followQDTO.setEnterpriseId(clerkDTO.getEnterpriseId());
            return followQDTO;
        }).collect(Collectors.toList());
        saveBatchFollow(list);
        return ServiceResponse.success();
    }

    @Override
    public ServiceResponse<Page<ContactFollowDTO>> pageFollow(String enterpriseId, ContactFollowSearchQDTO searchQDTO, BasePageInfo pageInfo) {
        Page<ContactFollowDTO> page = contactFollowService.pageFollow(enterpriseId, searchQDTO, pageInfo);
        return ServiceResponse.success(page);
    }

    @Override
    public ServiceResponse<Map<String, ContactFollowDTO>> mapMemberLastFollow(String enterpriseId, String clerkId, List<String> memberIdList) {
        Map<String, ContactFollowDTO> map = contactFollowService.mapMemberLastFollow(enterpriseId, clerkId, memberIdList);
        return ServiceResponse.success(map);
    }
}
