package com.ruixuan.data.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.ruixuan.common.utils.DateUtils;
import com.ruixuan.data.entity.KuaishouLiveDataOverview;
import com.ruixuan.data.mapper.KuaishouLiveDataOverviewMapper;
import com.ruixuan.data.service.IKuaishouLiveDataOverviewService;
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 KuaishouLiveDataOverviewServiceImpl implements IKuaishouLiveDataOverviewService {
    @Autowired
    private KuaishouLiveDataOverviewMapper kuaishouLiveDataOverviewMapper;

    /**
     * 查询直播数据概览
     *
     * @param ksId 直播数据概览主键
     * @return 直播数据概览
     */
    @Override
    public KuaishouLiveDataOverview selectKuaishouLiveDataOverviewByKsId(Long ksId) {
        return kuaishouLiveDataOverviewMapper.selectKuaishouLiveDataOverviewByKsId(ksId);
    }

    /**
     * 查询直播数据概览列表
     *
     * @param kuaishouLiveDataOverview 直播数据概览
     * @return 直播数据概览
     */
    @Override
    public List<KuaishouLiveDataOverview> selectKuaishouLiveDataOverviewList(KuaishouLiveDataOverview kuaishouLiveDataOverview) {
        return kuaishouLiveDataOverviewMapper.selectKuaishouLiveDataOverviewList(kuaishouLiveDataOverview);
    }

    /**
     * 新增直播数据概览
     *
     * @param kuaishouLiveDataOverview 直播数据概览
     * @return 结果
     */
    @Override
    public int insertKuaishouLiveDataOverview(KuaishouLiveDataOverview kuaishouLiveDataOverview) {
        kuaishouLiveDataOverview.setCreateTime(DateUtils.getNowDate());
        return kuaishouLiveDataOverviewMapper.insertKuaishouLiveDataOverview(kuaishouLiveDataOverview);
    }

    /**
     * 修改直播数据概览
     *
     * @param kuaishouLiveDataOverview 直播数据概览
     * @return 结果
     */
    @Override
    public int updateKuaishouLiveDataOverview(KuaishouLiveDataOverview kuaishouLiveDataOverview) {
        kuaishouLiveDataOverview.setUpdateTime(DateUtils.getNowDate());
        return kuaishouLiveDataOverviewMapper.updateKuaishouLiveDataOverview(kuaishouLiveDataOverview);
    }

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

    /**
     * 删除直播数据概览信息
     *
     * @param ksId 直播数据概览主键
     * @return 结果
     */
    @Override
    public int deleteKuaishouLiveDataOverviewByKsId(Long ksId) {
        return kuaishouLiveDataOverviewMapper.deleteKuaishouLiveDataOverviewByKsId(ksId);
    }

    @Override
    public int inserExcelInfo(KuaishouLiveBroadcast broadcast, List<JSONObject> list) throws Exception {
        /* excel表头：日期	成交金额
        对应字段编码：0,1
        */
        try {
            List<KuaishouLiveDataOverview> overviewList = new ArrayList<>();
            for (JSONObject cell : list) {
                if (cell.size() == 2) {
                    KuaishouLiveDataOverview overview = new KuaishouLiveDataOverview();
                    overview.setKsId(broadcast.getKsId());
                    overview.setKsName(broadcast.getKsName());
                    overview.setAccountId(broadcast.getAccountId());
                    overview.setAccountName(broadcast.getAccountName());
                    overview.setStatDate(cell.getString("0"));
                    overview.setAmounts(cell.getBigDecimal("1"));
                    overviewList.add(overview);
                } else {
                    throw new Exception("直播数据概览数据列不对");
                }
            }
            int i = kuaishouLiveDataOverviewMapper.inserExcelBatchInfo(overviewList);
            if (i > 0) {
                return 1;
            }
        } catch (Exception e) {
            e.printStackTrace();
            throw new Exception("直播数据概览数据列异常");
        }
        return 0;

    }
}
