package org.jeecg.modules.ctop.service.impl;

import cn.com.ctop.common.module.entity.Product;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.jeecg.common.util.DateUtils;
import org.jeecg.modules.ctop.mapper.MediaReportMapper;
import org.jeecg.modules.ctop.service.IMediaReportService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class MediaReportServiceImpl implements IMediaReportService {

    @Autowired
    MediaReportMapper mediaReportMapper;

    @Override
    public List<JSONObject> queryAdvertiserNameByCompanyId(String companyId) {
        return mediaReportMapper.queryAdvertiserNameByCompanyId(companyId);
    }

    @Override
    public List<JSONObject> queryProjectNameByAdvertiserId(String advertiserId) {
        return mediaReportMapper.queryProjectNameByAdvertiserId(advertiserId);
    }

    @Override
    public List<Product> getAllProductList(Integer mediaType, String advertiserId){
        return mediaReportMapper.getAllProductList(mediaType, advertiserId);
    }


    @Override
    public PageInfo<JSONObject> queryReportByCondition(int pageNO, int pageSize, JSONObject requestBody) {
        PageInfo<JSONObject> pageInfo;
        if("6weq589875c040119adst08751712368".equals(requestBody.getString("companyId"))){
            pageInfo=getAgentReportByCondition(pageNO,pageSize,requestBody);
        }else {
            pageInfo=getMediaReportByCondition(pageNO,pageSize,requestBody);
        }
        return pageInfo;
    }

    @Override
    public PageInfo<JSONObject> queryAllReportByCondition(int pageNo, int pageSize, JSONObject requestBody) {
        String todayDate = requestBody.getString("todayDate");
        String yesterdayDate = requestBody.getString("yesterdayDate");
        PageHelper.startPage(pageNo, pageSize);
        List<JSONObject> jsonObjectList = mediaReportMapper.queryAllBy(todayDate, yesterdayDate);
        return new PageInfo<JSONObject>(jsonObjectList);
    }

    @Override
    public List<JSONObject> queryAllByCondition(JSONObject requestBody) {
        String todayDate = requestBody.getString("todayDate");
        String yesterdayDate = requestBody.getString("yesterdayDate");
        return mediaReportMapper.queryAllBy(todayDate, yesterdayDate);
    }

    @Override
    public List<JSONObject> queryReportByCondition(JSONObject requestBody) {
        List<JSONObject> list;
        if("6weq589875c040119adst08751712368".equals(requestBody.getString("companyId"))){
            list=getAgentReportListByCondition(requestBody);
        }else {
            list=getMediaReportListByCondition(requestBody);
        }
        return list;
    }

    private PageInfo<JSONObject> getMediaReportByCondition(int pageNo, int pageSize, JSONObject requestBody) {
        String condition = getSqlByCondition(requestBody);
        PageHelper.startPage(pageNo, pageSize);
        List<JSONObject> jsonObjectList = mediaReportMapper.queryMediaReportBy(requestBody.getString("todayDate"), requestBody.getString("yesterdayDate"), condition);
        return new PageInfo<JSONObject>(jsonObjectList);
    }

    private PageInfo<JSONObject> getAgentReportByCondition(int pageNo, int pageSize, JSONObject requestBody) {
        PageHelper.startPage(pageNo, pageSize);
        List<JSONObject> jsonObjectList = mediaReportMapper.queryAgentReportBy(requestBody.getString("todayDate"), requestBody.getString("yesterdayDate"));
        return new PageInfo<JSONObject>(jsonObjectList);
    }

    private List<JSONObject> getMediaReportListByCondition(JSONObject requestBody) {
        String condition = getSqlByCondition(requestBody);
        return mediaReportMapper.queryMediaReportBy(requestBody.getString("todayDate"), requestBody.getString("yesterdayDate"), condition);
    }

    private List<JSONObject> getAgentReportListByCondition(JSONObject requestBody) {
        return mediaReportMapper.queryAgentReportBy(requestBody.getString("todayDate"), requestBody.getString("yesterdayDate"));
    }

    private String getSqlByCondition(JSONObject requestBody) {
        StringBuilder sql = new StringBuilder();
        if (!requestBody.getString("companyId").isEmpty()) {
            sql.append(" '")
                    .append(requestBody.getString("companyId"))
                    .append("' and ");
        }
        if (!requestBody.getString("advertiserId").isEmpty()) {
            sql.append("advertiserId='")
                    .append(requestBody.getString("advertiserId"))
                    .append("' and ");
        }
        if (!requestBody.getString("projectId").isEmpty()) {
            sql.append("projectId=")
                    .append(requestBody.getString("projectId"))
                    .append(" and ");
        }
        if (!requestBody.getString("mediaType").isEmpty()) {
            sql.append("mediaType=")
                    .append(requestBody.getString("mediaType"))
                    .append(" and ");
        }
        String result = sql.toString();
        if (result.endsWith("and ")) {
            result = result.substring(0,result.length() - 4);
        }
        return result;
    }


    //@Override
    //public PageInfo<JSONObject> toutiaoMediaReport(int pageNum, int pageSize, JSONObject requestBody) {
    //    String today = requestBody.getString("todayDate");
    //    Date todayDate = DateUtils.parseDate(today, "yyyy-MM-dd");
    //    Date yesterdayDate = DateUtils.addDay(todayDate, -1);
    //    String yesterday = DateUtils.formatDate(yesterdayDate);
    //
    //    String advertiserId = requestBody.getString("advertiserId");
    //    String companyId = requestBody.getString("companyId");
    //
    //    PageHelper.startPage(pageNum, pageSize);
    //    List<JSONObject> jsonObjectList = mediaReportMapper.toutiaoMediaReport(today, yesterday, advertiserId, companyId);
    //
    //    return new PageInfo<JSONObject>(jsonObjectList);
    //}


    @Override
    public PageInfo<JSONObject> mediaReport(int pageNum, int pageSize, JSONObject requestBody) {
        String today = requestBody.getString("todayDate");
        Date todayDate = DateUtils.parseDate(today, "yyyy-MM-dd");
        Date yesterdayDate = DateUtils.addDay(todayDate, -1);
        String yesterday = DateUtils.formatDate(yesterdayDate);

        String advertiserId = requestBody.getString("advertiserId");
        String companyId = requestBody.getString("companyId");
        Long productId = requestBody.getLong("productId");

        Integer mediaType = requestBody.getInteger("mediaType");

        List<JSONObject> jsonObjectList = null;
        if(mediaType == 1){
            PageHelper.startPage(pageNum, pageSize);
            jsonObjectList = mediaReportMapper.toutiaoMediaReport(today, yesterday, advertiserId, companyId, productId);
        }else if(mediaType == 2){
            PageHelper.startPage(pageNum, pageSize);
            jsonObjectList = mediaReportMapper.kuaishouMediaReport(today, yesterday, advertiserId, companyId, productId);
        }

        return new PageInfo<JSONObject>(jsonObjectList);
    }

    @Override
    public List<JSONObject> exportMediaReportExcel(JSONObject requestBody) {
        String today = requestBody.getString("todayDate");
        Date todayDate = DateUtils.parseDate(today, "yyyy-MM-dd");
        Date yesterdayDate = DateUtils.addDay(todayDate, -1);
        String yesterday = DateUtils.formatDate(yesterdayDate);

        String advertiserId = requestBody.getString("advertiserId");
        String companyId = requestBody.getString("companyId");
        Long productId = requestBody.getLong("productId");

        Integer mediaType = requestBody.getInteger("mediaType");

        if(mediaType == 2){
            return mediaReportMapper.kuaishouMediaReport(today, yesterday, advertiserId, companyId, productId);
        }else if(mediaType == 1){
            return mediaReportMapper.toutiaoMediaReport(today, yesterday, advertiserId, companyId, productId);
        }
        return null;
    }





}
