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

import cn.com.ctop.common.module.constant.CtopRoleCodeConstant;
import cn.com.ctop.toutiao.modules.report.DTO.AccountVideoDTO;
import cn.com.ctop.toutiao.modules.report.entity.ByteDanceVideoReportDaily;
import cn.com.ctop.toutiao.modules.report.mapper.ByteDanceVideoReportDailyMapper;
import cn.com.ctop.toutiao.modules.report.service.IByteDanceVideoReportDailyService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.common.util.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 视频按时间段统计
 * @author jeecg-boot
 * @date   2020-05-13
 * @version V1.0
 */
@Service
@Slf4j
public class ByteDanceVideoReportDailyServiceImpl extends ServiceImpl<ByteDanceVideoReportDailyMapper, ByteDanceVideoReportDaily> implements IByteDanceVideoReportDailyService {

    @Autowired
    private ByteDanceVideoReportDailyMapper byteDanceVideoReportDailyMapper;

    @Override
    public void videoInfoList(String startDate, String endDate){
        //华南
        videoInfoListByCompanyId(startDate, endDate, "6608ed13c0dd42de93c790dbdf124234");
        //华北
        videoInfoListByCompanyId(startDate, endDate, "d57fecdcf7a94d009736d9c850731582");
        //华东
        videoInfoListByCompanyId(startDate, endDate, "4b10089775c040119e139087517aed88");
    }

    private void videoInfoListByCompanyId(String startDate, String endDate, String companyId){
        QueryWrapper<ByteDanceVideoReportDaily> wrapper = new QueryWrapper<>();
        wrapper.ge("stat_datetime",startDate);
        wrapper.le("stat_datetime",endDate);
        wrapper.eq("company_id", companyId);
        byteDanceVideoReportDailyMapper.delete(wrapper);

        List<ByteDanceVideoReportDaily> mList = byteDanceVideoReportDailyMapper.videoInfoList(startDate, endDate, companyId, null);
        if(null!=mList&&!mList.isEmpty()){
            List<ByteDanceVideoReportDaily> reports = new ArrayList<>();
            for(ByteDanceVideoReportDaily report :mList){
                if(null == report.getMaterialName()|| "".equals(report.getMaterialName().trim())){
                    report.setMaterialName("-");
                }
                if(null == report.getProjectName()|| "".equals(report.getProjectName().trim())){
                    report.setProjectName("-");
                }
                if(null == report.getAdvertiserName()|| "".equals(report.getAdvertiserName().trim())){
                    report.setAdvertiserName("-");
                }
                reports.add(report);
            }
            byteDanceVideoReportDailyMapper.insertBatch(reports);
        }
    }

    //根据accountId和时间清洗头条素材为头条视频报表数据
    @Override
    public void videoInfoListByAccountId(String startDate, String endDate, Long accountId){
        QueryWrapper<ByteDanceVideoReportDaily> wrapper = new QueryWrapper<>();
        wrapper.ge("stat_datetime",startDate);
        wrapper.le("stat_datetime",endDate);
        wrapper.eq("account_id", accountId);
        byteDanceVideoReportDailyMapper.delete(wrapper);

        String companyId = byteDanceVideoReportDailyMapper.getCompanyIdByAccountId(accountId);

        List<ByteDanceVideoReportDaily> mList = byteDanceVideoReportDailyMapper.videoInfoList(startDate, endDate, companyId, accountId);
        for (ByteDanceVideoReportDaily m:mList){
            byteDanceVideoReportDailyMapper.insert(m);
        }
        log.info("根据accountId和时间清洗头条素材为头条视频报表数据结束，开始时间：{},结束时间:{},accountId:{}",startDate,endDate,accountId );
    }

