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.ElderlyLifeAssistantInspectionPageReqVO;
import cn.iocoder.yudao.module.system.controller.admin.elderly.vo.ElderlyLifeAssistantInspectionRespVO;
import cn.iocoder.yudao.module.system.controller.admin.elderly.vo.ElderlyLifeAssistantInspectionSaveReqVO;
import cn.iocoder.yudao.module.system.controller.admin.elderly.vo.excel.ElderlyLifeAssistantInspectionExportExcelVO;
import cn.iocoder.yudao.module.system.service.elderly.ElderlyLifeAssistantInspectionService;
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-life-assistant-inspection")
@Validated
public class ElderlyLifeAssistantInspectionController {

    @Resource
    private ElderlyLifeAssistantInspectionService inspectionService;

    @PostMapping("/create")
    @Operation(summary = "创建生活助理巡视")
    public CommonResult<Long> create(@Valid @RequestBody ElderlyLifeAssistantInspectionSaveReqVO createReqVO) {
        createReqVO.setTenantId(Objects.isNull(createReqVO.getTenantId())
                ? TenantContextHolder.getTenantId() : createReqVO.getTenantId());
        return success(inspectionService.create(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新生活助理巡视")
    public CommonResult<Boolean> update(@Valid @RequestBody ElderlyLifeAssistantInspectionSaveReqVO updateReqVO) {
        updateReqVO.setTenantId(Objects.isNull(updateReqVO.getTenantId())
                ? TenantContextHolder.getTenantId() : updateReqVO.getTenantId());
        inspectionService.update(updateReqVO);
        return success(Boolean.TRUE);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除生活助理巡视")
    @Parameter(name = "id", description = "记录编号", required = true)
    public CommonResult<Boolean> delete(@RequestParam("id") Long id) {
        inspectionService.delete(id);
        return success(Boolean.TRUE);
    }

    @GetMapping("/get")
    @Operation(summary = "获得生活助理巡视")
    @Parameter(name = "id", description = "记录编号", required = true, example = "1024")
    public CommonResult<ElderlyLifeAssistantInspectionRespVO> get(@RequestParam("id") Long id) {
        return success(inspectionService.get(id));
    }

    @GetMapping("/page")
    @Operation(summary = "获得生活助理巡视分页")
    public CommonResult<PageResult<ElderlyLifeAssistantInspectionRespVO>> getPage(@Valid ElderlyLifeAssistantInspectionPageReqVO pageReqVO) {
        pageReqVO.setTenantIds(Objects.isNull(pageReqVO.getTenantIds())
                ? new Long[]{TenantContextHolder.getTenantId()} : pageReqVO.getTenantIds());
        return success(inspectionService.getPage(pageReqVO));
    }

    @GetMapping("/exportExcel")
    @Operation(summary = "导出生活助理巡视 Excel")
    @ApiAccessLog(operateType = EXPORT)
    public void exportExcel(@Valid ElderlyLifeAssistantInspectionPageReqVO 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<ElderlyLifeAssistantInspectionRespVO> list = inspectionService.getPage(pageReqVO).getList();

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

        ExcelUtils.write(response, "生活助理巡视.xls", "数据", ElderlyLifeAssistantInspectionExportExcelVO.class, exportList);
    }
}

