package com.spider.quartz.controller;

import com.spider.common.annotation.Log;
import com.spider.common.core.controller.BaseController;
import com.spider.common.core.domain.AjaxResult;
import com.spider.common.core.page.TableDataInfo;
import com.spider.common.enums.BusinessType;
import com.spider.common.utils.StringUtils;
import com.spider.common.utils.poi.ExcelUtil;
import com.spider.quartz.domain.UsUnqualifiedGoods;
import com.spider.quartz.service.IUsUnqualifiedGoodsService;
import com.spider.quartz.task.CpscTask;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

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

/**
 * 美国不合格商品展示Controller
 * 
 * @author ruoyi
 * @date 2025-11-26
 */
@Slf4j
@RestController
@RequestMapping("/bus/us")
public class UsUnqualifiedGoodsController extends BaseController
{
    @Autowired
    private IUsUnqualifiedGoodsService usUnqualifiedGoodsService;
    @Autowired
    private CpscTask cpscTask;

    /**
     * 查询美国不合格商品展示列表
     */
    @GetMapping("/list")
    @PreAuthorize("@ss.hasPermi('bus:us:list')")
    public TableDataInfo list(UsUnqualifiedGoods usUnqualifiedGoods)
    {
        startPage();
        List<UsUnqualifiedGoods> list = usUnqualifiedGoodsService.selectUsUnqualifiedGoodsList(usUnqualifiedGoods);
        return getDataTable(list);
    }

    /**
     * 导出美国不合格商品展示列表
     */
    @Log(title = "美国不合格商品展示", businessType = BusinessType.EXPORT)
    @PreAuthorize("@ss.hasPermi('bus:us:export')")
    @PostMapping("/export")
    public void export(HttpServletResponse response, UsUnqualifiedGoods usUnqualifiedGoods)
    {
        List<UsUnqualifiedGoods> list = usUnqualifiedGoodsService.selectUsUnqualifiedGoodsList(usUnqualifiedGoods);
        ExcelUtil<UsUnqualifiedGoods> util = new ExcelUtil<UsUnqualifiedGoods>(UsUnqualifiedGoods.class);
        util.exportExcel(response, list, "美国不合格商品展示数据");
    }

    /**
     * 获取美国不合格商品展示详细信息
     */
    @GetMapping(value = "/{id}")
    @PreAuthorize("@ss.hasPermi('bus:us:query')")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(usUnqualifiedGoodsService.selectUsUnqualifiedGoodsById(id));
    }

    /**
     * 新增美国不合格商品展示
     */
    @Log(title = "美国不合格商品展示", businessType = BusinessType.INSERT)
    @PreAuthorize("@ss.hasPermi('bus:us:add')")
    @PostMapping
    public AjaxResult add(@RequestBody UsUnqualifiedGoods usUnqualifiedGoods)
    {
        return toAjax(usUnqualifiedGoodsService.insertUsUnqualifiedGoods(usUnqualifiedGoods));
    }

    /**
     * 修改美国不合格商品展示
     */
    @Log(title = "美国不合格商品展示", businessType = BusinessType.UPDATE)
    @PreAuthorize("@ss.hasPermi('bus:us:updata')")
    @PutMapping
    public AjaxResult edit(@RequestBody UsUnqualifiedGoods usUnqualifiedGoods)
    {
        return toAjax(usUnqualifiedGoodsService.updateUsUnqualifiedGoods(usUnqualifiedGoods));
    }

    /**
     * 删除美国不合格商品展示
     */
    @Log(title = "美国不合格商品展示", businessType = BusinessType.DELETE)
    @PreAuthorize("@ss.hasPermi('bus:us:delete')")
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(usUnqualifiedGoodsService.deleteUsUnqualifiedGoodsByIds(ids));
    }

    @PostMapping("/manual")
    @PreAuthorize("@ss.hasPermi('bus:us:manual')")
    public String manualCrawl() {
        new Thread(() -> {
            cpscTask.startCrawl(true);
        }).start();
        return "手动爬取任务已启动";
    }

    @PostMapping("/scheduled")
    public String scheduledCrawl() {
        new Thread(() -> {
            cpscTask.startCrawl(false);
        }).start();
        return "定时爬取任务已启动";
    }

}
