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.tenant.core.aop.TenantIgnore;
import cn.iocoder.yudao.framework.tenant.core.context.TenantContextHolder;
import cn.iocoder.yudao.module.system.controller.admin.biz.vo.ContractPageReqVO;
import cn.iocoder.yudao.module.system.controller.admin.biz.vo.ElderlyContractResVO;
import cn.iocoder.yudao.module.system.controller.admin.biz.vo.ElderlyContractVO;
import cn.iocoder.yudao.module.system.dal.dataobject.biz.ElderlyContractDO;
import cn.iocoder.yudao.module.system.dal.dataobject.biz.ElderlyHealthRecordDO;
import cn.iocoder.yudao.module.system.service.biz.ElderlyContractService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
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.util.List;
import java.util.Objects;

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


@Tag(name = "管理后台 - 长者合同管理")
@RestController
@RequestMapping("/elderly/contract")
@Validated
public class ElderlyContractController {

    @Resource
    private ElderlyContractService contractService;


    @PostMapping("/create")
    @Operation(summary = "创建长者合同")
    public CommonResult<Long> createContract(@Valid @RequestBody ElderlyContractDO contractDO) {
        return success(contractService.createContract(contractDO));
    }


    @GetMapping("/findPage")
    @Operation(summary = "长者合同列表")
    @TenantIgnore
    public CommonResult<PageResult<ElderlyContractDO>> findPage(@Valid ContractPageReqVO reqVO) {
        reqVO.setTenantIds(Objects.isNull(reqVO.getTenantIds()) ? new Long[]{TenantContextHolder.getTenantId()} : reqVO.getTenantIds());
        PageResult<ElderlyContractDO> pageResult = contractService.findPage(reqVO);
        return success(BeanUtils.toBean(pageResult, ElderlyContractDO.class));
    }



    @PutMapping("/update")
    @Operation(summary = "更新长者合同管理")
    @TenantIgnore
    public CommonResult<Boolean> updateContract(@Valid @RequestBody ElderlyContractDO updateReqVO) {
        contractService.updateContract(updateReqVO);
        return success(true);
    }
//
//    @DeleteMapping("/delete")
//    @Operation(summary = "删除长者合同管理")
//    @Parameter(name = "id", description = "编号", required = true)
//    @PreAuthorize("@ss.hasPermission('elderly:contract:delete')")
//    public CommonResult<Boolean> deleteContract(@RequestParam("id") Long id) {
//        contractService.deleteContract(id);
//        return success(true);
//    }
//
    @GetMapping("/get")
    @Operation(summary = "获得长者合同详情")
    @TenantIgnore
    public CommonResult<ElderlyContractVO> getContract(@RequestParam("id") Long id) {
        return success(contractService.getContract(id));
    }


    @GetMapping("/getContractByElderId")
    @Operation(summary = "根据长者ID查询合同信息")
    @TenantIgnore
    public CommonResult<ElderlyContractResVO> getContractByElderId(@RequestParam("elderId") Long elderId) {
        return success(contractService.getContractByElderId(elderId));
    }


    @PostMapping("/saveContractDetails")
    @Operation(summary = "保存合同详情(上传合同)")
    @TenantIgnore
    public CommonResult<Boolean> saveContractDetails(@RequestBody ElderlyContractDO contractDO){
        contractService.saveContractDetails(contractDO);
        return success(true);
    }

    @GetMapping("/expiring-within-one-month")
    @Operation(summary = "获取当前日期一个月内合同即将到期的长者和合同信息")
    @TenantIgnore
    public CommonResult<List<ElderlyContractDO>> getExpiringWithinOneMonthContracts() {
        return success(contractService.getExpiringWithinOneMonthContracts());
    }

//
//    @GetMapping("/page")
//    @Operation(summary = "获得长者合同管理分页")
//    @PreAuthorize("@ss.hasPermission('elderly:contract:query')")
//    public CommonResult<PageResult<ContractRespVO>> getContractPage(@Valid ContractPageReqVO pageReqVO) {
//        PageResult<ContractDO> pageResult = contractService.getContractPage(pageReqVO);
//        return success(BeanUtils.toBean(pageResult, ContractRespVO.class));
//    }
//
//    @GetMapping("/export-excel")
//    @Operation(summary = "导出长者合同管理 Excel")
//    @PreAuthorize("@ss.hasPermission('elderly:contract:export')")
//    @ApiAccessLog(operateType = EXPORT)
//    public void exportContractExcel(@Valid ContractPageReqVO pageReqVO,
//              HttpServletResponse response) throws IOException {
//        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
//        List<ContractDO> list = contractService.getContractPage(pageReqVO).getList();
//        // 导出 Excel
//        ExcelUtils.write(response, "长者合同管理.xls", "数据", ContractRespVO.class,
//                        BeanUtils.toBean(list, ContractRespVO.class));
//    }


}