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

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.modules.bytedance.common.entity.MaterialInfo;
import org.jeecg.modules.bytedance.common.service.IMaterialInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;
import java.util.List;
import java.util.UUID;

/**
 * 头条智能投放账户配置信息表
 * @author jeecg-boot
 * @date   2021-04-20
 * @version V1.0
 */
@RestController
@RequestMapping("/advertiser/materialInfoController")
@Api(tags="视频素材")
@Slf4j
public class MaterialInfoController {
	@Autowired
	private IMaterialInfoService materialInfoService;


	@ApiOperation(value = "素材下线", notes = "素材下线")
	@GetMapping(value = "/updMaterialOfflineById")
	public Result updMaterialOfflineById(@RequestParam("id") String id,
										 @RequestParam("offlineFlag") Integer offlineFlag,
										 @RequestParam("userId") String userId
	) {
		try {
			List<String> listIds = Arrays.asList(id.split(","));
			if (listIds.size() > 8) {
				return Result.errorMsg("已选视频素材最多支持8个。");
			}
			return materialInfoService.updMaterialOfflineById(listIds, offlineFlag,userId);
		} catch (Exception e) {
			log.error("素材下线异常", e);
			return Result.error("请求失败，请联系开发人员!");
		}
	}


	/**
	 * ============================================================================================================
	 * 2021-09-23  zianY
	 * describe: 该项目 淘宝特价版-拉活-头条 (1740895)  中 素材名称 包含 ‘0元购’ 同步素材数据到 头条-淘特-拉活-0元购素材项目(4380885)
	 */
	@GetMapping(value = "/initMaterialInfoDate")
	public Result initMaterialInfoDate() {
		QueryWrapper<MaterialInfo> queryWrapper = new QueryWrapper<>();
		queryWrapper.eq("project_id", 1740895);
		queryWrapper.like("material_name", "%0元购%");
		List<MaterialInfo> list = materialInfoService.list(queryWrapper);
		for (MaterialInfo materialInfo : list) {
			materialInfo.setId(UUID.randomUUID().toString().replaceAll("-", ""));
			materialInfo.setProjectId(4380885L);
			materialInfoService.save(materialInfo);
		}
		return Result.successMsg("成功", null);
	}
//* ============================================================================================================



}
