package org.jeecg.modules.apply.service.impl;

import org.activiti.engine.runtime.ProcessInstance;
import org.apache.shiro.SecurityUtils;
import org.jeecg.common.Constant;
import org.jeecg.common.system.api.ISysBaseAPI;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.common.system.vo.SysDepartModel;
import org.jeecg.common.utils.DictEnum;
import org.jeecg.modules.apply.entity.ApplyLeave;
import org.jeecg.modules.apply.mapper.ApplyLeaveMapper;
import org.jeecg.modules.apply.service.IApplyLeaveService;
import org.jeecg.modules.applyForm.entity.ApplyForm;
import org.jeecg.modules.applyForm.service.IApplyFormService;
import org.jeecg.modules.approval.model.vo.ApprovalVO;
import org.jeecg.modules.approval.service.ApprovalService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.util.HashMap;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @Description: apply_leave
 * @Author: jeecg-boot
 * @Date:   2021-08-18
 * @Version: V1.0
 */
@Service
public class ApplyLeaveServiceImpl extends ServiceImpl<ApplyLeaveMapper, ApplyLeave> implements IApplyLeaveService {

   @Autowired
   private ApprovalService approvalService;
   @Autowired
   private ISysBaseAPI iSysBaseAPI;
   @Autowired
   private IApplyFormService applyFormService;

   @Override
   public boolean saveLeave(ApplyLeave applyLeave) {

      //据request中的token获取用户账号
      //String userName = JwtUtil.getUserNameByToken(request);
      //直接获取当前用户不适用前端token
      LoginUser loginUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
      //获取部门信息
      SysDepartModel sysDepartModel = (SysDepartModel) iSysBaseAPI.getAllSysDepart().stream()
              .filter(s -> s.getId().equals(iSysBaseAPI.getDepartIdsByOrgCode(loginUser.getOrgCode()))).collect(Collectors.toList());

     // applyLeave.setId(IdWorker.getIdStr());
      applyLeave.setUserId(loginUser.getId());
      applyLeave.setUserName(loginUser.getRealname());
      applyLeave.setDeptId(sysDepartModel.getId());
      applyLeave.setDeptName(sysDepartModel.getDepartName());
      //DictEnum.FORMKEY_APPLY.getKey() 获取表单key
      applyLeave.setFormKey(DictEnum.FORMKEY_APPLY.getKey());
      //获取流程 获取已启用的流程
      ApplyForm formByFormKey = applyFormService.getAppleyFormByFormKey(DictEnum.FORMKEY_APPLY.getKey());
      applyLeave.setProcessKey(formByFormKey.getProcessKey());
      applyLeave.setApplyStatus(DictEnum.APPLY_STATUS_PROCESSING.getKey());
      //添加流转条件
      Map<String, Object> map = new HashMap<>();
      map.put(Constant.ACT_FLOW_TOTAL, applyLeave.getTotal());
      applyLeave.setParam(map);
      // 发起审批流程
      ProcessInstance processInstance = approvalService.addApproval(new ApprovalVO<ApplyLeave>(applyLeave));
      applyLeave.setProcessInstanceId(processInstance.getId());
      applyLeave.setProcessInstanceName(processInstance.getName());
      boolean result = save(applyLeave);

      return result;
   }
}
