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

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.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.ElderlyTurnOverPageReqVO;
import cn.iocoder.yudao.module.system.controller.admin.elderly.vo.ElderlyTurnOverRespVO;
import cn.iocoder.yudao.module.system.controller.admin.elderly.vo.ElderlyTurnOverSaveReqVO;
import cn.iocoder.yudao.module.system.controller.admin.elderly.vo.excel.ElderlyTurnOverExportExcelVO;
import cn.iocoder.yudao.module.system.service.elderly.ElderlyTurnOverService;
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.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-turn-over")
@Validated
public class ElderlyTurnOverController {

    @Resource
    private ElderlyTurnOverService turnOverService;

    @PostMapping("/create")
    @Operation(summary = "创建翻身登记")
    public CommonResult<Long> create(@Valid @RequestBody ElderlyTurnOverSaveReqVO createReqVO) {
        createReqVO.setTenantId(Objects.isNull(createReqVO.getTenantId())
                ? TenantContextHolder.getTenantId() : createReqVO.getTenantId());
        return success(turnOverService.create(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新翻身登记")
    public CommonResult<Boolean> update(@Valid @RequestBody ElderlyTurnOverSaveReqVO updateReqVO) {
        updateReqVO.setTenantId(Objects.isNull(updateReqVO.getTenantId())
                ? TenantContextHolder.getTenantId() : updateReqVO.getTenantId());
        turnOverService.update(updateReqVO);
        return success(Boolean.TRUE);
    }

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

    @GetMapping("/get")
    @Operation(summary = "获得翻身登记")
    @Parameter(name = "id", description = "记录编号", required = true, example = "1024")
    public CommonResult<ElderlyTurnOverRespVO> get(@RequestParam("id") Long id) {
        return success(turnOverService.get(id));
    }

    @GetMapping("/page")
    @Operation(summary = "获得翻身登记分页")
    public CommonResult<PageResult<ElderlyTurnOverRespVO>> getPage(@Valid ElderlyTurnOverPageReqVO pageReqVO) {
        pageReqVO.setTenantIds(Objects.isNull(pageReqVO.getTenantIds())
                ? new Long[]{TenantContextHolder.getTenantId()} : pageReqVO.getTenantIds());
        return success(turnOverService.getPage(pageReqVO));
    }

    @GetMapping("/exportExcel")
    @Operation(summary = "导出翻身登记 Excel")
    @ApiAccessLog(operateType = EXPORT)
    public void exportExcel(@Valid ElderlyTurnOverPageReqVO pageReqVO,
                            HttpServletResponse response) throws IOException {
        pageReqVO.setTenantIds(Objects.isNull(pageReqVO.getTenantIds())
                ? new Long[]{TenantContextHolder.getTenantId()} : pageReqVO.getTenantIds());
        pageReqVO.setPageSize(cn.iocoder.yudao.framework.common.pojo.PageParam.PAGE_SIZE_NONE); // 导出所有数据
        List<ElderlyTurnOverRespVO> list = turnOverService.getPage(pageReqVO).getList();

        // 展开明细
        java.util.List<ElderlyTurnOverExportExcelVO> exportList = new java.util.ArrayList<>();
        for (ElderlyTurnOverRespVO master : list) {
            if (cn.hutool.core.collection.CollectionUtil.isEmpty(master.getItems())) {
                ElderlyTurnOverExportExcelVO excelVO = new ElderlyTurnOverExportExcelVO();
                BeanUtils.copyProperties(master, excelVO);
                exportList.add(excelVO);
            } else {
                for (cn.iocoder.yudao.module.system.controller.admin.elderly.vo.ElderlyTurnOverItemRespVO item : master.getItems()) {
                    ElderlyTurnOverExportExcelVO excelVO = new ElderlyTurnOverExportExcelVO();
                    BeanUtils.copyProperties(master, excelVO);
                    BeanUtils.copyProperties(item, excelVO);
                    exportList.add(excelVO);
                }
            }
        }

        ExcelUtils.write(response, "翻身登记.xls", "数据", ElderlyTurnOverExportExcelVO.class, exportList);
    }
}

