package com.openhis.web.charge.patientcardrenewal;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.core.common.core.domain.R;
import lombok.extern.slf4j.Slf4j;

/**
 * 患者换卡控制器
 * 
 * @author system
 * @date 2024-01-01
 */
@RestController
@RequestMapping("/charge/patientCardRenewal")
@Slf4j
public class PatientCardRenewalController {

    @Autowired
    private PatientCardRenewalService patientCardRenewalService;

    /**
     * 执行患者换卡操作
     * 
     * @param request 换卡请求参数
     * @return 换卡结果
     */
    @PostMapping("/renewCard")
    public R<?> renewCard(@RequestBody RenewalRequest request) {
        try {
            log.info("患者换卡请求: 旧卡号={}, 新卡号={}, 患者ID={}", 
                     request.getOldCardNo(), request.getNewCardNo(), request.getPatientId());
            
            // 执行换卡操作
            boolean success = patientCardRenewalService.renewCard(request);
            
            if (success) {
                log.info("患者换卡成功: 旧卡号={} -> 新卡号={}", 
                         request.getOldCardNo(), request.getNewCardNo());
                return R.ok("换卡成功");
            } else {
                return R.fail("换卡失败");
            }
        } catch (Exception e) {
            log.error("患者换卡异常: ", e);
            return R.fail("换卡操作异常: " + e.getMessage());
        }
    }
}