    //视频按天列表
    @Override
    public PageInfo<ByteDanceVideoReportDaily> videoInfoListDaily(String startDate, String endDate, String signature, BigDecimal cost, Integer pageNo, Integer pageSize, Long[] projectIds, Long[] accountIds, String sort){
        LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        String userId = sysUser.getId();
        String roleCode = byteDanceVideoReportDailyMapper.getRoleCodeByUserId(userId);
        String companyId = byteDanceVideoReportDailyMapper.getCompanyIdByUserId(userId);

        if(StringUtils.isBlank(sort)){
            sort = "cost-";
        }
        sort = sort.replace("-", " desc,").replace("+", " asc,");  //sort=id-name+age+
        sort = sort.substring(0, sort.length() - 1);
        List<ByteDanceVideoReportDaily> mList = new ArrayList<>();

        List<Long> accountIdsList = null;
        if(accountIds != null && accountIds.length != 0){
            accountIdsList = Arrays.asList(accountIds);
        }
        //admin能看所有视频
        if(roleCode.equals(CtopRoleCodeConstant.COMMON_ROLE_CODE_ADMIN)){
            PageHelper.startPage(pageNo,pageSize,sort);
            mList = byteDanceVideoReportDailyMapper.videoInfoListGroupAdmin(startDate, endDate, accountIdsList, signature, cost);
        }else if("touTiaoOperationManager".equals(roleCode) || "operator".equals(roleCode) || "operationAssistant".equals(roleCode)|| "huananyunying".equals(roleCode)) {
            if (accountIdsList != null && !accountIdsList.isEmpty()){
                PageHelper.startPage(pageNo,pageSize,sort);
                mList = byteDanceVideoReportDailyMapper.videoInfoListGroupOperator(startDate,endDate,accountIdsList,signature,cost);
            }else{
                List<Long> accoutnIds= byteDanceVideoReportDailyMapper.getAccountIdsByOperatorIdAndMediaId(userId);
                if(accoutnIds == null || accoutnIds.isEmpty()){
                    return new PageInfo<>(mList);
                }
                PageHelper.startPage(pageNo,pageSize,sort);
                mList = byteDanceVideoReportDailyMapper.videoInfoListGroupOperator(startDate,endDate,accoutnIds,signature,cost);
            }
        }else if("shot".equals(roleCode)){
            PageHelper.startPage(pageNo,pageSize,sort);
            mList = byteDanceVideoReportDailyMapper.videoInfoListGroupShot(startDate, endDate, companyId, userId, accountIdsList, signature, cost);
        }else if("plan".equals(roleCode)){
            PageHelper.startPage(pageNo,pageSize,sort);
            mList = byteDanceVideoReportDailyMapper.videoInfoListGroupPlan(startDate, endDate, companyId, userId, accountIdsList, signature, cost);
        }else if("plane".equals(roleCode)  || "planeLeader".equals(roleCode)){
            PageHelper.startPage(pageNo,pageSize,sort);
            mList = byteDanceVideoReportDailyMapper.videoInfoListGroupPlaneAndPlaneLeader(startDate, endDate, companyId, userId, accountIdsList, signature, cost);
        }else if("clip".equals(roleCode)){
            PageHelper.startPage(pageNo,pageSize,sort);
            mList = byteDanceVideoReportDailyMapper.videoInfoListGroupClip(startDate, endDate, companyId, userId, accountIdsList, signature, cost);
        }else if("designTeamLeader".equals(roleCode)){
            PageHelper.startPage(pageNo,pageSize,sort);
            mList = byteDanceVideoReportDailyMapper.videoInfoListGroupdesignTeamLeader(startDate, endDate, companyId, accountIdsList, signature, cost);
        }else{
            if (accountIdsList != null && !accountIdsList.isEmpty()) {
                PageHelper.startPage(pageNo,pageSize,sort);
                mList = byteDanceVideoReportDailyMapper.videoInfoListGroupOtherRoles(startDate,endDate,accountIdsList,signature,cost);
            }else{
                List<Integer> mediaIds = new ArrayList<>();
                mediaIds.add(1);
                mediaIds.add(3);
                accountIdsList = byteDanceVideoReportDailyMapper.getAccountIdsByUserIdAndMediaIds(userId,mediaIds);
                if(accountIdsList == null || accountIdsList.isEmpty()){
                    return new PageInfo<>(mList);
                }
                PageHelper.startPage(pageNo,pageSize,sort);
                mList = byteDanceVideoReportDailyMapper.videoInfoListGroupOtherRoles(startDate,endDate,accountIdsList,signature,cost);
            }
        }

        //计算环比
        for(ByteDanceVideoReportDaily m:mList){
            Integer datediff = DateUtils.dateDiff(startDate, endDate);
            String startMonthDate = DateUtils.formatDate(DateUtils.addDay(startDate,-(datediff + 1) ) );
            String endMonthDate = DateUtils.formatDate(DateUtils.addDay(startDate,-1));

            BigDecimal costBefore = byteDanceVideoReportDailyMapper.getVideoReportDailyByDateAndSignature(startMonthDate,endMonthDate,m.getSignature(),accountIdsList);

            BigDecimal huanbi =BigDecimal.ZERO;
            if(costBefore != null && costBefore.compareTo(BigDecimal.ZERO)>0){
                huanbi = m.getCost().subtract(costBefore).divide(costBefore,7, BigDecimal.ROUND_HALF_UP);
            }

            m.setHuanbi(huanbi);
        }

        return new PageInfo<>(mList);
    }


