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.OaClueVo;
import com.ruoyi.oa.domain.bo.OaClueBo;
import com.ruoyi.oa.service.IOaClueService;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * CRM 线索
 *
 * @author ruoyi
 * @date 2025-03-12
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/oa/clue")
public class OaClueController extends BaseController {

    private final IOaClueService iOaClueService;

    /**
     * 查询CRM 线索列表
     */
    @GetMapping("/list")
    public TableDataInfo<OaClueVo> list(OaClueBo bo, PageQuery pageQuery) {
        return iOaClueService.queryPageListVo(bo, pageQuery);
    }

    /**
     * 导出CRM 线索列表
     */
    @Log(title = "CRM 线索", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(OaClueBo bo, HttpServletResponse response) {
        List<OaClueVo> list = iOaClueService.queryList(bo);
        ExcelUtil.exportExcel(list, "CRM 线索", OaClueVo.class, response);
    }

    /**
     * 获取CRM 线索详细信息
     *
     * @param clueId 主键
     */
    @GetMapping("/{clueId}")
    public R<OaClueVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long clueId) {
        return R.ok(iOaClueService.queryById(clueId));
    }

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

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

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