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


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.aop.TenantIgnore;
import cn.iocoder.yudao.framework.tenant.core.context.TenantContextHolder;
import cn.iocoder.yudao.module.system.controller.admin.biz.vo.*;
import cn.iocoder.yudao.module.system.controller.admin.biz.vo.excel.DailyExpensesExcelVO;
import cn.iocoder.yudao.module.system.controller.admin.biz.vo.excel.DailyExpensesExportExcelVO;
import cn.iocoder.yudao.module.system.controller.admin.biz.vo.excel.DailyMedicalExpensesVO;
import cn.iocoder.yudao.module.system.controller.admin.biz.vo.excel.ElderlyFileImportExcelVO;
import cn.iocoder.yudao.module.system.dal.dataobject.biz.DailyExpensesDO;
import cn.iocoder.yudao.module.system.service.biz.DailyExpensesService;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
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 org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;
import java.util.Map;
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("/elderly/dailyExpenses")
@Validated
public class DailyExpensesController {

    @Resource
    private DailyExpensesService dailyExpensesService;

    @PostMapping("/create")
    @Operation(summary = "新增日常费用")
    @TenantIgnore
    public CommonResult<Boolean> createDailyExpenses(@Valid @RequestBody DailyExpensesSaveReqVO createReqVO) {
        dailyExpensesService.createDailyExpenses(createReqVO);
        return success(true);
    }

    @PutMapping("/supplementaryExpenses")
    @Operation(summary = "补录日常费用")
    @TenantIgnore
    public CommonResult<Boolean> supplementaryExpenses(@Valid @RequestBody DailyExpensesSaveReqVO updateReqVO) {
        dailyExpensesService.supplementaryExpenses(updateReqVO);
        return success(true);
    }

    @PostMapping("/delete")
    @Operation(summary = "删除日常费用")
    @TenantIgnore
    public CommonResult<Boolean> deleteDailyExpenses(@RequestBody Long[] ids) {
        for (Long id : ids) {
            dailyExpensesService.deleteDailyExpenses(id);
        }
        return success(true);
    }

    @PostMapping("/editDailyExpensesMonth")
    @Operation(summary = "编辑日常费用归属月")
    @TenantIgnore
    public CommonResult<Boolean> editDailyExpensesMonth(@Valid @RequestBody DailyExpensesSaveReqVO updateReqVO) {
        dailyExpensesService.editDailyExpensesMonth(updateReqVO);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得日常费用详情")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @TenantIgnore
    public CommonResult<DailyExpensesRespVO> getDailyExpenses(@RequestParam("id") Long id) {
        return success(dailyExpensesService.getDailyExpenses(id));
    }

    @GetMapping("/page")
    @Operation(summary = "获得日常费用分页")
    @TenantIgnore
    public CommonResult<PageResult<DailyExpensesDO>> getDailyExpensesPage(@Valid DailyExpensesPageReqVO pageReqVO) {
        pageReqVO.setTenantIds(Objects.isNull(pageReqVO.getTenantIds()) ? new Long[]{TenantContextHolder.getTenantId()} : pageReqVO.getTenantIds());
        PageResult<DailyExpensesDO> pageResult = dailyExpensesService.getDailyExpensesPage(pageReqVO);
        return success(pageResult);
    }

    @GetMapping("/downloadExcel")
    @Operation(summary = "下载日常费用导入模板")
    public void downloadFile(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("/importDailyExpensesExcel")
    @Operation(summary = "导入日常费用 Excel")
    public CommonResult<Map<String, Object>> importDailyExpensesExcel(@RequestBody MultipartFile file) throws IOException {
        List<DailyExpensesExcelVO> list = ExcelUtils.read(file, DailyExpensesExcelVO.class);
        return success(dailyExpensesService.importDailyExpensesExcel(list));
    }

    @PostMapping("/downFileErrorExcel")
    @Operation(summary = "导出失败的日常费用excel")
    public void downFileErrorExcel(@RequestBody List<DailyExpensesExcelVO> payload, HttpServletResponse response) throws IOException {
//        List<DailyExpensesExcelVO> data = JSONArray.parseArray(payload.toString(), DailyExpensesExcelVO.class);
        ExcelUtils.write(response, "日常费用数据", "失败数据", DailyExpensesExcelVO.class, payload);
    }

    @GetMapping("/exportExcel")
    @Operation(summary = "日常费用Excel")
    @ApiAccessLog(operateType = EXPORT)
    @TenantIgnore
    public void exportExcel(@Valid DailyExpensesPageReqVO pageReqVO,
                                  HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        pageReqVO.setIsSelectBed(false);
        List<DailyExpensesDO> list = dailyExpensesService.getDailyExpensesPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "日常费用.xls", "数据", DailyExpensesExportExcelVO.class,
                BeanUtils.toBean(list, DailyExpensesExportExcelVO.class));
    }


    @GetMapping("/getBillingMonthByElderId")
    @Operation(summary = "根据长者ID查询长者账单月份")
    @TenantIgnore
    public CommonResult<List<String>> getBillingMonthByElderId(@RequestParam("elderId") Long elderId) {
        return success(dailyExpensesService.getBillingMonthByElderId(elderId));
    }

    @GetMapping("/exportMedicalExcel")
    @Operation(summary = "医疗费用Excel")
    @ApiAccessLog(operateType = EXPORT)
    public void exportMedicalExcel(@Valid DailyMedicalExpensesReqVO reqVO,
                            HttpServletResponse response) throws IOException {
        List<DailyExpensesDO> list = dailyExpensesService.getMedicalDailyExpenses(reqVO);
        // 导出 Excel
        ExcelUtils.write(response, "医疗费用.xls", "数据", DailyMedicalExpensesVO.class,
                BeanUtils.toBean(list, DailyMedicalExpensesVO.class));
    }

    @PostMapping("/use-medicine")
    @Operation(summary = "用药")
    public CommonResult<Boolean> useMedicine(@Valid @RequestBody UseMedicineVO useMedicineVO) {
        dailyExpensesService.useMedicine(useMedicineVO);
        return success(true);
    }

}