    //视频报表月列表
    @Override
    public PageInfo<ByteDanceVideoReportDaily> videoInfoListMonth(String startDate, String endDate, String signature, BigDecimal cost, Integer pageNo, Integer pageSize, Long[] projectIds, Long[] accountIds, String sort){
        LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        String userId = sysUser.getId();
        String roleCode = byteDanceVideoReportDailyMapper.getRoleCodeByUserId(userId);
        String companyId = byteDanceVideoReportDailyMapper.getCompanyIdByUserId(userId);

        if(StringUtils.isBlank(sort)){
            sort = "cost-";
        }
        sort = sort.replace("-", " desc,").replace("+", " asc,");  //sort=id-name+age+
        sort = sort.substring(0, sort.length() - 1);
        List<ByteDanceVideoReportDaily> mList = new ArrayList<>();

        List<Long> accountIdsList = null;
        if(accountIds != null && accountIds.length != 0){
            accountIdsList = Arrays.asList(accountIds);
        }
        //admin能看所有视频
        if(roleCode.equals(CtopRoleCodeConstant.COMMON_ROLE_CODE_ADMIN)){
            PageHelper.startPage(pageNo,pageSize,sort);
            mList = byteDanceVideoReportDailyMapper.videoInfoListGroupAdmin(startDate, endDate, accountIdsList, signature, cost);
        }else if("touTiaoOperationManager".equals(roleCode) || "operator".equals(roleCode) || "operationAssistant".equals(roleCode)|| "huananyunying".equals(roleCode)) {
            if (accountIdsList != null && !accountIdsList.isEmpty()){
                PageHelper.startPage(pageNo,pageSize,sort);
                mList = byteDanceVideoReportDailyMapper.videoInfoListGroupOperator(startDate,endDate,accountIdsList,signature,cost);
            }else{
                List<Long> accoutnIds= byteDanceVideoReportDailyMapper.getAccountIdsByOperatorIdAndMediaId(userId);
                if(accoutnIds == null || accoutnIds.isEmpty()){
                    return new PageInfo<>(mList);
                }
                PageHelper.startPage(pageNo,pageSize,sort);
                mList = byteDanceVideoReportDailyMapper.videoInfoListGroupOperator(startDate,endDate,accoutnIds,signature,cost);
            }
        }else if("shot".equals(roleCode)){
                PageHelper.startPage(pageNo,pageSize,sort);
                mList = byteDanceVideoReportDailyMapper.videoInfoListGroupShot(startDate, endDate, companyId, userId, accountIdsList, signature, cost);
        }else if("plan".equals(roleCode)){
                PageHelper.startPage(pageNo,pageSize,sort);
                mList = byteDanceVideoReportDailyMapper.videoInfoListGroupPlan(startDate, endDate, companyId, userId, accountIdsList, signature, cost);
        }else if("plane".equals(roleCode)  || "planeLeader".equals(roleCode)){
                PageHelper.startPage(pageNo,pageSize,sort);
                mList = byteDanceVideoReportDailyMapper.videoInfoListGroupPlaneAndPlaneLeader(startDate, endDate, companyId, userId, accountIdsList, signature, cost);
        }else if("clip".equals(roleCode)){
                PageHelper.startPage(pageNo,pageSize,sort);
                mList = byteDanceVideoReportDailyMapper.videoInfoListGroupClip(startDate, endDate, companyId, userId, accountIdsList, signature, cost);
        }else if("designTeamLeader".equals(roleCode)){
                PageHelper.startPage(pageNo,pageSize,sort);
                mList = byteDanceVideoReportDailyMapper.videoInfoListGroupdesignTeamLeader(startDate, endDate, companyId, accountIdsList, signature, cost);
        }else {
            if (accountIdsList != null && !accountIdsList.isEmpty()) {
                PageHelper.startPage(pageNo,pageSize,sort);
                mList = byteDanceVideoReportDailyMapper.videoInfoListGroupOtherRoles(startDate,endDate,accountIdsList,signature,cost);
            }else{
                List<Integer> mediaIds = new ArrayList<>();
                mediaIds.add(1);
                mediaIds.add(3);
                accountIdsList = byteDanceVideoReportDailyMapper.getAccountIdsByUserIdAndMediaIds(userId,mediaIds);
                if(accountIdsList == null || accountIdsList.isEmpty()){
                    return new PageInfo<>(mList);
                }
                PageHelper.startPage(pageNo,pageSize,sort);
                mList = byteDanceVideoReportDailyMapper.videoInfoListGroupOtherRoles(startDate,endDate,accountIdsList,signature,cost);
            }
        }

        //计算环比
        for(ByteDanceVideoReportDaily m:mList){
            Integer datediff = DateUtils.dateDiff(startDate, endDate);
            String startMonthDate = DateUtils.formatDate(DateUtils.addDay(startDate,-(datediff + 1) ) );
            String endMonthDate = DateUtils.formatDate(DateUtils.addDay(startDate,-1));

            BigDecimal costBefore = byteDanceVideoReportDailyMapper.getVideoReportDailyByDateAndSignature(startMonthDate,endMonthDate,m.getSignature(),accountIdsList);

            BigDecimal huanbi =BigDecimal.ZERO;
            if(costBefore != null && costBefore.compareTo(BigDecimal.ZERO)>0){
                huanbi = m.getCost().subtract(costBefore).divide(costBefore,7, BigDecimal.ROUND_HALF_UP);
            }
            m.setHuanbi(huanbi);
        }

        return new PageInfo<>(mList);
    }


