package org.hzhub.crm.controller;

import lombok.RequiredArgsConstructor;
import org.hzhub.common.core.domain.R;
import org.hzhub.common.idempotent.annotation.RepeatSubmit;
import org.hzhub.common.log.annotation.Log;
import org.hzhub.common.log.enums.BusinessType;
import org.hzhub.common.mybatis.core.page.PageQuery;
import org.hzhub.common.mybatis.core.page.TableDataInfo;
import org.hzhub.common.web.core.BaseController;
import org.hzhub.crm.domain.bo.CrmOpportunityBo;
import org.hzhub.crm.domain.vo.CrmOpportunityVo;
import org.hzhub.crm.service.ICrmOpportunityService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * CRM商机管理 Controller
 * 员工门户版本（无需Sa-Token权限注解，权限由Gateway控制）
 *
 * 注意：Gateway已配置 /crm/** 路由并StripPrefix=1（去除/crm前缀）
 * 所以Controller使用 /opportunity 路径，实际对外接口为 /crm/opportunity/**
 *
 * @author hzhub
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/opportunity")
public class CrmOpportunityController extends BaseController {

    private final ICrmOpportunityService opportunityService;

    /**
     * 获取商机列表
     */
    @GetMapping("/list")
    public TableDataInfo<CrmOpportunityVo> list(CrmOpportunityBo opportunity, PageQuery pageQuery) {
        return opportunityService.selectPageOpportunityList(opportunity, pageQuery);
    }

    /**
     * 获取商机详情
     *
     * @param opportunityId 商机ID
     */
    @GetMapping("/{opportunityId}")
    public R<CrmOpportunityVo> getInfo(@PathVariable Long opportunityId) {
        return R.ok(opportunityService.selectOpportunityById(opportunityId));
    }

    /**
     * 新增商机
     */
    @Log(title = "商机管理", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping
    public R<Void> add(@Validated @RequestBody CrmOpportunityBo opportunity) {
        return toAjax(opportunityService.insertOpportunity(opportunity));
    }

    /**
     * 修改商机
     */
    @Log(title = "商机管理", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping
    public R<Void> edit(@Validated @RequestBody CrmOpportunityBo opportunity) {
        return toAjax(opportunityService.updateOpportunity(opportunity));
    }

    /**
     * 删除商机
     *
     * @param opportunityIds 商机ID串
     */
    @Log(title = "商机管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{opportunityIds}")
    public R<Void> remove(@PathVariable Long[] opportunityIds) {
        return toAjax(opportunityService.deleteOpportunityByIds(opportunityIds));
    }
}