package com.gxwebsoft.clinic.controller;

import com.gxwebsoft.clinic.entity.ClinicDoctorUser;
import com.gxwebsoft.clinic.param.ClinicDoctorUserParam;
import com.gxwebsoft.clinic.service.ClinicDoctorUserService;
import com.gxwebsoft.common.core.annotation.OperationLog;
import com.gxwebsoft.common.core.utils.RequestUtil;
import com.gxwebsoft.common.core.web.ApiResult;
import com.gxwebsoft.common.core.web.BaseController;
import com.gxwebsoft.common.core.web.BatchParam;
import com.gxwebsoft.common.core.web.PageResult;
import com.gxwebsoft.common.system.entity.User;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 分销商用户记录表控制器
 *
 * @author 科技小王子
 * @since 2025-10-23 15:58:21
 */
@Tag(name = "分销商用户记录表管理")
@RestController
@RequestMapping("/api/clinic/clinic-doctor-user")
public class ClinicDoctorUserController extends BaseController {
    @Resource
    private ClinicDoctorUserService clinicDoctorUserService;

    @Operation(summary = "分页查询分销商用户记录表")
    @GetMapping("/page")
    public ApiResult<PageResult<ClinicDoctorUser>> page(ClinicDoctorUserParam param) {
        // 使用关联查询
        return success(clinicDoctorUserService.pageRel(param));
    }

    @Operation(summary = "查询全部分销商用户记录表")
    @GetMapping()
    public ApiResult<List<ClinicDoctorUser>> list(ClinicDoctorUserParam param) {
        // 使用关联查询
        return success(clinicDoctorUserService.listRel(param));
    }

    @Operation(summary = "根据id查询分销商用户记录表")
    @GetMapping("/{id}")
    public ApiResult<ClinicDoctorUser> get(@PathVariable("id") Integer id) {
        // 使用关联查询
        return success(clinicDoctorUserService.getByIdRel(id));
    }

    @Operation(summary = "根据id查询分销商用户记录表")
    @GetMapping("/getByUserId/{doctorUserId}")
    public ApiResult<ClinicDoctorUser> getByUserId(@PathVariable("doctorUserId") Integer id) {
        // 使用关联查询
        return success(clinicDoctorUserService.getByUserId(id));
    }

    
    @Operation(summary = "添加分销商用户记录表")
    @PostMapping()
    public ApiResult<?> save(@RequestBody ClinicDoctorUser clinicDoctorUser) {
        RequestUtil requestUtil = new RequestUtil();
        requestUtil.setTenantId(getTenantId().toString());
        User user = requestUtil.getUserByPhone(clinicDoctorUser.getPhone());
        if (user == null) {
            return fail("该手机号无法查询到用户");
        }
        clinicDoctorUser.setUserId(user.getUserId());
        if (clinicDoctorUserService.save(clinicDoctorUser)) {
            return success("添加成功");
        }
        return fail("添加失败");
    }

    
    @Operation(summary = "修改分销商用户记录表")
    @PutMapping()
    public ApiResult<?> update(@RequestBody ClinicDoctorUser clinicDoctorUser) {
        if (clinicDoctorUserService.updateById(clinicDoctorUser)) {
            return success("修改成功");
        }
        return fail("修改失败");
    }

    
    @Operation(summary = "删除分销商用户记录表")
    @DeleteMapping("/{id}")
    public ApiResult<?> remove(@PathVariable("id") Integer id) {
        if (clinicDoctorUserService.removeById(id)) {
            return success("删除成功");
        }
        return fail("删除失败");
    }

    
    @Operation(summary = "批量添加分销商用户记录表")
    @PostMapping("/batch")
    public ApiResult<?> saveBatch(@RequestBody List<ClinicDoctorUser> list) {
        if (clinicDoctorUserService.saveBatch(list)) {
            return success("添加成功");
        }
        return fail("添加失败");
    }

    
    @Operation(summary = "批量修改分销商用户记录表")
    @PutMapping("/batch")
    public ApiResult<?> removeBatch(@RequestBody BatchParam<ClinicDoctorUser> batchParam) {
        if (batchParam.update(clinicDoctorUserService, "id")) {
            return success("修改成功");
        }
        return fail("修改失败");
    }

    
    @Operation(summary = "批量删除分销商用户记录表")
    @DeleteMapping("/batch")
    public ApiResult<?> removeBatch(@RequestBody List<Integer> ids) {
        if (clinicDoctorUserService.removeByIds(ids)) {
            return success("删除成功");
        }
        return fail("删除失败");
    }

}