    //视频总报表列表
    @Override
    public PageInfo<ByteDanceVideoReportDaily> videoInfoListTotal(String startDate, String endDate, String signature, BigDecimal cost , Integer pageNo, Integer pageSize, Long[] projectIds, Long[] accountIds, String sort){
        LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        String userId = sysUser.getId();
        String roleCode = byteDanceVideoReportDailyMapper.getRoleCodeByUserId(userId);
        String companyId = byteDanceVideoReportDailyMapper.getCompanyIdByUserId(userId);

        if(StringUtils.isBlank(sort)){
            sort = "cost-";
        }
        sort = sort.replace("-", " desc,").replace("+", " asc,");  //sort=id-name+age+
        sort = sort.substring(0, sort.length() - 1);
        List<ByteDanceVideoReportDaily> mList = new ArrayList<>();

        List<Long> accountIdsList = null;
        if(accountIds != null && accountIds.length != 0){
            accountIdsList = Arrays.asList(accountIds);
        }
        //admin能看所有视频
        if(roleCode.equals(CtopRoleCodeConstant.COMMON_ROLE_CODE_ADMIN)){
            PageHelper.startPage(pageNo,pageSize,sort);
            mList = byteDanceVideoReportDailyMapper.videoInfoListGroupAdmin(startDate, endDate, accountIdsList, signature, cost);
        }else if("touTiaoOperationManager".equals(roleCode) || "operator".equals(roleCode) || "operationAssistant".equals(roleCode)|| "huananyunying".equals(roleCode)) {
            if (accountIdsList != null && !accountIdsList.isEmpty()){
                PageHelper.startPage(pageNo,pageSize,sort);
                mList = byteDanceVideoReportDailyMapper.videoInfoListGroupOperator(startDate,endDate,accountIdsList,signature,cost);
            }else{
                List<Long> accoutnIds= byteDanceVideoReportDailyMapper.getAccountIdsByOperatorIdAndMediaId(userId);
                if(accoutnIds == null || accoutnIds.isEmpty()){
                    return new PageInfo<>(mList);
                }
                PageHelper.startPage(pageNo,pageSize,sort);
                mList = byteDanceVideoReportDailyMapper.videoInfoListGroupOperator(startDate,endDate,accoutnIds,signature,cost);
            }
        }else if("shot".equals(roleCode)){
            PageHelper.startPage(pageNo,pageSize,sort);
            mList = byteDanceVideoReportDailyMapper.videoInfoListGroupShot(startDate, endDate, companyId, userId, accountIdsList, signature, cost);
        }else if("plan".equals(roleCode)){
            PageHelper.startPage(pageNo,pageSize,sort);
            mList = byteDanceVideoReportDailyMapper.videoInfoListGroupPlan(startDate, endDate, companyId, userId, accountIdsList, signature, cost);
        }else if("plane".equals(roleCode)  || "planeLeader".equals(roleCode)){
            PageHelper.startPage(pageNo,pageSize,sort);
            mList = byteDanceVideoReportDailyMapper.videoInfoListGroupPlaneAndPlaneLeader(startDate, endDate, companyId, userId, accountIdsList, signature, cost);
        }else if("clip".equals(roleCode)){
            PageHelper.startPage(pageNo,pageSize,sort);
            mList = byteDanceVideoReportDailyMapper.videoInfoListGroupClip(startDate, endDate, companyId, userId, accountIdsList, signature, cost);
        }else if("designTeamLeader".equals(roleCode)){
            PageHelper.startPage(pageNo,pageSize,sort);
            mList = byteDanceVideoReportDailyMapper.videoInfoListGroupdesignTeamLeader(startDate, endDate, companyId, accountIdsList, signature, cost);
        }else {
            if (accountIdsList != null && !accountIdsList.isEmpty()) {
                PageHelper.startPage(pageNo,pageSize,sort);
                mList = byteDanceVideoReportDailyMapper.videoInfoListGroupOtherRoles(startDate,endDate,accountIdsList,signature,cost);
            }else{
                List<Integer> mediaIds = new ArrayList<>();
                mediaIds.add(1);
                mediaIds.add(3);
                accountIdsList = byteDanceVideoReportDailyMapper.getAccountIdsByUserIdAndMediaIds(userId,mediaIds);
                if(accountIdsList == null || accountIdsList.isEmpty()){
                    return new PageInfo<>(mList);
                }
                PageHelper.startPage(pageNo,pageSize,sort);
                mList = byteDanceVideoReportDailyMapper.videoInfoListGroupOtherRoles(startDate,endDate,accountIdsList,signature,cost);
            }
        }

        //计算环比
        for(ByteDanceVideoReportDaily m:mList){
            Integer datediff = DateUtils.dateDiff(startDate, endDate);
            String startMonthDate = DateUtils.formatDate(DateUtils.addDay(startDate,-(datediff + 1) ) );
            String endMonthDate = DateUtils.formatDate(DateUtils.addDay(startDate,-1));

            BigDecimal costBefore = byteDanceVideoReportDailyMapper.getVideoReportDailyByDateAndSignature(startMonthDate,endMonthDate,m.getSignature(),accountIdsList);

            BigDecimal huanbi =BigDecimal.ZERO;
            if(costBefore != null && costBefore.compareTo(BigDecimal.ZERO)>0){
                huanbi = m.getCost().subtract(costBefore).divide(costBefore,7, BigDecimal.ROUND_HALF_UP);
            }

            m.setHuanbi(huanbi);
        }
        return new PageInfo<>(mList);
    }

