package cn.com.ctop.kuaishou.modules.batch.controller;


import cn.com.ctop.common.module.entity.CtopOauthToken;
import cn.com.ctop.common.module.service.ICtopOauthTokenService;
import cn.com.ctop.common.module.utils.Check;
import cn.com.ctop.kuaishou.modules.batch.entity.*;
import cn.com.ctop.kuaishou.modules.batch.entity.vo.SpendVo;
import cn.com.ctop.kuaishou.modules.batch.mapper.KuaiShouRegionListParentMapper;
import cn.com.ctop.kuaishou.modules.batch.mapper.KuaiShouTargetingTagsMapper;
import cn.com.ctop.kuaishou.modules.batch.service.*;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.query.QueryGenerator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

@Slf4j
@Api(tags = "快手-批量工具")
@RestController
@RequestMapping("/kuaishou/batch")
public class BatchController {
    @Autowired
    private ICtopOauthTokenService oauthTokenService;
    @Autowired
    private IKuaishouInterfaceService iKuaishouInterfaceService;
    @Autowired
    private IBatchService batchService;
    @Autowired
    private IKuaiShouCampaignService kuaiShouCampaignService;
    @Autowired
    private IKuaiShouUpdateService updateService;
    @Autowired
    private ICtopOauthTokenService tokenService;

    @Autowired
    private IKuaiShouGroupService kuaiShouGroupService;
    @Autowired
    private KuaiShouRegionListParentMapper regionListParentMapper;
    @Autowired
    private IKuaiShouAppListService kuaiShouAppListService;
    @Autowired
    private KuaiShouTargetingTagsMapper targetingTagsMapper;


    /**
     * 获取花费信息
     *
     * @param accountId
     * @return
     */
    @GetMapping(value = "/spend")
    public Result<SpendVo> spend(Long accountId) {
        Result<SpendVo> result = new Result<>();
        try {

            CtopOauthToken oauthToken = oauthTokenService.getTokenByAccountId(accountId);
            if (Check.isNull(oauthToken)) {
                throw new Exception("未获取到账户信息");
            }
            SpendVo spendVo = new SpendVo();
            JSONObject fundJson = iKuaishouInterfaceService.fundGet(oauthToken);
            if (!Check.isNull(fundJson)) {
                spendVo.setBalance(fundJson.getBigDecimal("balance")); // 余额
            }
            BigDecimal cost = batchService.getCost(accountId);
            spendVo.setCost(cost);

            BigDecimal budget = batchService.getBudget(accountId);
            spendVo.setBudget(budget);
            result.setSuccess(true);
            result.setResult(spendVo);

        } catch (Exception e) {
            e.printStackTrace();
            result.setSuccess(false);
            result.setMessage(e.getMessage());
        }

        return result;
    }


    /**
     * 获取广告计划列表
     *
     * @param kuaiShouCampaign
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    @GetMapping(value = "/getCampaignList")
    public Result<IPage<KuaiShouCampaign>> getCampaignList(KuaiShouCampaign kuaiShouCampaign,
                                                           @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                                           @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                                           HttpServletRequest req) {
        Result<IPage<KuaiShouCampaign>> result = new Result<IPage<KuaiShouCampaign>>();
        QueryWrapper<KuaiShouCampaign> queryWrapper = QueryGenerator.initQueryWrapper(kuaiShouCampaign, req.getParameterMap());
        queryWrapper.orderByDesc("put_create_time");
        Page<KuaiShouCampaign> page = new Page<KuaiShouCampaign>(pageNo, pageSize);
        IPage<KuaiShouCampaign> pageList = kuaiShouCampaignService.page(page, queryWrapper);
        result.setSuccess(true);
        result.setResult(pageList);
        return result;
    }


    /**
     * 批量修改广告计划状态
     *
     * @param requestJson
     * @return
     */
    @PostMapping(value = "/batchUpdateStatus")
    public Result<JSONObject> batchUpdateStatus(@RequestBody JSONObject requestJson) {
        Result<JSONObject> result = new Result<>();


        try {
            Long accountId = requestJson.getLong("accountId");
            CtopOauthToken token = tokenService.getTokenByAccountId(accountId);
            if (Check.isNull(token)) {
                throw new Exception("账号信息为空");
            }
            Integer putStatus = requestJson.getInteger("putStatus");
            if (putStatus == null) {
                throw new Exception("请选择状态");
            }
            String userId = requestJson.getString("userId");
            JSONArray campaignIds = requestJson.getJSONArray("campaignIds");
            JSONArray failArr = new JSONArray();
            if (!Check.isNull(campaignIds)) {
                for (int i = 0; i < campaignIds.size(); i++) {
                    Long campaignId = campaignIds.getLong(i);
                    if (!Check.isNull(campaignId)) {
                        Map<String, Object> updateMap = updateService.updateCampaignStatus(token.getAccessToken(), accountId, campaignId, putStatus, userId);
                        if (!Check.isNull(updateMap)) {
                            Integer code = (Integer) updateMap.get("code");
                            if (code != 0) {
                                JSONObject failJson = new JSONObject();
                                failJson.put("message", updateMap.get("message"));
                                KuaiShouCampaign campaign = batchService.getCampaignInfo(accountId, campaignId);
                                if (!Check.isNull(campaign)) {
                                    failJson.put("campaignName", campaign.getCampaignName());
                                }
                                failArr.add(failJson);
                            }
                        }
                    }
                }
            }
            JSONObject json = new JSONObject();
            json.put("totalCount", campaignIds.size());
            json.put("failCount", failArr.size());
            json.put("failInfo", failArr);
            result.setResult(json);
            result.setSuccess(true);
        } catch (Exception e) {
            e.printStackTrace();
            result.setSuccess(false);
            result.setMessage(e.getMessage());
        }
        return result;
    }

