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

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.gic.api.base.commons.BasePageInfo;
import com.gic.api.base.commons.Page;
import com.gic.api.base.commons.ServiceResponse;
import com.gic.commons.util.EntityUtil;
import com.gic.commons.webapi.reponse.RestResponse;
import com.gic.haoban.manage.api.dto.contact.ContactConfigDTO;
import com.gic.haoban.manage.api.dto.contact.ContactFollowDTO;
import com.gic.haoban.manage.api.qdto.contact.ContactFollowQDTO;
import com.gic.haoban.manage.api.qdto.contact.ContactFollowSearchQDTO;
import com.gic.haoban.manage.api.qdto.contact.ContactLogCheckQDTO;
import com.gic.haoban.manage.api.qdto.contact.ContactLogQDTO;
import com.gic.haoban.manage.api.service.contact.ContactConfigApiService;
import com.gic.haoban.manage.api.service.contact.ContactFollowApiService;
import com.gic.haoban.manage.api.service.contact.ContactLogApiService;
import com.gic.haoban.manage.web.qo.contact.*;
import com.gic.haoban.manage.web.vo.contact.ContactLogCheckVO;
import com.google.common.collect.Lists;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
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;

/**
 * 潜客雷达-跟进记录
 */
@RestController
@RequestMapping(path = "/contact")
public class ContactController {

    @Autowired
    private ContactFollowApiService contactFollowApiService;
    @Autowired
    private ContactLogApiService contactLogApiService;
    @Autowired
    private ContactConfigApiService contactConfigApiService;

    /**
     * 添加跟进记录
     * @param contactFollowQO
     * @return
     */
    @RequestMapping(path = "/follow")
    public RestResponse<Void> saveFollow(@RequestBody ContactFollowQO contactFollowQO) {
        List<String> memberIdList = contactFollowQO.getMemberIdList();
        if (CollectionUtils.isEmpty(memberIdList)) {
            return RestResponse.successResult();
        }
        DateTime followTime = DateUtil.date();
        List<ContactFollowQDTO> list = memberIdList.stream().map(x -> {
            ContactFollowQDTO followQDTO = EntityUtil.changeEntityNew(ContactFollowQDTO.class, contactFollowQO);
            followQDTO.setMemberId(x);
            followQDTO.setFollowTime(followTime);
            return followQDTO;
        }).collect(Collectors.toList());
        ServiceResponse<Void> serviceResponse = contactFollowApiService.saveBatchFollow(list);
        if (!serviceResponse.isSuccess()) {
           return RestResponse.failure(serviceResponse.getCode(), serviceResponse.getMessage());
        }
        return RestResponse.successResult();
    }

    /**
     * 会员最后一次跟进记录
     * @param contactLastFollowQO
     * @return
     */
    @RequestMapping(path = "/last-follow")
    public RestResponse<ContactFollowDTO> lastFollow(@RequestBody ContactLastFollowQO contactLastFollowQO) {
        String memberId = contactLastFollowQO.getMemberId();
        String enterpriseId = contactLastFollowQO.getEnterpriseId();
        if (StringUtils.isAnyBlank(memberId,enterpriseId)) {
            return RestResponse.successResult();
        }
        ServiceResponse<Map<String, ContactFollowDTO>> serviceResponse = contactFollowApiService.mapMemberLastFollow(enterpriseId,null, Lists.newArrayList(memberId));
        if (!serviceResponse.isSuccess()) {
            return RestResponse.failure(serviceResponse.getCode(), serviceResponse.getMessage());
        }
        if (serviceResponse.getResult() == null) {
            return RestResponse.successResult();
        }
        ContactFollowDTO contactFollowDTO = serviceResponse.getResult().get(memberId);
        return RestResponse.successResult(contactFollowDTO);
    }

    /**
     * 分页查询跟进记录
     * @param contactFollowPageQO
     * @return
     */
    @RequestMapping(path = "/page-follow")
    public RestResponse<Page<ContactFollowDTO>> pageFollow(@RequestBody ContactFollowPageQO contactFollowPageQO) {
        String enterpriseId = contactFollowPageQO.getEnterpriseId();
        String clerkId = contactFollowPageQO.getClerkId();
        String memberId = contactFollowPageQO.getMemberId();
        ContactFollowSearchQDTO searchQDTO = new ContactFollowSearchQDTO();
        searchQDTO.setMemberId(memberId);
        BasePageInfo basePageInfo = contactFollowPageQO.getBasePageInfo();
        ServiceResponse<Page<ContactFollowDTO>> serviceResponse = contactFollowApiService.pageFollow(enterpriseId,searchQDTO,basePageInfo);
        if (!serviceResponse.isSuccess()) {
            return RestResponse.failure(serviceResponse.getCode(), serviceResponse.getMessage());
        }
        return RestResponse.successResult(serviceResponse.getResult());
    }

    /**
     * 建联
     * @param contactLogQO
     * @return
     */
    @RequestMapping(path = "/log")
    public RestResponse<Void> saveLog(@RequestBody ContactLogQO contactLogQO) {
        ContactLogQDTO logQDTO = EntityUtil.changeEntityNew(ContactLogQDTO.class, contactLogQO);
        logQDTO.setSourceType(contactLogQO.getBizType());
        ServiceResponse<Void> serviceResponse = contactLogApiService.saveContactLog(logQDTO);
        if (!serviceResponse.isSuccess()) {
            return RestResponse.failure(serviceResponse.getCode(), serviceResponse.getMessage());
        }
        return RestResponse.successResult();
    }

    /**
     * 是否可以建联校验
     * @param contactLogCheckQO
     * @return
     */
    @RequestMapping(path = "/check")
    public RestResponse<ContactLogCheckVO> check(@RequestBody ContactLogCheckQO contactLogCheckQO) {
        ContactLogCheckQDTO logCheckQDTO = EntityUtil.changeEntityNew(ContactLogCheckQDTO.class, contactLogCheckQO);
        ServiceResponse<Boolean> serviceResponse = contactLogApiService.contactCheck(logCheckQDTO);
        if (!serviceResponse.isSuccess()) {
            return RestResponse.failure(serviceResponse.getCode(), serviceResponse.getMessage());
        }
        return RestResponse.successResult(new ContactLogCheckVO(serviceResponse.getResult()));
    }

    /**
     * 建联配置
     * @param enterpriseId
     * @return
     */
    @RequestMapping(path = "/config")
    public RestResponse<ContactConfigDTO> config(String enterpriseId) {
        ServiceResponse<ContactConfigDTO> serviceResponse = contactConfigApiService.getContactConfig(enterpriseId);
        if (!serviceResponse.isSuccess()) {
            return RestResponse.failure(serviceResponse.getCode(), serviceResponse.getMessage());
        }
        return RestResponse.successResult(serviceResponse.getResult());
    }

}
