package cn.com.ctop.toutiao.modules.report.service.impl;

import cn.com.ctop.toutiao.modules.report.mapper.BytedanceNewPlanReportMapper;
import cn.com.ctop.toutiao.modules.report.service.IBytedanceNewPlanReportService;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.util.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;

/**
 *  Created By JQ.bi on 2020.11.4
 */
@Slf4j
@Service
public class BytedanceNewPlanReportServiceImpl implements IBytedanceNewPlanReportService {

    @Autowired
    private BytedanceNewPlanReportMapper newPlanReportMapper;

    @Override
    public List<JSONObject> getBytedanceNewPlanGroupByAccount(JSONArray accountIds, String startDate, String endDate, String target, String order) {
        return newPlanReportMapper.queryBytedanceNewPlanGroupByAccount(accountIds,startDate,endDate,target,order);
    }

    @Override
    public List<JSONObject> getBytedanceNewPlanGroupByStatDate(JSONArray accountIds, String startDate, String endDate) {
        List<JSONObject> resultList= new ArrayList<>();
        try {
            if(DateUtils.isMoreSixMonth(startDate,endDate)){
                resultList = newPlanReportMapper.queryBytedanceNewPlanGroupByMonth(accountIds,startDate,endDate);
            }
            else {
                resultList = newPlanReportMapper.queryBytedanceNewPlanGroupByStatDate(accountIds,startDate,endDate);
            }
        } catch (ParseException e) {
            log.error(e.getMessage());
        }
        return resultList;
    }

    @Override
    public JSONObject getSumNewPlanBy(JSONArray accountIds, String startDate, String endDate) {
        return newPlanReportMapper.querySumNewPlanBy(accountIds,startDate,endDate);
    }

    @Override
    public List<JSONObject> getAllOperator() {
        return newPlanReportMapper.queryAllOperator();
    }

    @Override
    public JSONArray getAccountIdsByUserId(String userId) {
        return newPlanReportMapper.queryAccountIdsByUserId(userId);
    }

    @Override
    public JSONArray getAccountIdsByMedia() {
        return newPlanReportMapper.queryAccountIdsByMedia();
    }

}
