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

import cn.com.ctop.common.module.utils.Check;
import cn.com.ctop.kuaishou.modules.batch.entity.KuaiShouCampaignCreate;
import cn.com.ctop.kuaishou.modules.batch.entity.KuaiShouCampaignTemplate;
import cn.com.ctop.kuaishou.modules.batch.entity.KuaiShouCreateAppTemplate;
import cn.com.ctop.kuaishou.modules.batch.entity.KuaiShouGroupCreateLog;
import cn.com.ctop.kuaishou.modules.batch.mapper.KuaiShouCampaignCreateMapper;
import cn.com.ctop.kuaishou.modules.batch.mapper.KuaiShouCampaignTemplateMapper;
import cn.com.ctop.kuaishou.modules.batch.mapper.KuaiShouCreateAppTemplateMapper;
import cn.com.ctop.kuaishou.modules.batch.mapper.KuaiShouGroupCreateLogMapper;
import cn.com.ctop.kuaishou.modules.batch.service.IKuaiShouCreateService;
import cn.com.ctop.kuaishou.modules.batch.service.IKuaiShouImageService;
import cn.com.ctop.kuaishou.modules.batch.service.IKuaiShouVideoService;
import cn.com.ctop.kuaishou.modules.batch.service.IKuaishouInterfaceService;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.extern.slf4j.Slf4j;
import cn.com.ctop.common.module.entity.CtopOauthToken;
import cn.com.ctop.common.module.service.ICtopOauthTokenService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author yumeng
 * 2019年11月12日10:04:00
 */
@Slf4j
@Service
public class KuaiShouCreateServiceImpl implements IKuaiShouCreateService {
    @Autowired
    private KuaiShouCampaignTemplateMapper campaignTemplateMapper;
    @Autowired
    private IKuaishouInterfaceService kuaishouInterfaceService;
    @Autowired
    private KuaiShouCreateAppTemplateMapper createAppTemplateMapper;
    @Autowired
    private KuaiShouGroupCreateLogMapper groupCreateLogMapper;
    @Autowired
    private ICtopOauthTokenService oauthTokenService;
    @Autowired
    private KuaiShouCampaignCreateMapper campaignCreateMapper;
    @Autowired
    private IKuaiShouVideoService videoService;
    @Autowired
    private IKuaiShouImageService imageService;


