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

import com.alibaba.fastjson.JSONObject;
import com.gic.api.base.commons.BusinessException;
import com.gic.api.base.commons.ServiceResponse;
import com.gic.clerk.api.dto.ClerkDTO;
import com.gic.clerk.api.service.ClerkService;
import com.gic.commons.util.EntityUtil;
import com.gic.haoban.manage.api.dto.contact.ContactLogDTO;
import com.gic.haoban.manage.api.qdto.contact.ContactLogCheckQDTO;
import com.gic.haoban.manage.api.qdto.contact.ContactLogQDTO;
import com.gic.haoban.manage.api.service.contact.ContactLogApiService;
import com.gic.haoban.manage.service.entity.contact.TabContactLog;
import com.gic.haoban.manage.service.service.contact.ContactLogService;
import com.gic.member.api.dto.MemberStoreClerkDTO;
import com.gic.member.api.service.MemberService;
import lombok.extern.slf4j.Slf4j;
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.List;
import java.util.Objects;

/**
 * Created by wangzubo on 2022/11/1.
 */
@Slf4j
@Service("contactLogApiService")
public class ContactLogApiServiceImpl implements ContactLogApiService {

    @Autowired
    private ContactLogService contactLogService;
    @Autowired
    private ClerkService clerkService;
    @Autowired
    private MemberService memberService;

    @Override
    public ServiceResponse<ContactLogDTO> getClerkContactTime(String memberId) {
        TabContactLog contactLog = contactLogService.getClerkContactTime(memberId);
        ContactLogDTO contactLogDTO = EntityUtil.changeEntityNew(ContactLogDTO.class, contactLog);
        return ServiceResponse.success(contactLogDTO);
    }

    @Override
    public ServiceResponse<List<ContactLogDTO>> getClerkContactTimeList(String memberId) {
        List<TabContactLog> list = contactLogService.getClerkContactTimeList(memberId);
        if (list == null) {
            return ServiceResponse.success(new ArrayList<>());
        }
        List<ContactLogDTO> dtos = EntityUtil.changeEntityListNew(ContactLogDTO.class, list);
        return ServiceResponse.success(dtos);
    }

    @Override
    public ServiceResponse<Void> saveContactLog(ContactLogQDTO qdto) {
        try {
            contactLogService.saveContactLog(qdto);
        } catch (BusinessException e) {
            return ServiceResponse.failure(e.getCode(), e.getMessage());
        }
        return ServiceResponse.success();
    }

    @Override
    public ServiceResponse<Void> autoContactLogForMQ(String message) {
        if (StringUtils.isBlank(message)) {
            return ServiceResponse.success();
        }
        ContactLogQDTO contactLogQDTO = JSONObject.parseObject(message, ContactLogQDTO.class);
        Integer contactAutoType = contactLogQDTO.getContactAutoType();
        if (contactAutoType != null) {
            contactLogService.autoContactLog(contactLogQDTO);
        }else {
            contactLogService.syncContactLog(contactLogQDTO);
        }
        return ServiceResponse.success();
    }

    @Override
    public ServiceResponse<Boolean> contactCheck(ContactLogCheckQDTO checkQDTO) {
        ClerkDTO clerkDTO = clerkService.getclerkById(checkQDTO.getClerkId());
        if (clerkDTO == null) {
            return ServiceResponse.success(Boolean.FALSE);
        }
        MemberStoreClerkDTO memberStoreClerk = memberService.getMemberStoreClerk(checkQDTO.getMemberId());
        if (memberStoreClerk == null) {
            log.info("无专属导购无法建联");
            return ServiceResponse.success(Boolean.FALSE);
        }
        String mainClerkId = memberStoreClerk.getClerkId();
        if (!Objects.equals(mainClerkId, checkQDTO.getClerkId())) {
            log.info("非专属导购无法建联");
            return ServiceResponse.success(Boolean.FALSE);
        }
        Integer contactCheck = contactLogService.contactCheck(checkQDTO.getEnterpriseId(), checkQDTO.getMemberId(), clerkDTO, checkQDTO.getBizType());
        return ServiceResponse.success(contactCheck != null);
    }
}
