package com.hzs.sale.deliver.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hzs.common.core.constant.msg.SaOrderMsgConstants;
import com.hzs.common.core.enums.EDelFlag;
import com.hzs.common.core.exception.ServiceException;
import com.hzs.common.domain.sale.deliver.SaDeliverBarCode;
import com.hzs.common.util.TransactionUtils;
import com.hzs.sale.deliver.mapper.SaDeliverBarCodeMapper;
import com.hzs.sale.deliver.param.BindCodeParam;
import com.hzs.sale.deliver.service.ISaDeliverBarCodeService;
import com.hzs.system.base.IBarCodeServiceApi;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import java.util.stream.Collectors;

/**
 * 发货条码表 服务实现类
 *
 * @author hzs
 * @since 2022-11-02
 */
@Service
public class SaDeliverBarCodeServiceImpl extends ServiceImpl<SaDeliverBarCodeMapper, SaDeliverBarCode> implements ISaDeliverBarCodeService {

    @DubboReference
    IBarCodeServiceApi iBarCodeServiceApi;

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void codeReset(Long id, Integer pkCountry, Long userId) {
        SaDeliverBarCode saDeliverBarCode = baseMapper.selectById(id);

        if (null != saDeliverBarCode) {
            // 清除发货单条码表
            LambdaUpdateWrapper<SaDeliverBarCode> updateWrapper = new LambdaUpdateWrapper<>();
            updateWrapper.eq(SaDeliverBarCode::getPkCountry, pkCountry);
            updateWrapper.eq(SaDeliverBarCode::getPkId, id);
            updateWrapper.set(SaDeliverBarCode::getDelFlag, EDelFlag.DELETE.getValue());
            updateWrapper.set(SaDeliverBarCode::getPkModified, userId);
            updateWrapper.set(SaDeliverBarCode::getModifiedTime, new Date());
            baseMapper.update(null, updateWrapper);

            // 调用条码服务，解绑条码
            if (!iBarCodeServiceApi.clearBarCode(Collections.singletonList(saDeliverBarCode.getPkBarCodeItems()), pkCountry, userId).isSuccess()) {
                // 重置条码失败
                throw new ServiceException(TransactionUtils.getContent(SaOrderMsgConstants.DELIVER_CLEAR_CODE));
            }
        }
    }


    @Transactional(rollbackFor = Exception.class)
    @Override
    public void codeClear(List<Integer> idList, Integer pkCountry, Long userId) {
        LambdaQueryWrapper<SaDeliverBarCode> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SaDeliverBarCode::getPkCountry, pkCountry);
        queryWrapper.in(SaDeliverBarCode::getPkId, idList);
        List<SaDeliverBarCode> deliverBarCodeList = baseMapper.selectList(queryWrapper);

        // 清除发货单条码表
        LambdaUpdateWrapper<SaDeliverBarCode> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(SaDeliverBarCode::getPkCountry, pkCountry);
        updateWrapper.in(SaDeliverBarCode::getPkId, idList);
        updateWrapper.set(SaDeliverBarCode::getDelFlag, EDelFlag.DELETE.getValue());
        updateWrapper.set(SaDeliverBarCode::getPkModified, userId);
        updateWrapper.set(SaDeliverBarCode::getModifiedTime, new Date());
        baseMapper.update(null, updateWrapper);

        if (deliverBarCodeList.size() > 0) {
            List<Integer> barCodeIdList = deliverBarCodeList.stream().map(SaDeliverBarCode::getPkBarCodeItems).collect(Collectors.toList());
            // 调用条码服务，解绑条码
            if (!iBarCodeServiceApi.clearBarCode(barCodeIdList, pkCountry, userId).isSuccess()) {
                // 重置条码失败
                throw new ServiceException(TransactionUtils.getContent(SaOrderMsgConstants.DELIVER_CLEAR_CODE));
            }
        }
    }


    @Transactional(rollbackFor = Exception.class)
    @Override
    public void codeBind(List<BindCodeParam> paramList, Long userId, Integer pkCountry) {
        Date nowDate = new Date();

        // 新增发货单条码数据
        List<SaDeliverBarCode> list = new ArrayList<>(paramList.size());

        // 封装插入新条码数据
        for (BindCodeParam bindCodeParam : paramList) {
            SaDeliverBarCode saDeliverBarCode = new SaDeliverBarCode();
            saDeliverBarCode.setPkDeliver(bindCodeParam.getPkDeliver());
            saDeliverBarCode.setPkDeliverItems(bindCodeParam.getPkId());
            saDeliverBarCode.setPkProduct(bindCodeParam.getPkProduct());
            saDeliverBarCode.setPkBarCodeItems(bindCodeParam.getDeliverBarCodeId());
            saDeliverBarCode.setBarCode(bindCodeParam.getBarCode());
            saDeliverBarCode.setPkCountry(pkCountry);
            saDeliverBarCode.setPkCreator(userId);
            saDeliverBarCode.setCreationTime(nowDate);
            saDeliverBarCode.setPkModified(userId);
            saDeliverBarCode.setModifiedTime(nowDate);
            list.add(saDeliverBarCode);
        }
        // 插入发货单条码
        baseMapper.insertBatch(list);
    }

    @Override
    public int insertBatch(List<SaDeliverBarCode> list) {
        return baseMapper.insertBatch(list);
    }

}