    //视频总报表列表导出excel
    @Override
    public List<ByteDanceVideoReportDaily> videoInfoListTotalExcel(String startDate, String endDate, String signature, BigDecimal cost , Long[] accountIds, String sort){
        LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        String userId = sysUser.getId();
        String roleCode = byteDanceVideoReportDailyMapper.getRoleCodeByUserId(userId);
        String companyId = byteDanceVideoReportDailyMapper.getCompanyIdByUserId(userId);

        //排序
        if(StringUtils.isBlank(sort)){
            sort = "cost-";
        }
        sort = sort.replace("-", " desc,").replace("+", " asc,");  //sort=id-name+age+
        sort = sort.substring(0, sort.length() - 1);
        //

        List<ByteDanceVideoReportDaily> mList = new ArrayList<>();

        List<Long> accountIdsList = null;
        if(accountIds != null && accountIds.length != 0){
            accountIdsList = Arrays.asList(accountIds);
        }

        //admin能看所有视频
        if(roleCode.equals(CtopRoleCodeConstant.COMMON_ROLE_CODE_ADMIN)){
            PageHelper.orderBy(sort);
            mList = byteDanceVideoReportDailyMapper.videoInfoListGroupAdmin(startDate, endDate, accountIdsList, signature, cost);
        }else if("touTiaoOperationManager".equals(roleCode) || "operator".equals(roleCode) || "operationAssistant".equals(roleCode)|| "huananyunying".equals(roleCode)) {
            if (accountIdsList != null && !accountIdsList.isEmpty()){
                PageHelper.orderBy(sort);
                mList = byteDanceVideoReportDailyMapper.videoInfoListGroupOperator(startDate,endDate,accountIdsList,signature,cost);
            }else{
                List<Long> accoutnIds= byteDanceVideoReportDailyMapper.getAccountIdsByOperatorIdAndMediaId(userId);
                if(accoutnIds == null || accoutnIds.isEmpty()){
                    return mList;
                }
                PageHelper.orderBy(sort);
                mList = byteDanceVideoReportDailyMapper.videoInfoListGroupOperator(startDate,endDate,accoutnIds,signature,cost);
            }
        }else if("shot".equals(roleCode)){
            PageHelper.orderBy(sort);
            mList = byteDanceVideoReportDailyMapper.videoInfoListGroupShot(startDate, endDate, companyId, userId, accountIdsList, signature, cost);
        }else if("plan".equals(roleCode)){
            PageHelper.orderBy(sort);
            mList = byteDanceVideoReportDailyMapper.videoInfoListGroupPlan(startDate, endDate, companyId, userId, accountIdsList, signature, cost);
        }else if("plane".equals(roleCode)  || "planeLeader".equals(roleCode)){
            PageHelper.orderBy(sort);
            mList = byteDanceVideoReportDailyMapper.videoInfoListGroupPlaneAndPlaneLeader(startDate, endDate, companyId, userId, accountIdsList, signature, cost);
        }else if("clip".equals(roleCode)){
            PageHelper.orderBy(sort);
            mList = byteDanceVideoReportDailyMapper.videoInfoListGroupClip(startDate, endDate, companyId, userId, accountIdsList, signature, cost);
        }else if("designTeamLeader".equals(roleCode)){
            PageHelper.orderBy(sort);
            mList = byteDanceVideoReportDailyMapper.videoInfoListGroupdesignTeamLeader(startDate, endDate, companyId, accountIdsList, signature, cost);
        }else {
            if (accountIdsList != null && !accountIdsList.isEmpty()) {
                mList = byteDanceVideoReportDailyMapper.videoInfoListGroupOtherRoles(startDate,endDate,accountIdsList,signature,cost);
            }else{
                List<Integer> mediaIds = new ArrayList<>();
                mediaIds.add(1);
                mediaIds.add(3);
                accountIdsList = byteDanceVideoReportDailyMapper.getAccountIdsByUserIdAndMediaIds(userId,mediaIds);
                if(accountIdsList == null || accountIdsList.isEmpty()){
                    return mList;
                }

                mList = byteDanceVideoReportDailyMapper.videoInfoListGroupOtherRoles(startDate,endDate,accountIdsList,signature,cost);
            }
        }
        return mList;
    }

