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

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
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.biz.vo.elderlyitemsround.*;
import cn.iocoder.yudao.module.system.service.biz.ElderlyItemsRoundService;
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.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.annotation.security.PermitAll;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;

import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.EXPORT;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

@Tag(name = "管理后台 - 长者巡房项目")
@RestController
@RequestMapping("/elderly-items-round")
@Validated
public class ElderlyItemsRoundController {

    @Resource
    private ElderlyItemsRoundService elderlyItemsRoundService;

    @PostMapping("/create")
    @Operation(summary = "创建长者巡房项目")
    public CommonResult<Long> create(@Valid @RequestBody ElderlyItemsRoundSaveReqVO createReqVO) {
        if (createReqVO.getTenantId() == null) {
            createReqVO.setTenantId(TenantContextHolder.getTenantId());
        }
        return success(elderlyItemsRoundService.create(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新长者巡房项目")
    @TenantIgnore
    public CommonResult<Boolean> update(@Valid @RequestBody ElderlyItemsRoundSaveReqVO updateReqVO) {
        if (updateReqVO.getTenantId() == null) {
            updateReqVO.setTenantId(TenantContextHolder.getTenantId());
        }
        elderlyItemsRoundService.update(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除长者巡房项目")
    @Parameter(name = "id", description = "编号", required = true)
    @TenantIgnore
    public CommonResult<Boolean> delete(@RequestParam("id") Long id) {
        elderlyItemsRoundService.delete(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得长者巡房项目详情（按长者分页）")
    @TenantIgnore
    public CommonResult<ElderlyItemsRoundRespVO> get(@Valid ElderlyItemsRoundPageReqVO reqVO) {
        return success(elderlyItemsRoundService.get(reqVO));
    }

    @GetMapping("/page")
    @Operation(summary = "获得长者巡房项目分页")
    @TenantIgnore
    public CommonResult<PageResult<ElderlyItemsRoundPageRespVO>> page(@Valid ElderlyItemsRoundPageReqVO pageReqVO) {
        if (pageReqVO.getTenantIds() == null) {
            pageReqVO.setTenantIds(new Long[]{TenantContextHolder.getTenantId()});
        }
        return success(elderlyItemsRoundService.getPage(pageReqVO));
    }

    @PostMapping("/batch-create")
    @Operation(summary = "批量新增长者巡房项目")
    @PermitAll
    @TenantIgnore
    public CommonResult<Boolean> batchCreate(@Valid @RequestBody ElderlyItemsRoundBatchSaveReqVO reqVO) {
        if (reqVO.getTenantId() == null) {
            reqVO.setTenantId(TenantContextHolder.getTenantId());
        }
        return success(elderlyItemsRoundService.batchCreate(reqVO));
    }

    @GetMapping("/exportExcel")
    @Operation(summary = "导出长者巡房项目 Excel")
    @ApiAccessLog(operateType = EXPORT)
    @TenantIgnore
    public void exportExcel(@RequestParam("yearMonth") String yearMonth,
                            HttpServletResponse response) throws IOException {
        elderlyItemsRoundService.exportExcel(yearMonth, response);
    }
}
