package com.prolog.cs.biz.outbound.service.impl;

import com.prolog.cs.biz.outbound.dao.BarcodeHMapper;
import com.prolog.cs.biz.outbound.funcflow.innnercheckfunc.MergeLCLFunc;
import com.prolog.cs.biz.outbound.funcflow.innnercheckfunc.ValidLCLSecondFunc;
import com.prolog.cs.biz.outbound.model.dto.check.LclDTO;
import com.prolog.cs.biz.outbound.model.vo.LcLSecondVO;
import com.prolog.cs.biz.outbound.service.LclService;
import com.prolog.framework.common.message.RestMessage;
import com.prolog.framework.core.exception.BizException;
import com.prolog.framework.utils.AssertUtils;
import com.prolog.framework.utils.SnowFlakeUtils;

import com.prolog.function.annotation.Flow;
import com.prolog.function.annotation.FuncImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import java.util.List;

@Service
public class LclServiceImpl implements LclService {

    @Autowired
    private BarcodeHMapper barcodeHMapper;

    private ValidLCLSecondFunc validLCLSecondFunc;
    private MergeLCLFunc mergeLCLFunc;

    @Value("${prolog.dao.keyGenerator.snowFlake.dataCenterId:15}")
    private long dataCenterId;

    @Value("${prolog.dao.keyGenerator.snowFlake.machineId:2}")
    private long machineId;

    @PostConstruct
    public void init() {
        validLCLSecondFunc = this::validLCLSecondFunc;
        mergeLCLFunc = this::mergeLCLFunc;
    }

    @Override
    public List<LcLSecondVO> query(LclDTO lclDTO) {
        List<LcLSecondVO> barcodeHDOS = barcodeHMapper.query(lclDTO);
        return barcodeHDOS;
    }


    @Override
    @Flow(name = "二次拼箱", category = "出库/复核管理/内复核/拼箱", index = 7, description = "二次拼箱")
    @FuncImpl(name = "波次创建入参校验", func = ValidLCLSecondFunc.class, description = "波次创建入参校验")
    public RestMessage<String> merge(List<String> barcodeIds) {
        ;
        return mergeLCLFunc.mergeLCLFunc( barcodeIds);
    }

    @FuncImpl(name = "波次创建入参校验", func = ValidLCLSecondFunc.class, description = "波次创建入参校验")
    public void validLCLSecondFunc(List<String> barcodeIds) {
        AssertUtils.isNotEmpty(barcodeIds);
        List<LcLSecondVO> byIds = barcodeHMapper.getByIds(barcodeIds);
        if(byIds!=null && byIds.size()>0){
            for (int i = 0; i < byIds.size(); i++) {
                if(byIds.get(i).getIsMerged()!=null && byIds.get(i).getIsMerged()==1){
                    throw new BizException("已经拼箱，不能重复拼箱");
                }
            }
        }
    }

    @FuncImpl(name = "波次创建入参校验", func = MergeLCLFunc.class, description = "波次创建入参校验")
    public RestMessage<String> mergeLCLFunc(List<String> barcodeIds) {
        String mergedCode = String.valueOf(SnowFlakeUtils.getId(dataCenterId,machineId));
        barcodeHMapper.updateMergedLcl(barcodeIds, mergedCode);
        return RestMessage.doSuccess("合单成功");
    }
}
