package com.gic.haoban.manage.web.controller.content;

import com.gic.api.base.commons.Page;
import com.gic.api.base.commons.ServiceResponse;
import com.gic.commons.webapi.reponse.RestResponse;
import com.gic.content.api.dto.material.ContentMaterialBaseDTO;
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.ShareBizType;
import com.gic.haoban.manage.api.service.content.PotentialCustomerApiService;
import com.gic.haoban.manage.web.controller.content.adaptor.ContentMaterialAdaptor;
import com.gic.haoban.manage.web.qo.content.potential.PotentialCustomerMarkRecordVO;
import com.gic.haoban.manage.web.qo.content.potential.PotentialCustomerQO;
import com.gic.haoban.manage.web.vo.content.ContentMaterialInfoVO;
import com.gic.haoban.manage.web.vo.content.potential.PotentialCustomerVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

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

/**
 * 好办小程序-销售线索
 **/
@Slf4j
@RestController
@RequestMapping(path = "/potential/customer")
public class PotentialCustomerController {

    @Autowired
    private PotentialCustomerApiService potentialCustomerApiService;
    @Autowired
    ContentMaterialAdaptor contentMaterialAdaptor;
    /**
     * 销售线索列表
     *
     * @param potentialCustomerQO
     * @return
     */
    @RequestMapping(path = "/list")
    public RestResponse<Page<PotentialCustomerVO>> queryPotentialCustomerList(@RequestBody PotentialCustomerQO potentialCustomerQO) {

        PotentialCustomerQDTO searchQDTO = new PotentialCustomerQDTO();
        BeanUtils.copyProperties(potentialCustomerQO, searchQDTO);
        ServiceResponse<Page<PotentialCustomerDTO>> serviceResponse =
                potentialCustomerApiService.queryPotentialCustomer(searchQDTO);
        if (!serviceResponse.isSuccess()) {
            return RestResponse.failure(serviceResponse.getCode(), serviceResponse.getMessage());
        }
        List<PotentialCustomerDTO> customerDTOS = serviceResponse.getResult().getResult();
        List<Long> materialIds = customerDTOS
                .stream()
                .filter(item -> ShareBizType.MATERIAL.getCode().equals(item.getBizType()))
                .map(item -> Long.parseLong(item.getBizId()))
                .collect(Collectors.toList());
        Map<Long, ContentMaterialBaseDTO> materialInfoMap = contentMaterialAdaptor.queryMaterialInfoMap(potentialCustomerQO.getEnterpriseId(), materialIds);

        List<PotentialCustomerVO> customerVOS = customerDTOS
                .stream()
                .map(item -> {
                    PotentialCustomerVO temp = new PotentialCustomerVO();
                    BeanUtils.copyProperties(item, temp);
                    temp.setPotentialCustomerId(item.getPotentialCustomerId());
                    ContentMaterialBaseDTO contentMaterialBaseDTO = materialInfoMap.get(Long.parseLong(item.getBizId()));
                    if (contentMaterialBaseDTO != null) {
                        ContentMaterialInfoVO contentMaterialInfoVO = new ContentMaterialInfoVO();
                        BeanUtils.copyProperties(contentMaterialBaseDTO, contentMaterialInfoVO);
                        temp.setContentMaterialInfoVO(contentMaterialInfoVO);
                    }
                    return temp;
                })
                .collect(Collectors.toList());
        Page<PotentialCustomerVO> result = new Page<>();
        result.setTotalCount(serviceResponse.getResult().getTotalCount());
        result.setResult(customerVOS);
        return RestResponse.successResult(result);
    }



    /**
     * 标记销售线索为已看
     *
     * @return
     */
    @RequestMapping(path = "/mark/record/see")
    public RestResponse<?> markRecordSee(@RequestBody PotentialCustomerMarkRecordVO potentialCustomerMarkRecordVO) {

        PotentialCustomerMarkRecordDTO markRecord = new PotentialCustomerMarkRecordDTO();
        BeanUtils.copyProperties(potentialCustomerMarkRecordVO, markRecord);
        ServiceResponse<Long> serviceResponse = potentialCustomerApiService.markPotentialCustomer(markRecord);
        if (!serviceResponse.isSuccess()) {
            return RestResponse.failure(serviceResponse.getCode(), serviceResponse.getMessage());
        }
        return RestResponse.successResult();
    }

    /**
     * 标记客户为星标客户
     *
     * @return
     */
    @RequestMapping(path = "/mark/member/star")
    public RestResponse<?> markMemberStar(@RequestBody PotentialCustomerMarkRecordVO potentialCustomerMarkRecordVO) {
        PotentialCustomerMarkRecordDTO markRecord = new PotentialCustomerMarkRecordDTO();
        BeanUtils.copyProperties(potentialCustomerMarkRecordVO, markRecord);
        ServiceResponse<Long> serviceResponse = potentialCustomerApiService.markPotentialCustomer(markRecord);
        if (!serviceResponse.isSuccess()) {
            return RestResponse.failure(serviceResponse.getCode(), serviceResponse.getMessage());
        }
        return RestResponse.successResult();
    }
}
