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

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.FoodSatisfactionSurveyPageReqVO;
import cn.iocoder.yudao.module.system.controller.admin.evaluation.vo.FoodSatisfactionSurveyRespVO;
import cn.iocoder.yudao.module.system.controller.admin.evaluation.vo.FoodSatisfactionSurveySaveReqVO;
import cn.iocoder.yudao.module.system.controller.admin.evaluation.vo.excel.FoodSatisfactionSurveyExportExcelVO;
import cn.iocoder.yudao.module.system.dal.dataobject.tenant.TenantDO;
import cn.iocoder.yudao.module.system.dal.mysql.tenant.TenantMapper;
import cn.iocoder.yudao.module.system.service.evaluation.FoodSatisfactionSurveyService;
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.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.annotation.security.PermitAll;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.stream.Collectors;

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("/food-satisfaction-survey")
@Validated
public class FoodSatisfactionSurveyController {

    @Resource
    private FoodSatisfactionSurveyService foodSatisfactionSurveyService;
    @Resource
    private TenantMapper tenantMapper;

    @PostMapping("/create")
    @Operation(summary = "创建膳食满意度调查")
    @PermitAll
    public CommonResult<Long> create(@Valid @RequestBody FoodSatisfactionSurveySaveReqVO createReqVO) {
        createReqVO.setTenantId(Objects.isNull(createReqVO.getTenantId())
                ? TenantContextHolder.getTenantId() : createReqVO.getTenantId());
        return success(foodSatisfactionSurveyService.create(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新膳食满意度调查")
    public CommonResult<Boolean> update(@Valid @RequestBody FoodSatisfactionSurveySaveReqVO updateReqVO) {
        updateReqVO.setTenantId(Objects.isNull(updateReqVO.getTenantId())
                ? TenantContextHolder.getTenantId() : updateReqVO.getTenantId());
        foodSatisfactionSurveyService.update(updateReqVO);
        return success(Boolean.TRUE);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除膳食满意度调查")
    @Parameter(name = "id", description = "记录编号", required = true)
    public CommonResult<Boolean> delete(@RequestParam("id") Long id) {
        foodSatisfactionSurveyService.delete(id);
        return success(Boolean.TRUE);
    }

    @GetMapping("/get")
    @Operation(summary = "获得膳食满意度调查")
    @Parameter(name = "id", description = "记录编号", required = true, example = "1")
    public CommonResult<FoodSatisfactionSurveyRespVO> get(@RequestParam("id") Long id) {
        return success(foodSatisfactionSurveyService.get(id));
    }

    @GetMapping("/page")
    @Operation(summary = "获得膳食满意度调查分页")
    public CommonResult<PageResult<FoodSatisfactionSurveyRespVO>> getPage(@Valid FoodSatisfactionSurveyPageReqVO pageReqVO) {
        pageReqVO.setTenantId(Objects.isNull(pageReqVO.getTenantId())
                ? TenantContextHolder.getTenantId() : pageReqVO.getTenantId());
        return success(foodSatisfactionSurveyService.getPage(pageReqVO));
    }

    @GetMapping("/exportExcel")
    @Operation(summary = "导出膳食满意度调查 Excel")
    @ApiAccessLog(operateType = EXPORT)
    public void exportExcel(@Valid FoodSatisfactionSurveyPageReqVO pageReqVO,
                            HttpServletResponse response) throws IOException {
        pageReqVO.setTenantId(Objects.isNull(pageReqVO.getTenantId())
                ? TenantContextHolder.getTenantId() : pageReqVO.getTenantId());
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<FoodSatisfactionSurveyRespVO> list = foodSatisfactionSurveyService.getPage(pageReqVO).getList();
        Set<Long> tenantIds = list.stream().map(FoodSatisfactionSurveyRespVO::getTenantId)
                .filter(Objects::nonNull).collect(Collectors.toSet());
        Map<Long, String> tenantNameMap = tenantIds.isEmpty() ? java.util.Collections.emptyMap()
                : tenantMapper.selectBatchIds(tenantIds).stream()
                .filter(Objects::nonNull)
                .filter(t -> t.getId() != null)
                .collect(Collectors.toMap(TenantDO::getId, TenantDO::getName, (a, b) -> a));

        List<FoodSatisfactionSurveyExportExcelVO> exportList = new ArrayList<>(list.size());
        for (FoodSatisfactionSurveyRespVO item : list) {
            FoodSatisfactionSurveyExportExcelVO excelVO = BeanUtils.toBean(item, FoodSatisfactionSurveyExportExcelVO.class);
            excelVO.setTenantName(tenantNameMap.get(item.getTenantId()));
            exportList.add(excelVO);
        }

        ExcelUtils.write(response, "膳食满意度调查.xls", "数据", FoodSatisfactionSurveyExportExcelVO.class, exportList);
    }
}
