package com.ruoyi.oa.controller;

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

import com.ruoyi.oa.domain.bo.OaFollowUpRecordAddBo;
import com.ruoyi.oa.service.IOaFollowUpRecordService;
import lombok.RequiredArgsConstructor;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.*;
import org.springframework.beans.factory.annotation.Autowired;
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.OaCustomerVo;
import com.ruoyi.oa.domain.bo.OaCustomerBo;
import com.ruoyi.oa.service.IOaCustomerService;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * CRM 客户
 *
 * @author ruoyi
 * @date 2025-03-12
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/oa/customer")
public class OaCustomerController extends BaseController {

    private final IOaCustomerService iOaCustomerService;

    @Autowired
    private IOaFollowUpRecordService iOaFollowUpRecordService;

    /**
     * 查询CRM 客户列表
     */
    @GetMapping("/list")
    public TableDataInfo<OaCustomerVo> list(OaCustomerBo bo, PageQuery pageQuery) {
        return iOaCustomerService.queryPageListToUser(bo, pageQuery);
    }

    /**
     * 查询CRM 客户列表
     */
    @GetMapping("/list-sea")
    public TableDataInfo<OaCustomerVo> seaList(OaCustomerBo bo, PageQuery pageQuery) {
        return iOaCustomerService.queryPageListToSea(bo, pageQuery);
    }

    /**
     * 导出CRM 客户列表
     */
    @Log(title = "CRM 客户", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(OaCustomerBo bo, HttpServletResponse response) {
        List<OaCustomerVo> list = iOaCustomerService.queryList(bo);
        ExcelUtil.exportExcel(list, "CRM 客户", OaCustomerVo.class, response);
    }

    /**
     * 获取CRM 客户详细信息
     *
     * @param customerId 主键
     */
    @GetMapping("/{customerId}")
    public R<OaCustomerVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long customerId) {
        return R.ok(iOaCustomerService.queryById(customerId));
    }

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

    /**
     * 修改CRM 客户
     */

    @Log(title = "CRM 客户", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody OaCustomerBo bo) {
        return toAjax(iOaCustomerService.updateByBo(bo));
    }

    /**
     * 删除CRM 客户
     *
     * @param customerIds 主键串
     */

    @Log(title = "CRM 客户", businessType = BusinessType.DELETE)
    @DeleteMapping("/{customerIds}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] customerIds) {
        return toAjax(iOaCustomerService.deleteWithValidByIds(Arrays.asList(customerIds), true));
    }

    /**
     * 添加跟进记录
     */
    @PostMapping("/follow")
    public R<Void> addFollow(@Validated(AddGroup.class) @RequestBody OaFollowUpRecordAddBo bo) {
        return toAjax(iOaFollowUpRecordService.insertByAddBo(bo));

    }
}