    //视频报表周列表
    @Override
    public PageInfo<ByteDanceVideoReportDaily> videoInfoListWeek(String startDate, String endDate, String signature, BigDecimal cost, Integer pageNo, Integer pageSize, Long[] projectIds, Long[] accountIds, String sort){
        LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        String userId = sysUser.getId();
        String roleCode = byteDanceVideoReportDailyMapper.getRoleCodeByUserId(userId);
        String companyId = byteDanceVideoReportDailyMapper.getCompanyIdByUserId(userId);

        if(StringUtils.isBlank(sort)){
            sort = "cost-";
        }
        sort = sort.replace("-", " desc,").replace("+", " asc,");  //sort=id-name+age+
        sort = sort.substring(0, sort.length() - 1);
        List<ByteDanceVideoReportDaily> mList = new ArrayList<>();

        List<Long> accountIdsList = null;
        if(accountIds != null && accountIds.length != 0){
            accountIdsList = Arrays.asList(accountIds);
        }
        //admin能看所有视频
        if(roleCode.equals(CtopRoleCodeConstant.COMMON_ROLE_CODE_ADMIN)){
            PageHelper.startPage(pageNo,pageSize,sort);
            mList = byteDanceVideoReportDailyMapper.videoInfoListGroupAdmin(startDate, endDate, accountIdsList, signature, cost);
        }else if("touTiaoOperationManager".equals(roleCode) || "operator".equals(roleCode) || "operationAssistant".equals(roleCode)|| "huananyunying".equals(roleCode)) {
            if (accountIdsList != null && !accountIdsList.isEmpty()){
                PageHelper.startPage(pageNo,pageSize,sort);
                mList = byteDanceVideoReportDailyMapper.videoInfoListGroupOperator(startDate,endDate,accountIdsList,signature,cost);
            }else{
                List<Long> accoutnIds= byteDanceVideoReportDailyMapper.getAccountIdsByOperatorIdAndMediaId(userId);
                if(accoutnIds == null || accoutnIds.isEmpty()){
                    return new PageInfo<>(mList);
                }
                PageHelper.startPage(pageNo,pageSize,sort);
                mList = byteDanceVideoReportDailyMapper.videoInfoListGroupOperator(startDate,endDate,accoutnIds,signature,cost);
            }
        }else if("shot".equals(roleCode)){
            PageHelper.startPage(pageNo,pageSize,sort);
            mList = byteDanceVideoReportDailyMapper.videoInfoListGroupShot(startDate, endDate, companyId, userId, accountIdsList, signature, cost);
        }else if("plan".equals(roleCode)){
            PageHelper.startPage(pageNo,pageSize,sort);
            mList = byteDanceVideoReportDailyMapper.videoInfoListGroupPlan(startDate, endDate, companyId, userId, accountIdsList, signature, cost);
        }else if("plane".equals(roleCode)  || "planeLeader".equals(roleCode)){
            PageHelper.startPage(pageNo,pageSize,sort);
            mList = byteDanceVideoReportDailyMapper.videoInfoListGroupPlaneAndPlaneLeader(startDate, endDate, companyId, userId, accountIdsList, signature, cost);
        }else if("clip".equals(roleCode)){
            PageHelper.startPage(pageNo,pageSize,sort);
            mList = byteDanceVideoReportDailyMapper.videoInfoListGroupClip(startDate, endDate, companyId, userId, accountIdsList, signature, cost);
        }else if("designTeamLeader".equals(roleCode)){
            PageHelper.startPage(pageNo,pageSize,sort);
            mList = byteDanceVideoReportDailyMapper.videoInfoListGroupdesignTeamLeader(startDate, endDate, companyId, accountIdsList, signature, cost);
        }else{
            if (accountIdsList != null && !accountIdsList.isEmpty()) {
                PageHelper.startPage(pageNo,pageSize,sort);
                mList = byteDanceVideoReportDailyMapper.videoInfoListGroupOtherRoles(startDate,endDate,accountIdsList,signature,cost);
            }else{
                List<Integer> mediaIds = new ArrayList<>();
                mediaIds.add(1);
                mediaIds.add(3);
                accountIdsList = byteDanceVideoReportDailyMapper.getAccountIdsByUserIdAndMediaIds(userId,mediaIds);
                if(accountIdsList == null || accountIdsList.isEmpty()){
                    return new PageInfo<>(mList);
                }
                PageHelper.startPage(pageNo,pageSize,sort);
                mList = byteDanceVideoReportDailyMapper.videoInfoListGroupOtherRoles(startDate,endDate,accountIdsList,signature,cost);
            }
        }

        //计算环比
        for(ByteDanceVideoReportDaily m:mList){
            Integer datediff = DateUtils.dateDiff(startDate, endDate);
            String startMonthDate = DateUtils.formatDate(DateUtils.addDay(startDate,-(datediff + 1) ) );
            String endMonthDate = DateUtils.formatDate(DateUtils.addDay(startDate,-1));

            BigDecimal costBefore = byteDanceVideoReportDailyMapper.getVideoReportDailyByDateAndSignature(startMonthDate,endMonthDate,m.getSignature(),accountIdsList);

            BigDecimal huanbi =BigDecimal.ZERO;
            if(costBefore != null && costBefore.compareTo(BigDecimal.ZERO)>0){
                huanbi = m.getCost().subtract(costBefore).divide(costBefore,7, BigDecimal.ROUND_HALF_UP);
            }

            m.setHuanbi(huanbi);
        }

        return new PageInfo<>(mList);
    }

