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

import cn.com.ctop.common.module.service.ISysUserService;
import cn.com.ctop.common.module.utils.ResultMapUtils;
import cn.com.ctop.common.module.utils.StatusCode;
import cn.com.ctop.common.module.vo.*;
import cn.com.ctop.kuaishou.modules.report.mapper.KuaishouReportDailyImageMapper;
import cn.com.ctop.toutiao.modules.report.mapper.BytedanceReportMaterialDailyMapper;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.jeecg.modules.ctop.service.IImageReportDailyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class ImageReportDailyServiceImpl implements IImageReportDailyService {
    @Resource
    private BytedanceReportMaterialDailyMapper bytedanceReportMaterialDailyMapper;
    @Resource
    private KuaishouReportDailyImageMapper kuaishouReportDailyImageMapper;
    @Autowired
    private ISysUserService sysUserService;


    @Override
    public Map<String,Object> bytedanceImageCost(JSONObject data) {
        Map<String,Object>result = new HashMap<>();
        String order = data.getString("order");
        if(null == order|| "".equals(order.trim())){
            order = "desc";
        }
        String target = data.getString("target");
        if(null == target|| "".equals(target.trim())){
            target = "cost";
        }
        String startDate = data.getString("startDate");
        if(null == startDate|| "".equals(startDate.trim())){
            startDate = null;
        }
        String endDate = data.getString("endDate");
        if(null == endDate|| "".equals(endDate.trim())){
            endDate = null;
        }
        JSONArray accountList = data.getJSONArray("accountList");
        if(null==accountList||accountList.isEmpty()){
            accountList = null;
        }
        String code = data.getString("code");
        if(null == code|| "".equals(code.trim())){
            code = null;
        }
        Integer pageSize = data.getInteger("pageSize");
        Integer pageNumber = data.getInteger("pageNumber")
                ;
        if(null == pageNumber||pageNumber ==0){
            pageNumber = 1;
        }
        if(null == pageSize||pageSize ==0){
            pageSize = 10;
        }
        PageHelper.startPage(pageNumber,pageSize);
        List<BytedanceImageCostVO> vos = bytedanceReportMaterialDailyMapper.bytedanceImageCost(startDate,endDate,accountList,code,order,target);
        PageInfo<BytedanceImageCostVO> pageInfo = new PageInfo<>(vos);
        result.put("data",pageInfo);
        ResultMapUtils.setResultMap(result, StatusCode.COMMON_SUCCESS);
        return result;
    }

    @Override
    public Map<String, Object> bytedanceImageCostTotal(JSONObject data) {
        Map<String,Object>result = new HashMap<>();
        String startDate = data.getString("startDate");
        if(null == startDate|| "".equals(startDate.trim())){
            startDate = null;
        }
        String endDate = data.getString("endDate");
        if(null == endDate|| "".equals(endDate.trim())){
            endDate = null;
        }
        JSONArray accountList = data.getJSONArray("accountList");
        if(null==accountList||accountList.isEmpty()){
            accountList = null;
        }
        String code = data.getString("code");
        if(null == code|| "".equals(code.trim())){
            code = null;
        }
        result.put("data",bytedanceReportMaterialDailyMapper.bytedanceImageCostTotal(startDate,endDate,accountList,code));
        ResultMapUtils.setResultMap(result, StatusCode.COMMON_SUCCESS);
        return result;
    }

    @Override
    public SheetInfoVo getbytedanceImageCostVo(JSONObject data) {
        SheetInfoVo sheetInfoVo = new SheetInfoVo<BytedanceImageCostVOEntity>();
        String order = data.getString("order");
        if(null == order|| "".equals(order.trim())){
            order = "desc";
        }
        String target = data.getString("target");
        if(null == target|| "".equals(target.trim())){
            target = "cost";
        }
        String startDate = data.getString("startDate");
        if(null == startDate|| "".equals(startDate.trim())){
            startDate = null;
        }
        String endDate = data.getString("endDate");
        if(null == endDate|| "".equals(endDate.trim())){
            endDate = null;
        }
        JSONArray accountList = data.getJSONArray("accountList");
        if(null==accountList||accountList.isEmpty()){
            accountList = null;
        }
        String code = data.getString("code");
        if(null == code|| "".equals(code.trim())){
            code = null;
        }
        List<BytedanceImageCostVOEntity>details = bytedanceReportMaterialDailyMapper.bytedanceImageCostEntity(startDate,endDate,accountList,code,order,target);
        sheetInfoVo.setDetails(details);
        return sheetInfoVo;
    }

    @Override
    public Map<String, Object> kuaishouImageCost(JSONObject data) {
        Map<String,Object>result = new HashMap<>();
        String order = data.getString("order");
        if(null == order|| "".equals(order.trim())){
            order = "desc";
        }
        String target = data.getString("target");
        if(null == target|| "".equals(target.trim())){
            target = "cost";
        }
        String startDate = data.getString("startDate");
        if(null == startDate|| "".equals(startDate.trim())){
            startDate = null;
        }
        String endDate = data.getString("endDate");
        if(null == endDate|| "".equals(endDate.trim())){
            endDate = null;
        }
        JSONArray accountList = data.getJSONArray("accountList");
        if(null==accountList||accountList.isEmpty()){
            accountList = null;
        }
        String code = data.getString("code");
        if(null == code|| "".equals(code.trim())){
            code = null;
        }
        Integer pageSize = data.getInteger("pageSize");
        Integer pageNumber = data.getInteger("pageNumber");
        if(null == pageNumber||pageNumber ==0){
            pageNumber = 1;
        }
        if(null == pageSize||pageSize ==0){
            pageSize = 10;
        }
        PageHelper.startPage(pageNumber,pageSize);
        List<KuaishouImageCostVO> vos = kuaishouReportDailyImageMapper.imageCost(startDate,endDate,accountList,code,order,target);
        PageInfo<KuaishouImageCostVO> pageInfo = new PageInfo<>(vos);
        result.put("data",pageInfo);
        ResultMapUtils.setResultMap(result, StatusCode.COMMON_SUCCESS);
        return result;
    }

    @Override
    public Map<String, Object> kuaishouImageCostTotal(JSONObject data) {
        Map<String,Object>result = new HashMap<>();
        String startDate = data.getString("startDate");
        if(null == startDate|| "".equals(startDate.trim())){
            startDate = null;
        }
        String endDate = data.getString("endDate");
        if(null == endDate|| "".equals(endDate.trim())){
            endDate = null;
        }
        JSONArray accountList = data.getJSONArray("accountList");
        if(null==accountList||accountList.isEmpty()){
            accountList = null;
        }
        String code = data.getString("code");
        if(null == code|| "".equals(code.trim())){
            code = null;
        }
        result.put("data",kuaishouReportDailyImageMapper.imageCostTotal(startDate,endDate,accountList,code));
        ResultMapUtils.setResultMap(result, StatusCode.COMMON_SUCCESS);
        return result;
    }

    @Override
    public SheetInfoVo getkuaishouImageCostVo(JSONObject data) {
        SheetInfoVo sheetInfoVo = new SheetInfoVo<KuaishouImageCostVOEntity>();
        String order = data.getString("order");
        if(null == order|| "".equals(order.trim())){
            order = "desc";
        }
        String target = data.getString("target");
        if(null == target|| "".equals(target.trim())){
            target = "cost";
        }
        String startDate = data.getString("startDate");
        if(null == startDate|| "".equals(startDate.trim())){
            startDate = null;
        }
        String endDate = data.getString("endDate");
        if(null == endDate|| "".equals(endDate.trim())){
            endDate = null;
        }
        JSONArray accountList = data.getJSONArray("accountList");
        if(null==accountList||accountList.isEmpty()){
            accountList = null;
        }
        String code = data.getString("code");
        if(null == code|| "".equals(code.trim())){
            code = null;
        }
        sheetInfoVo.setSheetName("快手图片消耗统计");
        List<KuaishouImageCostVOEntity> details = kuaishouReportDailyImageMapper.imageCostEntity(startDate,endDate,accountList,code,order,target);
        sheetInfoVo.setDetails(details);
        return sheetInfoVo;
    }
}
