package com.fs.doctor.controller;

import com.fs.common.BeanCopyUtils;
import com.fs.common.annotation.Log;
import com.fs.common.core.controller.BaseController;
import com.fs.common.core.domain.AjaxResult;
import com.fs.common.core.page.TableDataInfo;
import com.fs.common.enums.BusinessType;
import com.fs.common.utils.poi.ExcelUtil;
import com.fs.company.vo.DoctorCompanyUserVO;
import com.fs.doctor.domain.DoctorCompanyUser;
import com.fs.doctor.service.IDoctorCompanyUserService;
import com.fs.doctor.vo.ProductGroupReqVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 讲者业务员绑定Controller
 * 
 * @author fs
 * @date 2025-12-30
 */
@RestController
@RequestMapping("/doctor/user")
public class DoctorCompanyUserController extends BaseController
{
    @Autowired
    private IDoctorCompanyUserService doctorCompanyUserService;

    /**
     * 查询讲者业务员绑定列表
     */
    @PreAuthorize("@ss.hasPermi('doctor:user:list')")
    @GetMapping("/list")
    public TableDataInfo list(DoctorCompanyUser doctorCompanyUser)
    {
        startPage();
        List<DoctorCompanyUserVO> doctorCompanyUserVOS = doctorCompanyUserService.selectDoctorCompanyUserList(doctorCompanyUser);
        return getDataTable(doctorCompanyUserVOS);
    }

    /**
     * 导出讲者业务员绑定列表
     */
    @PreAuthorize("@ss.hasPermi('doctor:user:export')")
    @Log(title = "讲者业务员绑定", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(DoctorCompanyUser doctorCompanyUser)
    {
        List<DoctorCompanyUserVO> doctorCompanyUserVOS = doctorCompanyUserService.selectDoctorCompanyUserList(doctorCompanyUser);
        ExcelUtil<DoctorCompanyUserVO> util = new ExcelUtil<DoctorCompanyUserVO>(DoctorCompanyUserVO.class);
        return util.exportExcel(doctorCompanyUserVOS, "讲者业务员绑定数据");
    }

    /**
     * 获取讲者业务员绑定详细信息
     */
    @PreAuthorize("@ss.hasPermi('doctor:user:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(doctorCompanyUserService.selectDoctorCompanyUserById(id));
    }

    /**
     * 新增讲者业务员绑定
     */
    @PreAuthorize("@ss.hasPermi('doctor:user:add')")
    @Log(title = "讲者业务员绑定", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody DoctorCompanyUser doctorCompanyUser)
    {
        return toAjax(doctorCompanyUserService.insertDoctorCompanyUser(doctorCompanyUser));
    }

    /**
     * 修改讲者业务员绑定
     */
    @PreAuthorize("@ss.hasPermi('doctor:user:edit')")
    @Log(title = "讲者业务员绑定", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody DoctorCompanyUser doctorCompanyUser)
    {
        return toAjax(doctorCompanyUserService.updateDoctorCompanyUser(doctorCompanyUser));
    }

    /**
     * 删除讲者业务员绑定
     */
    @PreAuthorize("@ss.hasPermi('doctor:user:remove')")
    @Log(title = "讲者业务员绑定", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(doctorCompanyUserService.deleteDoctorCompanyUserByIds(ids));
    }

    @GetMapping("/getProductGroup")
    @Log(title = "获取产品组")
    public AjaxResult getProductGroup(ProductGroupReqVO req) {

        String pg = doctorCompanyUserService.queryProductGroup(req.getCompanyId(), req.getDoctorId());
        return AjaxResult.success(pg);
    }


    /**
     * 按公司+业务员联想查询讲者（只返回该业务员已绑定的讲者，且讲者审核通过）
     * 参数：
     *  companyId（必填）
     *  companyUserId（必填）
     *  keyword（可选：讲者姓名/手机号模糊）
     */
    @GetMapping("/options/optionsByCompanyUser")
    public AjaxResult doctorOptionsByCompanyUser(
            @RequestParam Long companyId,
            @RequestParam Long companyUserId,
            @RequestParam(required = false) String productCode,
            @RequestParam(required = false) String keyword
    ) {
        return AjaxResult.success(
                doctorCompanyUserService.getDoctorOptionsByCompanyUser(companyId, companyUserId,productCode, keyword)
        );
    }



    @GetMapping("/options/productoptionsByCompanyUser")
    public AjaxResult productOptionsByCompanyUser(
            @RequestParam Long companyId,
            @RequestParam Long companyUserId,
            @RequestParam(required = false) String keyword
    ) {
        return AjaxResult.success(
                doctorCompanyUserService.getProductOptionsByCompanyUser(companyId, companyUserId, keyword)
        );
    }


}
