package com.openhis.administration.service.impl;

import java.util.List;

import javax.validation.constraints.NotNull;

import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.openhis.administration.domain.Account;
import com.openhis.administration.mapper.AccountMapper;
import com.openhis.administration.service.IAccountService;
import com.openhis.common.constant.CommonConstants;
import com.openhis.common.constant.YbCommonConstants;
import com.openhis.common.enums.AccountStatus;
import com.openhis.common.enums.AccountType;
import com.openhis.common.enums.Whether;

/**
 * 就诊账户管理Service业务层处理
 *
 * @author system
 * @date 2025-02-20
 */
@Service
public class AccountServiceImpl extends ServiceImpl<AccountMapper, Account> implements IAccountService {

    /**
     * 门诊挂号时保存账号信息
     *
     * @param account 账号信息
     */
    @Override
    public Long saveAccountByRegister(Account account) {
        account.setEncounterFlag(Whether.YES.getValue());
        baseMapper.insert(account);
        return account.getId();
    }

    /**
     * 获取就诊患者的自费账户id
     *
     * @param encounterId 就诊id
     * @return 账户id
     */
    @Override
    public Long getSelfPayAccount(Long encounterId) {
        Account account = baseMapper
            .selectOne(new LambdaQueryWrapper<Account>().select(Account::getId).eq(Account::getEncounterId, encounterId)
                .eq(Account::getContractNo, CommonConstants.BusinessName.DEFAULT_CONTRACT_NO)
                .last(YbCommonConstants.sqlConst.LIMIT1));
        if (account != null) {
            return account.getId();
        }
        return null;
    }

    /**
     * 获取就诊患者的医保账户id
     *
     * @param encounterId 就诊id
     * @return 账户id
     */
    @Override
    public Long getMedicalInsuranceAccount(Long encounterId) {
        Account account = baseMapper
            .selectOne(new LambdaQueryWrapper<Account>().select(Account::getId).eq(Account::getEncounterId, encounterId)
                .in(Account::getTypeCode, AccountType.MEDICAL_ELECTRONIC_CERTIFICATE.getCode(),
                    AccountType.ID_CARD.getCode(), AccountType.SOCIAL_SECURITY_CARD.getCode())
                .ne(Account::getContractNo, CommonConstants.BusinessName.DEFAULT_CONTRACT_NO)
                .ne(Account::getContractNo, CommonConstants.BusinessName.DEFAULT_STUDENT_CONTRACT_NO)
                .last(YbCommonConstants.sqlConst.LIMIT1));
        if (account != null) {
            return account.getId();
        }
        return null;
    }

    /**
     * 插入或更新 Account 实体
     * 
     * @param account 实体对象
     * @return 是否成功
     */
    /**
     * 插入或更新 Account 实体
     * 
     * @param account 实体对象
     * @return 是否成功
     */
    @Override
    public boolean saveOrUpdateAccount(Account account) {
        // 创建 LambdaQueryWrapper
        LambdaQueryWrapper<Account> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Account::getPatientId, account.getPatientId())
            .eq(Account::getEncounterId, account.getEncounterId())
            // 账户状态是有效的
            .eq(Account::getStatusEnum, AccountStatus.ACTIVE.getValue());

        // 查询是否存在记录
        Account existingAccount = baseMapper.selectOne(queryWrapper);
        if (existingAccount != null) {
            // 如果记录存在，更新记录
            account.setId(existingAccount.getId()); // 设置主键
            return baseMapper.updateById(account) > 0;
        } else {
            // 如果记录不存在，插入新记录
            return baseMapper.insert(account) > 0;
        }
    }

    /**
     * 插入或更新 Account 实体
     *
     * @param account 实体对象
     * @return 是否成功
     */
    @Override
    public boolean isSelfPay(@NotNull Account account) {
        return CommonConstants.BusinessName.DEFAULT_CONTRACT_NO.equals(account.getContractNo());
    }

    /**
     * 查找挂号时使用的账户
     * 
     * @param encounterId 就诊id
     * @return 账户实体
     */
    @Override
    public Account getAccountByEncounterFlag(Long encounterId) {
        return baseMapper
            .selectOne(new LambdaQueryWrapper<Account>().eq(Account::getEncounterFlag, Whether.YES.getValue())
                .eq(Account::getEncounterId, encounterId).last(YbCommonConstants.sqlConst.LIMIT1));
    }

    /**
     * 查找挂号时使用的账户
     *
     * @param encounterId 就诊id
     * @return 账户实体
     */
    @Override
    public Account getYbAccount(Long encounterId) {
        return baseMapper.selectOne(new LambdaQueryWrapper<Account>()
            .ne(Account::getContractNo, CommonConstants.BusinessName.DEFAULT_CONTRACT_NO)
            .eq(Account::getEncounterId, encounterId).last(YbCommonConstants.sqlConst.LIMIT1));
    }

    /**
     * 查找挂号时使用的账户
     *
     * @param encounterId 就诊id
     * @return 账户实体
     */
    @Override
    public Account getSelfAccount(Long encounterId) {
        return baseMapper.selectOne(new LambdaQueryWrapper<Account>()
            .eq(Account::getContractNo, CommonConstants.BusinessName.DEFAULT_CONTRACT_NO)
            .eq(Account::getEncounterId, encounterId).last(YbCommonConstants.sqlConst.LIMIT1));
    }

    @Override
    public List<Account> getAccountListByEncounter(Long encounterId) {
        return baseMapper.selectList(new LambdaQueryWrapper<Account>().eq(Account::getEncounterId, encounterId));
    }

    /**
     * 学生医保转学生自费
     *
     * @param encounterId 就诊id
     * @return 账户id
     */
    @Override
    public Long getStudentSelfAccount(Long encounterId) {
        Account account = baseMapper
            .selectOne(new LambdaQueryWrapper<Account>().select(Account::getId).eq(Account::getEncounterId, encounterId)
                .in(Account::getTypeCode, AccountType.MEDICAL_ELECTRONIC_CERTIFICATE.getCode(),
                    AccountType.ID_CARD.getCode(), AccountType.SOCIAL_SECURITY_CARD.getCode())
                .ne(Account::getContractNo, CommonConstants.BusinessName.DEFAULT_CONTRACT_NO)
                .eq(Account::getContractNo, CommonConstants.BusinessName.DEFAULT_STUDENT_CONTRACT_NO)
                .last(YbCommonConstants.sqlConst.LIMIT1));
        if (account != null) {
            return account.getId();
        }
        return null;
    }

    /**
     * 学生自费转学生医保
     *
     * @param encounterId 就诊id
     * @return 账户id
     */
    @Override
    public Long getStudentYbAccount(Long encounterId) {
        Account account = baseMapper
            .selectOne(new LambdaQueryWrapper<Account>().select(Account::getId).eq(Account::getEncounterId, encounterId)
                .in(Account::getTypeCode, AccountType.MEDICAL_ELECTRONIC_CERTIFICATE.getCode(),
                    AccountType.ID_CARD.getCode(), AccountType.SOCIAL_SECURITY_CARD.getCode())
                .ne(Account::getContractNo, CommonConstants.BusinessName.DEFAULT_CONTRACT_NO)
                .ne(Account::getContractNo, CommonConstants.BusinessName.DEFAULT_STUDENT_CONTRACT_NO)
                .like(Account::getContractNo, CommonConstants.BusinessName.CONTRACT_STUDENT)
                .last(YbCommonConstants.sqlConst.LIMIT1));
        if (account != null) {
            return account.getId();
        }
        return null;
    }
}