package org.hzhub.crm.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
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.CrmLeadBo;
import org.hzhub.crm.domain.bo.CrmLeadConvertBo;
import org.hzhub.crm.domain.vo.CrmLeadStatsVo;
import org.hzhub.crm.domain.vo.CrmLeadVo;
import org.hzhub.crm.service.ICrmLeadService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

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

    private final ICrmLeadService leadService;

    /**
     * 获取线索列表
     */
    @GetMapping("/list")
    public TableDataInfo<CrmLeadVo> list(CrmLeadBo lead, PageQuery pageQuery) {
        return leadService.selectPageLeadList(lead, pageQuery);
    }

    /**
     * 获取线索统计数据
     */
    @GetMapping("/stats")
    public R<CrmLeadStatsVo> getStats() {
        return R.ok(leadService.getLeadStats());
    }

    /**
     * 获取线索详情
     *
     * @param leadId 线索ID
     */
    @GetMapping("/detail/{leadId}")
    public R<CrmLeadVo> getInfo(@PathVariable Long leadId) {
        return R.ok(leadService.selectLeadById(leadId));
    }

    /**
     * 新增线索
     */
    @Log(title = "线索管理", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping
    public R<Void> add(@Validated @RequestBody CrmLeadBo lead) {
        return toAjax(leadService.insertLead(lead));
    }

    /**
     * 修改线索
     */
    @Log(title = "线索管理", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping
    public R<Void> edit(@Validated @RequestBody CrmLeadBo lead) {
        return toAjax(leadService.updateLead(lead));
    }

    /**
     * 删除线索
     *
     * @param leadIds 线索ID串
     */
    @Log(title = "线索管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{leadIds}")
    public R<Void> remove(@PathVariable Long[] leadIds) {
        return toAjax(leadService.deleteLeadByIds(leadIds));
    }

    /**
     * 分配线索
     */
    @Log(title = "线索分配", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping("/assign")
    public R<Void> assign(@RequestBody AssignRequest request) {
        return toAjax(leadService.assignLead(request.getLeadId(), request.getOwnerUserId()));
    }

    /**
     * 分配请求DTO
     */
    @lombok.Data
    public static class AssignRequest {
        private Long leadId;
        private Long ownerUserId;
    }

    /**
     * 线索转经销商
     */
    @Log(title = "线索转化", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping("/convert")
    public R<Void> convert(@Validated @RequestBody CrmLeadConvertBo convert) {
        return toAjax(leadService.convertToDealer(convert));
    }

    /**
     * 作废线索
     *
     * @param leadId 线索ID
     */
    @Log(title = "线索作废", businessType = BusinessType.UPDATE)
    @PutMapping("/invalidate/{leadId}")
    public R<Void> invalidate(@PathVariable Long leadId) {
        return toAjax(leadService.invalidateLead(leadId));
    }

    /**
     * 恢复线索
     *
     * @param leadId 线索ID
     */
    @Log(title = "线索恢复", businessType = BusinessType.UPDATE)
    @PutMapping("/restore/{leadId}")
    public R<Void> restore(@PathVariable Long leadId) {
        return toAjax(leadService.restoreLead(leadId));
    }
}