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.NinePreventiveMeasuresExportExcelVO;
import cn.iocoder.yudao.module.system.service.elderly.NinePreventiveMeasuresService;
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("/nine-preventive-measures")
@Validated
public class NinePreventiveMeasuresController {

    @Resource
    private NinePreventiveMeasuresService measuresService;

    @PostMapping("/create")
    @Operation(summary = "创建九防措施单")
    public CommonResult<Long> create(@Valid @RequestBody NinePreventiveMeasuresSaveReqVO createReqVO) {
        createReqVO.setTenantId(Objects.isNull(createReqVO.getTenantId())
                ? TenantContextHolder.getTenantId() : createReqVO.getTenantId());
        return success(measuresService.create(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新九防措施单")
    public CommonResult<Boolean> update(@Valid @RequestBody NinePreventiveMeasuresSaveReqVO updateReqVO) {
        updateReqVO.setTenantId(Objects.isNull(updateReqVO.getTenantId())
                ? TenantContextHolder.getTenantId() : updateReqVO.getTenantId());
        measuresService.update(updateReqVO);
        return success(Boolean.TRUE);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除九防措施单")
    @Parameter(name = "id", description = "记录编号", required = true)
    public CommonResult<Boolean> delete(@RequestParam("id") Long id) {
        measuresService.delete(id);
        return success(Boolean.TRUE);
    }

    @GetMapping("/get")
    @Operation(summary = "获得九防措施单")
    @Parameter(name = "id", description = "记录编号", required = true, example = "1024")
    public CommonResult<NinePreventiveMeasuresRespVO> get(@RequestParam("id") Long id) {
        return success(measuresService.get(id));
    }

    @GetMapping("/page")
    @Operation(summary = "获得九防措施单分页")
    public CommonResult<PageResult<NinePreventiveMeasuresRespVO>> getPage(@Valid NinePreventiveMeasuresPageReqVO pageReqVO) {
        pageReqVO.setTenantIds(Objects.isNull(pageReqVO.getTenantIds())
                ? new Long[]{TenantContextHolder.getTenantId()} : pageReqVO.getTenantIds());
        return success(measuresService.getPage(pageReqVO));
    }

    @GetMapping("/exportExcel")
    @Operation(summary = "导出九防措施单 Excel")
    @ApiAccessLog(operateType = EXPORT)
    public void exportExcel(@Valid NinePreventiveMeasuresPageReqVO pageReqVO,
                            HttpServletResponse response) throws IOException {
        pageReqVO.setTenantIds(Objects.isNull(pageReqVO.getTenantIds())
                ? new Long[]{TenantContextHolder.getTenantId()} : pageReqVO.getTenantIds());
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<NinePreventiveMeasuresRespVO> list = measuresService.getPage(pageReqVO).getList();

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

        ExcelUtils.write(response, "九防措施单.xls", "数据", NinePreventiveMeasuresExportExcelVO.class, exportList);
    }
}

