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

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 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.text.ParseException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

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

    @Autowired
    private ByteDanceVideoReportDailyMapper byteDanceVideoReportDailyMapper;

    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);
        for (ByteDanceVideoReportDaily m:mList){
            byteDanceVideoReportDailyMapper.insert(m);
        }

        //QueryWrapper<ByteDanceVideoReportDaily> queryWrapper = new QueryWrapper<>();

        List<ByteDanceVideoReportDaily> mList2 = byteDanceVideoReportDailyMapper.selectList(wrapper);

        for(ByteDanceVideoReportDaily m:mList2){
            String statDate  = DateUtils.formatDate(DateUtils.addDay(m.getStatDatetime(),-1));
            BigDecimal cost = byteDanceVideoReportDailyMapper.getVideoReportDailyByDateAndSignature(statDate,statDate,m.getSignature(),companyId);

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

            m.setHuanbi(huanbi);
            byteDanceVideoReportDailyMapper.updateById(m);
        }
    }

    //视频按天列表
    public PageInfo<ByteDanceVideoReportDaily> videoInfoListDaily(String startDate, String endDate, String signature, BigDecimal cost, Integer pageNo, Integer pageSize, Long[] projectIds, 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<>();
        //admin能看所有视频
        if(roleCode.equals("admin")){
            if (projectIds != null && projectIds.length != 0){
                List<Long> projectIdsList = Arrays.asList(projectIds);
                List<Long> accountIdsList = byteDanceVideoReportDailyMapper.getAccountIdByprojectIds(projectIdsList);
                if(accountIdsList.size()==0 || accountIdsList == null){
                    accountIdsList.add(-1L);
                }
                PageHelper.startPage(pageNo,pageSize);
                mList = byteDanceVideoReportDailyMapper.videoInfoListGroupAdmin(startDate, endDate, accountIdsList, signature, cost);
            }else{
                PageHelper.startPage(pageNo,pageSize);
                mList = byteDanceVideoReportDailyMapper.videoInfoListGroupAdmin(startDate, endDate, null, signature, cost);
            }
        }else if(roleCode.equals("touTiaoOperationManager") || roleCode.equals("operator") || roleCode.equals("operationAssistant")) {
            if (projectIds != null && projectIds.length != 0){
                List<Long> projectIdsList = Arrays.asList(projectIds);
                List<Long> accountIdsList = byteDanceVideoReportDailyMapper.getAccountIdByprojectIds(projectIdsList);
                if(accountIdsList.size()==0 || accountIdsList == null){
                    accountIdsList.add(-1L);
                }
                PageHelper.startPage(pageNo,pageSize);
                mList = byteDanceVideoReportDailyMapper.videoInfoListGroupOperator(startDate,endDate,accountIdsList,signature,cost);
            }else{
                List<Long> accoutnIds= byteDanceVideoReportDailyMapper.getAccountIdsByOperatorIdAndMediaId(1,userId);
                PageHelper.startPage(pageNo,pageSize);
                mList = byteDanceVideoReportDailyMapper.videoInfoListGroupOperator(startDate,endDate,accoutnIds,signature,cost);
            }
        }else if(roleCode.equals("shot")){
            //设计师传入projectIds，根据projectIds解析出accountIdsList
            if (projectIds != null && projectIds.length != 0){
                List<Long> projectIdsList = Arrays.asList(projectIds);
                List<Long> accountIdsList = byteDanceVideoReportDailyMapper.getAccountIdByprojectIds(projectIdsList);
                if(accountIdsList.size() != 0){
                    if(accountIdsList.size()==0 || accountIdsList == null){
                        accountIdsList.add(-1L);
                    }
                    PageHelper.startPage(pageNo,pageSize);
                    mList = byteDanceVideoReportDailyMapper.videoInfoListGroupShot(startDate, endDate, companyId, userId, accountIdsList, signature, cost);
                }
            }else{
                PageHelper.startPage(pageNo,pageSize);
                mList = byteDanceVideoReportDailyMapper.videoInfoListGroupShot(startDate, endDate, companyId, userId, null, signature, cost);
            }
        }else if(roleCode.equals("plan")){
            if (projectIds != null && projectIds.length != 0){
                List<Long> projectIdsList = Arrays.asList(projectIds);
                List<Long> accountIdsList = byteDanceVideoReportDailyMapper.getAccountIdByprojectIds(projectIdsList);
                if(accountIdsList.size()==0 || accountIdsList == null){
                    accountIdsList.add(-1L);
                }
                if(accountIdsList.size() != 0){
                    PageHelper.startPage(pageNo,pageSize);
                    mList = byteDanceVideoReportDailyMapper.videoInfoListGroupPlan(startDate, endDate, companyId, userId, accountIdsList, signature, cost);
                }
            }else{
                PageHelper.startPage(pageNo,pageSize);
                mList = byteDanceVideoReportDailyMapper.videoInfoListGroupPlan(startDate, endDate, companyId, userId, null, signature, cost);
            }
        }else if(roleCode.equals("plane")){
            if (projectIds != null && projectIds.length != 0){
                List<Long> projectIdsList = Arrays.asList(projectIds);
                List<Long> accountIdsList = byteDanceVideoReportDailyMapper.getAccountIdByprojectIds(projectIdsList);
                if(accountIdsList.size()==0 || accountIdsList == null){
                    accountIdsList.add(-1L);
                }
                if(accountIdsList.size() != 0){
                    PageHelper.startPage(pageNo,pageSize);
                    mList = byteDanceVideoReportDailyMapper.videoInfoListGroupPlaneAndPlaneLeader(startDate, endDate, companyId, userId, accountIdsList, signature, cost);
                }
            }else{
                PageHelper.startPage(pageNo,pageSize);
                mList = byteDanceVideoReportDailyMapper.videoInfoListGroupPlaneAndPlaneLeader(startDate, endDate, companyId, userId, null, signature, cost);
            }
        }else if(roleCode.equals("clip")){
            if (projectIds != null && projectIds.length != 0){
                List<Long> projectIdsList = Arrays.asList(projectIds);
                List<Long> accountIdsList = byteDanceVideoReportDailyMapper.getAccountIdByprojectIds(projectIdsList);
                if(accountIdsList.size()==0 || accountIdsList == null){
                    accountIdsList.add(-1L);
                }
                if(accountIdsList.size() != 0){
                    PageHelper.startPage(pageNo,pageSize);
                    mList = byteDanceVideoReportDailyMapper.videoInfoListGroupClip(startDate, endDate, companyId, userId, accountIdsList, signature, cost);
                }
            }else{
                PageHelper.startPage(pageNo,pageSize);
                mList = byteDanceVideoReportDailyMapper.videoInfoListGroupClip(startDate, endDate, companyId, userId, null, signature, cost);
            }
        }else if(roleCode.equals("designTeamLeader")){
            if (projectIds != null && projectIds.length != 0){
                List<Long> projectIdsList = Arrays.asList(projectIds);
                List<Long> accountIdsList = byteDanceVideoReportDailyMapper.getAccountIdByprojectIds(projectIdsList);
                if(accountIdsList.size()==0 || accountIdsList == null){
                    accountIdsList.add(-1L);
                }
                if(accountIdsList.size() != 0){
                    PageHelper.startPage(pageNo,pageSize);
                    mList = byteDanceVideoReportDailyMapper.videoInfoListGroupdesignTeamLeader(startDate, endDate, companyId, accountIdsList, signature, cost);
                }
            }else{
                PageHelper.startPage(pageNo,pageSize);
                mList = byteDanceVideoReportDailyMapper.videoInfoListGroupdesignTeamLeader(startDate, endDate, companyId, null, signature, cost);
            }
        }

        PageInfo<ByteDanceVideoReportDaily> pageInfo = new PageInfo<>(mList);
        return pageInfo;
    }


    //视频报表月列表
    public PageInfo<ByteDanceVideoReportDaily> videoInfoListMonth(String startDate, String endDate, String signature, BigDecimal cost, Integer pageNo, Integer pageSize, Long[] projectIds, 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<>();
        //admin能看所有视频
        if(roleCode.equals("admin")){
            if (projectIds != null && projectIds.length != 0){
                List<Long> projectIdsList = Arrays.asList(projectIds);
                List<Long> accountIdsList = byteDanceVideoReportDailyMapper.getAccountIdByprojectIds(projectIdsList);
                if(accountIdsList.size()==0 || accountIdsList == null){
                    accountIdsList.add(-1L);
                }
                PageHelper.startPage(pageNo,pageSize);
                mList = byteDanceVideoReportDailyMapper.videoInfoListGroupAdmin(startDate, endDate, accountIdsList, signature, cost);
            }else{
                PageHelper.startPage(pageNo,pageSize);
                mList = byteDanceVideoReportDailyMapper.videoInfoListGroupAdmin(startDate, endDate, null, signature, cost);
            }
        }else if(roleCode.equals("touTiaoOperationManager") || roleCode.equals("operator") || roleCode.equals("operationAssistant")) {
            if (projectIds != null && projectIds.length != 0){
                List<Long> projectIdsList = Arrays.asList(projectIds);
                List<Long> accountIdsList = byteDanceVideoReportDailyMapper.getAccountIdByprojectIds(projectIdsList);
                if(accountIdsList.size()==0 || accountIdsList == null){
                    accountIdsList.add(-1L);
                }
                PageHelper.startPage(pageNo,pageSize);
                mList = byteDanceVideoReportDailyMapper.videoInfoListGroupOperator(startDate,endDate,accountIdsList,signature,cost);
            }else{
                List<Long> accoutnIds= byteDanceVideoReportDailyMapper.getAccountIdsByOperatorIdAndMediaId(1,userId);
                PageHelper.startPage(pageNo,pageSize);
                mList = byteDanceVideoReportDailyMapper.videoInfoListGroupOperator(startDate,endDate,accoutnIds,signature,cost);
            }
        }else if(roleCode.equals("shot")){
            //设计师传入projectIds，根据projectIds解析出accountIdsList
            if (projectIds != null && projectIds.length != 0){
                List<Long> projectIdsList = Arrays.asList(projectIds);
                List<Long> accountIdsList = byteDanceVideoReportDailyMapper.getAccountIdByprojectIds(projectIdsList);
                if(accountIdsList.size() != 0){
                    if(accountIdsList.size()==0 || accountIdsList == null){
                        accountIdsList.add(-1L);
                    }
                    PageHelper.startPage(pageNo,pageSize);
                    mList = byteDanceVideoReportDailyMapper.videoInfoListGroupShot(startDate, endDate, companyId, userId, accountIdsList, signature, cost);
                }
            }else{
                PageHelper.startPage(pageNo,pageSize);
                mList = byteDanceVideoReportDailyMapper.videoInfoListGroupShot(startDate, endDate, companyId, userId, null, signature, cost);
            }
        }else if(roleCode.equals("plan")){
            if (projectIds != null && projectIds.length != 0){
                List<Long> projectIdsList = Arrays.asList(projectIds);
                List<Long> accountIdsList = byteDanceVideoReportDailyMapper.getAccountIdByprojectIds(projectIdsList);
                if(accountIdsList.size()==0 || accountIdsList == null){
                    accountIdsList.add(-1L);
                }
                if(accountIdsList.size() != 0){
                    PageHelper.startPage(pageNo,pageSize);
                    mList = byteDanceVideoReportDailyMapper.videoInfoListGroupPlan(startDate, endDate, companyId, userId, accountIdsList, signature, cost);
                }
            }else{
                PageHelper.startPage(pageNo,pageSize);
                mList = byteDanceVideoReportDailyMapper.videoInfoListGroupPlan(startDate, endDate, companyId, userId, null, signature, cost);
            }
        }else if(roleCode.equals("plane")){
            if (projectIds != null && projectIds.length != 0){
                List<Long> projectIdsList = Arrays.asList(projectIds);
                List<Long> accountIdsList = byteDanceVideoReportDailyMapper.getAccountIdByprojectIds(projectIdsList);
                if(accountIdsList.size()==0 || accountIdsList == null){
                    accountIdsList.add(-1L);
                }
                if(accountIdsList.size() != 0){
                    PageHelper.startPage(pageNo,pageSize);
                    mList = byteDanceVideoReportDailyMapper.videoInfoListGroupPlaneAndPlaneLeader(startDate, endDate, companyId, userId, accountIdsList, signature, cost);
                }
            }else{
                PageHelper.startPage(pageNo,pageSize);
                mList = byteDanceVideoReportDailyMapper.videoInfoListGroupPlaneAndPlaneLeader(startDate, endDate, companyId, userId, null, signature, cost);
            }
        }else if(roleCode.equals("clip")){
            if (projectIds != null && projectIds.length != 0){
                List<Long> projectIdsList = Arrays.asList(projectIds);
                List<Long> accountIdsList = byteDanceVideoReportDailyMapper.getAccountIdByprojectIds(projectIdsList);
                if(accountIdsList.size()==0 || accountIdsList == null){
                    accountIdsList.add(-1L);
                }
                if(accountIdsList.size() != 0){
                    PageHelper.startPage(pageNo,pageSize);
                    mList = byteDanceVideoReportDailyMapper.videoInfoListGroupClip(startDate, endDate, companyId, userId, accountIdsList, signature, cost);
                }
            }else{
                PageHelper.startPage(pageNo,pageSize);
                mList = byteDanceVideoReportDailyMapper.videoInfoListGroupClip(startDate, endDate, companyId, userId, null, signature, cost);
            }
        }else if(roleCode.equals("designTeamLeader")){
            if (projectIds != null && projectIds.length != 0){
                List<Long> projectIdsList = Arrays.asList(projectIds);
                List<Long> accountIdsList = byteDanceVideoReportDailyMapper.getAccountIdByprojectIds(projectIdsList);
                if(accountIdsList.size()==0 || accountIdsList == null){
                    accountIdsList.add(-1L);
                }
                if(accountIdsList.size() != 0){
                    PageHelper.startPage(pageNo,pageSize);
                    mList = byteDanceVideoReportDailyMapper.videoInfoListGroupdesignTeamLeader(startDate, endDate, companyId, accountIdsList, signature, cost);
                }
            }else{
                PageHelper.startPage(pageNo,pageSize);
                mList = byteDanceVideoReportDailyMapper.videoInfoListGroupdesignTeamLeader(startDate, endDate, companyId, null, signature, cost);
            }
        }

        //计算环比
        for(ByteDanceVideoReportDaily m:mList){
            String startMonthDate = null;
            String endMonthDate = null;
            try {
                startMonthDate = DateUtils.addMonth( DateUtils.formatDate(DateUtils.addDay(m.getStatDatetime(),1)) , -1);
                endMonthDate = DateUtils.formatDate(DateUtils.addDay(m.getStatDatetime(),-1));
            } catch (ParseException e) {
                e.printStackTrace();
            }

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

            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);
        }

        PageInfo<ByteDanceVideoReportDaily> pageInfo = new PageInfo<>(mList);
        return pageInfo;
    }


    //视频总报表列表
    public PageInfo<ByteDanceVideoReportDaily> videoInfoListTotal(String startDate, String endDate, String signature, BigDecimal cost , Integer pageNo, Integer pageSize, Long[] projectIds, 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<>();
        //admin能看所有视频
        if(roleCode.equals("admin")){
            if (projectIds != null && projectIds.length != 0){
                List<Long> projectIdsList = Arrays.asList(projectIds);
                List<Long> accountIdsList = byteDanceVideoReportDailyMapper.getAccountIdByprojectIds(projectIdsList);
                if(accountIdsList.size()==0 || accountIdsList == null){
                    accountIdsList.add(-1L);
                }
                PageHelper.startPage(pageNo,pageSize);
                mList = byteDanceVideoReportDailyMapper.videoInfoListGroupAdmin(startDate, endDate, accountIdsList, signature, cost);
            }else{
                PageHelper.startPage(pageNo,pageSize);
                mList = byteDanceVideoReportDailyMapper.videoInfoListGroupAdmin(startDate, endDate, null, signature, cost);
            }
        }else if(roleCode.equals("touTiaoOperationManager") || roleCode.equals("operator") || roleCode.equals("operationAssistant")) {
            if (projectIds != null && projectIds.length != 0){
                List<Long> projectIdsList = Arrays.asList(projectIds);
                List<Long> accountIdsList = byteDanceVideoReportDailyMapper.getAccountIdByprojectIds(projectIdsList);
                if(accountIdsList.size()==0 || accountIdsList == null){
                    accountIdsList.add(-1L);
                }
                PageHelper.startPage(pageNo,pageSize);
                mList = byteDanceVideoReportDailyMapper.videoInfoListGroupOperator(startDate,endDate,accountIdsList,signature,cost);
            }else{
                List<Long> accoutnIds= byteDanceVideoReportDailyMapper.getAccountIdsByOperatorIdAndMediaId(1,userId);
                PageHelper.startPage(pageNo,pageSize);
                mList = byteDanceVideoReportDailyMapper.videoInfoListGroupOperator(startDate,endDate,accoutnIds,signature,cost);
            }
        }else if(roleCode.equals("shot")){
            //设计师传入projectIds，根据projectIds解析出accountIdsList
            if (projectIds != null && projectIds.length != 0){
                List<Long> projectIdsList = Arrays.asList(projectIds);
                List<Long> accountIdsList = byteDanceVideoReportDailyMapper.getAccountIdByprojectIds(projectIdsList);
                if(accountIdsList.size() != 0){
                    if(accountIdsList.size()==0 || accountIdsList == null){
                        accountIdsList.add(-1L);
                    }
                    PageHelper.startPage(pageNo,pageSize);
                    mList = byteDanceVideoReportDailyMapper.videoInfoListGroupShot(startDate, endDate, companyId, userId, accountIdsList, signature, cost);
                }
            }else{
                PageHelper.startPage(pageNo,pageSize);
                mList = byteDanceVideoReportDailyMapper.videoInfoListGroupShot(startDate, endDate, companyId, userId, null, signature, cost);
            }
        }else if(roleCode.equals("plan")){
            if (projectIds != null && projectIds.length != 0){
                List<Long> projectIdsList = Arrays.asList(projectIds);
                List<Long> accountIdsList = byteDanceVideoReportDailyMapper.getAccountIdByprojectIds(projectIdsList);
                if(accountIdsList.size()==0 || accountIdsList == null){
                    accountIdsList.add(-1L);
                }
                if(accountIdsList.size() != 0){
                    PageHelper.startPage(pageNo,pageSize);
                    mList = byteDanceVideoReportDailyMapper.videoInfoListGroupPlan(startDate, endDate, companyId, userId, accountIdsList, signature, cost);
                }
            }else{
                PageHelper.startPage(pageNo,pageSize);
                mList = byteDanceVideoReportDailyMapper.videoInfoListGroupPlan(startDate, endDate, companyId, userId, null, signature, cost);
            }
        }else if(roleCode.equals("plane")){
            if (projectIds != null && projectIds.length != 0){
                List<Long> projectIdsList = Arrays.asList(projectIds);
                List<Long> accountIdsList = byteDanceVideoReportDailyMapper.getAccountIdByprojectIds(projectIdsList);
                if(accountIdsList.size()==0 || accountIdsList == null){
                    accountIdsList.add(-1L);
                }
                if(accountIdsList.size() != 0){
                    PageHelper.startPage(pageNo,pageSize);
                    mList = byteDanceVideoReportDailyMapper.videoInfoListGroupPlaneAndPlaneLeader(startDate, endDate, companyId, userId, accountIdsList, signature, cost);
                }
            }else{
                PageHelper.startPage(pageNo,pageSize);
                mList = byteDanceVideoReportDailyMapper.videoInfoListGroupPlaneAndPlaneLeader(startDate, endDate, companyId, userId, null, signature, cost);
            }
        }else if(roleCode.equals("clip")){
            if (projectIds != null && projectIds.length != 0){
                List<Long> projectIdsList = Arrays.asList(projectIds);
                List<Long> accountIdsList = byteDanceVideoReportDailyMapper.getAccountIdByprojectIds(projectIdsList);
                if(accountIdsList.size()==0 || accountIdsList == null){
                    accountIdsList.add(-1L);
                }
                if(accountIdsList.size() != 0){
                    PageHelper.startPage(pageNo,pageSize);
                    mList = byteDanceVideoReportDailyMapper.videoInfoListGroupClip(startDate, endDate, companyId, userId, accountIdsList, signature, cost);
                }
            }else{
                PageHelper.startPage(pageNo,pageSize);
                mList = byteDanceVideoReportDailyMapper.videoInfoListGroupClip(startDate, endDate, companyId, userId, null, signature, cost);
            }
        }else if(roleCode.equals("designTeamLeader")){
            if (projectIds != null && projectIds.length != 0){
                List<Long> projectIdsList = Arrays.asList(projectIds);
                List<Long> accountIdsList = byteDanceVideoReportDailyMapper.getAccountIdByprojectIds(projectIdsList);
                if(accountIdsList.size()==0 || accountIdsList == null){
                    accountIdsList.add(-1L);
                }
                if(accountIdsList.size() != 0){
                PageHelper.startPage(pageNo,pageSize);
                mList = byteDanceVideoReportDailyMapper.videoInfoListGroupdesignTeamLeader(startDate, endDate, companyId, accountIdsList, signature, cost);
                }
            }else{
                PageHelper.startPage(pageNo,pageSize);
                mList = byteDanceVideoReportDailyMapper.videoInfoListGroupdesignTeamLeader(startDate, endDate, companyId, null, signature, cost);
            }
        }

        PageInfo<ByteDanceVideoReportDaily> pageInfo = new PageInfo<>(mList);
        return pageInfo;
    }

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

        List<ByteDanceVideoReportDaily> mList = byteDanceVideoReportDailyMapper.videoInfoListWeek(startDate, endDate, roleCode, userId, signature, companyId, cost);

        return mList;
    }

    //视频报表周列表
    public PageInfo<ByteDanceVideoReportDaily> videoInfoListWeek(String startDate, String endDate, String signature, BigDecimal cost, Integer pageNo, Integer pageSize, Long[] projectIds, 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<>();
        //PageHelper.startPage(pageNo,pageSize);
        //mList = byteDanceVideoReportDailyMapper.videoInfoListWeek(startDate, endDate, roleCode, userId, signature, companyId, cost);

        //admin能看所有视频
        if(roleCode.equals("admin")){
            if (projectIds != null && projectIds.length != 0){
                List<Long> projectIdsList = Arrays.asList(projectIds);
                List<Long> accountIdsList = byteDanceVideoReportDailyMapper.getAccountIdByprojectIds(projectIdsList);
                if(accountIdsList.size()==0 || accountIdsList == null){
                    accountIdsList.add(-1L);
                }
                PageHelper.startPage(pageNo,pageSize);
                mList = byteDanceVideoReportDailyMapper.videoInfoListGroupAdmin(startDate, endDate, accountIdsList, signature, cost);
            }else{
                PageHelper.startPage(pageNo,pageSize);
                mList = byteDanceVideoReportDailyMapper.videoInfoListGroupAdmin(startDate, endDate, null, signature, cost);
            }
        }else if(roleCode.equals("touTiaoOperationManager") || roleCode.equals("operator") || roleCode.equals("operationAssistant")) {
            if (projectIds != null && projectIds.length != 0){
                List<Long> projectIdsList = Arrays.asList(projectIds);
                List<Long> accountIdsList = byteDanceVideoReportDailyMapper.getAccountIdByprojectIds(projectIdsList);
                if(accountIdsList.size()==0 || accountIdsList == null){
                    accountIdsList.add(-1L);
                }
                PageHelper.startPage(pageNo,pageSize);
                mList = byteDanceVideoReportDailyMapper.videoInfoListGroupOperator(startDate,endDate,accountIdsList,signature,cost);
            }else{
                List<Long> accoutnIds= byteDanceVideoReportDailyMapper.getAccountIdsByOperatorIdAndMediaId(1,userId);
                PageHelper.startPage(pageNo,pageSize);
                mList = byteDanceVideoReportDailyMapper.videoInfoListGroupOperator(startDate,endDate,accoutnIds,signature,cost);
            }
        }else if(roleCode.equals("shot")){
            //设计师传入projectIds，根据projectIds解析出accountIdsList
            if (projectIds != null && projectIds.length != 0){
                List<Long> projectIdsList = Arrays.asList(projectIds);
                List<Long> accountIdsList = byteDanceVideoReportDailyMapper.getAccountIdByprojectIds(projectIdsList);
                if(accountIdsList.size() != 0){
                    if(accountIdsList.size()==0 || accountIdsList == null){
                        accountIdsList.add(-1L);
                    }
                    PageHelper.startPage(pageNo,pageSize);
                    mList = byteDanceVideoReportDailyMapper.videoInfoListGroupShot(startDate, endDate, companyId, userId, accountIdsList, signature, cost);
                }
            }else{
                PageHelper.startPage(pageNo,pageSize);
                mList = byteDanceVideoReportDailyMapper.videoInfoListGroupShot(startDate, endDate, companyId, userId, null, signature, cost);
            }
        }else if(roleCode.equals("plan")){
            if (projectIds != null && projectIds.length != 0){
                List<Long> projectIdsList = Arrays.asList(projectIds);
                List<Long> accountIdsList = byteDanceVideoReportDailyMapper.getAccountIdByprojectIds(projectIdsList);
                if(accountIdsList.size()==0 || accountIdsList == null){
                    accountIdsList.add(-1L);
                }
                if(accountIdsList.size() != 0){
                    PageHelper.startPage(pageNo,pageSize);
                    mList = byteDanceVideoReportDailyMapper.videoInfoListGroupPlan(startDate, endDate, companyId, userId, accountIdsList, signature, cost);
                }
            }else{
                PageHelper.startPage(pageNo,pageSize);
                mList = byteDanceVideoReportDailyMapper.videoInfoListGroupPlan(startDate, endDate, companyId, userId, null, signature, cost);
            }
        }else if(roleCode.equals("plane")){
            if (projectIds != null && projectIds.length != 0){
                List<Long> projectIdsList = Arrays.asList(projectIds);
                List<Long> accountIdsList = byteDanceVideoReportDailyMapper.getAccountIdByprojectIds(projectIdsList);
                if(accountIdsList.size()==0 || accountIdsList == null){
                    accountIdsList.add(-1L);
                }
                if(accountIdsList.size() != 0){
                    PageHelper.startPage(pageNo,pageSize);
                    mList = byteDanceVideoReportDailyMapper.videoInfoListGroupPlaneAndPlaneLeader(startDate, endDate, companyId, userId, accountIdsList, signature, cost);
                }
            }else{
                PageHelper.startPage(pageNo,pageSize);
                mList = byteDanceVideoReportDailyMapper.videoInfoListGroupPlaneAndPlaneLeader(startDate, endDate, companyId, userId, null, signature, cost);
            }
        }else if(roleCode.equals("clip")){
            if (projectIds != null && projectIds.length != 0){
                List<Long> projectIdsList = Arrays.asList(projectIds);
                List<Long> accountIdsList = byteDanceVideoReportDailyMapper.getAccountIdByprojectIds(projectIdsList);
                if(accountIdsList.size()==0 || accountIdsList == null){
                    accountIdsList.add(-1L);
                }
                if(accountIdsList.size() != 0){
                    PageHelper.startPage(pageNo,pageSize);
                    mList = byteDanceVideoReportDailyMapper.videoInfoListGroupClip(startDate, endDate, companyId, userId, accountIdsList, signature, cost);
                }
            }else{
                PageHelper.startPage(pageNo,pageSize);
                mList = byteDanceVideoReportDailyMapper.videoInfoListGroupClip(startDate, endDate, companyId, userId, null, signature, cost);
            }
        }else if(roleCode.equals("designTeamLeader")){
            if (projectIds != null && projectIds.length != 0){
                List<Long> projectIdsList = Arrays.asList(projectIds);
                List<Long> accountIdsList = byteDanceVideoReportDailyMapper.getAccountIdByprojectIds(projectIdsList);
                if(accountIdsList.size()==0 || accountIdsList == null){
                    accountIdsList.add(-1L);
                }
                if(accountIdsList.size() != 0){
                    PageHelper.startPage(pageNo,pageSize);
                    mList = byteDanceVideoReportDailyMapper.videoInfoListGroupdesignTeamLeader(startDate, endDate, companyId, accountIdsList, signature, cost);
                }
            }else{
                PageHelper.startPage(pageNo,pageSize);
                mList = byteDanceVideoReportDailyMapper.videoInfoListGroupdesignTeamLeader(startDate, endDate, companyId, null, signature, cost);
            }
        }

        //计算环比
        for(ByteDanceVideoReportDaily m:mList){
            String startWeekDate  = DateUtils.formatDate(DateUtils.addDay(DateUtils.getDate(startDate),-1));
            String endWeekDate = DateUtils.formatDate(DateUtils.addDay(startDate,-7));
            BigDecimal costBefore = byteDanceVideoReportDailyMapper.getVideoReportDailyByDateAndSignature(startWeekDate,endWeekDate,m.getSignature(),companyId);

            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);
        }

        PageInfo<ByteDanceVideoReportDaily> pageInfo = new PageInfo<>(mList);

        return pageInfo;
    }

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

        return byteDanceVideoReportDailyMapper.videoInfoListDetail(companyId, startDate, endDate, signature, roleCode);
    }

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

    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);
        }
    }

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

        if(roleCode.equals("admin")  || roleCode.equals("designTeamLeader") ){
            return 1;
        }else{
            return 0;
        }
    }

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

}
