package com.zd.airbottle.service.impl;

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

import com.alibaba.fastjson.JSONObject;
import com.zd.airbottle.domain.*;
import com.zd.airbottle.domain.vo.QpBottleStorageOutVO;
import com.zd.airbottle.domain.vo.QpBottleStorageVO;
import com.zd.airbottle.mapper.*;
import com.zd.common.core.domain.per.PerPrefix;
import com.zd.common.core.exception.ServiceException;
import com.zd.common.datascope.annotation.DataScope;
import com.zd.common.security.service.TokenService;

import com.zd.system.api.domain.SysUser;
import com.zd.system.api.message.RemoteMessageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.zd.airbottle.service.IQpBottleStorageOutService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 气瓶出库Service业务层处理
 *
 * @author zd
 * @date 2022-05-23
 */
@Service
public class QpBottleStorageOutServiceImpl implements IQpBottleStorageOutService
{
    @Autowired
    private QpBottleStorageOutMapper qpBottleStorageOutMapper;

    @Autowired
    private TokenService tokenService;

    @Autowired
    private QpBottleStorageMapper bottleStorageMapper;

    @Autowired
    private QpUseRecordMapper qpUseRecordMapper;

    @Autowired
    private RemoteMessageService remoteMessageService;

    @Autowired
    private QpTaskDetailMapper taskDetailMapper;

    @Autowired
    private QpSupplierMapper supplierMapper;

    @Autowired
    private QpRfidTagMapper rfidTagMapper;
    /**
     * 查询气瓶出库
     *
     * @param id 气瓶出库主键
     * @return 气瓶出库
     */
    @Override
    public QpBottleStorageOut selectQpBottleStorageOutById(Long id)
    {
        return qpBottleStorageOutMapper.selectQpBottleStorageOutById(id);
    }

    /**
     * 查询气瓶出库列表
     *
     * @param qpBottleStorageOut 气瓶出库
     * @return 气瓶出库
     */
    @Override
    public List<QpBottleStorageOutVO> selectQpBottleStorageOutList(QpBottleStorageOutVO qpBottleStorageOut)
    {
        return qpBottleStorageOutMapper.selectQpBottleStorageOutList(qpBottleStorageOut);
    }

    /**
     * 信息记录-出库记录
     *
     * @param qpBottleStorageOut 出库记录
     * @return 气瓶出库集合
     */
    @Override
    @DataScope(deptAlias = "d", userAlias = "u", permi = PerPrefix.AIRBOTTLE_INFORMATIONRECORD)
    public List<QpBottleStorageOutVO> selectQpBottleStorageOutRecordList(QpBottleStorageOutVO qpBottleStorageOut) {
        return qpBottleStorageOutMapper.selectQpBottleStorageOutList(qpBottleStorageOut);
    }

