package org.jeecg.modules.bytedance.advertise.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.modules.bytedance.advertise.service.IMonitorBytedanceService;
import org.jeecg.modules.bytedance.advertise.vo.MonitorBytedanceAdVo;
import org.jeecg.modules.bytedance.common.constant.BytedanceConstant;
import org.jeecg.modules.bytedance.common.service.IUserAllocationService;
import org.jeecg.modules.bytedance.common.utils.Check;
import org.jeecg.modules.system.service.ISysRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashSet;
import java.util.Set;

/**
 * 头条定时任务-监测连接预警
 * @author zian Y
 * @date   2021-12-28
 * @version V1.0
 */
@RestController
@RequestMapping("/ad/bytedancePlanJobController")
@Api(tags="头条定时任务")
@Slf4j
public class BytedancePlanJobController {

	@Autowired
	private IMonitorBytedanceService monitorBytedanceService;

	@Resource
	private IUserAllocationService userAllocationService;

	@Autowired
	private ISysRoleService sysRoleService;;

	@ApiOperation(value = "监测预警-点击监测链接", notes = "监测预警-点击监测链接")
	@GetMapping(value = "/monitorBytedanceActionTrackUrl")
	public Result monitorBytedanceActionTrackUrl(@RequestParam String accountId) {
		return monitorBytedanceService.monitorBytedanceActionTrackUrl(accountId);
	}


	@ApiOperation(value = "监测预警-新增", notes = "监测预警-新增")
	@PostMapping(value = "/insertAccountTrackUrl")
	public Result insertAccountTrackUrl(@RequestBody MonitorBytedanceAdVo monitorBytedanceAdVo) {
		return monitorBytedanceService.insertAccountTrackUrl(monitorBytedanceAdVo);
	}

	@ApiOperation(value = "监测预警-修改", notes = "监测预警-修改")
	@PostMapping(value = "/updAccountActionTrackUrl")
	public Result updAccountActionTrackUrl(@RequestBody MonitorBytedanceAdVo monitorBytedanceAdVo) {
		return monitorBytedanceService.updAccountActionTrackUrl(monitorBytedanceAdVo);
	}


	@ApiOperation(value = "监测预警-查询", notes = "监测预警-查询")
	@GetMapping(value = "/selectAccountActionTrackUrlList")
	public Result selectAccountActionTrackUrlList(
			@ApiParam("用户id") @RequestParam(value = "userId") String userId,
			@ApiParam("账户id") @RequestParam(value = "accountId",required = false) String accountId,
			@ApiParam("是否开启监测链接预警 1-开启 2-不开启") @RequestParam(value = "warningFlag",required = false) String warningFlag,
			@RequestParam(value = "pageNo",defaultValue = "1") Integer pageNo,
			@RequestParam(value = "pageSize",defaultValue = "10") Integer pageSize) {
		//获取用户角色code
		String roleCode = sysRoleService.getRoleCodeByUserId(userId);
		if (Check.isNull(roleCode)){
			return Result.errorMsg("无法查询到该用户角色信息。");
		}
		Set<String> operatorUserIds = new HashSet<>();
		if(!BytedanceConstant.COMMON_ROLE_CODE_ADMIN.equals(roleCode)){
			//查询所有包含自己的下级
			operatorUserIds = userAllocationService.recursiveQuerySubordinate(userId);
		}
		return monitorBytedanceService.selectAccountActionTrackUrlList(accountId,warningFlag,operatorUserIds,pageNo,pageSize);


	}



}
