package org.jeecg.modules.ctop.controller;

import cn.com.ctop.common.module.service.IAdqAccountClaimService;
import com.alibaba.fastjson.JSONObject;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.vo.LoginUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 腾讯ADQ账户认领
 *
 * @author jeecg-boot
 */
@Slf4j
@Api(tags = "腾讯ADQ账户认领")
@RestController
@RequestMapping("/ctop/adqAccountClaim")
public class AdqAccountClaimController {

    @Autowired
    private IAdqAccountClaimService adqAccountClaimService;

    /**
     * 查询未认领的ADQ账户列表
     */
    @ApiOperation(value = "查询未认领的ADQ账户", notes = "查询在adq_advertiser_info中且不在ctop_user_allocation中的账户")
    @GetMapping(value = "/unclaimedList")
    public Result<JSONObject> queryUnclaimedAccounts(
            @RequestParam(name = "accountIds", required = false) String accountIds,
            @RequestParam(name = "advertiserName", required = false) String advertiserName,
            @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
            @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize) {
        Result<JSONObject> result = new Result<>();
        try {
            JSONObject data = adqAccountClaimService.queryUnclaimedAccounts(accountIds, advertiserName, pageNo, pageSize);
            result.setSuccess(true);
            result.setResult(data);
        } catch (Exception e) {
            log.error("查询未认领账户失败", e);
            result.setSuccess(false);
            result.setMessage("查询失败：" + e.getMessage());
        }
        return result;
    }

    /**
     * 查询腾讯ADQ项目列表（media_id=16，且当前用户有权限的项目）
     */
    @ApiOperation(value = "查询腾讯ADQ项目列表", notes = "查询media_id=16且当前用户在ctop_project_member中的项目")
    @GetMapping(value = "/projectList")
    public Result<List<JSONObject>> queryAdqProjects(
            @RequestParam(name = "projectName", required = false) String projectName,
            HttpServletRequest req) {
        Result<List<JSONObject>> result = new Result<>();
        try {
            LoginUser loginUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
            String userId = loginUser.getId();
            List<JSONObject> projects = adqAccountClaimService.queryAdqProjects(projectName, userId);
            result.setSuccess(true);
            result.setResult(projects);
        } catch (Exception e) {
            log.error("查询项目列表失败", e);
            result.setSuccess(false);
            result.setMessage("查询失败：" + e.getMessage());
        }
        return result;
    }

    /**
     * 批量认领账户
     */
    @ApiOperation(value = "批量认领账户", notes = "将选中的ADQ账户认领到指定项目下")
    @PostMapping(value = "/batchClaim")
    public Result<Object> batchClaimAccounts(@RequestBody JSONObject claimData) {
        Result<Object> result = new Result<>();
        try {
            adqAccountClaimService.batchClaimAccounts(claimData);
            result.setSuccess(true);
            result.setMessage("账户认领成功");
        } catch (Exception e) {
            log.error("批量认领账户失败", e);
            result.setSuccess(false);
            result.setMessage(e.getMessage());
        }
        return result;
    }
}
