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

import cn.com.ctop.common.module.vo.ByteDanceReportAccountDailyDTOEntity;
import cn.com.ctop.common.module.vo.KuaishouReportAccountDailyDTOEntity;
import cn.com.ctop.common.module.vo.SheetInfoVo;
import cn.com.ctop.kuaishou.modules.report.entity.KuaishouReportDailyAccount;
import cn.com.ctop.kuaishou.modules.report.entity.KuaishouReportDailyAccountDTO;
import cn.com.ctop.kuaishou.modules.report.entity.ProjectAccountDTO;
import cn.com.ctop.kuaishou.modules.report.mapper.KuaishouReportDailyAccountMapper;
import cn.com.ctop.kuaishou.modules.report.service.IKuaishouReportDailyAccountService;
import com.alibaba.fastjson.JSONObject;
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.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class KuaishouReportDailyAccountServiceImpl extends ServiceImpl<KuaishouReportDailyAccountMapper, KuaishouReportDailyAccount> implements IKuaishouReportDailyAccountService {

    @Autowired
    private KuaishouReportDailyAccountMapper dailyAccountMapper;

    @Override
    public List<JSONObject> selectCost(Long accountId, String startDate, String endDate) {
        return dailyAccountMapper.selectCost(accountId, startDate, endDate);
    }

    //获取快手参与的项目的信息
    @Override
    public PageInfo<ProjectAccountDTO> getKuaishouProjectInfo(String startDate, String endDate, List<Long> projectList, Integer pageSize, Integer pageNum, String sort ,Integer yn){
        if(projectList==null || projectList.size()==0){
            LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
            String userId = sysUser.getId();
            String roleCode = dailyAccountMapper.getRoleCodeByUserId(userId);

            List<Long> mediaIds = new ArrayList<>();
            mediaIds.add(2L);
            mediaIds.add(4L);

            if("admin".equals(roleCode)){
                projectList = dailyAccountMapper.queryProjectMemberByAdmins(mediaIds);
            }else{
                projectList = dailyAccountMapper.queryProjectMemberByUserId(userId,mediaIds);
            }
        }

        PageHelper.startPage(pageNum,pageSize);
        return new PageInfo<>(dailyAccountMapper.getKuaishouProjectInfo(startDate, endDate, projectList, yn));
    }

    @Override
    public PageInfo<ProjectAccountDTO> getKuaishouSaleProjectInfo(String startDate, String endDate, List<Long> projectList, Integer pageSize, Integer pageNum, String sort,Integer yn ){
        if(projectList==null || projectList.size()==0){
            LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
            String userId = sysUser.getId();
            String roleCode = dailyAccountMapper.getRoleCodeByUserId(userId);

            List<Long> mediaIds = new ArrayList<>();
            mediaIds.add(2L);
            mediaIds.add(4L);

            if("admin".equals(roleCode)){
                projectList = dailyAccountMapper.queryProjectMemberByAdmins(mediaIds);
            }else{
                projectList = dailyAccountMapper.queryProjectInfoBySaleId(userId,mediaIds);
            }
        }

        PageHelper.startPage(pageNum,pageSize);
        return new PageInfo<>(dailyAccountMapper.getKuaishouProjectInfo(startDate,endDate,projectList,yn));
    }

    @Override
    public PageInfo<KuaishouReportDailyAccountDTO> getKuaishouSaleAccountInfoByProjectId(List<Long> projectList, Long projectId, String startDate, String endDate, Integer pageSize, Integer pageNum, Integer yn){
        LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        String userId = sysUser.getId();
        String roleCode = dailyAccountMapper.getRoleCodeByUserId(userId);

        List<Long> accountIds = null;
        if(projectList == null && projectId==null){
            if("admin".equals(roleCode)){
                accountIds = dailyAccountMapper.getUserProjectAccountIdsByAdmin(0);
            }else {
                accountIds = dailyAccountMapper.getSaleProjectAccountIds(userId,0);
            }
        }

        PageHelper.startPage(pageNum,pageSize);
        return new PageInfo<>(dailyAccountMapper.getKuaishouAccountInfoByProjectId(projectId, startDate, endDate, projectList, accountIds, yn));
    }


    @Override
    public PageInfo<KuaishouReportDailyAccountDTO> getKuaishouAccountInfoByProjectId(Long projectId, List<Long> projectList, String startDate, String endDate, Integer pageSize, Integer pageNum, Integer yn){
        LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        String userId = sysUser.getId();
        String roleCode = dailyAccountMapper.getRoleCodeByUserId(userId);
        List<Long> accountIds = null;
        if(projectId==null && projectList==null){
            if("admin".equals(roleCode)){
                accountIds = dailyAccountMapper.getUserProjectAccountIdsByAdmin(0);
            }else {
                accountIds = dailyAccountMapper.getUserProjectAccountIds(userId,0);
            }
        }

        PageHelper.startPage(pageNum,pageSize);
        if(null == projectList||projectList.isEmpty()){
            projectList = null;
        }
        return new PageInfo<>(dailyAccountMapper.getKuaishouAccountInfoByProjectId(projectId, startDate, endDate, projectList, accountIds, yn));
    }

    @Override
    public PageInfo<KuaishouReportDailyAccountDTO> getKuaishouCampaignInfoByAccountId(List<Long> projectList, Long accountId, String startDate, String endDate, Integer pageSize, Integer pageNum, Integer yn){
        LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        String userId = sysUser.getId();
        String roleCode = dailyAccountMapper.getRoleCodeByUserId(userId);

        List<Long> accountIds = null;
        if(accountId == null && projectList==null){
            if("admin".equals(roleCode)){
                accountIds = dailyAccountMapper.getUserProjectAccountIdsByAdmin(1);
            }else {
                accountIds = dailyAccountMapper.getUserProjectAccountIds(userId,1);
            }
        }
        if(null == projectList||projectList.isEmpty()){
            projectList = null;
        }
        PageHelper.startPage(pageNum,pageSize);
        return new PageInfo<>(dailyAccountMapper.getKuaishouCampaignInfoByAccountId(projectList, startDate, endDate, accountId,accountIds,yn));
    }

    @Override
    public PageInfo<KuaishouReportDailyAccountDTO> getKuaishouUnitInfoByCampaignId(List<Long> projectList, Long campaignId, String startDate, String endDate, Integer pageSize, Integer pageNum, Integer yn){
        LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        String userId = sysUser.getId();
        String roleCode = dailyAccountMapper.getRoleCodeByUserId(userId);

        List<Long> accountIds = null;
        if(campaignId == null && projectList == null){
            if("admin".equals(roleCode)){
                accountIds = dailyAccountMapper.getUserProjectAccountIdsByAdmin(1);
            }else {
                accountIds = dailyAccountMapper.getUserProjectAccountIds(userId,1);
            }
        }
        if(null == projectList||projectList.isEmpty()){
            projectList = null;
        }
        PageHelper.startPage(pageNum,pageSize);
        return new PageInfo<>(dailyAccountMapper.getKuaishouUnitInfoByCampaignId(projectList, startDate, endDate, campaignId,accountIds, yn));
    }

    @Override
    public PageInfo<KuaishouReportDailyAccountDTO> getKuaishouCreativeInfoByUnitId(List<Long> projectList, Long unitId, String startDate, String endDate, Integer pageSize, Integer pageNum, Integer yn){
        LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        String userId = sysUser.getId();
        String roleCode = dailyAccountMapper.getRoleCodeByUserId(userId);

        List<Long> accountIds = null;
        if(unitId == null && projectList ==null){
            if("admin".equals(roleCode)){
                accountIds = dailyAccountMapper.getUserProjectAccountIdsByAdmin(1);
            }else {
                accountIds = dailyAccountMapper.getUserProjectAccountIds(userId,1);
            }
        }
        if(null == projectList||projectList.isEmpty()){
            projectList = null;
        }
        PageHelper.startPage(pageNum,pageSize);
        List<KuaishouReportDailyAccountDTO> kuaishouCreativeInfoList = dailyAccountMapper.getKuaishouCreativeInfoByUnitId(projectList, startDate, endDate, unitId,accountIds, yn);
        return new PageInfo<>(kuaishouCreativeInfoList);
    }

    @Override
    public SheetInfoVo getKuaishouSaleProjectInfoSheetVO(String startDate, String endDate, List<Long> projectList, int pageNum, int pageSize, String s, Integer yn) {
        SheetInfoVo vo = new SheetInfoVo<ByteDanceReportAccountDailyDTOEntity>();
        if(projectList==null || projectList.size()==0){
            LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
            String userId = sysUser.getId();
            String roleCode = dailyAccountMapper.getRoleCodeByUserId(userId);

            List<Long> mediaIds = new ArrayList<>();
            mediaIds.add(2L);
            mediaIds.add(4L);

            if("admin".equals(roleCode)){
                projectList = dailyAccountMapper.queryProjectMemberByAdmins(mediaIds);
            }else{
                projectList = dailyAccountMapper.queryProjectInfoBySaleId(userId,mediaIds);
            }
        }

        List<KuaishouReportAccountDailyDTOEntity> dtos = dailyAccountMapper.getKuaishouProjectInfoEntity(startDate,endDate,projectList,yn);
        vo.setSheetName("快手销售报表");
        vo.setDetails(dtos);
        return vo;
    }

}