    /**
     * 批量修改广告计划预算
     *
     * @param requestJson
     * @return
     */
    @PostMapping(value = "/batchUpdateDayBudget")
    public Result<JSONObject> batchUpdateDayBudget(@RequestBody JSONObject requestJson) {
        Result<JSONObject> result = new Result<>();


        try {
            Long accountId = requestJson.getLong("accountId");
            CtopOauthToken token = tokenService.getTokenByAccountId(accountId);
            if (Check.isNull(token)) {
                throw new Exception("账号信息为空");
            }
            Long dayBudget = requestJson.getLong("dayBudget");
            if (Check.isNull(dayBudget)) {
                throw new Exception("预算信息为空");
            }
            String userId = requestJson.getString("userId");
            JSONArray campaignIds = requestJson.getJSONArray("campaignIds");
            JSONArray failArr = new JSONArray();
            if (!Check.isNull(campaignIds)) {
                for (int i = 0; i < campaignIds.size(); i++) {
                    Long campaignId = campaignIds.getLong(i);
                    if (!Check.isNull(campaignId)) {
                        Map<String, Object> updateMap = updateService.updateCampaign(token.getAccessToken(), accountId, campaignId, dayBudget, userId);
                        if (!Check.isNull(updateMap)) {
                            Integer code = (Integer) updateMap.get("code");
                            if (code != 0) {
                                JSONObject failJson = new JSONObject();
                                failJson.put("message", updateMap.get("message"));
                                KuaiShouCampaign campaign = batchService.getCampaignInfo(accountId, campaignId);
                                if (!Check.isNull(campaign)) {
                                    failJson.put("campaignName", campaign.getCampaignName());
                                }
                                failArr.add(failJson);
                            }
                        }
                    }
                }
            }
            JSONObject json = new JSONObject();
            json.put("totalCount", campaignIds.size());
            json.put("failCount", failArr.size());
            json.put("failInfo", failArr);
            result.setResult(json);
            result.setSuccess(true);
        } catch (Exception e) {
            e.printStackTrace();
            result.setSuccess(false);
            result.setMessage(e.getMessage());
        }
        return result;
    }


    /**
     * 查询广告组列表
     *
     * @param kuaiShouGroup
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    @GetMapping(value = "/getUnitList")
    public Result<IPage<KuaiShouGroup>> getUnitList(KuaiShouGroup kuaiShouGroup,
                                                    @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                                    @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                                    HttpServletRequest req) {
        Result<IPage<KuaiShouGroup>> result = new Result<IPage<KuaiShouGroup>>();
        QueryWrapper<KuaiShouGroup> queryWrapper = QueryGenerator.initQueryWrapper(kuaiShouGroup, req.getParameterMap());
        queryWrapper.orderByDesc("unit_id");
        Page<KuaiShouGroup> page = new Page<KuaiShouGroup>(pageNo, pageSize);
        IPage<KuaiShouGroup> pageList = kuaiShouGroupService.page(page, queryWrapper);
        result.setSuccess(true);
        result.setResult(pageList);
        return result;
    }


    /**
     * 获取区域列表
     *
     * @param
     * @return
     */
    @Autowired
    private IKuaiShouRegionListParentService kuaiShouRegionListParentService;