    @Override
    public List<ByteDanceVideoReportDaily> videoInfoListDetail(String startDate, String endDate, String signature, Long[] accountIds){
        LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        String userId = sysUser.getId();
        String roleCode = byteDanceVideoReportDailyMapper.getRoleCodeByUserId(userId);
        String companyId = byteDanceVideoReportDailyMapper.getCompanyIdByUserId(userId);

        List<ByteDanceVideoReportDaily> mList =new ArrayList<>();

        List<Long> accountIdsList = null;
        if(accountIds != null && accountIds.length != 0){
            accountIdsList = Arrays.asList(accountIds);
        }
        //admin能看所有视频
        if(roleCode.equals(CtopRoleCodeConstant.COMMON_ROLE_CODE_ADMIN)){
            mList = byteDanceVideoReportDailyMapper.videoInfoListDetail(companyId, startDate, endDate, signature, accountIdsList);
        }else if("touTiaoOperationManager".equals(roleCode) || "operator".equals(roleCode) || "operationAssistant".equals(roleCode)|| "huananyunying".equals(roleCode)) {
            if (accountIdsList != null && !accountIdsList.isEmpty()){
                mList = byteDanceVideoReportDailyMapper.videoInfoListDetailOperator(companyId, startDate,endDate,signature, accountIdsList);
            }else{
                accountIdsList= byteDanceVideoReportDailyMapper.getAccountIdsByOperatorIdAndMediaId(userId);
                mList = byteDanceVideoReportDailyMapper.videoInfoListDetailOperator(companyId, startDate,endDate,signature, accountIdsList);
            }
        }else if("shot".equals(roleCode) || "plan".equals(roleCode) || "plane".equals(roleCode)  || "planeLeader".equals(roleCode) || "clip".equals(roleCode) || "designTeamLeader".equals(roleCode)){
            mList = byteDanceVideoReportDailyMapper.videoInfoListDetailDesign(companyId, startDate, endDate, signature, accountIdsList);
        }else if("designTeamLeader".equals(roleCode)){
            accountIdsList = byteDanceVideoReportDailyMapper.getAccountIds(companyId, signature);
            mList = byteDanceVideoReportDailyMapper.videoInfoListDetailDesign(companyId, startDate,endDate,signature, accountIdsList);
        }else {
            if (accountIdsList != null && !accountIdsList.isEmpty()) {
                mList = byteDanceVideoReportDailyMapper.videoInfoListDetailOtherRoles(companyId, startDate, endDate, signature, accountIdsList);
            }else{
                List<Integer> mediaIds = new ArrayList<>();
                mediaIds.add(1);
                mediaIds.add(3);
                accountIdsList = byteDanceVideoReportDailyMapper.getAccountIdsByUserIdAndMediaIds(userId,mediaIds);
                if(accountIdsList == null || accountIdsList.isEmpty()){
                    return mList;
                }
                mList = byteDanceVideoReportDailyMapper.videoInfoListDetailOtherRoles(companyId, startDate, endDate, signature, accountIdsList);
            }
        }

        //计算环比
        for(ByteDanceVideoReportDaily m:mList){
            String beforeDate = DateUtils.formatDate(DateUtils.addDay(m.getStatDatetime(),-1));
            BigDecimal costBefore = byteDanceVideoReportDailyMapper.getVideoReportDailyByDateAndSignature(beforeDate,beforeDate,m.getSignature(),accountIdsList);

            BigDecimal huanbi =BigDecimal.ZERO;
            if(costBefore != null && costBefore.compareTo(BigDecimal.ZERO)>0){
                huanbi = m.getCost().subtract(costBefore).divide(costBefore,7, BigDecimal.ROUND_HALF_UP);
            }

            m.setHuanbi(huanbi);
        }

        return mList;
    }

