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

import cn.hnthyy.thmz.entity.MzException;
import cn.hnthyy.thmz.entity.his.*;
import cn.hnthyy.thmz.mapper.his.*;
import cn.hnthyy.thmz.mapper.his.yf.YfDivideMapper;
import cn.hnthyy.thmz.service.his.yf.YfDivideService;
import cn.hnthyy.thmz.vo.YpBaseYfVo;
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.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @description:药品分装组合接口
 * @author: WANGJIALIANG
 * @time: 2021/9/15 16:34
 */
@Service
public class YfDivideServiceImpl implements YfDivideService {

    @Autowired
    YfDivideMapper yfDivideMapper;
    @Autowired
    YpBaseYfMapper ypBaseYfMapper;
    @Autowired
    YpInDetlYfMapper ypInDetlYfMapper;
    @Autowired
    YpOutDetlYfMapper ypOutDetlYfMapper;
    @Autowired
    YpZdDictMapper ypZdDictMapper;
    @Autowired
    YpConfigMapper ypConfigMapper;

    @Override
    @Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.DEFAULT, timeout = 36000, rollbackFor = Exception.class)
    public int saveYfDivide(List<YpBaseYfVo> ypBaseYfVos,String userIdCode) throws MzException {
        for (YpBaseYfVo ypBaseYfVo : ypBaseYfVos) {
            YpBaseYf ypBaseYf = ypBaseYfMapper.selectYpBaseYf(ypBaseYfVo.getChargeCode(), ypBaseYfVo.getSerial(), ypBaseYfVo.getGroupNo());
            YpBaseYf ypBaseYfOld = ypBaseYfMapper.selectYpBaseYf(ypBaseYfVo.getChargeCode(), ypBaseYfVo.getOldSerial(), ypBaseYfVo.getGroupNo());
            if(ypBaseYfOld.getStockAmount() < ypBaseYfVo.getOldAmount()){
                throw new MzException("药品编码【"+ypBaseYfOld.getChargeCode()+"】药房实际库存小于原包装分装库存！");
            }
            YpConfig ypConfig=ypConfigMapper.selectYpConfig();
            ypConfig.setYfInNo(ypConfig.getYfInNo()+1);
            ypConfig.setYfOutNo(ypConfig.getYfOutNo()+1);
            ypConfigMapper.updateYpConfig(ypConfig);
            YpMzDivide ypMzDivide = new YpMzDivide();
            int inSeri = yfDivideMapper.selectMaxInSeri(ypBaseYfVo.getGroupNo())+1;
            ypMzDivide.setInSeri(inSeri);
            ypMzDivide.setAcctSign("0");
            ypMzDivide.setChargeCode(ypBaseYfVo.getChargeCode());
            ypMzDivide.setGroupNo(ypBaseYfVo.getGroupNo());
            ypMzDivide.setSerial(ypBaseYfVo.getSerial());
            ypMzDivide.setAmount(ypBaseYfVo.getAmount());
            ypMzDivide.setDivider(userIdCode);
            yfDivideMapper.insertYpMzDivide(ypMzDivide);
            YpZdDict ypZdDict = ypZdDictMapper.selectYpZdDictByCodeAndSerial(ypBaseYfVo.getChargeCode(), ypBaseYfVo.getSerial());
            YpInDetlYf ypInDetlYf = new YpInDetlYf();
            ypInDetlYf.setInDate(new Date());
            ypInDetlYf.setInSeri(inSeri);
            ypInDetlYf.setChargeCode(ypBaseYfVo.getChargeCode());
            ypInDetlYf.setSerial(ypBaseYfVo.getSerial());
            ypInDetlYf.setBuyAmt(ypBaseYfVo.getAmount());
            ypInDetlYf.setBuyPrice(ypZdDict.getBuyPrice());
            ypInDetlYf.setPackRetprice(ypZdDict.getPackRetprice());
            SimpleDateFormat sdf = new SimpleDateFormat("yyMMdd");
            String inDocuNo = sdf.format(new Date())+ypConfig.getYfInNo();
            String outDrawNo = sdf.format(new Date());
            if(ypConfig.getYfOutNo() < 1000){
                outDrawNo += "0";
            }
            outDrawNo += ypConfig.getYfOutNo();
            ypInDetlYf.setInDocuNo(inDocuNo);
            ypInDetlYf.setOutDrawNo(outDrawNo);
            ypInDetlYf.setPurchaser(userIdCode);
            ypInDetlYf.setChecker(userIdCode);
            ypInDetlYf.setInputId(userIdCode);
            ypInDetlYf.setInComment("分装入库");
            ypInDetlYf.setInType("9");
            ypInDetlYf.setAcctType("1");
            ypInDetlYf.setGroupOut(ypBaseYfVo.getGroupNo());
            ypInDetlYf.setGroupNo(ypBaseYfVo.getGroupNo());
            ypInDetlYf.setCurrentStock(ypBaseYfVo.getAmount());
            ypInDetlYf.setStockAmount(ypBaseYf.getStockAmount());
            ypInDetlYf.setSysDate(new Date());
            ypInDetlYfMapper.insertYpInDetlYf(ypInDetlYf);
            YpOutDetlYf ypOutDetlYf = new YpOutDetlYf();
            ypOutDetlYf.setOutDate(new Date());
            ypOutDetlYf.setOutSeri(0);
            ypOutDetlYf.setInSeri(inSeri);
            ypOutDetlYf.setDrawNo(outDrawNo);
            if(ypBaseYfVo.getGroupNo().equals("73")){
                ypOutDetlYf.setDeptCode("2060700");
            }else if(ypBaseYfVo.getGroupNo().equals("71")){
                ypOutDetlYf.setDeptCode("2060400");
            }
            YpZdDict ypZdDictOld = ypZdDictMapper.selectYpZdDictByCodeAndSerial(ypBaseYfVo.getChargeCode(), ypBaseYfVo.getOldSerial());
            ypOutDetlYf.setChargeCode(ypBaseYfVo.getChargeCode());
            ypOutDetlYf.setSerial(ypBaseYfVo.getOldSerial());
            ypOutDetlYf.setPackRetprice(ypZdDictOld.getPackRetprice());
            ypOutDetlYf.setOutAmt(ypBaseYfVo.getOldAmount());
            ypOutDetlYf.setDrawer(userIdCode);
            ypOutDetlYf.setKeeper(userIdCode);
            ypOutDetlYf.setInputId(userIdCode);
            ypOutDetlYf.setConfirmId(userIdCode);
            ypOutDetlYf.setEffDate(null);
            ypOutDetlYf.setOutType("9");
            ypOutDetlYf.setStockAmount(ypBaseYfOld.getStockAmount());
            ypOutDetlYf.setGroupNo(ypBaseYfVo.getGroupNo());
            ypOutDetlYf.setGroupNoOut(ypBaseYfVo.getGroupNo());
            ypOutDetlYf.setBuyPrice(ypZdDictOld.getBuyPrice());
            ypOutDetlYf.setConfirmFlag("1");
            ypOutDetlYf.setConfirmDate(new Date());
            ypOutDetlYf.setComment("分装出库");
            ypOutDetlYfMapper.insertYpOutDetlYf(ypOutDetlYf);
            YpBaseYf ypBaseYfNew = new YpBaseYf();
            double stockAmount = ypBaseYf.getStockAmount() + ypBaseYfVo.getAmount();
            ypBaseYfNew.setStockAmount(stockAmount);
            ypBaseYfNew.setStockAmountVirtual(stockAmount);
            ypBaseYfNew.setStockValue(ypZdDict.getPackRetprice().multiply(BigDecimal.valueOf(stockAmount)));
            ypBaseYfNew.setChargeCode(ypBaseYfVo.getChargeCode());
            ypBaseYfNew.setSerial(ypBaseYfVo.getSerial());
            ypBaseYfNew.setGroupNo(ypBaseYfVo.getGroupNo());
            ypBaseYfMapper.updateYpBaseYf(ypBaseYfNew);
            YpBaseYf ypBaseYfNewOld = new YpBaseYf();
            double stockAmountOld = ypBaseYfOld.getStockAmount() - ypBaseYfVo.getOldAmount();
            ypBaseYfNewOld.setStockAmount(stockAmountOld);
            ypBaseYfNewOld.setStockValue(ypZdDictOld.getPackRetprice().multiply(BigDecimal.valueOf(stockAmountOld)));
            ypBaseYfNewOld.setChargeCode(ypBaseYfVo.getChargeCode());
            ypBaseYfNewOld.setSerial(ypBaseYfVo.getOldSerial());
            ypBaseYfNewOld.setGroupNo(ypBaseYfVo.getGroupNo());
            ypBaseYfMapper.updateYpBaseYf(ypBaseYfNewOld);
        }
        return 0;
    }
}