    @GetMapping(value = "/getRegion")
    public Result<List<KuaiShouRegionListParent>> getRegion(KuaiShouRegionListParent kuaiShouRegionListParent, HttpServletRequest req) {
        Result<List<KuaiShouRegionListParent>> result = new Result<>();


        try {
            /*JSONArray regionArr = new JSONArray();
            QueryWrapper<KuaiShouRegionListParent> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("level", 1);
            List<KuaiShouRegionListParent> regionListParents = regionListParentMapper.selectList(queryWrapper);
            if (!Check.isNull(regionListParents)) {

                for (KuaiShouRegionListParent regionListParent : regionListParents) {
                    if (Check.isNull(regionListParent)) {
                        continue;
                    }
                    JSONObject parentJson = new JSONObject();
                    Long regionId = regionListParent.getRegionId();
                    parentJson.put("regionId", regionId);
                    parentJson.put("name", regionListParent.getName());
                    QueryWrapper<KuaiShouRegionListParent> childrenQueryWrapper = new QueryWrapper<>();
                    childrenQueryWrapper.eq("parent", regionId);
                    List<KuaiShouRegionListParent> childrenRegions = regionListParentMapper.selectList(childrenQueryWrapper);

                    JSONArray childrenArr = new JSONArray();
                    if (!Check.isNull(childrenRegions)) {
                        for (KuaiShouRegionListParent childrenRegion : childrenRegions) {
                            if (!Check.isNull(childrenRegion)) {
                                JSONObject childrenJson = new JSONObject();
                                childrenJson.put("regionId", childrenRegion.getRegionId());
                                childrenJson.put("name", childrenRegion.getName());
                                childrenArr.add(childrenJson);
                            }
                        }
                        parentJson.put("children", childrenArr);

                    }

                    regionArr.add(parentJson);
                }
            }*/


            QueryWrapper<KuaiShouRegionListParent> queryWrapper = QueryGenerator.initQueryWrapper(kuaiShouRegionListParent, req.getParameterMap());

            List<KuaiShouRegionListParent> pageList = kuaiShouRegionListParentService.list(queryWrapper);

            result.setSuccess(true);
            result.setResult(pageList);
        } catch (Exception e) {
            e.printStackTrace();
            result.setSuccess(false);

        }
        return result;
    }


    /**
     * 创建广告计划
     *
     * @param requestJson
     * @return
     */
    @PostMapping(value = "/campaignCreate")
    public Result<Object> campaignCreate(@RequestBody JSONObject requestJson) {
        Result<Object> result = new Result<>();
        try {
            Long accountId = requestJson.getLong("accountId");
            CtopOauthToken oauthToken = oauthTokenService.getTokenByAccountId(accountId);
            if (Check.isNull(oauthToken)) {
                throw new Exception("未获取到账户信息");
            }
            JSONObject campaignJson = new JSONObject();
            campaignJson.put("campaign_name", requestJson.getString("campaignName"));
            campaignJson.put("type", requestJson.getInteger("type"));
            campaignJson.put("day_budget", requestJson.getLong("day_budget"));
            Map<String, Object> campaignMap = iKuaishouInterfaceService.campaignCreate(oauthToken.getAccessToken(), accountId, campaignJson);
            Integer code = (Integer) campaignMap.get("code");
            if (code != 0) {
                throw new Exception((String) campaignMap.get("message"));
            }
            result.setSuccess(true);
            result.setResult(campaignMap.get("campaignId"));
        } catch (Exception e) {
            e.printStackTrace();
            result.setSuccess(false);
            result.setMessage(e.getMessage());

        }
        return result;

    }


    /**
     * 获取应用列表
     */

    @GetMapping(value = "/getAppList")
    public Result<List<KuaiShouAppList>> getAppList(KuaiShouAppList kuaiShouAppList, HttpServletRequest req) {
        Result<List<KuaiShouAppList>> result = new Result<>();
        try {
            QueryWrapper<KuaiShouAppList> queryWrapper = QueryGenerator.initQueryWrapper(kuaiShouAppList, req.getParameterMap());
            queryWrapper.orderByDesc("return_time");

            List<KuaiShouAppList> appList = kuaiShouAppListService.list(queryWrapper);
            result.setSuccess(true);
            result.setResult(appList);
        } catch (Exception e) {
            e.printStackTrace();
            result.setSuccess(false);
        }

        return result;
    }


