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

import com.alibaba.fastjson.JSON;
import com.gic.commons.util.UniqueIdUtils;
import com.gic.haoban.manage.service.dao.mapper.content.TabHaobanClerkShareLogMapper;
import com.gic.haoban.manage.service.entity.content.TabHaobanClerkShareLog;
import com.gic.haoban.manage.service.pojo.bo.content.ClerkShareLogBO;
import com.gic.haoban.manage.service.pojo.qo.content.ClerkShareLogQO;
import com.gic.haoban.manage.service.service.content.ClerkShareLogService;
import jdk.nashorn.internal.runtime.regexp.joni.ast.StringNode;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author MUSI
 * @Date 2023/3/18 9:03 PM
 * @Description
 * @Version
 **/

@Component
public class ClerkShareLogServiceImpl implements ClerkShareLogService {

    private static final Logger log = LoggerFactory.getLogger(ClerkShareLogServiceImpl.class);

    @Autowired
    private TabHaobanClerkShareLogMapper clerkShareLogMapper;

    @Override
    public Long saveClerkShareLog(ClerkShareLogBO clerkShareLogBO) {
        TabHaobanClerkShareLog temp = new TabHaobanClerkShareLog();
        BeanUtils.copyProperties(clerkShareLogBO, temp);
        temp.setId(UniqueIdUtils.uniqueLong());
        temp.setCreateTime(new Date());
        temp.setUpdateTime(new Date());
        temp.setDeleteFlag(0);
        clerkShareLogMapper.insert(temp);
        return temp.getId();
    }

    /**
     * 查询导购分享日志
     *
     * @param clerkShareLogQO
     * @return
     */
    @Override
    public List<ClerkShareLogBO> queryClerkShareLog(ClerkShareLogQO clerkShareLogQO) {

        List<TabHaobanClerkShareLog> tabHaobanClerkShareLogs = clerkShareLogMapper.queryWithCondition(clerkShareLogQO);
        if (CollectionUtils.isEmpty(tabHaobanClerkShareLogs)) {
            log.info("查询导购分享记录为空 {}", JSON.toJSONString(clerkShareLogQO));
            return Collections.emptyList();
        }
        return tabHaobanClerkShareLogs
                .stream()
                .map(item -> {
                    ClerkShareLogBO temp = new ClerkShareLogBO();
                    BeanUtils.copyProperties(item, temp);
                    return temp;
                })
                .collect(Collectors.toList());
    }

    /**
     * 标记分享日志已处理
     *
     * @param enterpriseId
     * @param ids
     */
    @Override
    public void markClerkShareDone(String enterpriseId, List<Long> ids) {
        if (StringUtils.isBlank(enterpriseId) || CollectionUtils.isEmpty(ids)) {
            return;
        }
        clerkShareLogMapper.markShareLogWithDone(enterpriseId, ids);
    }
}
