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

import cn.hnthyy.thmz.entity.MzException;
import cn.hnthyy.thmz.entity.his.YpBaseYf;
import cn.hnthyy.thmz.entity.his.YpConfig;
import cn.hnthyy.thmz.entity.his.YpInDetlYf;
import cn.hnthyy.thmz.entity.his.YpZdDict;
import cn.hnthyy.thmz.entity.thmz.User;
import cn.hnthyy.thmz.mapper.his.YpBaseYfMapper;
import cn.hnthyy.thmz.mapper.his.YpConfigMapper;
import cn.hnthyy.thmz.mapper.his.YpInDetlYfMapper;
import cn.hnthyy.thmz.mapper.his.YpOutDetlMapper;
import cn.hnthyy.thmz.service.his.YpBaseYfService;
import cn.hnthyy.thmz.service.his.YpZdDictService;
import cn.hnthyy.thmz.vo.YpBaseYfVo;
import cn.hnthyy.thmz.vo.YpOutDetlVo;
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/7/28 17:09
 */
@Service
public class YpBaseYfServiceImpl implements YpBaseYfService {

    @Autowired
    private YpBaseYfMapper ypBaseYfMapper;
    @Autowired
    private YpConfigMapper ypConfigMapper;
    @Autowired
    private YpInDetlYfMapper ypInDetlYfMapper;
    @Autowired
    private YpOutDetlMapper ypOutDetlMapper;
    @Autowired
    private YpZdDictService ypZdDictService;

    @Override
    public int modifyResetVirtual(String chargeCode, String serial, String groupNo) {
        return ypBaseYfMapper.updateResetVirtual(chargeCode, serial, groupNo);
    }

    @Override
    public int modifyYpBaseYf(YpBaseYf ypBaseYf) {
        return ypBaseYfMapper.updateYpBaseYf(ypBaseYf);
    }

    @Override
    public YpBaseYf queryYpBaseYf(String chargeCode, String serial, String groupNo) {
        return ypBaseYfMapper.selectYpBaseYf(chargeCode,serial,groupNo);
    }

    @Override
    public Double querySplitPercent(String chargeCode, String groupNo) {
        return ypBaseYfMapper.selectSplitPercent(chargeCode, groupNo);
    }