    /**
     * 获取人群包接口
     *
     * @param accountId
     * @return
     */
    @GetMapping(value = "/getPopulationList")
    public Result<JSONArray> getAppList(Long accountId) {
        Result<JSONArray> result = new Result<>();
        try {
            CtopOauthToken oauthToken = oauthTokenService.getTokenByAccountId(accountId);
            if (Check.isNull(oauthToken)) {
                throw new Exception("未获取到账户信息");
            }
            JSONArray jsonArray = iKuaishouInterfaceService.getPopulationList(accountId, oauthToken.getAccessToken());
            result.setSuccess(true);
            result.setResult(jsonArray);
        } catch (Exception e) {
            e.printStackTrace();
            result.setSuccess(false);
        }
        return result;
    }


    /**
     * 获取商业兴趣
     *
     * @param kuaiShouTargetingTags BUSINESS_INTEREST：商业兴趣
     *                              APP_INTEREST：APP行为
     *                              FANS_STAR：网红类别
     *                              INTEREST_VIDEO：兴趣视频
     * @param req
     * @return
     */
    @GetMapping(value = "/getTargetList")
    public Result<List<KuaiShouTargetingTags>> getTargetList(KuaiShouTargetingTags kuaiShouTargetingTags, HttpServletRequest req) {
        Result<List<KuaiShouTargetingTags>> result = new Result<>();
        try {
            QueryWrapper<KuaiShouTargetingTags> queryWrapper = QueryGenerator.initQueryWrapper(kuaiShouTargetingTags, req.getParameterMap());
            queryWrapper.orderByAsc("tag_id");
            List<KuaiShouTargetingTags> targetingTagList = targetingTagsMapper.selectList(queryWrapper);
            result.setSuccess(true);
            result.setResult(targetingTagList);
        } catch (Exception e) {
            e.printStackTrace();
            result.setSuccess(false);
        }
        return result;
    }


    /**
     * 获取可选的深度类型
     *
     * @param accountId
     * @return
     */
    @GetMapping(value = "/getIsActivate")
    public Result<JSONObject> getIsActivate(Long accountId) {
        Result result = new Result<JSONObject>();
        try {
            CtopOauthToken token = oauthTokenService.getTokenByAccountId(accountId);
            if (Check.isNull(token)) {
                throw new Exception("未获取授权信息");
            }
            JSONObject deepConversionJson = iKuaishouInterfaceService.getDeepConversionInfosV2(accountId, token.getAccessToken());
            if (Check.isNull(deepConversionJson)) {
                throw new Exception("深度类型数据返回为空");
            }

            result.setSuccess(true);
            result.setResult(deepConversionJson);
        } catch (Exception e) {
            e.printStackTrace();
            result.setSuccess(false);
            result.setMessage(e.getMessage());
        }

        return result;

    }


    /**
     * 获取app列表
     *
     * @param accountId
     * @param appName
     * @return
     */
    @GetMapping(value = "/getAppSearch")
    public Result<JSONObject> getAppSearch(Long accountId, String appName) {
        Result result = new Result<JSONObject>();
        try {
            CtopOauthToken token = oauthTokenService.getTokenByAccountId(accountId);
            if (Check.isNull(token)) {
                throw new Exception("未获取授权信息");
            }
            JSONArray appArray = iKuaishouInterfaceService.getAppSearch(accountId, token.getAccessToken(), appName);
            result.setSuccess(true);
            result.setResult(appArray);
        } catch (Exception e) {
            e.printStackTrace();
            result.setSuccess(false);
            result.setMessage(e.getMessage());
        }

        return result;

    }

    /**
     * 获取项目最高出价
     *
     * @param accountId
     * @return
     */
    @GetMapping(value = "/getProjectMaxBid")
    public Result<BigDecimal> getProjectMaxBid(Long accountId) {
        Result<BigDecimal> result = new Result<>();
        try {
            if (Check.isNull(accountId)) {
                throw new Exception("请传入accountId");
            }
            BigDecimal maxBid = batchService.getProjectMaxBid(accountId);
            result.setSuccess(true);
            result.setResult(maxBid);

        } catch (Exception e) {
            e.printStackTrace();
            result.setSuccess(false);
            result.setMessage(e.getMessage());
        }

        return result;

    }


