package com.bonus.sgzb.base.service.impl;

import com.bonus.sgzb.base.api.domain.MaMachine;
import com.bonus.sgzb.base.mapper.MaLabelBindMapper;
import com.bonus.sgzb.base.service.IMaLabelBindService;
import com.bonus.sgzb.base.vo.MaLabelBindVO;
import com.bonus.sgzb.common.core.web.domain.AjaxResult;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

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

/**
 * 机具设备标签ma_label_bindService业务层处理
 *
 * @author bonus
 * @date 2023-12-13
 */
@Service
public class MaLabelBindServiceImpl implements IMaLabelBindService {
    @Resource
    private MaLabelBindMapper maLabelBindMapper;

    /**
     * 查询机具设备标签ma_label_bind
     *
     * @param maId 机具设备标签ma_label_bind主键
     * @return 机具设备标签ma_label_bind
     */
    @Override
    public MaLabelBindVO selectMaLabelBindByMaId(Long maId) {
        return maLabelBindMapper.selectMaLabelBindByMaId(maId);
    }

    /**
     * 查询机具设备标签ma_label_bind列表
     *
     * @param maLabelBindVO 机具设备标签ma_label_bind
     * @return 机具设备标签ma_label_bind
     */
    @Override
    public List<MaLabelBindVO> selectMaLabelBindList(MaLabelBindVO maLabelBindVO) {
        return maLabelBindMapper.selectMaLabelBindList(maLabelBindVO);
    }

    /**
     * 查询机具设备标签历史ma_label_bind列表
     *
     * @param maLabelBindVO 机具设备标签ma_label_bind
     * @return 机具设备标签ma_label_bind
     */
    @Override
    public List<MaLabelBindVO> selecthistoryList(MaLabelBindVO maLabelBindVO) {
        return maLabelBindMapper.selecthistoryList(maLabelBindVO);
    }

    /**
     * 绑定机具设备标签ma_label_bind
     *
     * @param maLabelBind 机具设备标签ma_label_bind
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public AjaxResult insertMaLabelBind(MaLabelBindVO maLabelBind) {
        int i = 0;
        int b = 0;
        MaLabelBindVO bean = maLabelBindMapper.selectMaMachineMaId(maLabelBind.getMaCode());
        if (null != bean) {
            return AjaxResult.error("设备编码已绑定！！！");
        } else {
            MaMachine maMachine = new MaMachine();
            maMachine.setMaCode(maLabelBind.getMaCode());
            maMachine.setTypeId(maLabelBind.getTypeId());
            maMachine.setQrCode(maLabelBind.getLabelCode());
            maLabelBindMapper.insertMaLabel(maMachine);
            //根据typeId修改库存
            maLabelBindMapper.updateMatypeNum(maLabelBind.getTypeId());
            maLabelBind.setMaId(String.valueOf(maMachine.getMaId()));
            maLabelBind.setStatus("1");
            maLabelBind.setIsBind("1");
            b = maLabelBindMapper.updateMaLabelBindMaIds(maLabelBind);
            i = maLabelBindMapper.insertMaLabelBindS(maLabelBind);
        }
        return AjaxResult.success("绑定成功");
    }

    /**
     * 修改机具设备标签ma_label_bind
     *
     * @param maLabelBind 机具设备标签ma_label_bind
     * @return 结果
     */
    @Override
    public int updateMaLabelBind(MaLabelBindVO maLabelBind) {
        return maLabelBindMapper.updateMaLabelBind(maLabelBind);
    }

    /**
     * 批量删除机具设备标签ma_label_bind
     *
     * @param id 需要删除的机具设备标签ma_label_bind主键
     * @return 结果
     */
    @Override
    public int updateMaLabelBindByMaIds(Long id) {
        //Long ma=Long.parseLong(String.valueOf(maId));
        return maLabelBindMapper.updateMaLabelBindByMaIds(id);
    }

    /**
     * 删除机具设备标签ma_label_bind信息
     *
     * @param maId 机具设备标签ma_label_bind主键
     * @return 结果
     */
    @Override
    public int deleteMaLabelBindByMaId(Long maId) {
        return maLabelBindMapper.deleteMaLabelBindByMaId(maId);
    }
}
