package com.jeelowcode.service.bpm.service.impl;

import com.jeelowcode.core.framework.entity.FormEntity;
import com.jeelowcode.core.framework.enums.ApproveStatusEnum;
import com.jeelowcode.core.framework.mapper.FormMapper;
import com.jeelowcode.core.framework.service.IDbformDataService;
import com.jeelowcode.framework.utils.tool.spring.SpringUtils;
import com.jeelowcode.service.bpm.controller.vo.instance.BpmProcessInstanceRespVO;
import com.jeelowcode.service.bpm.service.IBpmProcessInstanceService;
import com.jeelowcode.tool.framework.datapermission.core.annotation.DataPermission;
import lombok.extern.slf4j.Slf4j;
import org.flowable.engine.impl.persistence.entity.ExecutionEntityImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

@Service
@Slf4j
@DataPermission(enable=false)
public class BillApproveStatusService {

    @Resource
    private IDbformDataService dbformDataService;


    public void afterSubmit(String processInstanceId){

        Map<String,Object> map = new HashMap<>();
        //流程实例id
        BpmProcessInstanceRespVO vo = SpringUtils.getBean(IBpmProcessInstanceService.class).getProcessInstanceVO(processInstanceId);
        //获取到的自定义表单数据id
        String id = vo.getFormVariables().get("id").toString();
        //数据表id
        Long formId = vo.getProcessDefinition().getFormId();
        FormEntity formEntity = SpringUtils.getBean(FormMapper.class).getByWebId(formId);

        Map<String,Object> json = new HashMap<>();
        json.put(ApproveStatusEnum.codeField, ApproveStatusEnum.APPROVING.getCode());
        json.put(ApproveStatusEnum.nameField, ApproveStatusEnum.APPROVING.getDesc());
        json.put("ProcessInstanceId", processInstanceId);
        dbformDataService.update(formEntity.getTableName(),id,json);

    }

    public void afterApproved(String processInstanceId){

        Map<String,Object> map = new HashMap<>();
        //流程实例id
        BpmProcessInstanceRespVO vo = SpringUtils.getBean(IBpmProcessInstanceService.class).getProcessInstanceVO(processInstanceId);
        //获取到的自定义表单数据id
        String id = vo.getFormVariables().get("id").toString();
        //数据表id
        Long formId = vo.getProcessDefinition().getFormId();
        FormEntity formEntity = SpringUtils.getBean(FormMapper.class).getByWebId(formId);

        Map<String,Object> json = new HashMap<>();
        json.put(ApproveStatusEnum.codeField, ApproveStatusEnum.APPROVED.getCode());
        json.put(ApproveStatusEnum.nameField, ApproveStatusEnum.APPROVED.getDesc());
        dbformDataService.update(formEntity.getTableName(),id,json);

    }

    public void afterRejected(String processInstanceId){

        Map<String,Object> map = new HashMap<>();
        //流程实例id
        BpmProcessInstanceRespVO vo = SpringUtils.getBean(IBpmProcessInstanceService.class).getProcessInstanceVO(processInstanceId);
        //获取到的自定义表单数据id
        String id = vo.getFormVariables().get("id").toString();
        //数据表id
        Long formId = vo.getProcessDefinition().getFormId();
        FormEntity formEntity = SpringUtils.getBean(FormMapper.class).getByWebId(formId);

        Map<String,Object> json = new HashMap<>();
        json.put(ApproveStatusEnum.codeField, ApproveStatusEnum.REJECTED.getCode());
        json.put(ApproveStatusEnum.nameField, ApproveStatusEnum.REJECTED.getDesc());
        dbformDataService.update(formEntity.getTableName(),id,json);

    }


    public void afterCancel(String processInstanceId){

        Map<String,Object> map = new HashMap<>();
        //流程实例id
        BpmProcessInstanceRespVO vo = SpringUtils.getBean(IBpmProcessInstanceService.class).getProcessInstanceVO(processInstanceId);
        //获取到的自定义表单数据id
        String id = vo.getFormVariables().get("id").toString();
        //数据表id
        Long formId = vo.getProcessDefinition().getFormId();
        FormEntity formEntity = SpringUtils.getBean(FormMapper.class).getByWebId(formId);

        Map<String,Object> json = new HashMap<>();
        json.put(ApproveStatusEnum.codeField, ApproveStatusEnum.UNAPPROVED.getCode());
        json.put(ApproveStatusEnum.nameField, ApproveStatusEnum.UNAPPROVED.getDesc());
        dbformDataService.update(formEntity.getTableName(),id,json);

    }
}
