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


import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.util.DateUtils;
import org.jeecg.ctop.material.constants.Check;
import org.jeecg.ctop.material.entity.AccountTrendPojo;
import org.jeecg.ctop.material.entity.KuaishouUnitReportDailyPojo;
import org.jeecg.ctop.material.entity.KuaishouUnitReportHourlyPojo;
import org.jeecg.ctop.material.entity.vo.KuaiShouOperateProjectVo;
import org.jeecg.ctop.material.mapper.BossDataExhibitionMapper;
import org.jeecg.ctop.material.mapper.KuaishouOperationRecordMapper;
import org.jeecg.ctop.material.mapper.MaterialStareMapper;
import org.jeecg.ctop.material.service.IBossDataExhibitionService;
import org.jeecg.ctop.material.service.KuaishouOperationRecordService;
import org.jeecg.ctop.material.service.MaterialStareService;
import org.jeecg.ctop.material.utils.FeidNotNullUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;

@Slf4j
@Service
public class KuaishouOperationRecordServiceImpl implements KuaishouOperationRecordService {


    @Autowired
    private KuaishouOperationRecordMapper kuaishouOperationRecordMapper;


    @Resource
    MaterialStareMapper materialStareMapper;


    @Autowired
    MaterialStareService materialStareService;



    /**
     *
     * @description: 运营人员账户查询成功
     *
     * @param userId
     * @return: org.jeecg.common.api.vo.Result
     * @author: zianY
     */
    @Override
    public Result getAllSubordinateAccountByUserId(String userId) {
        Set<String> userIds = null;
        //查询用户角色
        String roleCode = materialStareMapper.getRoleCodeByUserId(userId);
        //角色 不为 管理员
        if (!"admin".equals(roleCode)) {
            //获取所有下级
            Result resultUserIds = materialStareService.getCompanyAllSubordinateByUserId(userId);
            if (resultUserIds.isSuccess()) {
                userIds = (Set<String>) resultUserIds.getResult();
            }
        }
        //根据运营 项目 查询账户
        List<JSONObject> jsonList = materialStareMapper.getAccountListByProjectListSubordinate(userIds,null);
        return Result.successMsg("运营人员账户查询成功",jsonList);
    }




    /**
     *
     * @description: 消耗前10的广告组
     *
     * @param userId
     * @param advertiserId
     * @param startTime
     * @param endTime
     * @return: org.jeecg.common.api.vo.Result<com.alibaba.fastjson.JSONObject>
     * @author: zianY
     */
    @Override
    public Result getKuaishouUnitChargeTopTenByAccountId(String userId,long advertiserId, String startTime, String endTime) throws NoSuchFieldException {

        //用户查询自定义列数据
        JSONObject jsonObject = materialStareMapper.getColumnInfo(userId,"1111");
        if (Check.isNull(jsonObject)){
            return Result.errorMsg("暂无自定义列。");
        }
        JSONArray array = jsonObject.getJSONArray("json");

        List<KuaishouUnitReportHourlyPojo> resultList = new ArrayList<>();
        //计算两个日期的差值
        long day = DateUtils.getDiscrepantDays(startTime,endTime);

        //时间转换 2022-08-20 ===>>> 20220820
        long stratDate = DateUtils.getDateInteger(startTime);
        long endDate = DateUtils.getDateInteger(endTime);

        //查询 日报
        if (day != 0){
            resultList = kuaishouOperationRecordMapper.getKuaishouUnitReportDaily(advertiserId,stratDate,endDate);
        }
        //查询 时报
        resultList = kuaishouOperationRecordMapper.getKuaishouUnitReportHourly(advertiserId,stratDate,endDate);

        //自定义列数据
        List<Map<String,Object>> list = FeidNotNullUtils.getCustomDateColumnList(resultList,array);

        return Result.successMsg("查询成功",list);
    }



    /**
     *
     * @description: 查看组的操作记录
     *
     * @param advertiserId
     * @param unitId
     * @param startTime
     * @param endTime
     * @return: org.jeecg.common.api.vo.Result
     * @author: zianY
     */
    @Override
    public Result getKuaishouOperationRecordByUnit(long advertiserId,long unitId,String startTime,String endTime){

        List<JSONObject> list =  kuaishouOperationRecordMapper.getKuaishouOperationRecordByAdvertiserId(advertiserId,unitId,2,startTime+" 00:00:00",endTime+" 23:59:59");

        return Result.successMsg("查询成功",list);
    }


}
