
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.DbAssetsStockAllot;
import com.xtd.project.assets.domain.DbAssetsStockOut;
import com.xtd.project.assets.mapper.DbAssetsStockAllotMapper;
import com.xtd.project.assets.mapper.DbAssetsStockMapper;
import com.xtd.project.assets.mapper.DbAssetsStockOutMapper;
import com.xtd.project.system.domain.SysUser;
import com.xtd.project.system.mapper.SysUserMapper;
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 javax.validation.constraints.NotBlank;
import javax.validation.constraints.Size;
import java.util.List;

@Component
public class AllotEndListener 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();
        DbAssetsStockAllot formData = (DbAssetsStockAllot)delegateExecution.getVariable("formData");
        //获取领用人id
        Long leadUserId = formData.getLeadUserId();
        SysUserMapper sysUserMapper = SpringUtil.getBean(SysUserMapper.class);
        SysUser sysUser = sysUserMapper.selectUserById(leadUserId);
        String userName = sysUser.getUserName();
        List<Long> stockValue = formData.getStockValue();
        for (Long aLong : stockValue) {
            //获取到库存id
            Long dbAsStId = aLong;
            DbAssetsStock dbAssetsStock = new DbAssetsStock();
            dbAssetsStock.setaId(dbAsStId);
            //设置领用人
            dbAssetsStock.setLeadUserId(leadUserId);
            dbAssetsStock.setLeadUser(userName);
            //设置为领用
            dbAssetsStock.setStorageStatus(1L);
            dbAssetsStockMapper.updateDbAssetsStock(dbAssetsStock);
            logger.info("更新库存信息");
        }
        DbAssetsStockAllotMapper dbAssetsStockAllotMapper = SpringUtil.getBean(DbAssetsStockAllotMapper.class);
        //设置为审核通过
        formData.setState(2);
        dbAssetsStockAllotMapper.updateDbAssetsStockAllot(formData);
        logger.info("更新出库状态");
    }

}

