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

import cn.hutool.core.collection.CollectionUtil;
import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.framework.tenant.core.context.TenantContextHolder;
import cn.iocoder.yudao.module.system.controller.admin.elderly.vo.*;
import cn.iocoder.yudao.module.system.controller.admin.elderly.vo.excel.ElderlySpecialServiceExportExcelVO;
import cn.iocoder.yudao.module.system.service.elderly.ElderlySpecialServiceService;
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.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

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-special-service")
@Validated
public class ElderlySpecialServiceController {

    @Resource
    private ElderlySpecialServiceService specialServiceService;

    @PostMapping("/create")
    @Operation(summary = "创建专护服务记录")
    public CommonResult<Long> create(@Valid @RequestBody ElderlySpecialServiceSaveReqVO createReqVO) {
        createReqVO.setTenantId(Objects.isNull(createReqVO.getTenantId())
                ? TenantContextHolder.getTenantId() : createReqVO.getTenantId());
        return success(specialServiceService.create(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新专护服务记录")
    public CommonResult<Boolean> update(@Valid @RequestBody ElderlySpecialServiceSaveReqVO updateReqVO) {
        updateReqVO.setTenantId(Objects.isNull(updateReqVO.getTenantId())
                ? TenantContextHolder.getTenantId() : updateReqVO.getTenantId());
        specialServiceService.update(updateReqVO);
        return success(Boolean.TRUE);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除专护服务记录")
    @Parameter(name = "id", description = "记录编号", required = true)
    public CommonResult<Boolean> delete(@RequestParam("id") Long id) {
        specialServiceService.delete(id);
        return success(Boolean.TRUE);
    }

    @GetMapping("/get")
    @Operation(summary = "获得专护服务记录")
    @Parameter(name = "id", description = "记录编号", required = true, example = "1024")
    public CommonResult<ElderlySpecialServiceRespVO> get(@RequestParam("id") Long id) {
        return success(specialServiceService.get(id));
    }

    @GetMapping("/page")
    @Operation(summary = "获得专护服务记录分页")
    public CommonResult<PageResult<ElderlySpecialServiceRespVO>> getPage(@Valid ElderlySpecialServicePageReqVO pageReqVO) {
        pageReqVO.setTenantIds(Objects.isNull(pageReqVO.getTenantIds())
                ? new Long[]{TenantContextHolder.getTenantId()} : pageReqVO.getTenantIds());
        return success(specialServiceService.getPage(pageReqVO));
    }

    @GetMapping("/exportExcel")
    @Operation(summary = "导出专护服务记录 Excel")
    @ApiAccessLog(operateType = EXPORT)
    public void exportExcel(@Valid ElderlySpecialServicePageReqVO pageReqVO,
                            HttpServletResponse response) throws IOException {
        pageReqVO.setTenantIds(Objects.isNull(pageReqVO.getTenantIds())
                ? new Long[]{TenantContextHolder.getTenantId()} : pageReqVO.getTenantIds());
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<ElderlySpecialServiceRespVO> list = specialServiceService.getPage(pageReqVO).getList();

        List<ElderlySpecialServiceExportExcelVO> exportList = new ArrayList<>();
        for (ElderlySpecialServiceRespVO master : list) {
            if (CollectionUtil.isEmpty(master.getItems())) {
                ElderlySpecialServiceExportExcelVO excelVO = new ElderlySpecialServiceExportExcelVO();
                BeanUtils.copyProperties(master, excelVO);
                exportList.add(excelVO);
            } else {
                for (ElderlySpecialServiceItemRespVO item : master.getItems()) {
                    ElderlySpecialServiceExportExcelVO excelVO = new ElderlySpecialServiceExportExcelVO();
                    BeanUtils.copyProperties(master, excelVO);
                    BeanUtils.copyProperties(item, excelVO);
                    exportList.add(excelVO);
                }
            }
        }

        ExcelUtils.write(response, "专护服务记录.xls", "数据", ElderlySpecialServiceExportExcelVO.class, exportList);
    }
}

