package com.ruoyi.oa.controller;

import java.io.IOException;
import java.util.List;
import java.util.Arrays;
import java.util.concurrent.TimeUnit;

import lombok.RequiredArgsConstructor;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.*;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.validate.AddGroup;
import com.ruoyi.common.core.validate.EditGroup;
import com.ruoyi.common.core.validate.QueryGroup;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.oa.domain.vo.OaExpressVo;
import com.ruoyi.oa.domain.bo.OaExpressBo;
import com.ruoyi.oa.service.IOaExpressService;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 物流预览
 *
 * @author hdka
 * @date 2025-07-20
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/oa/express")
public class OaExpressController extends BaseController {

    private final IOaExpressService iOaExpressService;

    /**
     * 查询物流预览列表
     */
    @GetMapping("/list")
    public TableDataInfo<OaExpressVo> list(OaExpressBo bo, PageQuery pageQuery) {
        return iOaExpressService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出物流预览列表
     */
    @Log(title = "物流预览", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(OaExpressBo bo, HttpServletResponse response) {
        List<OaExpressVo> list = iOaExpressService.queryList(bo);
        ExcelUtil.exportExcel(list, "物流预览", OaExpressVo.class, response);
    }

    /**
     * 获取物流预览详细信息
     *
     * @param expressId 主键
     */
    @GetMapping("/{expressId}")
    public R<OaExpressVo> getInfo(@NotNull(message = "主键不能为空")
                                  @PathVariable Long expressId) {
        return R.ok(iOaExpressService.queryById(expressId));
    }

    /**
     * 新增物流预览
     */
    @Log(title = "物流预览", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody OaExpressBo bo) {
        return toAjax(iOaExpressService.insertByBo(bo));
    }

    /**
     * 修改物流预览
     */
    @Log(title = "物流预览", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody OaExpressBo bo) {
        return toAjax(iOaExpressService.updateByBo(bo));
    }

    /**
     * 删除物流预览
     *
     * @param expressIds 主键串
     */
    @Log(title = "物流预览", businessType = BusinessType.DELETE)
    @DeleteMapping("/{expressIds}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] expressIds) {
        return toAjax(iOaExpressService.deleteWithValidByIds(Arrays.asList(expressIds), true));
    }


    @GetMapping("/refresh/{expressIds}")
    public R<Void> getRefreshExpress(@PathVariable Long[] expressIds) throws IOException {
        return toAjax(iOaExpressService.getRefreshExpress(Arrays.asList(expressIds)));
    }
}
