package com.ruoyi.oa.controller;

import java.util.List;
import java.util.Arrays;
import java.util.concurrent.TimeUnit;
import com.ruoyi.oa.domain.OaBusiness;
import com.ruoyi.oa.domain.vo.OaProductVo;
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.OaBusinessVo;
import com.ruoyi.oa.domain.bo.OaBusinessBo;
import com.ruoyi.oa.service.IOaBusinessService;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * CRM 商机
 *
 * @author ruoyi
 * @date 2025-03-12
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/oa/business")
public class OaBusinessController extends BaseController {

    private final IOaBusinessService iOaBusinessService;

    /**
     * 查询CRM 商机列表
     */
    @GetMapping("/list")
    public TableDataInfo<OaBusinessVo> list(OaBusinessBo bo, PageQuery pageQuery) {
        return iOaBusinessService.queryPageList(bo, pageQuery);
    }

    /**
     * 查询CRM 商机列表
     */
    @GetMapping("/listByCustomer")
    public TableDataInfo<OaBusinessVo> listByCustomer(OaBusinessBo bo, PageQuery pageQuery) {
        return iOaBusinessService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出CRM 商机列表
     */
    @Log(title = "CRM 商机", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(OaBusinessBo bo, HttpServletResponse response) {
        List<OaBusinessVo> list = iOaBusinessService.queryList(bo);
        ExcelUtil.exportExcel(list, "CRM 商机", OaBusinessVo.class, response);
    }

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

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

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

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


    /**
     * 联查商机与产品以及其关联表
     */
    @GetMapping("/BusinessProducts")
    public TableDataInfo<OaProductVo> getBusinessProducts(OaBusinessBo bo) {
        return iOaBusinessService.getBusinessProducts(bo);
    }
    /**
     * 根据客户ID获取商机信息
     * 该接口通过客户ID分页查询相关的商机信息，并返回包含商机信息的TableDataInfo对象
     *
     * @param customerId 客户ID，用于查询相关的商机信息
     * @param pageQuery  分页查询参数，包含分页信息和排序信息
     * @return 返回包含商机信息的TableDataInfo对象
     */
    @GetMapping("/customer/{customerId}")
    @Log(title = "CRM 商机", businessType = BusinessType.EXPORT)
    public TableDataInfo<OaBusiness> getBusinessByCustomerId(@PathVariable Long customerId, PageQuery pageQuery){
        return iOaBusinessService.getBusinessByCustomerId(customerId,pageQuery);
    }
    /**
     * 删除CRM商机
     * 根据传入的商机ID删除对应的商机记录
     *
     * @param businessId 商机ID
     * @return 操作成功返回R.ok()
     */
   @Log(title = "CRM 商机", businessType = BusinessType.DELETE)
    @DeleteMapping
    public R<Void> deleteBusiness(@RequestParam Long businessId){
        iOaBusinessService.deleteBusiness(businessId);
        return R.ok();
    }
    /**
     * 设置跟进业务接口
     * 通过商机ID设置关注该商机
     *
     * @param businessId 商机ID
     * @return 返回操作结果，成功则返回R.ok()
     */
    @GetMapping("/follow/{businessId}")
    public R<Void> setFollow(@PathVariable Long businessId){
        iOaBusinessService.setFollow(businessId);
        return R.ok();
    }

}
