package com.zd.bottle.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zd.bottle.domain.BottleStorage;
import com.zd.bottle.domain.RfidTag;
import com.zd.bottle.mapper.BottleStorageMapper;
import com.zd.bottle.service.BottleStorageService;
import com.zd.bottle.service.RfidTagService;
import com.zd.bottle.vo.BottleStorageUseVo;
import com.zd.bottle.vo.BottleStorageVo;
import com.zd.common.core.exception.ServiceException;
import com.zd.common.security.service.TokenService;
import com.zd.system.api.domain.SysUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

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

/**
 * <p>
 * 气瓶入库表 服务实现类
 * </p>
 *
 * @author hanson
 * @since 2022-09-08
 */
@Service
public class BottleStorageServiceImpl extends ServiceImpl<BottleStorageMapper, BottleStorage> implements BottleStorageService {

    @Autowired
    private BottleStorageMapper bottleStorageMapper;

    @Autowired
    private TokenService tokenService;

    @Autowired
    private RfidTagService rfidTagService;

    /**
     * 新增入库信息
     *
     * @param bottleStorage 实体
     * @return boolean
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean add(BottleStorage bottleStorage) {

        BottleStorageVo bsVo = new BottleStorageVo();
        bsVo.setElectronicTag(bottleStorage.getElectronicTag());
        List<BottleStorageVo> list =  bottleStorageMapper.getListVo(bsVo);
        if(!list.isEmpty()){
            throw  new ServiceException("该识别码已存在！");
        }

        //查询标签码
        RfidTag rfidTag = rfidTagService.getRfidByTagCode(bottleStorage.getElectronicTag());
        if(rfidTag==null){
            throw  new ServiceException("未查询到相关识别码！");
        }

        SysUser sysUser = tokenService.getLoginUser().getSysUser();
        //刷新识别码状态
        RfidTag rfid = new RfidTag();
        rfid.setIsBind((short) 1);
        rfid.setId(rfidTag.getId());
        rfid.setCommonValue(sysUser.getUserName(),sysUser.getUserId(),sysUser.getDeptId());
        rfidTagService.updateQpRfidTag(rfid);

        bottleStorage.setUserId(sysUser.getUserId());
        bottleStorage.setUserName(sysUser.getNickName());
        bottleStorage.setUserPhone(sysUser.getPhonenumber());
        bottleStorage.setDeptId(sysUser.getDept().getDeptId());
        bottleStorage.setDeptName(sysUser.getDept().getDeptName());
        bottleStorage.setCreateBy(sysUser.getNickName());
        bottleStorage.setCreateTime(new Date());
        bottleStorage.setCurrentPressure(bottleStorage.getGasPressure());
        bottleStorage.setAirNumber(String.valueOf(System.currentTimeMillis() ) );

        return save(bottleStorage);
    }

    @Override
    // @DataScope(deptAlias = "bs", userAlias = "bs", permi = PerPrefix.BOTTLE_BOTTLESTORAGE)
    public List<BottleStorageVo> getListVo(BottleStorageVo vo) {
        return bottleStorageMapper.getListVo(vo);
    }


    @Override
    public List <BottleStorage> receivingGasList(BottleStorage bottleStorage) {
        LambdaQueryWrapper <BottleStorage> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(BottleStorage::getStorageStatus,1);
        wrapper.orderByDesc(BottleStorage::getCreateTime);
        wrapper.setEntity(bottleStorage);
        return list(wrapper);
    }

    @Override
    public List <BottleStorageUseVo> returnGas(BottleStorageUseVo bottleStorageUseVo) {
        return bottleStorageMapper.returnGas(bottleStorageUseVo);
    }
}