    @Override
    public Map<String, Object> createGroups(KuaiShouGroupCreateLog groupCreateTemplate) {
        Map<String, Object> returnMap = new HashMap<>();
        try {
            Integer createCount = groupCreateTemplate.getCreateCount();
            if (Check.isNull(createCount) || createCount == 0) {
                returnMap.put("code", -1);
                returnMap.put("success", false);
                returnMap.put("desc", "迭代次数不能为空或为0");
                return returnMap;
            }
            Long accountId = groupCreateTemplate.getAccountId();
            if (Check.isNull(accountId)) {
                returnMap.put("code", -1);
                returnMap.put("success", false);
                returnMap.put("message", "请选择快手账号");
                return returnMap;
            }

            CtopOauthToken ctopOauthToken = oauthTokenService.getOauthTokenByAccountId(String.valueOf(accountId));
            if (Check.isNull(ctopOauthToken)) {
                log.error("未获取到token信息,accountId:{}", accountId);
                throw new Exception("未获取到token信息");
            }
            String accessToken = ctopOauthToken.getAccessToken();
            // 广告计划模板id
            Long campaignTemplateId = groupCreateTemplate.getCampaignTemplateId();
            if (Check.isNull(campaignTemplateId)) {
                returnMap.put("code", -1);
                returnMap.put("success", false);
                returnMap.put("message", "请选择广告计划模板");
                return returnMap;
            }

            String redundancy = groupCreateTemplate.getRedundancy();
            JSONObject redundancyJson = JSONObject.parseObject(redundancy);

            // 广告计划信息
            KuaiShouCampaignTemplate kuaiShouCampaignTemplate = campaignTemplateMapper.selectById(campaignTemplateId);
            if (Check.isNull(kuaiShouCampaignTemplate)) {
                log.error("查询广告计划为空:模板id:{}", campaignTemplateId);
                throw new Exception("根据模板id查询创意模板信息为空");
            }

            QueryWrapper<KuaiShouCampaignCreate> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("account_id", accountId);
            queryWrapper.eq("campaign_name", kuaiShouCampaignTemplate.getCampaignName());
            queryWrapper.orderByDesc("create_time");
            queryWrapper.last("limit 1");
            KuaiShouCampaignCreate campaignCreate = campaignCreateMapper.selectOne(queryWrapper);
            Long campaignId;
            if (Check.isNull(campaignCreate) || Check.isNull(campaignCreate.getCampaignId())) {
                JSONObject campaignJson = new JSONObject();
                campaignJson.put("campaign_name", kuaiShouCampaignTemplate.getCampaignName());
                campaignJson.put("type", kuaiShouCampaignTemplate.getType());
                campaignJson.put("day_budget", kuaiShouCampaignTemplate.getDayBudget());
                Map<String, Object> campaignMap = kuaishouInterfaceService.campaignCreate(accessToken, accountId, campaignJson);
                Integer code = (Integer) campaignMap.get("code");
                if (code != 0) {
                    return campaignMap;
                }
                campaignId = (Long) campaignMap.get("campaign_id");
            } else {
                campaignId = campaignCreate.getCampaignId();
            }

            groupCreateTemplate.setCampaignId(campaignId);

            JSONObject groupJson = new JSONObject();
            // 计划类型
            Integer type = kuaiShouCampaignTemplate.getType();
            if (type == 3 || type == 4 || type == 5) {
                if (type == 3) {
                    groupJson.put("url_type", kuaiShouCampaignTemplate.getUrlType());
                }
                if (!Check.isNull(redundancyJson.getString("redirectUrl"))) {
                    groupJson.put("url", redundancyJson.getString("redirectUrl"));
                } else {
                    groupJson.put("url", kuaiShouCampaignTemplate.getUrl());
                }
            }
            // app推广
            if (type == 2) {
                KuaiShouCreateAppTemplate appTemplate;
                // 是否重新选择应用进行创建
                if (!Check.isNull(redundancyJson.getLong("appId"))) {
                    appTemplate = createAppTemplateMapper.selectById(redundancyJson.getLong("appId"));
                } else {
                    appTemplate = createAppTemplateMapper.selectById(kuaiShouCampaignTemplate.getAppId());
                }

                if (Check.isNull(appTemplate)) {
                    log.error("快手上传视频未获取到本地app信息,appId:{}", kuaiShouCampaignTemplate.getAppId());
                    throw new Exception("快手上传视频未获取到本地app信息");
                }
                JSONObject apkJson = new JSONObject();
                String platform = appTemplate.getPlatform();
                apkJson.put("platform", platform);
                apkJson.put("app_name", appTemplate.getAppName());
                apkJson.put("app_version", appTemplate.getAppVersion());
                if (!Check.isNull(appTemplate.getPackageName())) {
                    apkJson.put("package_name", appTemplate.getPackageName());
                }
                if (!Check.isNull(appTemplate.getImageUrl()) && ("1".equals(platform) || "2".equals(platform))) {
                    Map<String, Object> imageMap = kuaishouInterfaceService.imageUpload(accountId, accessToken, (String) appTemplate.getImageUrl(), 1);
                    Integer imageCode = (Integer) imageMap.get("code");
                    if (imageCode != 0) {
                        return imageMap;
                    }
                    apkJson.put("image_token", imageMap.get("imageToken"));
                }
                Map<String, Object> apkMap;
                if ("1".equals(platform) && appTemplate.getUploadType() == 1) {
                    apkJson.put("file", appTemplate.getFile());
                    apkMap = kuaishouInterfaceService.appCreate(accountId, accessToken, apkJson);
                } else {
                    apkJson.put("url", appTemplate.getUrl());
                    apkMap = kuaishouInterfaceService.urlAppCreate(accountId, accessToken, apkJson);
                }
                if ((Integer) apkMap.get("code") != 0) {
                    return apkMap;
                }
                groupJson.put("app_id", apkMap.get("appId"));
            }

            groupJson.put("bid_type", groupCreateTemplate.getBidType());
            groupJson.put("campaign_id", campaignId);
            if (!Check.isNull(groupCreateTemplate.getBid())) {
                groupJson.put("bid", groupCreateTemplate.getBid());
            }
            if (!Check.isNull(groupCreateTemplate.getCpaBid())) {
                groupJson.put("cpa_bid", groupCreateTemplate.getCpaBid());
            }
            if (!Check.isNull(groupCreateTemplate.getOcpxActionType())) {
                groupJson.put("ocpx_action_type", groupCreateTemplate.getOcpxActionType());
            }
            if (!Check.isNull(groupCreateTemplate.getDeepConversionType())) {
                groupJson.put("deep_conversion_type", groupCreateTemplate.getDeepConversionType());
            }
            if (!Check.isNull(groupCreateTemplate.getBeginTime())) {
                groupJson.put("begin_time", groupCreateTemplate.getBeginTime());
            }
            if (!Check.isNull(groupCreateTemplate.getEndTime())) {
                groupJson.put("end_time", groupCreateTemplate.getEndTime());
            }
            if (!Check.isNull(groupCreateTemplate.getScheduleTime())) {
                groupJson.put("schedule_time", groupCreateTemplate.getScheduleTime());
            }
            if (!Check.isNull(groupCreateTemplate.getDayBudget())) {
                groupJson.put("day_budget", groupCreateTemplate.getDayBudget());
            }
            if (!Check.isNull(groupCreateTemplate.getShowModel())) {
                groupJson.put("show_mode", groupCreateTemplate.getShowModel());
            }
            if (!Check.isNull(groupCreateTemplate.getSpeed())) {
                groupJson.put("speed", groupCreateTemplate.getSpeed());
            }
            if (!Check.isNull(groupCreateTemplate.getSceneId())) {
                JSONArray sceneIds = new JSONArray();
                sceneIds.add(groupCreateTemplate.getSceneId());
                groupJson.put("scene_id", sceneIds);
            }
            if (!Check.isNull(groupCreateTemplate.getUnitType())) {
                groupJson.put("unit_type", groupCreateTemplate.getUnitType());
            }
            if (!Check.isNull(groupCreateTemplate.getDeepConversionType())) {
                groupJson.put("deep_conversion_type", groupCreateTemplate.getDeepConversionType());
            }
            if (!Check.isNull(groupCreateTemplate.getDeepConversionBid()) && groupCreateTemplate.getDeepConversionBid() > 0) {
                groupJson.put("deep_conversion_bid", groupCreateTemplate.getDeepConversionBid());
            }

            JSONObject targetJson = new JSONObject();
            Integer platformOs = redundancyJson.getInteger("platformOs");
            if (!Check.isNull(platformOs)) {
                targetJson.put("platform_os", platformOs);
                if (platformOs == 1) {
                    targetJson.put("android_osv", redundancyJson.getInteger("androidOsv"));
                } else if (platformOs == 2) {
                    targetJson.put("ios_osv", redundancyJson.getInteger("iosOsv"));
                }
            }

            groupJson.put("target", targetJson);
            String unitName = groupCreateTemplate.getUnitName();
            for (int i = 0; i < createCount; i++) {
                String name;
                if (createCount > 1) {
                    name = unitName + "_" + i;
                } else {
                    name = unitName;
                }
                groupJson.put("unit_name", name);
                Map<String, Object> groupMap = kuaishouInterfaceService.adUnitCreate(accessToken, accountId, groupJson);
                if ((Integer) groupMap.get("code") != 0) {
                    log.error("创建广告组失败,accountId:{},unitName：{}", accountId, unitName);
                    return groupMap;
                }
                groupCreateTemplate.setUnitId((Long) groupMap.get("unitId"));
                groupCreateTemplate.setUnitName(name);
                int y = groupCreateLogMapper.insert(groupCreateTemplate);
                if (y > 0) {
                    log.info("创建广告组记录信息入库完成,accountId:{},unitName：{}", accountId, unitName);
                }
            }
            returnMap.put("code", 0);
            returnMap.put("success", true);
            returnMap.put("message", "批量创建广告组成功");
        } catch (Exception e) {
            e.printStackTrace();
            returnMap.put("code", -1);
            returnMap.put("success", false);
            returnMap.put("message", "批量创建广告组失败");
        }
        return returnMap;
    }

