package com.hw.mes.service.impl;

import java.util.Date;
import java.util.List;

import com.hw.common.core.exception.ServiceException;
import com.hw.common.core.utils.DateUtils;
import com.hw.common.security.utils.SecurityUtils;
import com.hw.mes.api.domain.MesBaseBarcodeInfo;
import com.hw.mes.domain.vo.MesPalletInfoBindVo;
import com.hw.mes.mapper.MesBaseBarcodeInfoMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.hw.mes.mapper.MesBasePalletInfoMapper;
import com.hw.mes.domain.MesBasePalletInfo;
import com.hw.mes.service.IMesBasePalletInfoService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 托盘信息Service业务层处理
 *
 * @author Yinq
 * @date 2024-01-26
 */
@Service
public class MesBasePalletInfoServiceImpl implements IMesBasePalletInfoService {
    @Autowired
    private MesBasePalletInfoMapper mesBasePalletInfoMapper;

    @Autowired
    private MesBaseBarcodeInfoMapper mesBaseBarcodeInfoMapper;

    /**
     * 查询托盘信息
     *
     * @param palletInfoId 托盘信息主键
     * @return 托盘信息
     */
    @Override
    public MesBasePalletInfo selectMesBasePalletInfoByPalletInfoId(Long palletInfoId) {
        return mesBasePalletInfoMapper.selectMesBasePalletInfoByPalletInfoId(palletInfoId);
    }

    /**
     * 查询托盘信息列表
     *
     * @param mesBasePalletInfo 托盘信息
     * @return 托盘信息
     */
    @Override
    public List<MesBasePalletInfo> selectMesBasePalletInfoList(MesBasePalletInfo mesBasePalletInfo) {
        return mesBasePalletInfoMapper.selectMesBasePalletInfoList(mesBasePalletInfo);
    }

    /**
     * 新增托盘信息
     *
     * @param mesBasePalletInfo 托盘信息
     * @return 结果
     */
    @Override
    public int insertMesBasePalletInfo(MesBasePalletInfo mesBasePalletInfo) {
        mesBasePalletInfo.setCreateTime(DateUtils.getNowDate());
        mesBasePalletInfo.setCreateBy(SecurityUtils.getUsername());
        return mesBasePalletInfoMapper.insertMesBasePalletInfo(mesBasePalletInfo);
    }

    /**
     * 修改托盘信息
     *
     * @param mesBasePalletInfo 托盘信息
     * @return 结果
     */
    @Override
    public int updateMesBasePalletInfo(MesBasePalletInfo mesBasePalletInfo) {
        mesBasePalletInfo.setUpdateTime(DateUtils.getNowDate());
        mesBasePalletInfo.setUpdateBy(SecurityUtils.getUsername());
        return mesBasePalletInfoMapper.updateMesBasePalletInfo(mesBasePalletInfo);
    }

    /**
     * 批量删除托盘信息
     *
     * @param palletInfoIds 需要删除的托盘信息主键
     * @return 结果
     */
    @Override
    public int deleteMesBasePalletInfoByPalletInfoIds(Long[] palletInfoIds) {
        return mesBasePalletInfoMapper.deleteMesBasePalletInfoByPalletInfoIds(palletInfoIds);
    }

    /**
     * 删除托盘信息信息
     *
     * @param palletInfoId 托盘信息主键
     * @return 结果
     */
    @Override
    public int deleteMesBasePalletInfoByPalletInfoId(Long palletInfoId) {
        return mesBasePalletInfoMapper.deleteMesBasePalletInfoByPalletInfoId(palletInfoId);
    }


    /**
     * 绑定托盘信息
     * @param mesPalletInfoBindVo
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int bindPalletInfo(MesPalletInfoBindVo mesPalletInfoBindVo) {
        String palletInfoCode = mesPalletInfoBindVo.getPalletInfoCode();
        String materialBarcode = mesPalletInfoBindVo.getMaterialBarcode();

        MesBaseBarcodeInfo mesBaseBarcodeInfo = mesBaseBarcodeInfoMapper.selectMesBaseBarcodeInfoByBarcodeInfo(materialBarcode);
        if (mesBaseBarcodeInfo == null) {
            throw new ServiceException("物料条码有误");
        }

        MesBasePalletInfo mesBasePalletInfo = mesBasePalletInfoMapper.selectMesBasePalletInfoByPalletInfoCode(palletInfoCode);
        if (mesBasePalletInfo == null) {
            throw new ServiceException("托盘码有误");
        }

        String userName = SecurityUtils.getUsername();
        Date currentDate = new Date();
        mesBaseBarcodeInfo.setPalletInfoCode(palletInfoCode);
        mesBaseBarcodeInfo.setUpdateBy(userName);
        mesBaseBarcodeInfo.setUpdateTime(currentDate);
        mesBaseBarcodeInfoMapper.updateMesBaseBarcodeInfo(mesBaseBarcodeInfo);

        mesBasePalletInfo.setMaterialBarcode(materialBarcode);
        mesBasePalletInfo.setMaterialId(mesBaseBarcodeInfo.getMaterialId());
        mesBasePalletInfo.setMaterialName(mesBaseBarcodeInfo.getMaterialName());
        mesBasePalletInfo.setUpdateBy(userName);
        mesBasePalletInfo.setUpdateTime(currentDate);

        return mesBasePalletInfoMapper.updateMesBasePalletInfo(mesBasePalletInfo);

    }
}