    /**
     * 获取项目最高出价
     *
     * @param campaignId
     * @return
     */
    @GetMapping(value = "/checkUnitName")
    public Result<Boolean> checkUnitName(Long campaignId, String unitName) {
        Result<Boolean> result = new Result<>();
        try {
            if (Check.isNull(campaignId)) {
                throw new Exception("请传入广告计划id");
            }
            if (Check.isNull(unitName)) {
                throw new Exception("请传入广告组名称");
            }
            Boolean checkUnitName = batchService.checkUnitName(campaignId, unitName);
            result.setSuccess(true);
            result.setResult(checkUnitName);
            if (!checkUnitName) {
                result.setMessage("广告组名称该计划下已存在");
            }

        } catch (Exception e) {
            e.printStackTrace();
            result.setSuccess(false);
            result.setMessage(e.getMessage());
        }

        return result;
    }


    /**
     * 批量创建广告组
     *
     * @param requestJson
     * @return
     */
    @PostMapping(value = "/createUnit")
    public Result<JSONObject> createUnit(@RequestBody JSONObject requestJson) {
        Result<JSONObject> result = new Result<>();
        try {
            if (Check.isNull(requestJson)) {
                throw new Exception("入参为空");
            }

            JSONObject unitJson = batchService.createUnit(requestJson);
            result.setSuccess(true);
            result.setResult(unitJson);

        } catch (Exception e) {
            e.printStackTrace();
            result.setSuccess(false);
            result.setMessage(e.getMessage());
        }

        return result;
    }


    @Autowired
    private IKuaiShouCreativeService kuaiShouCreativeService;


    @AutoLog(value = "快手-创意信息-分页列表查询")
    @GetMapping(value = "/getCreativeList")
    public Result<IPage<KuaiShouCreative>> getCreativeList(KuaiShouCreative kuaiShouCreative, @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                                           @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize, HttpServletRequest req) {
        Result<IPage<KuaiShouCreative>> result = new Result<>();

        QueryWrapper<KuaiShouCreative> queryWrapper = QueryGenerator.initQueryWrapper(kuaiShouCreative, req.getParameterMap());
        queryWrapper.orderByDesc("creative_id");
        Page<KuaiShouCreative> page = new Page<>(pageNo, pageSize);
        IPage<KuaiShouCreative> creativeList = kuaiShouCreativeService.page(page, queryWrapper);
        result.setSuccess(true);
        result.setResult(creativeList);
        return result;
    }


    /**
     * 批量修改广告组预算
     *
     * @param requestJson
     * @return
     */
    @PostMapping(value = "/batchUpdateUnitBudget")
    public Result<JSONObject> batchUpdateUnitBudget(@RequestBody JSONObject requestJson) {
        Result<JSONObject> result = new Result<>();


        try {
            Long accountId = requestJson.getLong("accountId");
            CtopOauthToken token = tokenService.getTokenByAccountId(accountId);
            if (Check.isNull(token)) {
                throw new Exception("账号信息为空");
            }
            Long dayBudget = requestJson.getLong("dayBudget");
            if (Check.isNull(dayBudget)) {
                throw new Exception("预算信息为空");
            }
            String userId = requestJson.getString("userId");
            JSONArray unitIds = requestJson.getJSONArray("unitIds");
            JSONArray failArr = new JSONArray();
            if (!Check.isNull(unitIds)) {
                for (int i = 0; i < unitIds.size(); i++) {
                    Long unitId = unitIds.getLong(i);
                    if (!Check.isNull(unitId)) {
                        Map<String, Object> updateMap = updateService.updateUnitDayBudget(token.getAccessToken(), accountId, unitId, dayBudget, userId);
                        if (!Check.isNull(updateMap)) {
                            Integer code = (Integer) updateMap.get("code");
                            if (code != 0) {
                                JSONObject failJson = new JSONObject();
                                failJson.put("message", updateMap.get("message"));
                                KuaiShouGroup group = batchService.getUnitInfo(accountId, unitId);
                                if (!Check.isNull(group)) {
                                    failJson.put("unitName", group.getUnitName());
                                }
                                failArr.add(failJson);
                            }
                        }
                    }
                }
            }
            JSONObject json = new JSONObject();
            json.put("totalCount", unitIds.size());
            json.put("failCount", failArr.size());
            json.put("failInfo", failArr);
            result.setResult(json);
            result.setSuccess(true);
        } catch (Exception e) {
            e.printStackTrace();
            result.setSuccess(false);
            result.setMessage(e.getMessage());
        }
        return result;
    }


