package com.xtd.project.assets.service.impl;

import com.xtd.common.utils.DateUtils;
import com.xtd.common.utils.SecurityUtils;
import com.xtd.common.utils.StringUtils;
import com.xtd.framework.aspectj.lang.annotation.DataScope;
import com.xtd.project.assets.domain.DbAssetsStockIn;
import com.xtd.project.assets.domain.DbAssetsStockInForm;
import com.xtd.project.assets.mapper.DbAssetsStockInMapper;
import com.xtd.project.assets.service.IDbAssetsStockInService;
import com.xtd.project.assets.service.IDbTableHisService;
import com.xtd.project.system.domain.SysUser;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.TaskService;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.Task;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 资产入库Service业务层处理
 *
 * @author xtd
 * @date 2020-04-09
 */
@Service
public class DbAssetsStockInServiceImpl implements IDbAssetsStockInService {
    @Autowired
    private DbAssetsStockInMapper dbAssetsStockInMapper;
    @Autowired
    private RuntimeService runtimeService;
    @Autowired
    private TaskService taskService;
    @Autowired
    private IDbTableHisService iDbTableHisService;

    /**
     * 查询资产入库
     *
     * @param aId 资产入库ID
     * @return 资产入库
     */
    @Override
    public DbAssetsStockIn selectDbAssetsStockInById(Long aId) {
        return dbAssetsStockInMapper.selectDbAssetsStockInById(aId);
    }

    @Override
    public DbAssetsStockInForm form(Long aId) {
        DbAssetsStockInForm dbAssetsStockIn = dbAssetsStockInMapper.form(aId);
        return dbAssetsStockIn;
    }

    /**
     * 查询资产入库列表
     *
     * @param dbAssetsStockIn 资产入库
     * @return 资产入库
     */
    @Override
    @DataScope(deptAlias = "d", userAlias = "u")
    public List<DbAssetsStockIn> selectDbAssetsStockInList(DbAssetsStockIn dbAssetsStockIn) {
        Long userId = SecurityUtils.getLoginUser().getUser().getUserId();
        String appUserId = userId.toString();
        List<DbAssetsStockIn> dbAssetsStockIns = dbAssetsStockInMapper.selectDbAssetsStockInList(dbAssetsStockIn);
        for (DbAssetsStockIn assetsStockIn : dbAssetsStockIns) {
            String instanceId = assetsStockIn.getInstanceId();
            if (!StringUtils.isNull(instanceId)) {
                Task task = taskService.createTaskQuery().taskAssignee(appUserId).processInstanceId(instanceId).singleResult();
                if (!StringUtils.isNull(task)) {
                    assetsStockIn.setTaskId(task.getId());
                }
            }

        }
        return dbAssetsStockIns;
    }

    /**
     * 新增资产入库
     *
     * @param dbAssetsStockIn 资产入库
     * @return 结果
     */
    @Override
    public int insertDbAssetsStockIn(DbAssetsStockIn dbAssetsStockIn) {
        String userId = SecurityUtils.getLoginUser().getUser().getUserId().toString();
        dbAssetsStockIn.setCreateBy(userId);
        dbAssetsStockIn.setCreateTime(DateUtils.getNowDate());

        return dbAssetsStockInMapper.insertDbAssetsStockIn(dbAssetsStockIn);
    }

    /**
     * 修改资产入库
     *
     * @param dbAssetsStockIn 资产入库
     * @return 结果
     */
    @Override
    public int updateDbAssetsStockIn(DbAssetsStockIn dbAssetsStockIn) {
        dbAssetsStockIn.setUpdateTime(DateUtils.getNowDate());
        return dbAssetsStockInMapper.updateDbAssetsStockIn(dbAssetsStockIn);
    }

    /**
     * 批量删除资产入库
     *
     * @param aIds 需要删除的资产入库ID
     * @return 结果
     */
    @Override
    public int deleteDbAssetsStockInByIds(Long[] aIds) {
        return dbAssetsStockInMapper.deleteDbAssetsStockInByIds(aIds);
    }

    /**
     * 删除资产入库信息
     *
     * @param aId 资产入库ID
     * @return 结果
     */
    @Override
    public int deleteDbAssetsStockInById(Long aId) {
        return dbAssetsStockInMapper.deleteDbAssetsStockInById(aId);
    }


    @Override
    @Transactional
    public void commit(Long aId, String processId) {
        SysUser user = SecurityUtils.getLoginUser().getUser();
        Long userId = user.getUserId();
        // 启动流程时设置业务 key
        Map<String, Object> map = new HashMap<>();
        String appUserId = userId.toString();
        map.put("appUserId", appUserId);
        DbAssetsStockIn dbAssetsStockIn = dbAssetsStockInMapper.selectDbAssetsStockInById(aId);
        ProcessInstance processInstance = runtimeService
                .startProcessInstanceById(processId, map);
        String processInstanceId = processInstance.getId();
        Task task = taskService.createTaskQuery().taskAssignee(appUserId).processInstanceId(processInstanceId).singleResult();
        String taskId = task.getId();
        map.put("formData", dbAssetsStockIn);
        map.put("creatUserName", user.getUserName());
        map.put("creatDeptName", user.getDept().getDeptName());
        map.put("creatTime", DateUtils.getNowDate());
        taskService.setVariables(taskId,map);
        //设置签收
        taskService.claim(taskId, userId.toString());
        //完成task
        taskService.complete(taskId, map);
        dbAssetsStockIn.setInstanceId(processInstanceId);
        dbAssetsStockIn.setState(0);
        dbAssetsStockInMapper.updateDbAssetsStockIn(dbAssetsStockIn);
        //插入历史
        iDbTableHisService.insertDbTableHis(processInstanceId, aId, "db_assets_stock_in");
    }

    @Override
    @Transactional
    public void recommit(DbAssetsStockIn dbAssetsStockIn) {
        SysUser user = SecurityUtils.getLoginUser().getUser();
        Long userId = user.getUserId();
        String taskId = dbAssetsStockIn.getTaskId();
        // 启动流程时设置业务 key
        Map<String, Object> map = new HashMap<>();

        map.put("formData", dbAssetsStockIn);
        map.put("creatUserName", user.getUserName());
        map.put("creatDeptName", user.getDept().getDeptName());
        map.put("creatTime", DateUtils.getNowDate());
        taskService.setVariables(taskId,map);
        //设置签收
        taskService.claim(taskId, userId.toString());
        //完成task
        taskService.complete(taskId, map);
        dbAssetsStockIn.setState(0);
        dbAssetsStockInMapper.updateDbAssetsStockIn(dbAssetsStockIn);
    }
}