    @Override
    public String getColumnJson(Integer columnType){
        LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        String userId = sysUser.getId();
        return byteDanceVideoReportDailyMapper.getJsonByUserIdAndColumnType(columnType, userId);
    }

    @Override
    public void saveOrUpdateColumnJson(Integer columnType, String json){
        LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        String userId = sysUser.getId();

        String jsonObject = byteDanceVideoReportDailyMapper.getJsonByUserIdAndColumnType(columnType, userId);
        if(jsonObject == null){
            byteDanceVideoReportDailyMapper.insertColumnLoad(json,userId, columnType);
        }else{
            byteDanceVideoReportDailyMapper.updateJsonByUserIdAndType(json,userId,columnType);
        }
    }

    @Override
    public int videoInfoListTotalExportExcelPermission(){
        LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        String userId = sysUser.getId();
        String roleCode = byteDanceVideoReportDailyMapper.getRoleCodeByUserId(userId);

        //拥有头条视频报表权限的是 管理员-可看所有公司视频  设计组长-可看所在公司所有视频  设计-可以自己所在项目的所有视频
        if(roleCode.equals(CtopRoleCodeConstant.COMMON_ROLE_CODE_ADMIN)
                || "designTeamLeader".equals(roleCode) || "plan".equals(roleCode) ||
                "plane".equals(roleCode) || "clip".equals(roleCode) || "shot".equals(roleCode) || "planeLeader".equals(roleCode)
        ){
            return 1;
        }else{
            return 0;
        }
    }

    @Override
    public void accountVideoMap(){
        List<AccountVideoDTO> accountVideos = byteDanceVideoReportDailyMapper.accountVideoMap();
        byteDanceVideoReportDailyMapper.replaceIntoAccountVideoMapBatch(accountVideos);
    }

}