    /**
     * 批量修改广告组状态
     *
     * @param requestJson
     * @return
     */
    @PostMapping(value = "/batchUpdateUnitStatus")
    public Result<JSONObject> batchUpdateUnitStatus(@RequestBody JSONObject requestJson) {
        Result<JSONObject> result = new Result<>();


        try {
            Long accountId = requestJson.getLong("accountId");
            CtopOauthToken token = tokenService.getTokenByAccountId(accountId);
            if (Check.isNull(token)) {
                throw new Exception("账号信息为空");
            }
            Integer putStatus = requestJson.getInteger("putStatus");
            if (putStatus == null) {
                throw new Exception("请选择状态");
            }
            String userId = requestJson.getString("userId");
            JSONArray unitIds = requestJson.getJSONArray("unitIds");
            JSONArray failArr = new JSONArray();
            if (!Check.isNull(unitIds)) {
                for (int i = 0; i < unitIds.size(); i++) {
                    Long unitId = unitIds.getLong(i);
                    if (!Check.isNull(unitId)) {
                        Map<String, Object> updateMap = updateService.updateUnitStatus(token.getAccessToken(), accountId, unitId, putStatus, userId);
                        if (!Check.isNull(updateMap)) {
                            Integer code = (Integer) updateMap.get("code");
                            if (code != 0) {
                                JSONObject failJson = new JSONObject();
                                failJson.put("message", updateMap.get("message"));
                                KuaiShouGroup group = batchService.getUnitInfo(accountId, unitId);
                                if (!Check.isNull(group)) {
                                    failJson.put("campaignName", group.getUnitName());
                                }
                                failArr.add(failJson);
                            }
                        }
                    }
                }
            }
            JSONObject json = new JSONObject();
            json.put("totalCount", unitIds.size());
            json.put("failCount", failArr.size());
            json.put("failInfo", failArr);
            result.setResult(json);
            result.setSuccess(true);
        } catch (Exception e) {
            e.printStackTrace();
            result.setSuccess(false);
            result.setMessage(e.getMessage());
        }
        return result;
    }


    /**
     * 批量修改广告组状态
     *
     * @param requestJson
     * @return
     */
    @PostMapping(value = "/batchUpdateUnitBid")
    public Result<JSONObject> batchUpdateUnitBid(@RequestBody JSONObject requestJson) {
        Result<JSONObject> result = new Result<>();
        try {
            Long accountId = requestJson.getLong("accountId");
            CtopOauthToken token = tokenService.getTokenByAccountId(accountId);
            if (Check.isNull(token)) {
                throw new Exception("账号信息为空");
            }
            Long bid = requestJson.getLong("bid");
            if (bid == null) {
                throw new Exception("请输入出价");
            }
            String userId = requestJson.getString("userId");
            JSONArray unitIds = requestJson.getJSONArray("unitIds");
            JSONArray failArr = new JSONArray();
            if (!Check.isNull(unitIds)) {
                for (int i = 0; i < unitIds.size(); i++) {
                    Long unitId = unitIds.getLong(i);
                    if (!Check.isNull(unitId)) {
                        Map<String, Object> updateMap = updateService.updateUnitBid(token.getAccessToken(), accountId, unitId, bid, userId);
                        if (!Check.isNull(updateMap)) {
                            Integer code = (Integer) updateMap.get("code");
                            if (code != 0) {
                                JSONObject failJson = new JSONObject();
                                failJson.put("message", updateMap.get("message"));
                                KuaiShouGroup group = batchService.getUnitInfo(accountId, unitId);
                                if (!Check.isNull(group)) {
                                    failJson.put("campaignName", group.getUnitName());
                                }
                                failArr.add(failJson);
                            }
                        }
                    }
                }
            }
            JSONObject json = new JSONObject();
            json.put("totalCount", unitIds.size());
            json.put("failCount", failArr.size());
            json.put("failInfo", failArr);
            result.setResult(json);
            result.setSuccess(true);
        } catch (Exception e) {
            e.printStackTrace();
            result.setSuccess(false);
            result.setMessage(e.getMessage());
        }
        return result;
    }


}
