package com.ruoyi.oa.controller;

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 cn.dev33.satoken.annotation.SaCheckPermission;
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.OaProductVo;
import com.ruoyi.oa.domain.bo.OaProductBo;
import com.ruoyi.oa.service.IOaProductService;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * CRM 产品
 *
 * @author ruoyi
 * @date 2025-03-12
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/oa/product")
public class OaProductController extends BaseController {

    private final IOaProductService iOaProductService;

    /**
     * 查询CRM 产品列表
     */
    @GetMapping("/list")
    public TableDataInfo<OaProductVo> list(OaProductBo bo, PageQuery pageQuery) {
        return iOaProductService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出CRM 产品列表
     */
    @Log(title = "CRM 产品", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(OaProductBo bo, HttpServletResponse response) {
        List<OaProductVo> list = iOaProductService.queryList(bo);
        ExcelUtil.exportExcel(list, "CRM 产品", OaProductVo.class, response);
    }

    /**
     * 获取CRM 产品详细信息
     *
     * @param productId 主键
     */
    @GetMapping("/{productId}")
    public R<OaProductVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long productId) {
        return R.ok(iOaProductService.queryById(productId));
    }

    /**
     * 新增CRM 产品
     */
    @Log(title = "CRM 产品", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody OaProductBo bo) {
        return toAjax(iOaProductService.insertByBo(bo));
    }

    /**
     * 修改CRM 产品
     */
    @Log(title = "CRM 产品", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody OaProductBo bo) {
        return toAjax(iOaProductService.updateByBo(bo));
    }

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