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(null,"2001");
        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) throws Exception{
        List<JSONObject> resultList = new ArrayList<>();
        // 获取两个日期之间 所有的 时间 年-月-日
        List<String> betweenDaysList = DateUtils.getAllDatesOfTwoTimes(startTime,endTime);
        // 所有时间集
        List<String> allHoursList = DateUtils.getHoursListDifference(betweenDaysList);
        for (String time : allHoursList) {
            JSONObject jsonObject = new JSONObject();
            //2022090101 -年月日 时
            String date = time.substring(0,time.length()-2);
            String hour = time.substring(time.length()-2);
            //查询时报数据
            JSONObject unit =  kuaishouOperationRecordMapper.getKuaishouOperationUnitReport(advertiserId,unitId,date,hour);
            if (Check.isNull(unit)){
                jsonObject.put("charge",0);
                jsonObject.put("statDate",date);
                jsonObject.put("statHour",hour);
                jsonObject.put("time",time);
                resultList.add(jsonObject);
                continue;
            }

            long uId = Long.valueOf(unit.getString("unitId"));
            //查询操作记录
            List<JSONObject> list =  kuaishouOperationRecordMapper.getKuaishouOperationRecordByAdvertiserId(advertiserId,uId,2,time);
            if (!Check.isNull(list)){ list.forEach(json -> {json.put("content_log",json.getJSONArray("content_log"));});}
            jsonObject = unit;
            jsonObject.put("time",time);
            jsonObject.put("operationList",list);
            resultList.add(jsonObject);
        }
        return Result.successMsg("查询成功",resultList);
    }


}
