package org.jeecg.modules.third.controller;

import cn.com.ctop.common.module.utils.Check;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.modules.third.entity.ThirdConfig;
import org.jeecg.modules.third.entity.ThirdConfigAdvertiser;
import org.jeecg.modules.third.server.IThirdConfigAdvertiserService;
import org.jeecg.modules.third.server.IThirdConfigService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 对外配置
 *
 * @author jeecg-boot
 * @version V1.0
 * @date 2020-08-14
 */
@Slf4j
@Api(tags = "对外配置")
@RestController
@RequestMapping("/third")
public class ThirdConfigController {
    @Autowired
    private IThirdConfigService thirdConfigService;
    @Autowired
    private IThirdConfigAdvertiserService thirdConfigAdvertiserService;

    @PostMapping(value = "/getAdvertiserList")
    public Result<List<String>> getAdvertiserList(@RequestBody JSONObject requestJson) {

        Result<List<String>> result = new Result<>();
        try {
            Long appId = requestJson.getLong("appId");
            if (Check.isNull(appId)) {
                throw new Exception("必传参数appId不能为空");
            }
            QueryWrapper<ThirdConfig> thirdConfigQueryWrapper = new QueryWrapper<>();
            thirdConfigQueryWrapper.eq("app_id", appId);
            ThirdConfig config = thirdConfigService.getOne(thirdConfigQueryWrapper);
            if (Check.isNull(config)) {
                throw new Exception("appId不正确");
            }
            String secret = requestJson.getString("secret");
            if (Check.isNull(secret)) {
                throw new Exception("secret不能为空");
            }
            if (!secret.equals(config.getSecret())) {
                throw new Exception("secret不正确");
            }

            List<String> advertiserList = thirdConfigAdvertiserService.getAdvertiserListByAppId(appId);
            result.setResult(advertiserList);

        } catch (Exception e) {
            result.setSuccess(false);
            result.setMessage(e.getMessage());
            result.setCode(-1);

        }
        return result;
    }


    @PostMapping(value = "/getAdvertiserCost")
    public Result<List<JSONObject>> getAdvertiserCost(@RequestBody JSONObject requestJson) {
        Result<List<JSONObject>> result = new Result<>();
        try {

            Long appId = requestJson.getLong("appId");
            if (Check.isNull(appId)) {
                throw new Exception("必传参数appId不能为空");
            }
            QueryWrapper<ThirdConfig> thirdConfigQueryWrapper = new QueryWrapper<>();
            thirdConfigQueryWrapper.eq("app_id", appId);
            ThirdConfig config = thirdConfigService.getOne(thirdConfigQueryWrapper);
            if (Check.isNull(config)) {
                throw new Exception("appId不正确");
            }
            String secret = requestJson.getString("secret");
            if (Check.isNull(secret)) {
                throw new Exception("secret不能为空");
            }
            if (!secret.equals(config.getSecret())) {
                throw new Exception("secret不正确");
            }

            String advertiserName = requestJson.getString("advertiserName");
            if (Check.isNull(advertiserName)) {
                throw new Exception("请输入需要查询广告主名称");
            }

            QueryWrapper<ThirdConfigAdvertiser> thirtConfigAdvertiserQueryWrapper = new QueryWrapper<>();
            thirtConfigAdvertiserQueryWrapper.eq("app_id", appId);
            thirtConfigAdvertiserQueryWrapper.eq("advertiser_name", advertiserName);
            thirtConfigAdvertiserQueryWrapper.last("limit 1");
            ThirdConfigAdvertiser configAdvertiser = thirdConfigAdvertiserService.getOne(thirtConfigAdvertiserQueryWrapper);
            if (Check.isNull(configAdvertiser)) {
                throw new Exception("此appId下暂无此广告主信息");

            }
            String startDate = requestJson.getString("startDate");
            if (Check.isNull(startDate)) {
                throw new Exception("请输入查询开始时间");
            }

            String endDate = requestJson.getString("endDate");
            if (Check.isNull(endDate)) {
                throw new Exception("请输入查询结束时间");
            }


            List<JSONObject> costList = thirdConfigAdvertiserService.getAdvertiserCost(appId, advertiserName, startDate, endDate);
            result.setResult(costList);
        } catch (Exception e) {
            result.setCode(-1);
            result.setSuccess(false);
            result.setMessage(e.getMessage());
        }
        return result;
    }

}
