package com.gic.haoban.manage.service.service.content.impl;

import com.alibaba.fastjson.JSON;
import com.gic.api.base.commons.Page;
import com.gic.commons.util.UniqueIdUtils;
import com.gic.haoban.common.utils.PageUtil;
import com.gic.haoban.manage.service.dao.mapper.content.TabHaobanPotentialCustomerMapper;
import com.gic.haoban.manage.service.entity.content.TabHaobanPotentialCustomer;
import com.gic.haoban.manage.service.pojo.bo.NoticeMessageBO;
import com.gic.haoban.manage.service.pojo.bo.content.PotentialCustomerBO;
import com.gic.haoban.manage.service.pojo.bo.content.PotentialCustomerStaticsBO;
import com.gic.haoban.manage.service.pojo.bo.content.context.PotentialCustomerNotifyContext;
import com.gic.haoban.manage.service.pojo.qo.content.InteractRecordQO;
import com.gic.haoban.manage.service.pojo.qo.content.PotentialCustomerQO;
import com.gic.haoban.manage.service.service.content.InteractRecordService;
import com.gic.haoban.manage.service.service.content.PotentialCustomerService;
import com.gic.haoban.manage.service.service.content.adaptor.PotentialCustomerNotifyBuilder;
import com.gic.haoban.manage.service.service.notify.NoticeMessageService;
import com.github.pagehelper.PageHelper;
import com.google.common.collect.Lists;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 销售线索(TabHaobanPotentialCustomer)表服务实现类
 *
 * @author makejava
 * @since 2023-03-28 09:48:08
 */
@Slf4j
@Component
public class PotentialCustomerServiceImpl implements PotentialCustomerService {

    @Resource
    private TabHaobanPotentialCustomerMapper potentialCustomerMapper;
    @Autowired
    private PotentialCustomerNotifyBuilder potentialCustomerNotifyBuilder;
    @Autowired
    private InteractRecordService interactRecordService;
    @Autowired
    private NoticeMessageService noticeMessageService;


    /**
     * 查询销售线索
     *
     * @param potentialCustomerQO
     * @return
     */
    @Override
    public Page<PotentialCustomerBO> queryPotentialCustomerPage(PotentialCustomerQO potentialCustomerQO) {

        if (StringUtils.isBlank(potentialCustomerQO.getClerkId())) {
            log.info("参数缺失 {}", potentialCustomerQO.getClerkId());
            return new Page<>();
        }
        PageHelper.startPage(potentialCustomerQO.getPageNum(), potentialCustomerQO.getPageSize());
        com.github.pagehelper.Page<TabHaobanPotentialCustomer> pageResult =
                (com.github.pagehelper.Page<TabHaobanPotentialCustomer>) potentialCustomerMapper.queryPotentialCustomerList(potentialCustomerQO);
        return PageUtil.changePageHelperToCurrentPage(pageResult, PotentialCustomerBO.class);
    }

    /**
     * 查询详情
     *
     * @param clerkId
     * @param potentialCustomerId
     * @return
     */
    @Override
    public PotentialCustomerBO queryPotentialCustomerInfo(String clerkId, Long potentialCustomerId) {
        if (StringUtils.isBlank(clerkId)) {
            return null;
        }
        TabHaobanPotentialCustomer haobanPotentialCustomer = potentialCustomerMapper.queryById(potentialCustomerId, clerkId);
        if (haobanPotentialCustomer == null) {
            return null;
        }
        PotentialCustomerBO temp = new PotentialCustomerBO();
        BeanUtils.copyProperties(haobanPotentialCustomer, temp);
        return temp;
    }

    /**
     * 根据导购和会员id查询销售线索
     *
     * @param clerkId
     * @param memberId
     * @return
     */
    @Override
    public PotentialCustomerBO queryPotentialCustomerInfo(String clerkId, String memberId) {
        TabHaobanPotentialCustomer potentialCustomer = potentialCustomerMapper.queryWithClerkIdAndMemberId(clerkId, memberId);
        if (potentialCustomer == null) {
            return null;
        }
        PotentialCustomerBO temp = new PotentialCustomerBO();
        BeanUtils.copyProperties(potentialCustomer, temp);
        return temp;
    }

    /**
     * 标记销售线索为已读 或 星标客户
     *
     * @param potentialCustomerBO
     */
    @Override
    public void markPotentialCustomer(PotentialCustomerBO potentialCustomerBO) {
        if (potentialCustomerBO.getId() == null) {
            log.info("参数缺失");
            return;
        }
        TabHaobanPotentialCustomer temp = new TabHaobanPotentialCustomer();
        temp.setId(potentialCustomerBO.getId());
        temp.setStarFlag(potentialCustomerBO.getStarFlag());
        temp.setSeeFlag(potentialCustomerBO.getSeeFlag());
        potentialCustomerMapper.update(temp);
    }

    /**
     * 更新保存
     *
     * @param potentialCustomerBO
     * @return
     */
    @Override
    public Long saveOrUpdatePotentialCustomer(PotentialCustomerBO potentialCustomerBO) {

        TabHaobanPotentialCustomer temp = new TabHaobanPotentialCustomer();
        BeanUtils.copyProperties(potentialCustomerBO, temp);
        temp.setUpdateTime(new Date());
        if (temp.getId() == null) {
            temp.setId(UniqueIdUtils.uniqueLong());
            potentialCustomerMapper.insert(temp);
        } else {
            potentialCustomerMapper.update(temp);
        }
        return temp.getId();
    }

    /**
     * 销售线索通知
     *
     * @param context
     */
    @Override
    public void sendPotentialCustomerNotice(PotentialCustomerNotifyContext context) {

        log.info("处理企业{}下的销售线索通知", context.getEnterpriseId());

        // 根据企业id + 时间  循环查询时间段内的有新互动记录的导购id 去重
        InteractRecordQO searchQo = new InteractRecordQO();
        searchQo.setEnterpriseId(context.getEnterpriseId());
        searchQo.setStartTime(context.getStartTime());
        searchQo.setEndTime(context.getEndTime());
        List<PotentialCustomerStaticsBO> customerStaticsBos = interactRecordService.staticsClerkNewInteractRecord(searchQo);
        if (CollectionUtils.isEmpty(customerStaticsBos)) {
            log.info("企业下不存在新增线索的导购 {}", JSON.toJSONString(context));
            return;
        }
        context.setCustomerStaticsBos(customerStaticsBos);
        // 获取导购与成员关联关联关系
        potentialCustomerNotifyBuilder.buildClerkRelations(context);
        // 获取成员的主门店
        // 区经 忽略
        potentialCustomerNotifyBuilder.filterAreaManage(context);
        // 构建应用内消息
        potentialCustomerNotifyBuilder.sendHaoBanNotifyMessage(context);
        // 如果该导购id是成员在主门店时的导购id 发送应用消息
        potentialCustomerNotifyBuilder.sendApplicationMessage(context);
    }
}
