package com.ruixuan.data.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.ruixuan.common.utils.DateUtils;
import com.ruixuan.data.entity.KuaishouLiveDataKanban;
import com.ruixuan.data.mapper.KuaishouLiveDataKanbanMapper;
import com.ruixuan.data.service.IKuaishouLiveDataKanbanService;
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 KuaishouLiveDataKanbanServiceImpl implements IKuaishouLiveDataKanbanService {
    @Autowired
    private KuaishouLiveDataKanbanMapper kuaishouLiveDataKanbanMapper;

    /**
     * 查询直播数据看板
     *
     * @param ksId 直播数据看板主键
     * @return 直播数据看板
     */
    @Override
    public KuaishouLiveDataKanban selectKuaishouLiveDataKanbanByKsId(Long ksId) {
        return kuaishouLiveDataKanbanMapper.selectKuaishouLiveDataKanbanByKsId(ksId);
    }

    /**
     * 查询直播数据看板列表
     *
     * @param kuaishouLiveDataKanban 直播数据看板
     * @return 直播数据看板
     */
    @Override
    public List<KuaishouLiveDataKanban> selectKuaishouLiveDataKanbanList(KuaishouLiveDataKanban kuaishouLiveDataKanban) {
        return kuaishouLiveDataKanbanMapper.selectKuaishouLiveDataKanbanList(kuaishouLiveDataKanban);
    }

    /**
     * 新增直播数据看板
     *
     * @param kuaishouLiveDataKanban 直播数据看板
     * @return 结果
     */
    @Override
    public int insertKuaishouLiveDataKanban(KuaishouLiveDataKanban kuaishouLiveDataKanban) {
        kuaishouLiveDataKanban.setCreateTime(DateUtils.getNowDate());
        return kuaishouLiveDataKanbanMapper.insertKuaishouLiveDataKanban(kuaishouLiveDataKanban);
    }

    /**
     * 修改直播数据看板
     *
     * @param kuaishouLiveDataKanban 直播数据看板
     * @return 结果
     */
    @Override
    public int updateKuaishouLiveDataKanban(KuaishouLiveDataKanban kuaishouLiveDataKanban) {
        kuaishouLiveDataKanban.setUpdateTime(DateUtils.getNowDate());
        return kuaishouLiveDataKanbanMapper.updateKuaishouLiveDataKanban(kuaishouLiveDataKanban);
    }

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

    /**
     * 删除直播数据看板信息
     *
     * @param ksId 直播数据看板主键
     * @return 结果
     */
    @Override
    public int deleteKuaishouLiveDataKanbanByKsId(Long ksId) {
        return kuaishouLiveDataKanbanMapper.deleteKuaishouLiveDataKanbanByKsId(ksId);
    }

    @Override
    public int inserExcelInfo(KuaishouLiveBroadcast broadcast, List<JSONObject> list) throws Exception {
      /* excel表头：实时在线观看人数	观看人数	观看次数	人均观看时长	粉丝平均观看时长	打赏数	上架商品数
      新增粉丝数	点赞数	商品点击人数	小黄车点击数	累计成交金额	累计成交人数	累计成交订单数	成交转化率
           对应字段编码：0,1,2....         */
        try {
            List<KuaishouLiveDataKanban> kanbanList = new ArrayList<>();
            for (JSONObject cell : list) {
                if (cell.size() == 15) {
                    KuaishouLiveDataKanban kanban = new KuaishouLiveDataKanban();
                    kanban.setKsId(broadcast.getKsId());
                    kanban.setKsName(broadcast.getKsName());
                    kanban.setAccountId(broadcast.getAccountId());
                    kanban.setAccountName(broadcast.getAccountName());
                    kanban.setOnlineViewers(cell.getLong("0"));
                    kanban.setViewers(cell.getLong("1"));
                    kanban.setViewingTimes(cell.getLong("2"));
                    kanban.setPerViewingTime(cell.getString("3"));
                    kanban.setFansViewingTime(cell.getString("4"));
                    kanban.setRewards(cell.getLong("5"));
                    kanban.setPutWares(cell.getLong("6"));
                    kanban.setNewFans(cell.getLong("7"));
                    kanban.setLikes(cell.getLong("8"));
                    kanban.setWaresClicks(cell.getLong("9"));
                    kanban.setCarClicks(cell.getLong("10"));
                    kanban.setAllAmount(cell.getBigDecimal("11"));
                    kanban.setAllTurnover(cell.getLong("12"));
                    kanban.setAllOrders(cell.getLong("13"));
                    kanban.setTurnoverRatio(cell.getString("14"));
                    kanbanList.add(kanban);
                } else {
                    throw new Exception("直播数据看板数据列不对");
                }
            }

            int i = kuaishouLiveDataKanbanMapper.inserExcelBatchInfo(kanbanList);
            if (i > 0) {
                return 1;
            }
        } catch (Exception e) {
            e.printStackTrace();
            throw new Exception("直播数据看板数据列异常");
        }
        return 0;
    }
}
