package cn.newfeifan.mall.module.crm.service.contact;

import cn.hutool.core.collection.CollUtil;
import cn.newfeifan.mall.module.crm.controller.admin.contact.vo.CrmContactBusinessReqVO;
import cn.newfeifan.mall.module.crm.dal.dataobject.business.CrmBusinessDO;
import cn.newfeifan.mall.module.crm.dal.dataobject.contact.CrmContactBusinessDO;
import cn.newfeifan.mall.module.crm.dal.dataobject.contact.CrmContactDO;
import cn.newfeifan.mall.module.crm.dal.mysql.contactbusinesslink.CrmContactBusinessMapper;
import cn.newfeifan.mall.module.crm.service.business.CrmBusinessService;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

import static cn.newfeifan.mall.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.newfeifan.mall.module.crm.enums.ErrorCodeConstants.BUSINESS_NOT_EXISTS;
import static cn.newfeifan.mall.module.crm.enums.ErrorCodeConstants.CONTACT_NOT_EXISTS;

// TODO @puhui999：数据权限的校验；每个操作；
/**
 * 联系人与商机的关联 Service 实现类
 *
 * @author 非繁源码
 */
@Service
@Validated
public class CrmContactBusinessServiceImpl implements CrmContactBusinessService {

    @Resource
    private CrmContactBusinessMapper contactBusinessMapper;

    @Resource
    @Lazy // 延迟加载，为了解决延迟加载
    private CrmBusinessService businessService;
    @Resource
    @Lazy // 延迟加载，为了解决延迟加载
    private CrmContactService contactService;

    @Override
    public void createContactBusinessList(CrmContactBusinessReqVO createReqVO) {
        CrmContactDO contact = contactService.getContact(createReqVO.getContactId());
        if (contact == null) {
            throw exception(CONTACT_NOT_EXISTS);
        }
        // 遍历处理，考虑到一般数量不会太多，代码处理简单
        List<CrmContactBusinessDO> saveDOList = new ArrayList<>();
        createReqVO.getBusinessIds().forEach(businessId -> {
            CrmBusinessDO business = businessService.getBusiness(businessId);
            if (business == null) {
                throw exception(BUSINESS_NOT_EXISTS);
            }
            // 关联判重
            if (contactBusinessMapper.selectByContactIdAndBusinessId(createReqVO.getContactId(), businessId) != null) {
                return;
            }
            saveDOList.add(new CrmContactBusinessDO(null, createReqVO.getContactId(), businessId));
        });
        // 批量插入
        if (CollUtil.isNotEmpty(saveDOList)) {
            contactBusinessMapper.insertBatch(saveDOList);
        }
    }

    @Override
    public void deleteContactBusinessList(CrmContactBusinessReqVO deleteReqVO) {
        CrmContactDO contact = contactService.getContact(deleteReqVO.getContactId());
        if (contact == null) {
            throw exception(CONTACT_NOT_EXISTS);
        }
        // 直接删除
        contactBusinessMapper.deleteByContactIdAndBusinessId(
                deleteReqVO.getContactId(), deleteReqVO.getBusinessIds());
    }

    @Override
    public void deleteContactBusinessByContactId(Long contactId) {
        contactBusinessMapper.delete(CrmContactBusinessDO::getContactId,contactId);
    }

    @Override
    public List<CrmContactBusinessDO> getContactBusinessListByContactId(Long contactId) {
        return contactBusinessMapper.selectListByContactId(contactId);
    }

}