
package com.xtd.project.activiti.event.listener;

import com.xtd.common.utils.SpringUtil;
import com.xtd.project.activiti.service.impl.LeaveNewServiceImpl;
import com.xtd.project.assets.domain.DbAssetsStock;
import com.xtd.project.assets.domain.DbAssetsStockOut;
import com.xtd.project.assets.mapper.DbAssetsStockMapper;
import com.xtd.project.assets.mapper.DbAssetsStockOutMapper;
import org.activiti.engine.TaskService;
import org.activiti.engine.delegate.DelegateExecution;
import org.activiti.engine.delegate.ExecutionListener;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;

@Component
public class OutEndListener implements ExecutionListener {
    protected final Logger logger = LoggerFactory.getLogger(LeaveNewServiceImpl.class);

    @Autowired
    private TaskService taskService;


    @Override
    public void notify(DelegateExecution delegateExecution) {
        DbAssetsStockMapper dbAssetsStockMapper = SpringUtil.getBean(DbAssetsStockMapper.class);
        String id = delegateExecution.getId();
        DbAssetsStockOut formData = (DbAssetsStockOut)delegateExecution.getVariable("formData");
        //获取到库存id
        Long dbAsStId = formData.getDbAsStId();
        //获取领用人id
        Long leadUserId = formData.getLeadUserId();
        String leadUser = formData.getLeadUser();
        DbAssetsStock dbAssetsStock = new DbAssetsStock();

        dbAssetsStock.setaId(dbAsStId);
        //设置领用人
        dbAssetsStock.setLeadUserId(leadUserId);

        dbAssetsStock.setLeadUser(leadUser);
        //设置为领用
        dbAssetsStock.setStorageStatus(1L);
        //设置领用时间
        Date stockOutTime = formData.getStockOutTime();
        dbAssetsStock.setLeadTime(stockOutTime);
        dbAssetsStockMapper.updateDbAssetsStock(dbAssetsStock);
        logger.info("更新库存信息");

        DbAssetsStockOutMapper dbAssetsStockOutMapper = SpringUtil.getBean(DbAssetsStockOutMapper.class);
        //设置为审核通过
        formData.setState(2);
        dbAssetsStockOutMapper.updateDbAssetsStockOut(formData);
        logger.info("更新出库状态");
    }

}

