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

import com.gic.api.base.commons.Page;
import com.gic.api.base.commons.ServiceResponse;
import com.gic.commons.util.EntityUtil;
import com.gic.haoban.manage.api.dto.content.PotentialCustomerDTO;
import com.gic.haoban.manage.api.dto.qdto.content.PotentialCustomerMarkRecordDTO;
import com.gic.haoban.manage.api.dto.qdto.content.PotentialCustomerQDTO;
import com.gic.haoban.manage.api.enums.content.InteractRecordExtendInfoDTO;
import com.gic.haoban.manage.api.enums.content.ShareBizType;
import com.gic.haoban.manage.api.service.content.PotentialCustomerApiService;
import com.gic.haoban.manage.service.pojo.bo.content.InteractRecordExtendInfoBO;
import com.gic.haoban.manage.service.pojo.bo.content.PotentialCustomerBO;
import com.gic.haoban.manage.service.pojo.qo.content.PotentialCustomerQO;
import com.gic.haoban.manage.service.service.content.InteractRecordService;
import com.gic.haoban.manage.service.service.content.PotentialCustomerService;
import com.gic.haoban.manage.service.service.content.adaptor.InteractRecordBuilder;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @Author MUSI
 * @Date 2023/3/29 10:00 AM
 * @Description
 * @Version
 **/
@Component("potentialCustomerApiService")
public class PotentialCustomerApiServiceImpl implements PotentialCustomerApiService {

    @Autowired
    PotentialCustomerService potentialCustomerService;
    @Autowired
    InteractRecordService interactRecordService;
    @Autowired
    InteractRecordBuilder interactRecordBuilder;

    /**
     * 查询销售线索记录
     *
     * @param potentialCustomerQDTO
     * @return
     */
    @Override
    public ServiceResponse<Page<PotentialCustomerDTO>> queryPotentialCustomer(PotentialCustomerQDTO potentialCustomerQDTO) {
        PotentialCustomerQO searchQO = new PotentialCustomerQO();
        BeanUtils.copyProperties(potentialCustomerQDTO, searchQO);
        Page<PotentialCustomerBO> customerPage = potentialCustomerService.queryPotentialCustomerPage(searchQO);
        if (CollectionUtils.isEmpty(customerPage.getResult())) {
            return ServiceResponse.success(new Page<>());
        }
        List<String> wxaLinkIds = customerPage.getResult().stream()
                .filter(item -> Objects.equals(item.getBizType(), ShareBizType.MULTIPLE_GOODS.getCode()))
                .map(PotentialCustomerBO::getBizId).collect(Collectors.toList());
        Map<String, InteractRecordExtendInfoBO> extendInfoBOMap = interactRecordBuilder.buildGroupGoodsByWxaLinkId(potentialCustomerQDTO.getEnterpriseId(), wxaLinkIds);
        Page<PotentialCustomerDTO> resultPage = new Page<>();
        resultPage.setTotalCount(customerPage.getTotalCount());
        resultPage.setCurrentPage(potentialCustomerQDTO.getPageNum());
        resultPage.setPageSize(potentialCustomerQDTO.getPageSize());
        List<PotentialCustomerDTO> customerDTOS = customerPage.getResult()
                .stream()
                .map(item -> {
                    PotentialCustomerDTO temp = new PotentialCustomerDTO();
                    BeanUtils.copyProperties(item, temp);
                    temp.setPotentialCustomerId(item.getId());
                    temp.setDesc(interactRecordBuilder.buildDesc(item.getChannelSource(), item.getShareSource(),item.getEventType(), item.getBizType()));
                    InteractRecordExtendInfoBO extendInfoBO = extendInfoBOMap.get(item.getBizId());
                    if (extendInfoBO != null) {
                        temp.setExtendInfo(EntityUtil.changeEntityByJSON(InteractRecordExtendInfoDTO.class, extendInfoBO));
                    }
                    return temp;
                })
                .collect(Collectors.toList());
        resultPage.setResult(customerDTOS);
        return ServiceResponse.success(resultPage);
    }

    /**
     * 标记销售线索 已看、星标
     *
     * @param potentialCustomerMarkRecordDTO
     * @return
     */
    @Override
    public ServiceResponse<Long> markPotentialCustomer(PotentialCustomerMarkRecordDTO potentialCustomerMarkRecordDTO) {

        PotentialCustomerBO potentialCustomerBO = potentialCustomerService.queryPotentialCustomerInfo(potentialCustomerMarkRecordDTO.getClerkId(), potentialCustomerMarkRecordDTO.getPotentialCustomerId());
        if (potentialCustomerBO == null) {
            return ServiceResponse.failure("-701", "数据不存在");
        }
        potentialCustomerBO.setSeeFlag(potentialCustomerMarkRecordDTO.getSeeFlag());
        potentialCustomerBO.setStarFlag(potentialCustomerMarkRecordDTO.getStarFlag());
        potentialCustomerService.markPotentialCustomer(potentialCustomerBO);
        return ServiceResponse.success(potentialCustomerBO.getId());
    }

    @Override
    public ServiceResponse<Void> modifyUnionId(String enterpriseId, String oldUnionId, String newUnionId) {
        interactRecordService.modifyUnionId(enterpriseId,oldUnionId,newUnionId);
        potentialCustomerService.modifyUnionId(enterpriseId,oldUnionId,newUnionId);
        return ServiceResponse.success();
    }
}
