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

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.framework.tenant.core.aop.TenantIgnore;
import cn.iocoder.yudao.framework.tenant.core.context.TenantContextHolder;
import cn.iocoder.yudao.module.system.controller.admin.biz.vo.excel.ElderlyBirthdayExcelVO;
import cn.iocoder.yudao.module.system.dal.dataobject.biz.BirthdayCareDO;
import cn.iocoder.yudao.module.system.dal.dataobject.biz.ElderlyInfoDO;
import cn.iocoder.yudao.module.system.service.biz.BirthdayCareService;
import io.swagger.v3.oas.annotations.Operation;
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.common.pojo.CommonResult.success;


@Tag(name = "管理后台 - 长者生日关怀")
@RestController
@RequestMapping("/elderly/birthdayCare")
@Validated
public class BirthdayCareController {

    @Resource
    private BirthdayCareService birthdayCareService;


    @PostMapping("/createBirthdayCare")
    @Operation(summary = "创建长者生日关怀")
    @TenantIgnore
    public CommonResult<Long> createBirthdayCare(@Valid @RequestBody BirthdayCareDO birthdayCare) {
        birthdayCare.setTenantId(birthdayCare.getTenantId() == null ? TenantContextHolder.getTenantId() : birthdayCare.getTenantId());
        return success(birthdayCareService.createBirthdayCare(birthdayCare));
    }


    @GetMapping("/getElderBirthdayListByDate")
    @Operation(summary = "根据日期查询过生日的长者列表")
    @TenantIgnore
    public CommonResult<List<ElderlyInfoDO>> getElderBirthdayListByDate(@RequestParam("month") String month,
                                                                        @RequestParam(value = "orgType", required = false) Integer orgType,
                                                                        Long[] tenantIds) {
        tenantIds = Objects.isNull(tenantIds) ? new Long[]{TenantContextHolder.getTenantId()} : tenantIds;
        return success(birthdayCareService.getElderBirthdayListByDate(month, orgType, tenantIds));
    }


    @GetMapping("/getBirthdayCare")
    @Operation(summary = "获取长者生日关怀详情")
    @TenantIgnore
    public CommonResult<BirthdayCareDO> getBirthdayCare(@RequestParam("elderId") Long elderId) {
        return success(birthdayCareService.getBirthdayCare(elderId));
    }


    @GetMapping("/export-birthday-excel")
    @Operation(summary = "导出当月长者生日 Excel")
    @TenantIgnore
    public void exportBirthdayExcel(@RequestParam("month") String month,
                                    @RequestParam(value = "orgType", required = false) Integer orgType,
                                    @RequestParam(value = "tenantIds", required = false) Long[] tenantIds,
                                    HttpServletResponse response) throws IOException {
        tenantIds = Objects.isNull(tenantIds) ? new Long[]{TenantContextHolder.getTenantId()} : tenantIds;
        List<ElderlyBirthdayExcelVO> excelList = birthdayCareService.getElderBirthdayExportList(month, orgType, tenantIds);
        ExcelUtils.write(response, "当月长者生日", "数据", ElderlyBirthdayExcelVO.class, excelList);
    }


    @PutMapping("/update")
    @Operation(summary = "更新长者生日关怀")
    @TenantIgnore
    public CommonResult<Boolean> updateBirthdayCare(@Valid @RequestBody BirthdayCareDO updateReqVO) {
        birthdayCareService.updateBirthdayCare(updateReqVO);
        return success(true);
    }

}