package cn.gov.customs.wxjy.task.controller;

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

import cn.gov.customs.cacp.sdks.core.result.Result;
import cn.gov.customs.cacp.sdks.core.user.trans.CacpTransUser;
import cn.gov.customs.cacp.sdks.core.user.trans.UserContextHolder;
import cn.gov.customs.wxjy.common.utils.StringUtils;
import cn.gov.customs.wxjy.ibmmq.enums.BillTypeEnum;
import cn.gov.customs.wxjy.task.ScheduledTasks;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;
import cn.gov.customs.wxjy.common.core.controller.BaseController;
import org.springframework.web.multipart.MultipartFile;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import cn.gov.customs.wxjy.task.pojo.TaskLog;
import cn.gov.customs.wxjy.task.pojo.TaskLogQuery;
import cn.gov.customs.wxjy.task.service.ITaskLogService;
import cn.gov.customs.wxjy.common.utils.poi.ExcelUtil;
import cn.gov.customs.wxjy.common.core.page.TableDataInfo;

/**
 * 定时任务执行时间记录Controller
 *
 * @author xiong
 * @date 2025-12-17
 */
@RestController
@RequestMapping("/task/TaskLog")
@RequiredArgsConstructor
public class TaskLogController extends BaseController {

    private final ITaskLogService taskLogService;

    private final ScheduledTasks scheduledTasks;

    /**
     * 查询定时任务执行时间记录列表
     */
    @PostMapping("/get-list")
    public Result<PageInfo<TaskLog>> list(@RequestBody TaskLogQuery query) {
        PageHelper.startPage(query.getPageIndex(), query.getPageSize());
        PageInfo<TaskLog> list = taskLogService.selectPageList(query);
        return Result.success(list);
    }

    /**
     * 获取定时任务执行时间记录详细信息
     */
    @GetMapping(value = "/get-detail")
    public Result<TaskLog> getInfo(@RequestParam String pkTaskLog) {
        return Result.success(taskLogService.selectByPkTaskLog(pkTaskLog));
    }

    /**
     * 新增定时任务执行时间记录
     */
    @PostMapping("/insert-taskLog")
    public Result<Integer> add(@RequestBody TaskLog taskLog) {
        return Result.success(taskLogService.insert(taskLog));
    }

    /**
     * 修改定时任务执行时间记录
     */
    @PostMapping("/update-taskLog")
    public Result<Integer> edit(@RequestBody TaskLog taskLog) {
        return Result.success(taskLogService.update(taskLog));
    }

    /**
     * 删除定时任务执行时间记录
     */
    @PostMapping("/delete-taskLog")
    public Result<Integer> remove(@RequestBody String[] ids) {
        return Result.success(taskLogService.deleteByPkTaskLogs(ids));
    }

    /**
     * 收到触发数据同步，推送到企业操作
     * @param businessType 模块类型
     */
    @PostMapping("/scheduled-task")
    public void remove(@RequestBody String businessType) {
        if(businessType.equals(BillTypeEnum.BUS_TYPE_PACK_CATALOG.getCode())){
            //包装种类
            scheduledTasks.schedulePackCatalogTask();
        }else if(businessType.equals(BillTypeEnum.BUS_TYPE_CHEMICALS_CATALOG.getCode())){
            //危化品
            scheduledTasks.scheduleChemicalsCatalogTask();
        }else if(businessType.equals(BillTypeEnum.BUS_TYPE_GOODS_CATALOG.getCode())){
            //危险货物
            scheduledTasks.scheduleGoodsCatalogTask();
        }
    }
}
