package com.zd.bottle.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zd.bottle.domain.BottleStorage;
import com.zd.bottle.domain.BottleStorageOut;
import com.zd.bottle.domain.RfidTag;
import com.zd.bottle.domain.UseRecord;
import com.zd.bottle.mapper.BottleStorageOutMapper;
import com.zd.bottle.service.BottleStorageOutService;
import com.zd.bottle.service.BottleStorageService;
import com.zd.bottle.service.RfidTagService;
import com.zd.bottle.service.UseRecordService;
import com.zd.bottle.vo.BottleStorageOutVo;
import com.zd.common.core.exception.ServiceException;
import com.zd.common.core.utils.StringUtils;
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 BottleStorageOutServiceImpl extends ServiceImpl<BottleStorageOutMapper, BottleStorageOut> implements BottleStorageOutService {

    @Autowired
    private BottleStorageService bottleStorageService;

    @Autowired
    private TokenService tokenService;

    @Autowired
    private BottleStorageOutMapper outMapper;

    @Autowired
    private UseRecordService useRecordService;

    @Autowired
    private RfidTagService rfidTagService;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean add(BottleStorageOut bottleStorageOut) {
        if(StringUtils.isNull(bottleStorageOut.getStorageId())){
            throw new ServiceException("入库ID不能为空。");
        }
        SysUser sysUser = tokenService.getLoginUser().getSysUser();
        //查询入库信息
        BottleStorage bottleStorage =  bottleStorageService.getById(bottleStorageOut.getStorageId());
        if(bottleStorage.getStorageStatus()==3){
            throw new ServiceException("该条记录已出库。");
        }

        //查询是否存在未归还记录
        List<UseRecord> useRecordVoList = useRecordService.getListNotReturn(bottleStorageOut.getStorageId());
        if(!useRecordVoList.isEmpty()){
            throw new ServiceException("请先归还后再出库。");
        }

        //查询标签码
        RfidTag rfidTag = rfidTagService.getRfidByTagCode(bottleStorage.getElectronicTag());
        if(rfidTag!=null){
            //修改标签码状态
            RfidTag rfid = new RfidTag();
            rfid.setIsBind((short) 0);
            rfid.setId(rfidTag.getId());
            rfid.setCommonValue(sysUser.getUserName(),sysUser.getUserId(),sysUser.getDeptId());
            rfidTagService.updateQpRfidTag(rfid);
        }


        //出库状态
        bottleStorage.setStorageStatus(3);
        //标签码
        bottleStorage.setElectronicTag("--");
        bottleStorage.setCommonValue(sysUser.getUserName(),sysUser.getUserId(),sysUser.getDeptId());
        //刷新入库状态
        bottleStorageService.updateById(bottleStorage);

        bottleStorageOut.setDeptId(sysUser.getDept().getDeptId());
        bottleStorageOut.setDeptName(sysUser.getDept().getDeptName());
        bottleStorageOut.setUserId(sysUser.getUserId());
        bottleStorageOut.setUserName(sysUser.getUserName());
        bottleStorageOut.setPhone(sysUser.getPhonenumber());
        bottleStorageOut.setCreateTime(new Date());
        return save(bottleStorageOut);
    }

    @Override
    public List<BottleStorageOutVo> getOutListVo(BottleStorageOutVo vo) {
        return outMapper.getOutListVo(vo);
    }
}
