package cn.iocoder.yudao.module.system.controller.admin.home;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.tenant.core.aop.TenantIgnore;
import cn.iocoder.yudao.framework.tenant.core.context.TenantContextHolder;
import cn.iocoder.yudao.module.system.controller.admin.home.vo.HomeServiceMemberAssignReqVO;
import cn.iocoder.yudao.module.system.controller.admin.home.vo.HomeServiceMemeberPageReqVO;
import cn.iocoder.yudao.module.system.controller.admin.home.vo.HomeServiceMemeberPageRespVO;
import cn.iocoder.yudao.module.system.controller.admin.home.vo.HomeServiceMemeberSaveReqVO;
import cn.iocoder.yudao.module.system.service.home.HomeServiceMemberService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;

import java.util.List;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

@Tag(name = "管理后台 - 居家服务人员")
@RestController
@RequestMapping("/home/service-member")
@Validated
public class HomeServiceMemberController {

    @Resource
    private HomeServiceMemberService homeServiceMemberService;

    @PostMapping("/create")
    @Operation(summary = "新增居家服务人员")
    @TenantIgnore
    public CommonResult<Long> create(@Valid @RequestBody HomeServiceMemeberSaveReqVO createReqVO) {
        if (createReqVO.getTenantId() == null) {
            createReqVO.setTenantId(TenantContextHolder.getTenantId());
        }
        return success(homeServiceMemberService.createHomeServiceMemeber(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "修改居家服务人员")
    @TenantIgnore
    public CommonResult<Boolean> update(@Valid @RequestBody HomeServiceMemeberSaveReqVO updateReqVO) {
        if (updateReqVO.getTenantId() == null) {
            updateReqVO.setTenantId(TenantContextHolder.getTenantId());
        }
        homeServiceMemberService.updateHomeServiceMemeber(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除居家服务人员")
    @Parameter(name = "id", description = "编号", required = true, example = "1")
    @TenantIgnore
    public CommonResult<Boolean> delete(@RequestParam("id") Long id) {
        homeServiceMemberService.deleteHomeServiceMemeber(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得居家服务人员详情")
    @Parameter(name = "id", description = "编号", required = true, example = "1")
    @TenantIgnore
    public CommonResult<HomeServiceMemeberPageRespVO> get(@RequestParam("id") Long id) {
        return success(homeServiceMemberService.getHomeServiceMemeber(id));
    }

    @GetMapping("/page")
    @Operation(summary = "获得居家服务人员分页")
    @TenantIgnore
    public CommonResult<PageResult<HomeServiceMemeberPageRespVO>> page(@Valid HomeServiceMemeberPageReqVO pageReqVO) {
        if (pageReqVO.getTenantId() == null) {
            pageReqVO.setTenantId(TenantContextHolder.getTenantId());
        }
        if (pageReqVO.getTenantIds() == null) {
            pageReqVO.setTenantIds(new Long[]{TenantContextHolder.getTenantId()});
        }
        return success(homeServiceMemberService.getHomeServiceMemeberPage(pageReqVO));
    }

    @PostMapping("/assign")
    @Operation(summary = "分配服务人员")
    @TenantIgnore
    public CommonResult<Boolean> assign(@Valid @RequestBody List<HomeServiceMemberAssignReqVO> assignReqVOList) {
        homeServiceMemberService.assignHomeServiceMember(assignReqVOList);
        return success(true);
    }
}
