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.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.service.content.PotentialCustomerApiService;
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.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.stream.Collectors;

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

    @Autowired
    PotentialCustomerService potentialCustomerService;
    @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<>());
        }
        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.getEventType(), item.getBizType()));
                    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());
    }
}
