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

import cn.com.ctop.common.module.entity.ByteDanceCleanMaterialReport;
import cn.com.ctop.common.module.mapper.SysRoleExtMapper;
import cn.com.ctop.common.module.service.IByteDanceCleanMaterialReportService;
import cn.com.ctop.common.module.utils.Check;
import cn.com.ctop.toutiao.modules.report.entity.BytedanceCreativeDailyReport;
import cn.com.ctop.toutiao.modules.report.mapper.BytedanceCreativeDailyReportMapper;
import cn.com.ctop.toutiao.modules.report.service.IBytedanceCreativeDailyReportService;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Description: 广告创意日报表信息
 * @Author: jeecg-boot
 * @Date: 2019-09-11
 * @Version: V1.0
 */
@Service
@Primary

public class BytedanceCreativeDailyReportServiceImpl extends ServiceImpl<BytedanceCreativeDailyReportMapper, BytedanceCreativeDailyReport> implements IBytedanceCreativeDailyReportService {
    @Autowired
    private BytedanceCreativeDailyReportMapper bytedanceCreativeDailyReportMapper;
    @Autowired
    private SysRoleExtMapper sysRoleExtMapper;

    @Override
    public List<BytedanceCreativeDailyReport> bytedanceCreativeDailyReportList(JSONObject json) {
        String userId = json.getString("userId");
        String startDate = json.getString("startDate");
        String endDate = json.getString("endDate");
        if (Check.isNull(userId)) {
            return null;
        }
        String roleCode = sysRoleExtMapper.getRoleCodeByUserId(json.getString("userId"));
        if (!"admin".equals(roleCode)) {
            userId = "";
        }
        return bytedanceCreativeDailyReportMapper.bytedanceCreativeDailyReport(userId, startDate, endDate);
    }

    @Override
    public List<BytedanceCreativeDailyReport> bytedanceCreativeAccountReportList(JSONObject json) {
        Map<String, Object> paramsMap = new HashMap<>();
        JSONArray accountIds = json.getJSONArray("accountIds");
        if (Check.isNull(accountIds)) {
            return null;
        }
        List<String> list = JSONObject.parseArray(accountIds.toJSONString(), String.class);
        paramsMap.put("accountIds", list);
        String nowDate = json.getString("startDate");
        String endDate = json.getString("endDate");
        paramsMap.put("startDate", nowDate);
        paramsMap.put("endDate", endDate);

        return bytedanceCreativeDailyReportMapper.bytedanceCreativeAccountReport(paramsMap);
    }

    @Override
    public List<String> getSignatureList() {
        return bytedanceCreativeDailyReportMapper.getSignatureList();
    }

    @Override
    public void replaceBatch(List<BytedanceCreativeDailyReport> dailyReports) {
        bytedanceCreativeDailyReportMapper.replaceBatch(dailyReports);
    }

    @Override
    public void calculationMaterialReport(String signature) {
        if (Check.isNull(signature)) {
            return;
        }
        ByteDanceCleanMaterialReport calculationMaterialReport = bytedanceCreativeDailyReportMapper.calculationMaterialReport(signature);
        if (!Check.isNull(calculationMaterialReport)) {
            byteDanceCleanMaterialReportService.saveOrUpdate(calculationMaterialReport);
        }
    }
    @Autowired
    private IByteDanceCleanMaterialReportService byteDanceCleanMaterialReportService;

}
