package com.ruoyi.oa.controller;


import java.util.List;
import java.util.Arrays;

import com.ruoyi.oa.domain.vo.OaFollowUpRecordQueryVo;
import com.ruoyi.oa.service.IOaBusinessService;
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.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.oa.domain.vo.OaFollowUpRecordVo;
import com.ruoyi.oa.domain.bo.OaFollowUpRecordBo;
import com.ruoyi.oa.service.IOaFollowUpRecordService;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * CRM 跟进记录
 *
 * @author ruoyi
 * @date 2025-03-12
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/oa/followUpRecord")
public class OaFollowUpRecordController extends BaseController {

    private final IOaFollowUpRecordService iOaFollowUpRecordService;

    private final IOaBusinessService iOaBusinessService;

    /**
     * 查询CRM 跟进记录列表
     */

    @GetMapping("/list")
    public TableDataInfo<OaFollowUpRecordVo> list(OaFollowUpRecordBo bo, PageQuery pageQuery) {
        return iOaFollowUpRecordService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出CRM 跟进记录列表
     */
    @Log(title = "CRM 跟进记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(OaFollowUpRecordBo bo, HttpServletResponse response) {
        List<OaFollowUpRecordVo> list = iOaFollowUpRecordService.queryList(bo);
        ExcelUtil.exportExcel(list, "CRM 跟进记录", OaFollowUpRecordVo.class, response);
    }

    /**
     * 获取CRM 跟进记录详细信息
     *
     * @param followId 主键
     */
    @GetMapping("/{followId}")
    public R<OaFollowUpRecordVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long followId) {
        return R.ok(iOaFollowUpRecordService.queryById(followId));
    }

    /**
     * CRM 商机
     *
     * @param businessIds 主键串
     */
    @GetMapping("/listByIds/{businessIds}")
    public R<List<OaFollowUpRecordVo>> selectByBusinessIds(
                          @PathVariable("businessIds") Long[] businessIds) {
        return R.ok(iOaFollowUpRecordService.selectByBusinessIds(Arrays.asList(businessIds)));
    }

    /**
     * 新增CRM 跟进记录
     */
    @Log(title = "CRM 跟进记录", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody OaFollowUpRecordBo bo) {
        return toAjax(iOaFollowUpRecordService.insertByBo(bo));
    }

    /**
     * 修改CRM 跟进记录
     */
    @Log(title = "CRM 跟进记录", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody OaFollowUpRecordBo bo) {
        return toAjax(iOaFollowUpRecordService.updateByBo(bo));
    }

    /**
     * 删除CRM 跟进记录
     *
     * @param followIds 主键串
     */
    @Log(title = "CRM 跟进记录", businessType = BusinessType.DELETE)
    @DeleteMapping("/{followIds}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] followIds) {
        return toAjax(iOaFollowUpRecordService.deleteWithValidByIds(Arrays.asList(followIds), true));
    }
    /**
     * 根据用户id查询跟进记录列表
     */
    @GetMapping("/list/{customerId}")
    public R<OaFollowUpRecordQueryVo> selectByCustomerIdId(
            @NotNull(message = "顾客id不能为空")
            @PathVariable Long customerId) {
        return R.ok(iOaFollowUpRecordService.selectByCustomerId(customerId));
    }
}
