package com.ruixuan.data.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.ruixuan.common.utils.DateUtils;
import com.ruixuan.data.entity.KuaishouLiveDataFunnel;
import com.ruixuan.data.mapper.KuaishouLiveDataFunnelMapper;
import com.ruixuan.data.service.IKuaishouLiveDataFunnelService;
import com.ruixuan.live.entity.KuaishouLiveBroadcast;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 直播漏斗数据Service业务层处理
 *
 * @author ruoyi
 * @date 2022-05-31
 */
@Service
public class KuaishouLiveDataFunnelServiceImpl implements IKuaishouLiveDataFunnelService {
    @Autowired
    private KuaishouLiveDataFunnelMapper kuaishouLiveDataFunnelMapper;

    /**
     * 查询直播漏斗数据
     *
     * @param ksId 直播漏斗数据主键
     * @return 直播漏斗数据
     */
    @Override
    public KuaishouLiveDataFunnel selectKuaishouLiveDataFunnelByKsId(Long ksId) {
        return kuaishouLiveDataFunnelMapper.selectKuaishouLiveDataFunnelByKsId(ksId);
    }

    /**
     * 查询直播漏斗数据列表
     *
     * @param kuaishouLiveDataFunnel 直播漏斗数据
     * @return 直播漏斗数据
     */
    @Override
    public List<KuaishouLiveDataFunnel> selectKuaishouLiveDataFunnelList(KuaishouLiveDataFunnel kuaishouLiveDataFunnel) {
        return kuaishouLiveDataFunnelMapper.selectKuaishouLiveDataFunnelList(kuaishouLiveDataFunnel);
    }

    /**
     * 新增直播漏斗数据
     *
     * @param kuaishouLiveDataFunnel 直播漏斗数据
     * @return 结果
     */
    @Override
    public int insertKuaishouLiveDataFunnel(KuaishouLiveDataFunnel kuaishouLiveDataFunnel) {
        kuaishouLiveDataFunnel.setCreateTime(DateUtils.getNowDate());
        return kuaishouLiveDataFunnelMapper.insertKuaishouLiveDataFunnel(kuaishouLiveDataFunnel);
    }

    /**
     * 修改直播漏斗数据
     *
     * @param kuaishouLiveDataFunnel 直播漏斗数据
     * @return 结果
     */
    @Override
    public int updateKuaishouLiveDataFunnel(KuaishouLiveDataFunnel kuaishouLiveDataFunnel) {
        kuaishouLiveDataFunnel.setUpdateTime(DateUtils.getNowDate());
        return kuaishouLiveDataFunnelMapper.updateKuaishouLiveDataFunnel(kuaishouLiveDataFunnel);
    }

    /**
     * 批量删除直播漏斗数据
     *
     * @param ksIds 需要删除的直播漏斗数据主键
     * @return 结果
     */
    @Override
    public int deleteKuaishouLiveDataFunnelByKsIds(Long[] ksIds) {
        return kuaishouLiveDataFunnelMapper.deleteKuaishouLiveDataFunnelByKsIds(ksIds);
    }

    /**
     * 删除直播漏斗数据信息
     *
     * @param ksId 直播漏斗数据主键
     * @return 结果
     */
    @Override
    public int deleteKuaishouLiveDataFunnelByKsId(Long ksId) {
        return kuaishouLiveDataFunnelMapper.deleteKuaishouLiveDataFunnelByKsId(ksId);
    }

    @Override
    public int inserExcelInfo(KuaishouLiveBroadcast broadcast, List<JSONObject> list) throws Exception {
         /* excel表头：日期	累计观看人数
           对应字段编码：0,1         */
        try {
            List<KuaishouLiveDataFunnel> funnelList = new ArrayList<>();
            for (JSONObject cell : list) {
                if (cell.size() == 2) {
                    KuaishouLiveDataFunnel funnel = new KuaishouLiveDataFunnel();
                    funnel.setKsId(broadcast.getKsId());
                    funnel.setKsName(broadcast.getKsName());
                    funnel.setAccountId(broadcast.getAccountId());
                    funnel.setAccountName(broadcast.getAccountName());
                    funnel.setStatDate(cell.getString("0"));
                    funnel.setAllViewers(cell.getLong("1"));
                    funnelList.add(funnel);
                } else {
                    throw new Exception("直播漏斗数据列不对");
                }
            }
            int i = kuaishouLiveDataFunnelMapper.inserExcelBatchInfo(funnelList);
            if (i > 0) {
                return 1;
            }
        } catch (Exception e) {
            e.printStackTrace();
            throw new Exception("直播漏斗数据列异常");
        }
        return 0;
    }
}
