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

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.evaluation.vo.EvaluationServiceQualityPageReqVO;
import cn.iocoder.yudao.module.system.controller.admin.evaluation.vo.EvaluationServiceQualityRespVO;
import cn.iocoder.yudao.module.system.controller.admin.evaluation.vo.EvaluationServiceQualitySaveReqVO;
import cn.iocoder.yudao.module.system.controller.admin.evaluation.vo.excel.EvaluationServiceQualityExportExcelVO;
import cn.iocoder.yudao.module.system.service.evaluation.EvaluationServiceQualityService;
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("/evaluation-service-quality")
@Validated
public class EvaluationServiceQualityController {

    @Resource
    private EvaluationServiceQualityService qualityService;

    @PostMapping("/create")
    @Operation(summary = "创建卫生服务质量评价")
    public CommonResult<Integer> create(@Valid @RequestBody EvaluationServiceQualitySaveReqVO createReqVO) {
        createReqVO.setTenantId(Objects.isNull(createReqVO.getTenantId())
                ? TenantContextHolder.getTenantId() : createReqVO.getTenantId());
        return success(qualityService.create(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新卫生服务质量评价")
    public CommonResult<Boolean> update(@Valid @RequestBody EvaluationServiceQualitySaveReqVO updateReqVO) {
        updateReqVO.setTenantId(Objects.isNull(updateReqVO.getTenantId())
                ? TenantContextHolder.getTenantId() : updateReqVO.getTenantId());
        qualityService.update(updateReqVO);
        return success(Boolean.TRUE);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除卫生服务质量评价")
    @Parameter(name = "id", description = "记录编号", required = true)
    public CommonResult<Boolean> delete(@RequestParam("id") Integer id) {
        qualityService.delete(id);
        return success(Boolean.TRUE);
    }

    @GetMapping("/get")
    @Operation(summary = "获得卫生服务质量评价")
    @Parameter(name = "id", description = "记录编号", required = true, example = "1024")
    public CommonResult<EvaluationServiceQualityRespVO> get(@RequestParam("id") Integer id) {
        return success(qualityService.get(id));
    }

    @GetMapping("/page")
    @Operation(summary = "获得卫生服务质量评价分页")
    public CommonResult<PageResult<EvaluationServiceQualityRespVO>> getPage(@Valid EvaluationServiceQualityPageReqVO pageReqVO) {
        pageReqVO.setTenantIds(Objects.isNull(pageReqVO.getTenantIds())
                ? new Long[]{TenantContextHolder.getTenantId()} : pageReqVO.getTenantIds());
        return success(qualityService.getPage(pageReqVO));
    }

    @GetMapping("/exportExcel")
    @Operation(summary = "导出卫生服务质量评价 Excel")
    @ApiAccessLog(operateType = EXPORT)
    public void exportExcel(@Valid EvaluationServiceQualityPageReqVO pageReqVO,
                            HttpServletResponse response) throws IOException {
        pageReqVO.setTenantIds(Objects.isNull(pageReqVO.getTenantIds())
                ? new Long[]{TenantContextHolder.getTenantId()} : pageReqVO.getTenantIds());
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<EvaluationServiceQualityRespVO> list = qualityService.getPage(pageReqVO).getList();

        List<EvaluationServiceQualityExportExcelVO> exportList = new ArrayList<>();
        for (EvaluationServiceQualityRespVO master : list) {
            if (CollectionUtil.isEmpty(master.getItems())) {
                EvaluationServiceQualityExportExcelVO excelVO = new EvaluationServiceQualityExportExcelVO();
                BeanUtils.copyProperties(master, excelVO);
                exportList.add(excelVO);
            } else {
                for (cn.iocoder.yudao.module.system.controller.admin.evaluation.vo.EvaluationServiceQualityItemRespVO item : master.getItems()) {
                    EvaluationServiceQualityExportExcelVO excelVO = new EvaluationServiceQualityExportExcelVO();
                    BeanUtils.copyProperties(master, excelVO);
                    BeanUtils.copyProperties(item, excelVO);
                    exportList.add(excelVO);
                }
            }
        }

        ExcelUtils.write(response, "卫生服务质量评价.xls", "数据", EvaluationServiceQualityExportExcelVO.class, exportList);
    }
}

