package com.ruixuan.data.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.ruixuan.common.utils.DateUtils;
import com.ruixuan.data.entity.KuaishouLiveDataAnalysis;
import com.ruixuan.data.mapper.KuaishouLiveDataAnalysisMapper;
import com.ruixuan.data.service.IKuaishouLiveDataAnalysisService;
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 KuaishouLiveDataAnalysisServiceImpl implements IKuaishouLiveDataAnalysisService {
    @Autowired
    private KuaishouLiveDataAnalysisMapper kuaishouLiveDataAnalysisMapper;

    /**
     * 查询商品分析数据
     *
     * @param ksId 商品分析数据主键
     * @return 商品分析数据
     */
    @Override
    public KuaishouLiveDataAnalysis selectKuaishouLiveDataAnalysisByKsId(Long ksId) {
        return kuaishouLiveDataAnalysisMapper.selectKuaishouLiveDataAnalysisByKsId(ksId);
    }

    /**
     * 查询商品分析数据列表
     *
     * @param kuaishouLiveDataAnalysis 商品分析数据
     * @return 商品分析数据
     */
    @Override
    public List<KuaishouLiveDataAnalysis> selectKuaishouLiveDataAnalysisList(KuaishouLiveDataAnalysis kuaishouLiveDataAnalysis) {
        return kuaishouLiveDataAnalysisMapper.selectKuaishouLiveDataAnalysisList(kuaishouLiveDataAnalysis);
    }

    /**
     * 新增商品分析数据
     *
     * @param kuaishouLiveDataAnalysis 商品分析数据
     * @return 结果
     */
    @Override
    public int insertKuaishouLiveDataAnalysis(KuaishouLiveDataAnalysis kuaishouLiveDataAnalysis) {
        kuaishouLiveDataAnalysis.setCreateTime(DateUtils.getNowDate());
        return kuaishouLiveDataAnalysisMapper.insertKuaishouLiveDataAnalysis(kuaishouLiveDataAnalysis);
    }

    /**
     * 修改商品分析数据
     *
     * @param kuaishouLiveDataAnalysis 商品分析数据
     * @return 结果
     */
    @Override
    public int updateKuaishouLiveDataAnalysis(KuaishouLiveDataAnalysis kuaishouLiveDataAnalysis) {
        kuaishouLiveDataAnalysis.setUpdateTime(DateUtils.getNowDate());
        return kuaishouLiveDataAnalysisMapper.updateKuaishouLiveDataAnalysis(kuaishouLiveDataAnalysis);
    }

    /**
     * 批量删除商品分析数据
     *
     * @param ksIds 需要删除的商品分析数据主键
     * @return 结果
     */
    @Override
    public int deleteKuaishouLiveDataAnalysisByKsIds(Long[] ksIds) {
        return kuaishouLiveDataAnalysisMapper.deleteKuaishouLiveDataAnalysisByKsIds(ksIds);
    }

    /**
     * 删除商品分析数据信息
     *
     * @param ksId 商品分析数据主键
     * @return 结果
     */
    @Override
    public int deleteKuaishouLiveDataAnalysisByKsId(Long ksId) {
        return kuaishouLiveDataAnalysisMapper.deleteKuaishouLiveDataAnalysisByKsId(ksId);
    }

    @Override
    public int inserExcelInfo(KuaishouLiveBroadcast broadcast, List<JSONObject> list) throws Exception {
        /* excel表头：商品名称	商品上架时间	商品价格	商品销售额	商品销量	待支付订单数
           对应字段编码：0,1,2...
         */
        try {
            List<KuaishouLiveDataAnalysis> analysisList = new ArrayList<>();
            for (JSONObject cell : list) {
                if (cell.size() == 6) {
                    KuaishouLiveDataAnalysis analysis = new KuaishouLiveDataAnalysis();
                    analysis.setKsId(broadcast.getKsId());
                    analysis.setKsName(broadcast.getKsName());
                    analysis.setAccountId(broadcast.getAccountId());
                    analysis.setAccountName(broadcast.getAccountName());
                    analysis.setWaresName(cell.getString("0"));
                    analysis.setWaresPutDate(cell.getString("1"));
                    analysis.setWaresPrice(cell.getBigDecimal("2"));
                    analysis.setWaresSalesAmount(cell.getBigDecimal("3"));
                    analysis.setWaresSalesVolume(cell.getBigDecimal("4"));
                    analysis.setTopayOrders(cell.getLong("5"));
                    analysisList.add(analysis);
                } else {
                    throw new Exception("商品分析列不对");
                }
            }

            int i = kuaishouLiveDataAnalysisMapper.inserExcelBatchInfo(analysisList);
            if (i > 0) {
                return 1;
            }
        } catch (Exception e) {
            e.printStackTrace();
            throw new Exception("商品分析数据列异常");
        }
        return 0;
    }
}
