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

import cn.com.ctop.common.module.utils.Check;
import cn.com.ctop.kuaishou.modules.app.entity.KuaishouAccountBalanceBudget;
import cn.com.ctop.kuaishou.modules.app.mapper.KuaishouAccountBalanceBudgetMapper;
import cn.com.ctop.kuaishou.modules.batch.service.IKuaiShouGroupService;
import cn.com.ctop.kuaishou.modules.report.entity.KuaishouReportDailyExplore;
import cn.com.ctop.kuaishou.modules.report.mapper.KuaishouReportDailyExploreMapper;
import cn.com.ctop.kuaishou.modules.report.service.IKuaishouGroupExploreService;
import cn.com.ctop.kuaishou.modules.report.service.IKuaishouReportDailyExploreService;
import com.alibaba.fastjson.JSONArray;
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.jeecg.common.api.vo.Result;
import org.jeecg.common.util.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 广告组加速探索报表
 *
 * @author jeecg-boot
 * 2021-08-18
 * @version V1.0
 */
@Service
public class KuaishouReportDailyExploreServiceImpl extends ServiceImpl<KuaishouReportDailyExploreMapper, KuaishouReportDailyExplore> implements IKuaishouReportDailyExploreService {

    @Autowired
    private KuaishouReportDailyExploreMapper reportDailyExploreMapper;

    @Autowired
    private KuaishouAccountBalanceBudgetMapper kuaishouAccountBalanceBudgetMapper;

    @Autowired
    private IKuaishouGroupExploreService kuaishouGroupExploreService;

    @Autowired
    private IKuaiShouGroupService kuaiShouGroupService;

    @Override
    public void getExploreUnitReports(Long accountId, Long unitId) {
        Result<Object> result = kuaishouGroupExploreService.unitExploreInfoGet(accountId, unitId);
        if (result.getCode() == 200) {
            JSONObject resultResult = (JSONObject) result.getResult();
            if (!Check.isNull(resultResult)) {
                kuaishouGroupExploreService.querySpeedExploreReport(accountId, unitId, resultResult.getInteger("explore_status"), resultResult.getLong("max_explore_budget"));
            }
        }
    }

    @Override
    public List<KuaishouReportDailyExplore> getUnitListByStudyStatus() {
        return reportDailyExploreMapper.getUnitListByStudyStatus();
    }

    @Override
    public Result<Object> todaylist(JSONObject request) {
        Integer pageNo = request.getInteger("pageNo");
        Integer pageSize = request.getInteger("pageSize");
        if (Check.isNull(pageNo)) {
            pageNo = 1;
        }
        if (Check.isNull(pageSize)) {
            pageSize = 10;
        }
        Long accountId = request.getLong("accountId");
        if (Check.isNull(accountId)) {
            return Result.error("未选择账户");
        }
        String sortCode = request.getString("sortCode");
        String sortType = request.getString("sortType");
        if (Check.isNull(sortCode)) {
            sortCode = "charge";
        }
        if (Check.isNull(sortType)) {
            sortType = "DESC";
        }
        Long total = reportDailyExploreMapper.getTodaylistTotal(accountId);
        PageHelper.startPage(pageNo, pageSize, false);
        PageInfo pageInfo = new PageInfo(reportDailyExploreMapper.getTodaylist(accountId, sortCode, sortType));
        pageInfo.setTotal(total);
        JSONObject result = new JSONObject();
        result.put("pageInfo", pageInfo);
        KuaishouAccountBalanceBudget balanceBudget = kuaishouAccountBalanceBudgetMapper.queryByAccountId(accountId);
        if (!Check.isNull(balanceBudget)) {
            result.put("dayBudget", balanceBudget.getDayBudget());
        }
        return Result.ok(result);
    }

    @Override
    public Result<Object> historyList(JSONObject request) {
        Integer pageNo = request.getInteger("pageNo");
        Integer pageSize = request.getInteger("pageSize");
        if (Check.isNull(pageNo)) {
            pageNo = 1;
        }
        if (Check.isNull(pageSize)) {
            pageSize = 10;
        }
        Long accountId = request.getLong("accountId");
        if (Check.isNull(accountId)) {
            return Result.error("未选择账户");
        }
        String sortCode = request.getString("sortCode");
        String sortType = request.getString("sortType");
        if (Check.isNull(sortCode)) {
            sortCode = "total_charge";
        }
        if (Check.isNull(sortType)) {
            sortType = "DESC";
        }
        if (Check.isNull(request.getString("startTime"))) {
            request.put("startTime", DateUtils.getNowDate("yyyy-MM-dd"));
            request.put("endTime", DateUtils.getNowDate("yyyy-MM-dd"));
        }
        Long total = reportDailyExploreMapper.getHistoryListTotal(request);
        PageHelper.startPage(pageNo, pageSize, false);
        PageInfo pageInfo = new PageInfo(reportDailyExploreMapper.getHistoryList(request, sortCode, sortType));
        pageInfo.setTotal(total);
        JSONObject result = new JSONObject();
        result.put("pageInfo", pageInfo);
        KuaishouAccountBalanceBudget balanceBudget = kuaishouAccountBalanceBudgetMapper.queryByAccountId(accountId);
        if (!Check.isNull(balanceBudget)) {
            result.put("dayBudget", balanceBudget.getDayBudget());
        }
        return Result.ok(result);
    }

    @Override
    public Result<Object> openBudgets(Long accountId, JSONArray unitIds, Long exploreBudget) {
        try {
            StringBuilder errUnitIds = new StringBuilder();
            if (Check.isNull(unitIds) || unitIds.isEmpty()) {
                List<Long> unitids = reportDailyExploreMapper.getUnitIds(accountId);
                for (Long unitid : unitids) {
                    Result<Object> result = kuaishouGroupExploreService.unitExploreBudgetUpdate(accountId, unitid, exploreBudget);
                    if (result.getCode() != 200) {
                        if (errUnitIds.length() > 0) {
                            errUnitIds.append("，");
                        }
                        errUnitIds.append(unitid);
                    }
                }
            } else {
                for (int i = 0; i < unitIds.size(); i++) {
                    Long unitId = unitIds.getLong(i);
                    Result<Object> result = kuaishouGroupExploreService.unitExploreBudgetUpdate(accountId, unitId, exploreBudget);
                    if (result.getCode() != 200) {
                        if (errUnitIds.length() > 0) {
                            errUnitIds.append("，");
                        }
                        errUnitIds.append(unitId);
                    }
                }
            }
            if (errUnitIds.length() > 0) {
                return Result.ok(errUnitIds.insert(0, "启动完成，其中广告组：").append("启动失败"));
            }
            return Result.ok("全部启动完成");
        } catch (Exception e) {
            log.error("启动加速探索异常", e);
            return Result.error("启动加速探索失败");
        }
    }
}
