package thyyxxk.webserver.service.reports;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import thyyxxk.webserver.config.exception.ExceptionEnum;
import thyyxxk.webserver.dao.his.reports.MedicineStockCountDao;
import thyyxxk.webserver.entity.ResultVo;
import thyyxxk.webserver.entity.reports.medicinestockcount.*;
import thyyxxk.webserver.utils.DecimalUtil;
import thyyxxk.webserver.utils.ResultVoUtil;

import java.util.List;

/**
 * @author dj
 */
@Slf4j
@Service
public class MedicineStockCountService {
    private final MedicineStockCountDao dao;

    @Autowired
    public MedicineStockCountService(MedicineStockCountDao dao) {
        this.dao = dao;
    }

    public ResultVo<List<StockIndex>> getStockIndex(GetStockParam param) {
        return ResultVoUtil.success(dao.getStockIndex(param));
    }

    public ResultVo<List<StockDetail>> getStockDetail(GetStockParam param) {
        if (param.getEnd().before(param.getStart())) {
            return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "上一次盘点日期不能大于本次盘点日期！");
        }
        List<StockDetail> details = dao.getStockDetail(param);
        if (param.getLastDrawNo() != null) {
            List<StockDetail> lastDetails = dao.getLastStockDetail(param.getLastDrawNo());
            int size = lastDetails.size();
            for (StockDetail detail : details) {
                for (int i = 0; i < size; i++) {
                    StockDetail last = lastDetails.get(i);
                    if (last.getChargeCode().trim().equals(detail.getChargeCode().trim())
                            && last.getSerial().trim().equals(detail.getSerial().trim())) {
                        detail.setLastCurrStockAmount(last.getCurrStockAmount());
                        lastDetails.remove(last);
                        size--;
                    }
                }
            }
        }
        List<StockSuggestion> suggestions = dao.getStockSuggestion(param);
        int size = suggestions.size();
        for (StockDetail detail : details) {
            for (int i = 0; i < size; i++) {
                StockSuggestion suggestion = suggestions.get(i);
                if (suggestion.getChargeCodeMx().trim().equals(detail.getChargeCode().trim())
                        && suggestion.getSerial().trim().equals(detail.getSerial().trim())) {
                    detail.setAmount1(suggestion.getAmount1());
                    detail.setAmount2(suggestion.getAmount2());
                    detail.setAmount3(suggestion.getAmount3());
                    detail.setAmount4(suggestion.getAmount4());
                    detail.setZyTotal(suggestion.getZyTotal());
                    detail.setDiff1(suggestion.getDiff1());
                    detail.setDiff2(suggestion.getDiff2());
                    suggestions.remove(suggestion);
                    size--;
                }
            }
        }
        return ResultVoUtil.success(details);
    }

    @Transactional(rollbackFor = Exception.class)
    public ResultVo<String> saveStockChanges(SaveStockDetailParam param) {
        param.getSelections().forEach(item -> {
            item.setLastDrawNo(param.getLastDrawNo());
            item.setOutAmt(DecimalUtil.minus(item.getStockAmount(), item.getCurrStockAmount()));
            dao.updateStockChanges(item);
            if (null != item.getLastDrawNo() && !"".equals(item.getLastDrawNo().trim())) {
                dao.updateLastStockAmount(item);
            }
        });
        return ResultVoUtil.success();
    }
}
