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

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.content.PotentialCustomerBO;
import com.gic.haoban.manage.service.pojo.qo.content.PotentialCustomerQO;
import com.gic.haoban.manage.service.service.content.PotentialCustomerService;
import com.github.pagehelper.PageHelper;
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.stereotype.Component;

import javax.annotation.Resource;
import java.util.Date;

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

    @Resource
    private TabHaobanPotentialCustomerMapper potentialCustomerMapper;


    /**
     * 查询销售线索
     *
     * @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();
    }
}
