package cn.hnthyy.thmz.service.impl.his;

import cn.hnthyy.thmz.Utils.CloneUtil;
import cn.hnthyy.thmz.common.Constants;
import cn.hnthyy.thmz.entity.his.YpMzFytj;
import cn.hnthyy.thmz.enums.ConfirmFlagEnum;
import cn.hnthyy.thmz.mapper.his.YpMzFytjMapper;
import cn.hnthyy.thmz.service.his.YpMzFytjService;
import cn.hnthyy.thmz.vo.ChargeFeeParamsVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class YpMzFytjServiceImpl implements YpMzFytjService {
    @SuppressWarnings("all")
    @Autowired
    private YpMzFytjMapper ypMzFytjMapper;
    @Override
    public List<YpMzFytj> queryYpMzFytj(ChargeFeeParamsVo chargeFeeParamsVo) {
        return ypMzFytjMapper.selectYpMzFytj(chargeFeeParamsVo);
    }

    @Override
    public List<Map<String, Object>> queryYpMx(ChargeFeeParamsVo chargeFeeParamsVo) {
        return ypMzFytjMapper.selectYpMx(chargeFeeParamsVo);
    }

    @Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.DEFAULT, timeout = 36000, rollbackFor = Exception.class)
    @Override
    public int refundMedicine(ChargeFeeParamsVo chargeFeeParamsVo) {
        List<String> chargeItemCodeAndDecAmounts=chargeFeeParamsVo.getChargeItemCodeAndDecAmounts();
        if(chargeItemCodeAndDecAmounts==null || chargeItemCodeAndDecAmounts.size()==0){
            return 0;
        }
        Map<String,String> chargeItemCodeAndDecAmountMap = new HashMap<>();
        for (String str:chargeItemCodeAndDecAmounts){
            String [] arr=str.split(",");
            chargeItemCodeAndDecAmountMap.put(arr[0].trim(),arr[1].trim());
        }
        List<YpMzFytj> ypMzFytjList=ypMzFytjMapper.selectConfirdYpMzFytjBySerialNoAndGroupNo(chargeFeeParamsVo.getSerialNo(),chargeFeeParamsVo.getGroupNoOut(),chargeFeeParamsVo.getOrderNo());
        if(ypMzFytjList==null || ypMzFytjList.size()==0){
            return 0;
        }
        for(YpMzFytj ypMzFytj:ypMzFytjList){
            if(ypMzFytj!=null){
                YpMzFytj newYpMzFytj = CloneUtil.clone(ypMzFytj);
                ypMzFytj.setSerialNo(-ypMzFytj.getSerialNo());
                ypMzFytj.setRealNo(-ypMzFytj.getRealNo());
                ypMzFytjMapper.updateYpMzFytj(ypMzFytj);
                String decAmount=chargeItemCodeAndDecAmountMap.get(ypMzFytj.getChargeItemCode().trim());
                if(StringUtils.isNotBlank(decAmount)){
                    decAmount=decAmount.replace("<br>","");
                }
                if(StringUtils.isBlank(decAmount)){
                    newYpMzFytj.setQuantity(0D);
                }else {
                    if(Constants.CYF.equals(newYpMzFytj.getBillItemCode().trim())){
                        newYpMzFytj.setQuantity(BigDecimal.valueOf(Double.valueOf(decAmount)).divide(BigDecimal.valueOf(newYpMzFytj.getCyFy())).doubleValue());
                    }else {
                        newYpMzFytj.setQuantity(Double.valueOf(decAmount));
                    }

                }
                newYpMzFytj.setReceiptNo(-newYpMzFytj.getReceiptNo());
                newYpMzFytj.setSerialNo(-newYpMzFytj.getSerialNo());
                newYpMzFytj.setConfirmFlag(ConfirmFlagEnum.PRIENTED.code);
                ypMzFytjMapper.insertYpMzFytj(newYpMzFytj);
            }
        }
        return ypMzFytjList.size();
    }

    @Override
    public YpMzFytj querylastItem(String groupNo, Date beginDate, Date endDate) {
        return ypMzFytjMapper.selectlastItem(groupNo,beginDate,endDate);
    }
}
