package com.crm.manager.service.impl;

import com.alibaba.fastjson.JSON;
import com.crm.manager.dao.mapper.BusinessEmailReceiveRecordMapper;
import com.crm.manager.dao.repository.BusinessEmailReceiveRecordRepository;
import com.crm.manager.service.BusinessEmailReceiveRecordService;
import com.crm.manager.service.CustomInfoService;
import com.crm.manager.service.UserInfoService;
import com.crm.rely.backend.core.constant.Constants;
import com.crm.rely.backend.core.dto.base.PageDto;
import com.crm.rely.backend.core.dto.base.ResultWithPagerDto;
import com.crm.rely.backend.core.dto.business.email.receive.BusinessEmailReceiveRecordDto;
import com.crm.rely.backend.core.entity.tenant.email.receive.BusinessEmailReceiveRecordAddEntity;
import com.crm.rely.backend.core.entity.tenant.email.receive.BusinessEmailReceiveRecordSearchEntity;
import com.crm.rely.backend.core.pojo.table.BusinessEmailReceiveRecordTable;
import com.crm.rely.backend.core.pojo.table.UserInfoTable;
import com.crm.rely.backend.exception.ServiceException;
import lombok.extern.log4j.Log4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.annotation.JmsListener;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Log4j
@Service
public class BusinessEmailReceiveRecordServiceImpl implements BusinessEmailReceiveRecordService {

    private final static String BUSINESS_EMAIL_RECEIVE_RECORD = "BUSINESS_EMAIL.RECEIVE_RECORD";
    @Autowired
    private BusinessEmailReceiveRecordRepository repository;

    @Autowired
    private BusinessEmailReceiveRecordMapper mapper;

    @Autowired
    private CustomInfoService customInfoService;

    @Autowired
    private UserInfoService userInfoService;

    @Override
    @Transactional(rollbackFor = {Exception.class})
    public void add(BusinessEmailReceiveRecordAddEntity entity) throws ServiceException {


        BusinessEmailReceiveRecordTable table = new BusinessEmailReceiveRecordTable();
        BeanUtils.copyProperties(entity, table);

        Long salesId = customInfoService.getSalesIdByEmail(entity.getSentAccount());

        if (salesId == null) {
            return;
        }
        UserInfoTable agent = userInfoService.getDataById(salesId);
        if (agent != null) {

            table.setAgentIbNo(agent.getIbNo());
            table.setAgentName(agent.getName());
        }

        repository.save(table);

    }

    @JmsListener(destination = BUSINESS_EMAIL_RECEIVE_RECORD)
    @Transactional(rollbackFor = {Exception.class})
    public void callbackSentEmail(String content) throws ServiceException {
        BusinessEmailReceiveRecordAddEntity callbackEntity = JSON.parseObject(content,
                BusinessEmailReceiveRecordAddEntity.class);
        if (callbackEntity == null) {
            log.error("BUSINESS_EMAIL_RECEIVE_RECORD callback error:" + content);
        }
        add(callbackEntity);
    }

    @Override
    public ResultWithPagerDto<BusinessEmailReceiveRecordDto> searchList(BusinessEmailReceiveRecordSearchEntity entity) throws ServiceException {

        int count = selectCount(entity);
        PageDto pageDto = PageDto.format(entity, count);
        if (count <= 0) {
            return ResultWithPagerDto.success(pageDto);
        }
        List<BusinessEmailReceiveRecordDto> dtos = getDto(entity);
        if (dtos == null) {
            throw ServiceException.exception(Constants.SYSTEM_ERROR);
        }
        return ResultWithPagerDto.success(pageDto, dtos);
    }


    private List<BusinessEmailReceiveRecordDto> getDto(BusinessEmailReceiveRecordSearchEntity search) {
        List<BusinessEmailReceiveRecordTable> tables = mapper.selectList(search);
        if (tables == null || tables.size() <= 0) {
            return null;
        }
        List<BusinessEmailReceiveRecordDto> dtos = new ArrayList<>(tables.size());
        for (BusinessEmailReceiveRecordTable table : tables) {
            BusinessEmailReceiveRecordDto dto = transform(table);
            dtos.add(dto);
        }
        return dtos;
    }

    @Override
    public int selectCount(BusinessEmailReceiveRecordSearchEntity searchCount) {
        return mapper.selectCount(searchCount);
    }

    @Override
    public BusinessEmailReceiveRecordDto searchSingle(Long id) throws ServiceException {
        BusinessEmailReceiveRecordTable table = repository.getFirstById(id);
        if (table == null) {
            throw ServiceException.exception(Constants.INFO_NOT_FOUND);
        }
        return transform(table);
    }

    private BusinessEmailReceiveRecordDto transform(BusinessEmailReceiveRecordTable table) {
        BusinessEmailReceiveRecordDto dto = new BusinessEmailReceiveRecordDto();
        BeanUtils.copyProperties(table, dto);
        return dto;
    }
}