    /**
     * 新增气瓶出库
     *
     * @param qpBottleStorageOut 气瓶出库
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insertQpBottleStorageOut(QpBottleStorageOut qpBottleStorageOut)
    {
        //设置其他公共字段
        //SaveUtil.setCommonAttr(qpBottleStorageOut);

        SysUser sysUser = tokenService.getLoginUser().getSysUser();

        //查询最后一次使用记录
        QpUseRecord useRecord = qpUseRecordMapper.selectQpUseRecordByIdstorageId(qpBottleStorageOut.getStorageId());
        if(useRecord!=null && useRecord.getBackTime()==null){
            throw new ServiceException("该气瓶还未归还，暂不能出库！");
        }
        if(useRecord!=null && useRecord.getAfterUse()!=null){
            qpBottleStorageOut.setPressure(Double.parseDouble(useRecord.getAfterUse()+"") );
        }else{
            qpBottleStorageOut.setPressure(bottleStorageMapper.selectQpBottleStorageById(qpBottleStorageOut.getStorageId()).getCurrentPressure());
        }



        QpBottleStorageVO bottleStorageVo = bottleStorageMapper.selectQpBottleStorageById(qpBottleStorageOut.getStorageId());
        QpTaskDetail taskDetail = taskDetailMapper.selectQpTaskDetailById(bottleStorageVo.getTaskDetailId());
        QpSupplier supplier = supplierMapper.selectQpSupplierById(taskDetail.getSupplierId());

        //出库后解除标签绑定
        QpRfidTag rfidTag = rfidTagMapper.selectByTag(bottleStorageVo.getElectronicTag());
        if(rfidTag!=null){
            rfidTag.setIsBind(0);//解除绑定
            rfidTagMapper.updateQpRfidTag(rfidTag);
        }

        //出库申请
        qpBottleStorageOut.setApplyUserName(sysUser.getNickName());
        qpBottleStorageOut.setApplyUserid(sysUser.getUserId());
        qpBottleStorageOut.setCreateTime(new Date());
        qpBottleStorageOut.setPhone(sysUser.getPhonenumber());

        //更新入库表气瓶状态
        QpBottleStorage bottleStorage = new QpBottleStorage();
        bottleStorage.setId(qpBottleStorageOut.getStorageId());
        bottleStorage.setStorageStatus(3);//出库
        bottleStorage.setElectronicTag("--");
        bottleStorageMapper.updateQpBottleStorage(bottleStorage);

        int flg = qpBottleStorageOutMapper.insertQpBottleStorageOut(qpBottleStorageOut);
        if(flg>0){
            //获取气瓶出库相关规格信息
            JSONObject jsonStr = JSONObject.parseObject(bottleStorageVo.getSpecificationName());
            if(jsonStr!=null){
                String outType = jsonStr.get("airName")+":"+ jsonStr.get("gasLevel");
                // outType = "出库类型", outTime = "出库时间"  outType 出库类型：氧气-工业级(10L)
                //出库消息发送
                remoteMessageService.storageOut(supplier.getAccountId(),outType,new Date());
            }
        }
        return flg;
    }

    /**
     * 修改气瓶出库
     *
     * @param qpBottleStorageOut 气瓶出库
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updateQpBottleStorageOut(QpBottleStorageOut qpBottleStorageOut)
    {
        SysUser sysUser = tokenService.getLoginUser().getSysUser();
         if("1".equals(qpBottleStorageOut.getRemark())){
            //出库处理确认
             qpBottleStorageOut.setDisposeUserid(sysUser.getUserId());
             qpBottleStorageOut.setDisposeUsername(sysUser.getNickName());
             qpBottleStorageOut.setDisposeTime(new Date());
        }else if("2".equals(qpBottleStorageOut.getRemark())){
            //出库实际确认
             qpBottleStorageOut.setCompleteTime(new Date());
             qpBottleStorageOut.setCompleteUserid(sysUser.getUserId());
             qpBottleStorageOut.setCompleteUsername(sysUser.getNickName());
        }
        return qpBottleStorageOutMapper.updateQpBottleStorageOut(qpBottleStorageOut);
    }

    /**
     * 批量删除气瓶出库
     *
     * @param ids 需要删除的气瓶出库主键
     * @return 结果
     */
    @Override
    public int deleteQpBottleStorageOutByIds(Long[] ids)
    {
        return qpBottleStorageOutMapper.deleteQpBottleStorageOutByIds(ids);
    }

    /**
     * 删除气瓶出库信息
     *
     * @param id 气瓶出库主键
     * @return 结果
     */
    @Override
    public int deleteQpBottleStorageOutById(Long id)
    {
        return qpBottleStorageOutMapper.deleteQpBottleStorageOutById(id);
    }

    /**
     * 出库单 实验室关联查询
     */
    @Override
    public List<QpBottleStorageOutVO> selectQpBottleStorageOutRelationSubject(QpBottleStorageOutVO qpBottleStorageOutVo) {
        return qpBottleStorageOutMapper.selectQpBottleStorageOutRelationSubject(qpBottleStorageOutVo);
    }
}