    /**
     * 批量创建创意
     *
     * @param requestJson
     * @return
     */
    @Override
    public Map<String, Object> createCreative(JSONObject requestJson) {
        Map<String, Object> returnMap = new HashMap<>();
        try {
            if (Check.isNull(requestJson)) {
                log.error("创建创意入参为空");
                returnMap.put("code", -1);
                returnMap.put("success", false);
                returnMap.put("message", "入参为空");
                return returnMap;
            }

            JSONObject createJson = new JSONObject();
            Long accountId = requestJson.getLong("accountId");
            if (Check.isNull(accountId)) {
                log.error("创建创意广告主id为空，accountId:{}", accountId);
                returnMap.put("code", -1);
                returnMap.put("success", false);
                returnMap.put("message", "请选择快手账号");
                return returnMap;
            }

            CtopOauthToken cTopOauthToken = oauthTokenService.getOauthTokenByAccountId(String.valueOf(accountId));
            if (Check.isNull(cTopOauthToken)) {
                log.error("未获取到token信息,accountId:{}", accountId);
                throw new Exception("未获取到token信息");
            }
            String accessToken = cTopOauthToken.getAccessToken();
            Long unitId = requestJson.getLong("unitId");
            if (Check.isNull(unitId)) {
                log.error("创建创意广告组为空，accountId:{}", accountId);
                returnMap.put("code", -1);
                returnMap.put("success", false);
                returnMap.put("message", "请选择广告组");
                return returnMap;
            }
            createJson.put("unit_id", unitId);

            if (Check.isNull(requestJson.getString("description"))) {
                log.error("请填写广告语，accountId:{}", accountId);
                returnMap.put("code", -1);
                returnMap.put("success", false);
                returnMap.put("message", "请填写广告语");
                return returnMap;
            }

            if (!Check.isNull(requestJson.getString("actionBarText"))) {
                createJson.put("action_bar_text", requestJson.getString("actionBarText"));
            }

            // 1 竖版视频 2 横版视频 3 贴片图片
            Integer creativeMaterialType = requestJson.getInteger("creativeMaterialType");
            createJson.put("creative_material_type", creativeMaterialType);
            createJson.put("description", requestJson.getString("description"));
            String creativeName = requestJson.getString("creativeName");
            Integer sceneId = requestJson.getInteger("sceneId");
            // 普通 广告
            if (sceneId == 2) {
                JSONObject localVideoJson = new JSONObject();
                localVideoJson.put("videoType", requestJson.getString("creativeMaterialType"));
                localVideoJson.put("login_id", requestJson.getString("loginId"));
                localVideoJson.put("videoUrl", requestJson.getString("videoUrl"));
                localVideoJson.put("description", requestJson.getString("description"));
                localVideoJson.put("positionType", requestJson.getString("positionType"));
                localVideoJson.put("accountId", requestJson.getString("accountId"));
                // 校验文件是否在本地有留存记录
                String videoUrl = videoService.localInsert(localVideoJson);
                if (Check.isNull(videoUrl)) {
                    log.error("创建创意视频为空，accountId:{}", accountId);
                    returnMap.put("code", -1);
                    returnMap.put("success", false);
                    returnMap.put("message", "请选择上传的视频");
                    return returnMap;
                }
                // 上传视频到快手后台
                Map<String, Object> videoMap = kuaishouInterfaceService.videoUpload(accountId, accessToken, videoUrl);
                if ((Integer) videoMap.get("code") != 0) {
                    log.error("上传视频失败，accountId:{}", accountId);
                    return videoMap;
                }
                createJson.put("photo_id", videoMap.get("photoId"));
                if (!Check.isNull(requestJson.getString("clickTrackUrl"))) {
                    createJson.put("click_track_url", requestJson.getString("clickTrackUrl"));
                }
                JSONArray imageUrlArr = requestJson.getJSONArray("imageUrl");
                if (Check.isNull(imageUrlArr)) {
                    log.error("请选择封面图，accountId:{}", accountId);
                    returnMap.put("code", -1);
                    returnMap.put("success", false);
                    returnMap.put("message", "请选择广告创意封面图");
                    return returnMap;
                }


                for (int i = 0; i < imageUrlArr.size(); i++) {
                    String imageUrl = imageUrlArr.get(i).toString();
                    if (Check.isNull(imageUrl)) {
                        continue;
                    }
                    JSONObject imageJson = new JSONObject();
                    imageJson.put("loginId", requestJson.getString("loginId"));
                    imageJson.put("videoUploadType", requestJson.getString("creativeMaterialType"));
                    imageJson.put("url", imageUrl);
                    imageJson.put("accountId", accountId);
                    imageJson.put("positionType", requestJson.getString("positionType"));
                    String imagePath = imageService.localInsert(imageJson);

                    Map<String, Object> imageMap = kuaishouInterfaceService.imageUpload(accountId, accessToken, imagePath, 2);
                    if ((Integer) imageMap.get("code") != 0) {
                        return imageMap;
                    }
                    createJson.put("image_token", imageMap.get("imageToken"));
                    if (imageUrlArr.size() == 1) {
                        createJson.put("creative_name", creativeName);
                    } else {
                        createJson.put("creative_name", creativeName + "_" + i);
                    }
                    Map<String, Object> creativeMap = kuaishouInterfaceService.creativeCreate(accessToken, accountId, createJson);
                    if ((Integer) creativeMap.get("code") != 0) {
                        return creativeMap;
                    }
                }

            } else if (sceneId == 3) {
                // 后贴片 广告
                if (!Check.isNull(requestJson.getString("impressionUrl"))) {
                    createJson.put("impression_url", requestJson.getString("impressionUrl"));
                }

                if (!Check.isNull(requestJson.getString("adPhotoPlayedT3sUrl"))) {
                    createJson.put("ad_photo_played_t3s_url", requestJson.getString("adPhotoPlayedT3sUrl"));
                }
                if (creativeMaterialType != 3) {
                    // 后贴片 视频集合
                    JSONArray haveSinglePicVideoUrlArr = requestJson.getJSONArray("haveSinglePicVideoUrl");
                    if (Check.isNull(haveSinglePicVideoUrlArr)) {
                        returnMap.put("code", -1);
                        returnMap.put("success", false);
                        returnMap.put("message", "请选择视频素材进行上传素材");
                        return returnMap;
                    }

                    for (int i = 0; i < haveSinglePicVideoUrlArr.size(); i++) {
                        String haveSinglePicVideoUrl = haveSinglePicVideoUrlArr.get(i).toString();
                        if (Check.isNull(haveSinglePicVideoUrl)) {
                            continue;
                        }
                        JSONObject haveLocalVideoJson = new JSONObject();
                        haveLocalVideoJson.put("positionType", requestJson.getString("positionType"));
                        haveLocalVideoJson.put("videoUrl", haveSinglePicVideoUrl);
                        haveLocalVideoJson.put("accountId", accountId);
                        haveLocalVideoJson.put("description", requestJson.getString("description"));
                        haveLocalVideoJson.put("login_id", requestJson.getString("loginId"));
                        haveLocalVideoJson.put("videoType", requestJson.getString("creativeMaterialType"));
                        // 校验文件是否在本地有留存记录
                        String havaVideoUrl = videoService.localInsert(haveLocalVideoJson);
                        if (Check.isNull(havaVideoUrl)) {
                            log.error("创建创意视频为空，accountId:{}", accountId);
                            returnMap.put("code", -1);
                            returnMap.put("message", "请选择上传的视频");
                            returnMap.put("success", false);
                            return returnMap;
                        }

                        // 上传视频到快手后台
                        Map<String, Object> videoMap = kuaishouInterfaceService.videoUpload(accountId, accessToken, havaVideoUrl);
                        if ((Integer) videoMap.get("code") != 0) {
                            log.error("后贴片素材上传视频失败，accountId:{}", accountId);
                            return videoMap;
                        }
                        createJson.put("photo_id", videoMap.get("photoId"));

                        if (haveSinglePicVideoUrlArr.size() == 1) {
                            createJson.put("creative_name", creativeName);
                        } else {
                            createJson.put("creative_name", creativeName + "_" + i);
                        }
                        Map<String, Object> creativeMap = kuaishouInterfaceService.creativeCreate(accessToken, accountId, createJson);
                        if ((Integer) creativeMap.get("code") != 0) {
                            log.error("后贴片视频素材批量创建创意失败，accountId:{},creativeName:{}", accountId, creativeName + "_" + i);
                            return creativeMap;
                        }
                    }
                } else {
                    // 后贴片图片素材
                    JSONArray haveSinglePicimagesListArr = requestJson.getJSONArray("haveSinglePicimagesList");
                    if (Check.isNull(haveSinglePicimagesListArr)) {
                        returnMap.put("code", -1);
                        returnMap.put("success", false);
                        returnMap.put("message", "请选择后贴片图片素材进行上传");
                        return returnMap;
                    }

                    for (int i = 0; i < haveSinglePicimagesListArr.size(); i++) {
                        String imageUrl = haveSinglePicimagesListArr.get(i).toString();
                        if (Check.isNull(imageUrl)) {
                            continue;
                        }
                        JSONObject imageJson = new JSONObject();
                        imageJson.put("url", imageUrl);
                        imageJson.put("accountId", accountId);
                        imageJson.put("positionType", requestJson.getInteger("positionType"));
                        imageJson.put("loginId", requestJson.getString("loginId"));
                        imageJson.put("videoUploadType", requestJson.getString("singlePicTiepianType"));
                        String imagePath = imageService.localInsert(imageJson);
                        Map<String, Object> imageMap = kuaishouInterfaceService.imageUpload(accountId, accessToken, imagePath, 3);
                        if ((Integer) imageMap.get("code") != 0) {
                            log.error("上传后贴片单张图片素材失败，accountId:{}", accountId);
                            return imageMap;
                        }
                        if (haveSinglePicimagesListArr.size() == 1) {
                            createJson.put("creative_name", creativeName);
                        } else {
                            createJson.put("creative_name", creativeName + "_" + i);
                        }
                        JSONArray imageTokens = new JSONArray();
                        imageTokens.add(imageMap.get("imageToken"));
                        createJson.put("image_tokens", imageTokens);
                        Map<String, Object> creativeMap = kuaishouInterfaceService.creativeCreate(accessToken, accountId, createJson);
                        if ((Integer) creativeMap.get("code") != 0) {
                            log.error("后贴片素材单张图片批量创建创意失败，accountId:{}", accountId);
                            return creativeMap;
                        }
                    }
                }
            }
            returnMap.put("code", 0);
            returnMap.put("success", true);
            returnMap.put("message", "创建创意成功");
        } catch (Exception e) {
            returnMap.put("code", -1);
            returnMap.put("success", false);
            returnMap.put("message", "批量创建广告创意失败");
            e.printStackTrace();
        }
        return returnMap;
    }


