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.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.ContactLogQDTO;
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.ContactFollowPageQO;
import com.gic.haoban.manage.web.qo.contact.ContactFollowQO;
import com.gic.haoban.manage.web.qo.contact.ContactLastFollowQO;
import com.gic.haoban.manage.web.qo.contact.ContactLogQO;
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;

    /**
     * 添加跟进记录
     * @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);
        ServiceResponse<Void> serviceResponse = contactLogApiService.saveContactLog(logQDTO);
        if (!serviceResponse.isSuccess()) {
            return RestResponse.failure(serviceResponse.getCode(), serviceResponse.getMessage());
        }
        return RestResponse.successResult();
    }

}
