package org.xyzh.crontab.controller;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.xyzh.api.crontab.DataCollectionItemService;
import org.xyzh.common.core.domain.ResultDomain;
import org.xyzh.common.core.page.PageParam;
import org.xyzh.common.core.page.PageRequest;
import org.xyzh.common.dto.crontab.TbDataCollectionItem;
import org.xyzh.common.vo.DataCollectionItemVO;

import java.util.List;

/**
 * @description 数据采集项控制器
 * @filename DataCollectionItemController.java
 * @author yslg
 * @copyright xyzh
 * @since 2025-11-08
 */
@RestController
@RequestMapping("/crontab/collection/item")
public class DataCollectionItemController {

    private static final Logger logger = LoggerFactory.getLogger(DataCollectionItemController.class);

    @Autowired
    private DataCollectionItemService itemService;

    /**
     * @description    查看一个任务日志对应创建的所有数据采集项
	 * @param taskLogId 日志ID
	 * @return ResultDomain<DataCollectionItemVO>
     */
    @GetMapping("/task/{taskLogId}")
    public ResultDomain<DataCollectionItemVO> getTaskLogDataCollectionItemList(@PathVariable String taskLogId) {
		TbDataCollectionItem filter = new TbDataCollectionItem();
		filter.setLogId(taskLogId);
		return itemService.getItemList(filter);
    }

    /**
     * @description    获取数据采集项分页列表
	 * @param pageRequest 分页请求（filter支持按logId、taskId、status等过滤）
	 * @return ResultDomain<DataCollectionItemVO>
     */
    @PostMapping("/page")
	public ResultDomain<DataCollectionItemVO> getCollectionItemPage(@RequestBody PageRequest<TbDataCollectionItem> pageRequest) {
		TbDataCollectionItem filter = pageRequest.getFilter();
		PageParam pageParam = pageRequest.getPageParam();
		return itemService.getItemPage(filter, pageParam);
    }

    /**
     * @description    转换成文章
	 * @param request 转换请求（itemId、tagId）
	 * @return ResultDomain<DataCollectionItemVO>
     */
    @PostMapping("/resource")
	public ResultDomain<String> convertToArticle(@RequestBody ConvertRequest request) {
		return itemService.convertToResource(request.getItemId(), request.getTagId());
    }

	@PutMapping("/{itemId}/status/{status}")
	public ResultDomain<String> updateItemStatus(@PathVariable(name = "itemId") String itemId, @PathVariable(name = "status") int status) {
		return itemService.updateItemStatus(itemId, status);
	}

	/**
	 * @description 转换请求
	 */
	public static class ConvertRequest {
		private String itemId;
		private String tagId;

		public String getItemId() {
			return itemId;
		}

		public void setItemId(String itemId) {
			this.itemId = itemId;
		}

		public String getTagId() {
			return tagId;
		}

		public void setTagId(String tagId) {
			this.tagId = tagId;
		}
	}
}

