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 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;

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

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

    @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);
        }
        boolean check = contactLogService.contactCheck(checkQDTO.getEnterpriseId(), checkQDTO.getMemberId(), clerkDTO,checkQDTO.getBizType());
        return ServiceResponse.success(check);
    }
}
