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

    /**
     * 查询销售线索记录
     *
     * @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());
        List<PotentialCustomerDTO> customerDTOS = customerPage.getResult()
                .stream()
                .map(item -> {
                    PotentialCustomerDTO temp = new PotentialCustomerDTO();
                    BeanUtils.copyProperties(item, temp);
                    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", "数据不存在");
        }
        potentialCustomerService.markPotentialCustomer(potentialCustomerBO);
        return ServiceResponse.success(potentialCustomerBO.getId());
    }
}