    @Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.DEFAULT, timeout = 36000, rollbackFor = Exception.class)
    @Override
    public int saveYpBaseYf(List<YpOutDetlVo> ypOutDetlVos, User user) throws MzException {
        int num = 0;
        String inComment;
        double amount;//拆零后总数
        String serial;//药品序号
        BigDecimal stockValue;
        BigDecimal packRetprice;
        BigDecimal fixPrice;
        BigDecimal buyPrice;
        YpConfig ypConfig=ypConfigMapper.selectYpConfig();
        ypConfig.setYfInNo(ypConfig.getYfInNo()+1);
        ypConfigMapper.updateYpConfig(ypConfig);
        for (int i = 0; i < ypOutDetlVos.size() ; i++) {
            YpBaseYf yp;
            YpOutDetlVo ypOutDetlVo = ypOutDetlVos.get(i);
            if(ypOutDetlVo.getSplitPercent() == 1){
                serial = "01";
                inComment = "接收出库单(拆零)"+ypOutDetlVo.getDrawNo();
                amount = ypOutDetlVo.getOutAmt() * ypOutDetlVo.getPackSize().doubleValue();
                YpZdDict ypZdDict = ypZdDictService.queryYpZdDictByCodeAndSerial(ypOutDetlVo.getChargeCode(), serial);
                if(ypZdDict == null){
                    throw new MzException("入库失败，未查到药品字典含有该拆零规格的药品");
                }
                yp = ypBaseYfMapper.selectYpBaseYf(ypOutDetlVo.getChargeCode(), serial, ypOutDetlVo.getGroupNoOut());//查询当前药品在药房的最小规格信息
                packRetprice = ypZdDict.getRetprice();
                fixPrice = ypZdDict.getFixPrice();
                buyPrice = ypZdDict.getBuyPrice();
            }else if(ypOutDetlVo.getSplitPercent() == 0){
                serial = ypOutDetlVo.getSerial();
                inComment = "接收出库单"+ypOutDetlVo.getDrawNo();
                amount = ypOutDetlVo.getOutAmt();
                packRetprice = ypOutDetlVo.getPackRetprice();
                fixPrice = ypOutDetlVo.getFixPrice();
                buyPrice = ypOutDetlVo.getBuyPrice();
                yp = ypBaseYfMapper.selectYpBaseYf(ypOutDetlVo.getChargeCode(), serial, ypOutDetlVo.getGroupNoOut());
            }else{
                throw new MzException("拆零比例只允许0%和100%");
            }
            if(yp == null){//新增药房药品库存
                stockValue = ypOutDetlVo.getPackRetprice().multiply(new BigDecimal(amount));
                YpBaseYf ypBaseYf = new YpBaseYf();
                ypBaseYf.setChargeCode(ypOutDetlVo.getChargeCode());
                ypBaseYf.setSerial(serial);
                ypBaseYf.setStockAmount(amount);
                ypBaseYf.setStockValue(stockValue);
                ypBaseYf.setMaxStock(amount);
                ypBaseYf.setGroupNo(ypOutDetlVo.getGroupNoOut());
                ypBaseYf.setPackRetprice(packRetprice);
                ypBaseYf.setRetprice(ypOutDetlVo.getRetprice());
                ypBaseYf.setStockAmountVirtual(amount);
                ypBaseYf.setSplitPercent(ypOutDetlVo.getSplitPercent());
                if(ypBaseYfMapper.insertYpBaseYf(ypBaseYf) == 0){
                    throw new MzException("药品编码"+ypBaseYf.getChargeCode()+"入库出错");
                }
            }else{//更新药房药品库存
                stockValue = packRetprice.multiply(new BigDecimal(amount));
                if(ypBaseYfMapper.updateStockAmount(ypOutDetlVo.getChargeCode(),serial
                        ,ypOutDetlVo.getGroupNoOut(),amount, stockValue) == 0){
                    throw new MzException("药品编码"+ypOutDetlVo.getChargeCode()+"入库出错");
                }
            }
            //新增药房药品入库记录
            SimpleDateFormat sdf = new SimpleDateFormat("yyMMdd");
            String inDocuNo = sdf.format(new Date())+ypConfig.getYfInNo();
            int maxInSeri = ypInDetlYfMapper.selectMaxInSeri(ypOutDetlVo.getGroupNoOut(), inDocuNo);
            YpInDetlYf ypInDetlYf = new YpInDetlYf();
            ypInDetlYf.setInDate(new Date());
            ypInDetlYf.setInSeri(maxInSeri+1);
            ypInDetlYf.setChargeCode(ypOutDetlVo.getChargeCode());
            ypInDetlYf.setSerial(serial);
            ypInDetlYf.setBuyAmt(amount);
            ypInDetlYf.setBuyPrice(buyPrice);
            ypInDetlYf.setEffDate(ypOutDetlVo.getEffDate());
            ypInDetlYf.setInDocuNo(inDocuNo);
            ypInDetlYf.setManuCode(ypOutDetlVo.getManuCode());
            ypInDetlYf.setPurchaser(user.getUserIdCode());
            ypInDetlYf.setChecker(user.getUserIdCode());
            ypInDetlYf.setInType("0");
            ypInDetlYf.setPackRetprice(packRetprice);
            ypInDetlYf.setAcctType("1");
            ypInDetlYf.setFixPrice(fixPrice);
            ypInDetlYf.setGroupNo(ypOutDetlVo.getGroupNoOut());
            ypInDetlYf.setCurrentStock(amount+(yp.getStockAmount()==null?0:yp.getStockAmount()));
            ypInDetlYf.setSysDate(new Date());
            ypInDetlYf.setInputId(user.getUserIdCode());
            ypInDetlYf.setStockAmount(yp.getStockAmount()==null?0:yp.getStockAmount());
            ypInDetlYf.setGroupOut(ypOutDetlVo.getGroupNo());
            ypInDetlYf.setOutDrawNo(ypOutDetlVo.getDrawNo());
            ypInDetlYf.setInComment(inComment);
            if(ypInDetlYfMapper.insertYpInDetlYf(ypInDetlYf) == 0){
                throw new MzException("药品编码"+ypInDetlYf.getChargeCode()+"新增入库记录出错");
            }
            //更新药房入库状态
            if(ypOutDetlMapper.updateAcceptSign(ypOutDetlVo.getDrawNo(), ypOutDetlVo.getOutSeri()) == 0){
                throw new MzException("药品编码"+ypInDetlYf.getChargeCode()+"更新药房入库状态出错");
            }
            num++;
        }
        return num;
    }

    @Override
    public List<YpBaseYfVo> queryYpBaseYfLike(String searchText, String groupNo) {
        return ypBaseYfMapper.selectYpBaseYfLike(searchText, groupNo);
    }

    @Override
    public List<YpBaseYfVo> queryYpByChargeCodeAndGroupNo(String chargeCode, String groupNo) {
        return ypBaseYfMapper.selectYpByChargeCodeAndGroupNo(chargeCode, groupNo);
    }
}
