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.DateUtil;
import com.gic.commons.util.UniqueIdUtils;
import com.gic.haoban.manage.service.dao.mapper.content.TabHaobanInteractRecordMapper;
import com.gic.haoban.manage.service.entity.content.TabHaobanInteractRecord;
import com.gic.haoban.manage.service.pojo.bo.content.InteractRecordBO;
import com.gic.haoban.manage.service.pojo.qo.content.InteractRecordQO;
import com.gic.haoban.manage.service.service.content.InteractRecordService;
import com.gic.haoban.manage.service.service.content.adaptor.InteractRecordBuilder;
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.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

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

/**
 * 互动记录明细(TabHaobanInteractRecord)表服务实现类
 *
 * @author makejava
 * @since 2023-03-28 10:32:48
 */
@Slf4j
@Component
public class InteractRecordServiceImpl implements InteractRecordService {

    @Resource
    private TabHaobanInteractRecordMapper tabHaobanInteractRecordMapper;
    @Autowired
    private InteractRecordBuilder interactRecordBuilder;

    /**
     * 查询互动记录
     *
     * @param enterpriseId
     * @param traceId
     * @return
     */
    @Override
    public InteractRecordBO queryWithInteractInfo(String enterpriseId, String traceId) {

        if (StringUtils.isAnyBlank(enterpriseId, traceId)) {
            return null;
        }

        TabHaobanInteractRecord interactRecord = tabHaobanInteractRecordMapper.queryWithEnterpriseIdAndTraceId(enterpriseId, traceId);
        if (interactRecord == null) {
            return null;
        }
        InteractRecordBO temp = new InteractRecordBO();
        BeanUtils.copyProperties(interactRecord, temp);
        return temp;
    }

    /**
     * 互动记录查询
     *
     * @param interactRecordQO
     * @return
     */
    @Override
    public Page<InteractRecordBO> queryInteractRecordPage(InteractRecordQO interactRecordQO) {

        // 默认查最近一年内的的数据
        if (StringUtils.isBlank(interactRecordQO.getClerkId())) {
            log.info("queryInteractRecordPage 参数缺失");
            return new Page<>();
        }
        interactRecordQO.setStartTime(DateUtil.addNumForYear(new Date(), -1));
        PageHelper.startPage(interactRecordQO.getPageNum(), interactRecordQO.getPageSize());
        com.github.pagehelper.Page<TabHaobanInteractRecord> pageResult =
                (com.github.pagehelper.Page<TabHaobanInteractRecord>) tabHaobanInteractRecordMapper.queryInteractRecordList(interactRecordQO);
        if (pageResult == null || CollectionUtils.isEmpty(pageResult)) {
            log.info("查询互动记录数据为空 {}", JSON.toJSONString(interactRecordQO));
            return new Page<>();
        }
        Page<InteractRecordBO> result = new Page<>();
        result.setTotalCount((int) pageResult.getTotal());
        List<InteractRecordBO> recordBos = pageResult.getResult()
                .stream()
                .map(item -> {
                    InteractRecordBO temp = new InteractRecordBO();
                    BeanUtils.copyProperties(item, temp);
                    temp.setRecordDesc(this.interactRecordBuilder.buildDesc(item));
                    temp.setExtendInfo(interactRecordBuilder.convertExtendInfo(item));
                    return temp;
                })
                .collect(Collectors.toList());
        result.setResult(recordBos);
        return result;
    }

    /**
     * 统计总数
     *
     * @param interactRecordQO
     * @return
     */
    @Override
    public Integer countInteractRecord(InteractRecordQO interactRecordQO) {
        interactRecordQO.setPageNum(1);
        interactRecordQO.setPageSize(1);
        PageHelper.startPage(interactRecordQO.getPageNum(), interactRecordQO.getPageSize());
        com.github.pagehelper.Page<TabHaobanInteractRecord> pageResult =
                (com.github.pagehelper.Page<TabHaobanInteractRecord>) tabHaobanInteractRecordMapper.queryInteractRecordList(interactRecordQO);
        if (pageResult == null || CollectionUtils.isEmpty(pageResult)) {
            log.info("查询互动记录数据为空 {}", JSON.toJSONString(interactRecordQO));
            return 0;
        }
        return (int)pageResult.getTotal();
    }

    /**
     * 保存互动记录
     *
     * @param interactRecordBO
     * @return
     */
    @Override
    public Long saveOrUpdateInteractRecord(InteractRecordBO interactRecordBO) {
        TabHaobanInteractRecord temp = new TabHaobanInteractRecord();
        BeanUtils.copyProperties(interactRecordBO, temp);
        if (interactRecordBO.getExtendInfo() != null) {
            temp.setExtendInfo(JSON.toJSONString(interactRecordBO.getExtendInfo()));
        }
        if (interactRecordBO.getId() == null) {
            temp.setId(UniqueIdUtils.uniqueLong());
            tabHaobanInteractRecordMapper.insert(temp);
        }else {
            tabHaobanInteractRecordMapper.update(temp);
        }
        return temp.getId();
    }
}