    /**
     * 三级联动 查询账号 计划 模板信息
     *
     * @param loginId
     * @return
     */
    @Override
    public JSONArray getOptions(String loginId, Long accountId) {
        JSONArray result = new JSONArray();
        QueryWrapper<KuaiShouGroupCreateLog> wrapper = new QueryWrapper<>();
        wrapper.eq("login_id", loginId).eq("account_id", accountId).orderByDesc("create_time");
        wrapper.groupBy("campaign_id");
        List<KuaiShouGroupCreateLog> accounts = groupCreateLogMapper.selectList(wrapper);
        if (!Check.isNull(accounts)) {
            for (KuaiShouGroupCreateLog groupCreateLog : accounts) {
                JSONObject optionsJson = new JSONObject();
                Long campaignId = groupCreateLog.getCampaignId();
                optionsJson.put("value", campaignId);
                QueryWrapper<KuaiShouCampaignCreate> queryWrapper = new QueryWrapper<>();
                queryWrapper.eq("account_id", accountId).eq("campaign_id", campaignId).orderByDesc("create_time");
                queryWrapper.last("limit 1");
                KuaiShouCampaignCreate campaignCreate = campaignCreateMapper.selectOne(queryWrapper);
                if (Check.isNull(campaignCreate)) {
                    continue;
                }
                optionsJson.put("label", campaignCreate.getCampaignName());
                optionsJson.put("isLeaf", false);
                result.add(optionsJson);
            }
        }
        return result;
    }

    /**
     * 获取组信息
     *
     * @param loginId
     * @param campaignId
     * @return
     */
    @Override
    public JSONArray getChildren(String loginId, Long campaignId, Long accountId) {
        JSONArray result = new JSONArray();
        QueryWrapper<KuaiShouGroupCreateLog> wrapper = new QueryWrapper<>();
        wrapper.eq("account_id", accountId);
        wrapper.eq("campaign_id", campaignId);
        wrapper.eq("login_id", loginId);
        wrapper.orderByDesc("create_time");
        wrapper.groupBy("unit_name");
        List<KuaiShouGroupCreateLog> accounts = groupCreateLogMapper.selectList(wrapper);
        if (!Check.isNull(accounts)) {
            for (KuaiShouGroupCreateLog groupCreateLog : accounts) {
                JSONObject optionsJson = new JSONObject();
                optionsJson.put("value", groupCreateLog.getUnitId());
                optionsJson.put("label", groupCreateLog.getUnitName());
                result.add(optionsJson);
            }
        }
        return result;
    }
}
