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

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
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.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 PageInfo<JSONObject> queryReportByCondition(int pageNO, int pageSize, JSONObject requestBody) {
        PageInfo<JSONObject> pageInfo;
        if(requestBody.getString("companyId").equals("6weq589875c040119adst08751712368")){
            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(requestBody.getString("companyId").equals("6weq589875c040119adst08751712368")){
            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;
    }
}
