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

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.activiti.engine.delegate.DelegateExecution;
import org.jeecg.common.Constant;
import org.jeecg.common.base.entity.BaseTaskEntity;
import org.jeecg.common.exception.CustomException;
import org.jeecg.common.util.DateUtils;
import org.jeecg.common.utils.DictEnum;
import org.jeecg.modules.apply.mapper.ApplyProcessMapper;
import org.jeecg.modules.apply.service.ApplyProcessService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Map;

/**
 * @Author: yll
 * @Date: 2019/7/24 11:06
 */
@Service
@Slf4j
public class ApplyProcessServiceImpl extends ServiceImpl<ApplyProcessMapper, BaseTaskEntity> implements ApplyProcessService {

    @Autowired
    private ApplyProcessMapper applyProcessMapper;


    @Override
    public void updateProcessResult(DelegateExecution delegateExecution) {
        try {
            Object variable = delegateExecution.getVariable(Constant.ACT_BASE_TASK);
            BaseTaskEntity taskEntity = JSONObject.parseObject(variable.toString(), BaseTaskEntity.class);

            String opinionStr = delegateExecution.getVariable(Constant.ACT_APPLY_OPINION_LIST).toString();
            String tableName = taskEntity.getFormKey();

            //审批最终结果
            Object flagObj = delegateExecution.getVariable(Constant.ACT_TASK_FLAG);

            String flag = "";
            String result = DictEnum.APPLY_RESULT_AGREE.getKey();
            if (null == flagObj) {
                result = DictEnum.APPLY_RESULT_CANCEL.getKey();
            } else {
                flag = flagObj.toString();
            }

            if (DictEnum.Apply_APPROVAL_OPINION_REFUSE.getKey().equals(flag)) {
                //拒绝
                result = DictEnum.APPLY_RESULT_REFUSE.getKey();
            }
            if (DictEnum.Apply_APPROVAL_OPINION_AGREE.getKey().equals(flag)) {
                //同意
                result = DictEnum.APPLY_RESULT_AGREE.getKey();
            }
//            if (DictEnum.APPLY_APPROVAL_OPINION_ABANDON.getKey().equals(flag)) {
//                //取消申请
//                result = DictEnum.APPLY_RESULT_CANCEL.getKey();
//            }
            if ( DictEnum.APPLY_APPROVAL_OPINION_CONFIRM.getKey().equals(flag)) {
                //确认即为审批通过
                result = DictEnum.APPLY_RESULT_AGREE.getKey();
            }

            applyProcessMapper.updateProcessResult(tableName, taskEntity.getId(), DictEnum.APPLY_STATUS_FINISH.getKey(), result, DateUtils.getDate(), opinionStr);
        } catch (Exception e) {
            e.printStackTrace();
            throw new CustomException("保存审批结果失败!", e);
        }
    }

    @Override
    public void updateContactProcessResult(DelegateExecution delegateExecution) {
        try {
            Object variable = delegateExecution.getVariable(Constant.ACT_BASE_TASK);
            BaseTaskEntity taskEntity = JSONObject.parseObject(variable.toString(), BaseTaskEntity.class);

            String opinionStr = delegateExecution.getVariable(Constant.ACT_APPLY_OPINION_LIST).toString();
            String tableName = taskEntity.getFormKey();

            //审批最终结果
            Object flagObj = delegateExecution.getVariable(Constant.ACT_TASK_FLAG);

            String flag = "";
            String result = DictEnum.APPLY_RESULT_AGREE.getKey();
            if (null == flagObj) {
                result = DictEnum.APPLY_RESULT_CANCEL.getKey();
            } else {
                flag = flagObj.toString();
            }

            if (DictEnum.Apply_APPROVAL_OPINION_REFUSE.getKey().equals(flag)) {
                //拒绝
                result = DictEnum.APPLY_RESULT_REFUSE.getKey();
            }
            if (DictEnum.Apply_APPROVAL_OPINION_AGREE.getKey().equals(flag)) {
                //同意
                result = DictEnum.APPLY_RESULT_AGREE.getKey();
            }

            if ( DictEnum.APPLY_APPROVAL_OPINION_CONFIRM.getKey().equals(flag)) {
                //确认即为审批通过
                result = DictEnum.APPLY_RESULT_AGREE.getKey();
            }

            applyProcessMapper.updateContactProcessResult(tableName, taskEntity.getId(),2,DictEnum.APPLY_STATUS_FINISH.getKey(), result, DateUtils.getDate(), opinionStr);
        } catch (Exception e) {
            e.printStackTrace();
            throw new CustomException("保存审批结果失败!", e);
        }
    }

    @Override
    public void updateProblemProcessResult(DelegateExecution delegateExecution) {
        try {
            Object variable = delegateExecution.getVariable(Constant.ACT_BASE_TASK);
            BaseTaskEntity taskEntity = JSONObject.parseObject(variable.toString(), BaseTaskEntity.class);

            String opinionStr = delegateExecution.getVariable(Constant.ACT_APPLY_OPINION_LIST).toString();
            String tableName = taskEntity.getFormKey();

            //审批最终结果
            Object flagObj = delegateExecution.getVariable(Constant.ACT_TASK_FLAG);

            String flag = "";
            String result = DictEnum.APPLY_RESULT_AGREE.getKey();
            if (null == flagObj) {
                result = DictEnum.APPLY_RESULT_CANCEL.getKey();
            } else {
                flag = flagObj.toString();
            }
            if (DictEnum.Apply_APPROVAL_OPINION_REFUSE.getKey().equals(flag)) {
                //拒绝
                result = DictEnum.APPLY_RESULT_REFUSE.getKey();
            }
            if (DictEnum.Apply_APPROVAL_OPINION_AGREE.getKey().equals(flag)) {
                //同意
                result = DictEnum.APPLY_RESULT_AGREE.getKey();
            }
            if ( DictEnum.APPLY_APPROVAL_OPINION_CONFIRM.getKey().equals(flag)) {
                //确认即为审批通过
                result = DictEnum.APPLY_RESULT_AGREE.getKey();
            }
            applyProcessMapper.updateContactProcessResult(tableName, taskEntity.getId(),3,DictEnum.APPLY_STATUS_FINISH.getKey(), result, DateUtils.getDate(), opinionStr);
            //判断明细是否都审核
            Map<String, Object> param = taskEntity.getParam();
            applyProcessMapper.updateContactState(3,param.get("parentId").toString());
        } catch (Exception e) {
            e.printStackTrace();
            throw new CustomException("保存审批结果失败!", e);
        }
    }

}