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

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.aop.TenantIgnore;
import cn.iocoder.yudao.framework.tenant.core.context.TenantContextHolder;
import cn.iocoder.yudao.module.system.controller.admin.biz.vo.excel.ElderlyImportVO;
import cn.iocoder.yudao.module.system.controller.admin.biz.vo.excel.ExpenseSubsidyImportExcelVO;
import cn.iocoder.yudao.module.system.controller.admin.biz.vo.ExpenseSubsidyPageReqVO;
import cn.iocoder.yudao.module.system.controller.admin.biz.vo.ExpenseSubsidyRespVO;
import cn.iocoder.yudao.module.system.dal.dataobject.biz.ExpenseSubsidyDO;
import cn.iocoder.yudao.module.system.service.biz.ExpenseSubsidyService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import org.apache.commons.compress.utils.IOUtils;
import org.springframework.core.io.ClassPathResource;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

import org.springframework.validation.annotation.Validated;
import io.swagger.v3.oas.annotations.tags.Tag;


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/expenseSubsidy")
@Validated
public class ExpenseSubsidyController {

    @Resource
    private ExpenseSubsidyService expenseSubsidyService;

    @PostMapping("/create")
    @Operation(summary = "创建长者长护险")
    @TenantIgnore
    public CommonResult<Long> createExpenseSubsidy(@Valid @RequestBody ExpenseSubsidyDO createReqVO) {
        createReqVO.setTenantId(createReqVO.getTenantId() == null ? TenantContextHolder.getTenantId() : createReqVO.getTenantId());
        return success(expenseSubsidyService.createExpenseSubsidy(createReqVO));
    }

    @PutMapping("/updateExpenseSubsidy")
    @Operation(summary = "更新长者长护险")
    @TenantIgnore
    public CommonResult<Boolean> updateExpenseSubsidy(@Valid @RequestBody ExpenseSubsidyDO updateReqVO) {
        expenseSubsidyService.updateExpenseSubsidy(updateReqVO);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得长者长护险详情")
    @Parameter(name = "id", description = "长护险ID", required = true, example = "1024")
    @TenantIgnore
    public CommonResult<ExpenseSubsidyDO> getExpenseSubsidy(@RequestParam("id") Long id) {
        ExpenseSubsidyDO expenseSubsidy = expenseSubsidyService.getExpenseSubsidy(id);
        return success(expenseSubsidy);
    }


    @GetMapping("/getDetails")
    @Operation(summary = "长护险明细")
    @Parameter(name = "elderId", description = "长者ID", required = true, example = "1024")
    @TenantIgnore
    public CommonResult<ExpenseSubsidyRespVO> getDetails(@RequestParam("elderId") Long elderId,@RequestParam(required = false) String month) {
        ExpenseSubsidyRespVO expenseSubsidy = expenseSubsidyService.getDetails(elderId,month);
        return success(expenseSubsidy);
    }


    @GetMapping("/page")
    @Operation(summary = "获得长者长护险分页")
    @TenantIgnore
    public CommonResult<PageResult<ExpenseSubsidyDO>> getExpenseSubsidyPage(@Valid ExpenseSubsidyPageReqVO pageReqVO) {
        pageReqVO.setTenantIds(Objects.isNull(pageReqVO.getTenantIds()) ? new Long[]{TenantContextHolder.getTenantId()} : pageReqVO.getTenantIds());
        PageResult<ExpenseSubsidyDO> pageResult = expenseSubsidyService.getExpenseSubsidyPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, ExpenseSubsidyDO.class));
    }


    @GetMapping("/downloadExpenseSubsidyExcel")
    @Operation(summary = "下载长护险导入模板")
    public void downloadExpenseSubsidyExcel(HttpServletResponse response) throws IOException {
        String filePath = "files/长护险导入模版.xlsx";
        ClassPathResource classPathResource = new ClassPathResource(filePath);
        response.setContentType("application/octet-stream");
        response.setHeader("Content-Disposition", "attachment; filename=长护险导入模版");
        IOUtils.copy(classPathResource.getInputStream(), response.getOutputStream());
        response.flushBuffer();
    }


    @PostMapping("/importExpenseSubsidy")
    @Operation(summary = "导入长护险数据")
    public CommonResult<Boolean> importExpenseSubsidy(ElderlyImportVO elderlyImportVO) throws IOException {
        List<ExpenseSubsidyImportExcelVO> list = ExcelUtils.read(elderlyImportVO.getFile(), ExpenseSubsidyImportExcelVO.class);
        expenseSubsidyService.importExpenseSubsidy(list);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除长者长护险")
    @Parameter(name = "id", description = "长护险ID", required = true, example = "1024")
    @TenantIgnore
    public CommonResult<Boolean> deleteExpenseSubsidy(@RequestParam("id") Long id) {
        expenseSubsidyService.deleteExpenseSubsidy(id);
        return success(true);
    }
}