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

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.ElderlyImportExcelVO;
import cn.iocoder.yudao.module.system.controller.admin.sys.vo.ElderOverheadChargeRespVO;
import cn.iocoder.yudao.module.system.controller.admin.sys.vo.OverheadChargeImportExcelVO;
import cn.iocoder.yudao.module.system.controller.admin.sys.vo.SysOverheadChargePageReqVO;
import cn.iocoder.yudao.module.system.dal.dataobject.biz.OverheadChargeDO;
import cn.iocoder.yudao.module.system.service.biz.SysOverheadChargeService;
import com.alibaba.fastjson.JSONArray;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.apache.commons.compress.utils.IOUtils;
import org.springframework.core.io.ClassPathResource;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

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

import java.io.IOException;
import java.net.URLEncoder;
import java.util.List;
import java.util.Map;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;


@Tag(name = "管理后台 - 收费项目")
@RestController
@RequestMapping("/overheadCharge")
public class SysOverheadChargeController {

    @Resource
    private SysOverheadChargeService sysOverheadChargeService;


    @PostMapping("/add")
    @Operation(summary = "创建收费项目")
    @TenantIgnore
    public CommonResult<Boolean> add(@RequestBody OverheadChargeDO overheadChargeDO) {
        overheadChargeDO.setTenantId(overheadChargeDO.getTenantId() == null ? TenantContextHolder.getTenantId() : overheadChargeDO.getTenantId());
        sysOverheadChargeService.add(overheadChargeDO);
        return success(true);
    }


    @GetMapping("/getInfoById")
    @Operation(summary = "获取收费项目详情")
    @Parameter(name = "id", description = "收费项目ID", required = true, example = "1024")
    public CommonResult<OverheadChargeDO> getInfoById(@RequestParam Long id) {
        OverheadChargeDO overheadCharge = sysOverheadChargeService.getInfoById(id);
        return success(overheadCharge);
    }


    @GetMapping("/queryPage")
    @Operation(summary = "收费项目列表")
    //@PreAuthorize("@ss.hasPermission('overheadCharge:query')")
    @TenantIgnore
    public CommonResult<PageResult<OverheadChargeDO>> queryPage(@Valid SysOverheadChargePageReqVO pageVO) {
        pageVO.setTenantIds(pageVO.getTenantIds() == null ? new Long[]{TenantContextHolder.getTenantId()} : pageVO.getTenantIds());
        PageResult<OverheadChargeDO> pageResult = sysOverheadChargeService.queryPage(pageVO);
        return success(BeanUtils.toBean(pageResult, OverheadChargeDO.class));
    }


    @PostMapping("/edit")
    @Operation(summary = "编辑收费项目")
    public CommonResult<Boolean> edit(@RequestBody OverheadChargeDO overheadChargeDO) {
        sysOverheadChargeService.edit(overheadChargeDO);
        return success(true);
    }


    @DeleteMapping("/delete")
    @Operation(summary = "删除收费项目")
    @Parameter(name = "id", description = "收费项目ID", required = true, example = "1024")
    public CommonResult<Boolean> delete(@RequestParam("id") Long id) {
        sysOverheadChargeService.delete(id);
        return success(true);
    }


    @PostMapping("/move")
    @Operation(summary = "移动收费项目")
    public CommonResult<Boolean> move(@RequestBody OverheadChargeDO overheadChargeDO) {
        sysOverheadChargeService.move(overheadChargeDO);
        return success(true);
    }


    @GetMapping("/getListByType")
    @Operation(summary = "根据类型获取收费项列表")
    public CommonResult<List<OverheadChargeDO>> getListByType(@RequestParam("type") String type,
                                                              @RequestParam(value = "startTenantId", required = false) Long startTenantId) {
        return success(sysOverheadChargeService.getListByType(type, startTenantId));
    }

    @GetMapping("/getListByElderlyId")
    @Operation(summary = "根据长者id获取月度固定收费项列表")
    @TenantIgnore
    public CommonResult<List<ElderOverheadChargeRespVO>> getListByElderlyId(@RequestParam("elderlyId") Long elderlyId) {
        return success(sysOverheadChargeService.getListByElderlyId(elderlyId));
    }

    @GetMapping("/getListByChargeCategoryId")
    @Operation(summary = "根据类型id获取收费项列表")
    @TenantIgnore
    public CommonResult<List<OverheadChargeDO>> getListByChargeCategoryId(@RequestParam("chargeCategoryId") Long chargeCategoryId) {
        return success(sysOverheadChargeService.getListByChargeCategoryId(chargeCategoryId));
    }


    @PostMapping("/importOverheadCharge")
    @Operation(summary = "导入收费项")
    public CommonResult<Map<String, Object>> importOverheadCharge(@RequestBody MultipartFile file) throws IOException {
        List<OverheadChargeImportExcelVO> dataList = ExcelUtils.read(file, OverheadChargeImportExcelVO.class);
        return success(sysOverheadChargeService.importOverheadCharge(dataList));
    }

    @PostMapping("/importOverheadChargeBySheet")
    @Operation(summary = "按 sheet 导入收费项（sheet 名=费用分类名称）")
    public CommonResult<Map<String, Object>> importOverheadChargeBySheet(@RequestParam("file") MultipartFile file,
                                                                         @RequestParam(value = "updateSupport", required = false, defaultValue = "false") Boolean updateSupport) throws IOException {
        return success(sysOverheadChargeService.importOverheadChargeBySheet(file, updateSupport));
    }

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


    @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= " + URLEncoder.encode("收费项导入模板", "UTF-8"));
        IOUtils.copy(classPathResource.getInputStream(), response.getOutputStream());
        response.flushBuffer();
    }

}
