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.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.elderlyorder.ElderlyServiceOrderPageReqVO;
import cn.iocoder.yudao.module.system.controller.admin.biz.vo.elderlyorder.ElderlyServiceOrderRespVO;
import cn.iocoder.yudao.module.system.controller.admin.biz.vo.elderlyorder.ElderlyServiceOrderSaveReqVO;
import cn.iocoder.yudao.module.system.controller.admin.biz.vo.elderlyorder.excel.ElderlyServiceOrderExportExcelVO;
import cn.iocoder.yudao.module.system.controller.admin.biz.vo.elderlyorder.excel.ElderlyServiceOrderImportExcelVO;
import cn.iocoder.yudao.module.system.dal.dataobject.biz.ElderlyServiceOrderDO;
import cn.iocoder.yudao.module.system.service.biz.ElderlyServiceOrderService;
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.validation.annotation.Validated;
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.util.List;

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-service-order")
@Validated
public class ElderlyServiceOrderController {

    @Resource
    private ElderlyServiceOrderService elderlyServiceOrderService;

    @PostMapping("/create")
    @Operation(summary = "创建服务工单")
    public CommonResult<Long> create(@Valid @RequestBody ElderlyServiceOrderSaveReqVO createReqVO) {
        createReqVO.setTenantId(createReqVO.getTenantId() != null ? createReqVO.getTenantId() : TenantContextHolder.getTenantId());
        return success(elderlyServiceOrderService.create(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新服务工单")
    public CommonResult<Boolean> update(@Valid @RequestBody ElderlyServiceOrderSaveReqVO updateReqVO) {
        elderlyServiceOrderService.update(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除服务工单")
    @Parameter(name = "id", description = "编号", required = true)
    public CommonResult<Boolean> delete(@RequestParam("id") Long id) {
        elderlyServiceOrderService.delete(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得服务工单详情")
    @Parameter(name = "id", description = "编号", required = true, example = "1")
    public CommonResult<ElderlyServiceOrderDO> get(@RequestParam("id") Long id) {
        return success(elderlyServiceOrderService.get(id));
    }

    @GetMapping("/page")
    @Operation(summary = "获得服务工单分页")
    @TenantIgnore // tenantIds 由前端传入（上级可查多个下级）
    public CommonResult<PageResult<ElderlyServiceOrderRespVO>> page(@Valid ElderlyServiceOrderPageReqVO pageReqVO) {
        return success(elderlyServiceOrderService.getPage(pageReqVO));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出服务工单 Excel")
    @ApiAccessLog(operateType = EXPORT)
    @TenantIgnore
    public void exportExcel(@Validated ElderlyServiceOrderPageReqVO pageReqVO, HttpServletResponse response) throws IOException {
        List<ElderlyServiceOrderRespVO> list = elderlyServiceOrderService.exportList(pageReqVO);
        List<ElderlyServiceOrderExportExcelVO> excelList = BeanUtils.toBean(list, ElderlyServiceOrderExportExcelVO.class);
        // 输出 Excel
        ExcelUtils.write(response, "服务工单.xlsx", "数据", ElderlyServiceOrderExportExcelVO.class, excelList);
    }

    @GetMapping("/get-import-template")
    @Operation(summary = "获得导入模板")
    public void getImportTemplate(HttpServletResponse response) throws IOException {
        // 输出 Excel
        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("/import")
    @Operation(summary = "导入服务工单")
    @TenantIgnore
    public CommonResult<String> importExcel(@RequestParam("file") MultipartFile file) throws Exception {
        List<ElderlyServiceOrderImportExcelVO> list = ExcelUtils.read(file, ElderlyServiceOrderImportExcelVO.class);
        elderlyServiceOrderService.importExcel(list, TenantContextHolder.getTenantId());
        return success("导入成功");
    }

}

