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

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

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

    @Autowired
    private ContactFollowApiService contactFollowApiService;

    /**
     * 添加跟进记录
     * @param contactFollowQO
     * @return
     */
    @RequestMapping(path = "/follow")
    public RestResponse<Void> saveFollow(@RequestBody ContactFollowQO contactFollowQO) {
        ContactFollowQDTO followQDTO = EntityUtil.changeEntityNew(ContactFollowQDTO.class, contactFollowQO);
        followQDTO.setFollowTime(DateUtil.date());
        ServiceResponse<Void> serviceResponse = contactFollowApiService.saveFollow(followQDTO);
        if (!serviceResponse.isSuccess()) {
           return RestResponse.failure(serviceResponse.getCode(), serviceResponse.getMessage());
        }
        return RestResponse.successResult();
    }

    /**
     * 分页查询跟进记录
     * @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);
        searchQDTO.setClerkId(clerkId);
        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());
    }
}
