package com.hzs.scm.ic.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hzs.common.core.constant.CacheConstants;
import com.hzs.common.core.constant.SystemFieldConstants;
import com.hzs.common.core.constant.msg.ScmMsgConstants;
import com.hzs.common.core.enums.EScmOrderType;
import com.hzs.common.core.enums.EYesNo;
import com.hzs.common.core.service.RedisService;
import com.hzs.common.core.utils.DateUtils;
import com.hzs.common.domain.scm.ic.IcBatchCode;
import com.hzs.common.domain.scm.ic.ext.IcBatchCodeExt;
import com.hzs.common.domain.scm.po.PoOrder;
import com.hzs.scm.ic.param.BatchCodeParam;
import com.hzs.scm.ic.mapper.IcBatchCodeMapper;
import com.hzs.scm.ic.service.IIcBatchCodeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * <p>
 * 采购订单明细 服务实现类
 * </p>
 *
 * @author hzs
 * @since 2023-11-23
 */
@Service
public class IcBatchCodeServiceImpl extends ServiceImpl<IcBatchCodeMapper, IcBatchCode> implements IIcBatchCodeService {

    @Autowired
    private RedisService redisService;

    @Override
    public List<IcBatchCodeExt> queryBatchCodeByCondition(BatchCodeParam batchCodeParam) {
        return baseMapper.queryBatchCodeByCondition(batchCodeParam);
    }

    @Override
    public List<Long> queryBatchCodeByProduct(List<BatchCodeParam> batchCodeParamList) {
        return baseMapper.queryBatchCodeByProduct(batchCodeParamList);
    }

    @Override
    public void insertBatchCode(BatchCodeParam batchCodeParam, Long pkMember) {
        IcBatchCode icBatchCode = BeanUtil.copyProperties(batchCodeParam, IcBatchCode.class);
        icBatchCode.setPkCreator(pkMember);
        baseMapper.insert(icBatchCode);
    }

    @Override
    public void updateBatchCode(IcBatchCode icBatchCode, BatchCodeParam batchCodeParam, Long pkMember) {
        IcBatchCode saveBatchCode = BeanUtil.copyProperties(batchCodeParam, IcBatchCode.class);
        saveBatchCode.setPkId(icBatchCode.getPkId());
        saveBatchCode.setModifiedTime(DateUtils.currentDateTime());
        saveBatchCode.setPkModified(pkMember);
        updateById(saveBatchCode);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteBatchCode(Long pkBatchCode, Long pkMember) {
        String batchCode = CacheConstants.BATCH_CODE + pkBatchCode;
        if (!redisService.lockKeyOnce(batchCode)) {
            throw new RuntimeException(ScmMsgConstants.BILL_NOT_OPERATION);
        }
        UpdateWrapper<IcBatchCode> updateWrapper = new UpdateWrapper<>();
        updateWrapper.set(SystemFieldConstants.DEL_FLAG, EYesNo.NO.getIntValue());
        updateWrapper.set(SystemFieldConstants.PK_MODIFIED, pkMember);
        updateWrapper.set(SystemFieldConstants.MODIFIED_TIME, DateUtils.currentDateTime());
        updateWrapper.eq(SystemFieldConstants.PK_ID, pkBatchCode);
        update(updateWrapper);
        redisService.unlock(batchCode);
    }
}
