package com.saye.hospitalgd.service.impl.FinancialReconciliation;

import com.saye.hospitalgd.mapper.FinancialReconciliation.MobileYbReconciliationMapper;
import com.saye.hospitalgd.service.FinancialReconciliation.MobileYbReconciliationService;
import com.saye.hospitalgd.commons.uuid.UUIDGenerator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.HashMap;

@Service
public class MobileYbReconciliationServiceImpl implements MobileYbReconciliationService {

    @Autowired
    private MobileYbReconciliationMapper mobileYbReconciliationMapper;

    @Override
    public HashMap<Object, Object> reconcile(String tradeDate) throws Exception {
        HashMap<Object, Object> resp = new HashMap<>();
        String errCode = "0";
        String errMsg = "";

        try {
            HashMap<Object, Object> param = new HashMap<>();
            param.put("trade_date", tradeDate);

            HashMap<Object, Object> bank = mobileYbReconciliationMapper.sumBankMobileYb(param);
            HashMap<Object, Object> his = mobileYbReconciliationMapper.sumHisMobileYb(param);

            BigDecimal bankMedfee = getDecimal(bank.get("bank_medfee"));
            BigDecimal bankAcct = getDecimal(bank.get("bank_acct_pay"));
            BigDecimal bankFund = getDecimal(bank.get("bank_fund_pay"));

            BigDecimal hisMedfee = getDecimal(his.get("his_medfee"));
            BigDecimal hisAcct = getDecimal(his.get("his_acct_pay"));
            BigDecimal hisFund = getDecimal(his.get("his_fund_pay"));

            BigDecimal diffMedfee = bankMedfee.subtract(hisMedfee);
            BigDecimal diffAcct = bankAcct.subtract(hisAcct);
            BigDecimal diffFund = bankFund.subtract(hisFund);

            String stmtRslt = (diffMedfee.compareTo(BigDecimal.ZERO) == 0
                    && diffAcct.compareTo(BigDecimal.ZERO) == 0
                    && diffFund.compareTo(BigDecimal.ZERO) == 0) ? "0" : "1";

            // 覆盖写结果
            mobileYbReconciliationMapper.deleteResultByDate(param);
            HashMap<Object, Object> insert = new HashMap<>();
            insert.put("id", UUIDGenerator.getUUID());
            insert.put("trade_date", tradeDate);
            insert.put("bank_medfee", bankMedfee);
            insert.put("bank_acct_pay", bankAcct);
            insert.put("bank_fund_pay", bankFund);
            insert.put("his_medfee", hisMedfee);
            insert.put("his_acct_pay", hisAcct);
            insert.put("his_fund_pay", hisFund);
            insert.put("diff_medfee", diffMedfee);
            insert.put("diff_acct_pay", diffAcct);
            insert.put("diff_fund_pay", diffFund);
            insert.put("stmt_rslt", stmtRslt);
            insert.put("stmt_rslt_dscr", stmtRslt.equals("0") ? "平" : "不平");

            mobileYbReconciliationMapper.insertResult(insert);

            resp.putAll(insert);
        } catch (Exception e) {
            errCode = "999";
            errMsg = e.getMessage();
        }
        resp.put("errCode", errCode);
        resp.put("errMsg", errMsg);
        return resp;
    }

    @Override
    public HashMap<Object, Object> findResult(String tradeDate) {
        HashMap<Object, Object> param = new HashMap<>();
        param.put("trade_date", tradeDate);
        return mobileYbReconciliationMapper.findResultByDate(param);
    }

    private BigDecimal getDecimal(Object obj) {
        if (obj == null || "".equals(obj.toString().trim())) {
            return BigDecimal.ZERO;
        }
        return new BigDecimal(obj.toString());
    }
}






