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.OaBusinessProductVo;
import com.ruoyi.oa.domain.bo.OaBusinessProductBo;
import com.ruoyi.oa.service.IOaBusinessProductService;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * CRM 商机产品关联
 *
 * @author ruoyi
 * @date 2025-03-12
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/oa/businessProduct")
public class OaBusinessProductController extends BaseController {

    private final IOaBusinessProductService iOaBusinessProductService;

    /**
     * 查询CRM 商机产品关联列表
     */
    @GetMapping("/list")
    public TableDataInfo<OaBusinessProductVo> list(OaBusinessProductBo bo, PageQuery pageQuery) {
        return iOaBusinessProductService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出CRM 商机产品关联列表
     */
    @Log(title = "CRM 商机产品关联", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(OaBusinessProductBo bo, HttpServletResponse response) {
        List<OaBusinessProductVo> list = iOaBusinessProductService.queryList(bo);
        ExcelUtil.exportExcel(list, "CRM 商机产品关联", OaBusinessProductVo.class, response);
    }

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

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

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

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