package com.siwei.rsmonitoring.service.impl;

import java.util.*;

import com.siwei.common.core.exception.ServiceException;
import com.siwei.common.core.utils.DateUtils;
import com.siwei.common.core.utils.StringUtils;
import com.siwei.common.core.utils.bean.BeanReflectionUtils;
import com.siwei.common.core.utils.uuid.IdUtils;
import com.siwei.common.security.utils.SecurityUtils;
import com.siwei.rsmonitoring.cache.AuditFlowCacheUtil;
import com.siwei.rsmonitoring.domain.TDkAuditflowStep;
import com.siwei.rsmonitoring.domain.TDkjbxxState;
import com.siwei.rsmonitoring.mapper.TDkjbxxStateMapper;
import com.siwei.system.api.model.LoginUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.siwei.rsmonitoring.mapper.TDkAuditflowRecordMapper;
import com.siwei.rsmonitoring.domain.TDkAuditflowRecord;
import com.siwei.rsmonitoring.service.ITDkAuditflowRecordService;

/**
 * 地块核查流程Service业务层处理
 *
 * @author siwei
 * @date 2025-01-21
 */
@Service
public class TDkAuditflowRecordServiceImpl implements ITDkAuditflowRecordService {
    @Autowired
    private TDkAuditflowRecordMapper tDkAuditflowRecordMapper;
    @Autowired
    private TDkjbxxStateMapper tDkjbxxStateMapper;

    /**
     * 查询地块核查流程
     *
     * @param id 地块核查流程主键
     * @return 地块核查流程
     */
    @Override
    public TDkAuditflowRecord selectTDkAuditflowRecordById(String id) {
        return tDkAuditflowRecordMapper.selectTDkAuditflowRecordById(id);
    }

    /**
     * 查询地块核查流程列表
     *
     * @param tDkAuditflowRecord 地块核查流程
     * @return 地块核查流程
     */
    @Override
    public List<TDkAuditflowRecord> selectTDkAuditflowRecordList(TDkAuditflowRecord tDkAuditflowRecord) {
        return tDkAuditflowRecordMapper.selectTDkAuditflowRecordList(tDkAuditflowRecord);
    }

    @Override
    public List<TDkAuditflowRecord> selectTDkAuditflowRecordListBusiness(TDkAuditflowRecord tDkAuditflowRecord) {
        return tDkAuditflowRecordMapper.selectTDkAuditflowRecordListBusiness(tDkAuditflowRecord);
    }

    @Override
    public List<TDkAuditflowRecord> selectTDkAuditflowRecordListStep(String dkjdxxId) {
        TDkAuditflowRecord tDkAuditflowRecord = new TDkAuditflowRecord();
        tDkAuditflowRecord.setDkjdxxId(dkjdxxId);
        tDkAuditflowRecord.setAuditflowState("1");
        List<TDkAuditflowRecord> ret = tDkAuditflowRecordMapper.selectTDkAuditflowRecordListBusiness(tDkAuditflowRecord);
        tDkAuditflowRecord.setAuditflowState("2");
        List<TDkAuditflowRecord> ret2 = tDkAuditflowRecordMapper.selectTDkAuditflowRecordListBusiness(tDkAuditflowRecord);
        ret.addAll(ret2);
        return ret;
    }


    @Override
    public int insertTDkAuditflowRecord(TDkAuditflowRecord tDkAuditflowRecord) {
        return tDkAuditflowRecordMapper.insertTDkAuditflowRecord(tDkAuditflowRecord);
    }

    @Override
    public Map<String, String> initDkAuditflowRecord(TDkAuditflowRecord tDkAuditflowRecord) {
        Map<String, String> retMap = new HashMap<>();
        //判断条件，是否满足相应的条件
        if (tDkAuditflowRecord == null) {
            return retMap;
        }
        tDkAuditflowRecord.setCreateBy(SecurityUtils.getUsername());
        if (StringUtils.isEmpty(tDkAuditflowRecord.getCreateBy())) {
            return retMap;
        }
        if (StringUtils.isEmpty(tDkAuditflowRecord.getDkjdxxId())) {
            return retMap;
        }
        if (StringUtils.isEmpty(tDkAuditflowRecord.getAuditflowType())) {
            return retMap;
        }

        AuditFlowCacheUtil auditFlowCacheUtil = AuditFlowCacheUtil.getInstance();
        List<TDkAuditflowStep> stepList = auditFlowCacheUtil.getCodeCacheData(tDkAuditflowRecord.getAuditflowType());
        if (stepList == null || stepList.isEmpty() || stepList.size() < 2) {
            return retMap;
        }

        //数据下发
        Long xyjAuditflowOrder = 0L;
        //验证当前的数据是否可进行操作
        TDkAuditflowStep stepNewObj = null;
        TDkAuditflowRecord newDkAuditFlowRecord = tDkAuditflowRecord;
        String v0_id = IdUtils.fastSimpleUUID();
        for (TDkAuditflowStep obj : stepList) {
            stepNewObj = obj;
            newDkAuditFlowRecord.setId(v0_id);
            newDkAuditFlowRecord.setAuditflowStep(stepNewObj.getStepCode());
            newDkAuditFlowRecord.setAuditflowOrder(xyjAuditflowOrder);
            newDkAuditFlowRecord.setAuditflowOperateType(obj.getOperationType());
            newDkAuditFlowRecord.setCreateTime(DateUtils.getNowDate());
            newDkAuditFlowRecord.setSuccessType("0");
            newDkAuditFlowRecord.setAuditflowState("0");
            newDkAuditFlowRecord.setAuditflowOperationState("0");
            newDkAuditFlowRecord.setPreviousId(v0_id);
            v0_id = IdUtils.fastSimpleUUID();
            newDkAuditFlowRecord.setNextId(v0_id);
            tDkAuditflowRecordMapper.insertTDkAuditflowRecord(newDkAuditFlowRecord);
            if ("WYFF".equals(stepNewObj.getNextSuccess())) {
                break;
            }
            xyjAuditflowOrder++;
        }

        retMap.put("current", newDkAuditFlowRecord.getId());
        xyjAuditflowOrder++;
        TDkAuditflowRecord nestDkAuditFlowRecord = tDkAuditflowRecord;
        nestDkAuditFlowRecord.setId(IdUtils.fastSimpleUUID());
        nestDkAuditFlowRecord.setAuditflowStep(stepNewObj.getNextSuccess());
        nestDkAuditFlowRecord.setAuditflowOrder(xyjAuditflowOrder);
        nestDkAuditFlowRecord.setCreateTime(DateUtils.getNowDate());
        nestDkAuditFlowRecord.setPreviousId(v0_id);
        v0_id = IdUtils.fastSimpleUUID();
        newDkAuditFlowRecord.setNextId(v0_id);
        nestDkAuditFlowRecord.setAuditflowOperationState("2");
        nestDkAuditFlowRecord.setAuditflowState("1");
        tDkAuditflowRecordMapper.insertTDkAuditflowRecord(nestDkAuditFlowRecord);
        retMap.put("next", nestDkAuditFlowRecord.getId());

        TDkjbxxState tDkjbxxState = new TDkjbxxState();
        tDkjbxxState.setId(newDkAuditFlowRecord.getDkjdxxId());
        tDkjbxxState.setAuditflow(newDkAuditFlowRecord.getAuditflowType());
        tDkjbxxState.setStepCode(newDkAuditFlowRecord.getAuditflowStep());
        tDkjbxxState.setOperationBy(SecurityUtils.getUsername());
        tDkjbxxState.setOperationTime(DateUtils.getNowDate());
        tDkjbxxState.setOperationId(SecurityUtils.getUserId().toString());
        tDkjbxxState.setSuccessType("0");
        tDkjbxxStateMapper.insertTDkjbxxState(tDkjbxxState);
        return retMap;
    }


    /**
     * 刷新地块核查流程
     *
     * @param tDkAuditflowRecord 地块核查流程
     * @return 结果
     */
    @Override
    public Map<String, String> refreshTDkAuditflowRecord(TDkAuditflowRecord tDkAuditflowRecord) {
        //判断条件，是否满足相应的条件
        if (tDkAuditflowRecord == null || StringUtils.isEmpty(tDkAuditflowRecord.getDkjdxxId()) ||
                StringUtils.isEmpty(tDkAuditflowRecord.getSuccessType()) ||
                StringUtils.isEmpty(tDkAuditflowRecord.getAuditflowType()) ||
                StringUtils.isEmpty(tDkAuditflowRecord.getAuditflowStep())
        ) {
            throw new ServiceException("流程执行异常");
        }
        LoginUser loginUser = SecurityUtils.getLoginUser();
        if (loginUser == null) {
            throw new ServiceException("流程执行异常");
        }

        //验证当前的数据是否可进行操作
        TDkAuditflowRecord dkAuditFlowRecordQuery = new TDkAuditflowRecord();
        dkAuditFlowRecordQuery.setDkjdxxId(tDkAuditflowRecord.getDkjdxxId());
        dkAuditFlowRecordQuery.setAuditflowState("1");
        dkAuditFlowRecordQuery.setAuditflowType(tDkAuditflowRecord.getAuditflowType());
        List<TDkAuditflowRecord> tDkAuditFlowRecords = tDkAuditflowRecordMapper.selectTDkAuditflowRecordList(dkAuditFlowRecordQuery);

        if (!tDkAuditFlowRecords.isEmpty()) {
            //序号，当前流程，未选中流程
            Long xyjAuditflowOrder = 0L;
            TDkAuditflowRecord dkAuditFlowRecord_Data = null;
            List<TDkAuditflowRecord> tDkAuditFlowRecordsFull = new ArrayList<>();

            for (TDkAuditflowRecord recordObj : tDkAuditFlowRecords) {
                xyjAuditflowOrder = recordObj.getAuditflowOrder();
                if (tDkAuditflowRecord.getAuditflowStep().equals(recordObj.getAuditflowStep())) {
                    dkAuditFlowRecord_Data = recordObj;
                } else {
                    tDkAuditFlowRecordsFull.add(recordObj);
                }
            }
            if (null != dkAuditFlowRecord_Data) {
                if (dkAuditFlowRecord_Data.getAuditflowStep().equals("-9999")) {
                    throw new ServiceException("流程已经完成");
                }

                dkAuditFlowRecord_Data.setCreateBy(SecurityUtils.getUsername());
                dkAuditFlowRecord_Data.setCreateTime(DateUtils.getNowDate());
                dkAuditFlowRecord_Data.setAuditflowOperateData(tDkAuditflowRecord.getAuditflowOperateData());
                dkAuditFlowRecord_Data.setSuccessType(tDkAuditflowRecord.getSuccessType());
                return refreshTDkAuditflowRecordV1(dkAuditFlowRecord_Data, tDkAuditFlowRecordsFull, xyjAuditflowOrder, loginUser);
            }
        }

        dkAuditFlowRecordQuery.setAuditflowState("2");
        List<TDkAuditflowRecord> tDkAuditFlowRecords_v3 = tDkAuditflowRecordMapper.selectTDkAuditflowRecordList(dkAuditFlowRecordQuery);

        if (!tDkAuditFlowRecords_v3.isEmpty()) {
            for (TDkAuditflowRecord recordObj : tDkAuditFlowRecords_v3) {
                if (tDkAuditflowRecord.getAuditflowStep().equals(recordObj.getAuditflowStep())) {
                    if (recordObj.getAuditflowStep().equals("-9999")) {
                        throw new ServiceException("流程已经完成");
                    }
                    recordObj.setCreateBy(SecurityUtils.getUsername());
                    recordObj.setCreateTime(DateUtils.getNowDate());
                    recordObj.setAuditflowOperateData(tDkAuditflowRecord.getAuditflowOperateData());
                    recordObj.setSuccessType(tDkAuditflowRecord.getSuccessType());
                    recordObj.setAuditflowState("2");
                    return refreshTDkAuditflowRecordV2(recordObj, loginUser);
                }
            }
        }
        throw new ServiceException("流程执行异常");
    }

    private Map<String, String> refreshTDkAuditflowRecordV1(TDkAuditflowRecord dkAuditFlowRecord_Data, List<TDkAuditflowRecord> tDkAuditFlowRecordsFull, Long xyjAuditflowOrder, LoginUser loginUser) {
        Map<String, String> retMap = new HashMap<>();
        //验证当前的数据是否可进行操作
        //序号，当前流程，未选中流程
        String v_auditflowStateD0 = dkAuditFlowRecord_Data.getAuditflowState();

        //校验用户是否可以进行操作,校验分为两种，一种是角色校验，另外一种是方法校验
        //判断用户权限，是否可进行此操作 。暂时忽略 ，后期加上
        if (!loginUser.getSysUser().isAdmin()) {
            Set<String> auditFlows = loginUser.getAuditFlows();
            String auditFlowHandle = dkAuditFlowRecord_Data.getAuditflowType() + ":" + dkAuditFlowRecord_Data.getAuditflowStep();
            if (!auditFlows.contains(auditFlowHandle)) {
                throw new ServiceException("无权限执行");
            }
        }

        //获取当前流程的信息
        TDkAuditflowStep presentStep = AuditFlowCacheUtil.getInstance().getAuditFlowTypePresentStep(dkAuditFlowRecord_Data.getAuditflowType(), dkAuditFlowRecord_Data.getAuditflowStep());
        if (presentStep == null) {
            throw new ServiceException("流程执行异常");
        }

        //方法校验 做二次扩展 true为校验成功
        //方法校验 做二次扩展 true为校验成功
        if (!checkBeanMethod(presentStep, dkAuditFlowRecord_Data.getDkjdxxId(), loginUser)) {
            throw new ServiceException("流程执行异常");
        }

        //第三步校验是否可以进行多次执行
        //操作状态 0执行一次 1可重复操作 空默认执行一次
        String stepOperationState_d0 = presentStep.getOperationState();
        if (StringUtils.isEmpty(stepOperationState_d0)) {
            stepOperationState_d0 = "0";
        }

        //操作状态 0执行一次 1可重复操作 2未执行
        //操作状态 0执行一次 1可重复操作 2未执行
        if ("0".equals(stepOperationState_d0)) {
            if ("0".equals(dkAuditFlowRecord_Data.getAuditflowOperationState())) {
                throw new ServiceException("流程不可重复执行");
            }
            dkAuditFlowRecord_Data.setAuditflowState("0");
        } else {
            dkAuditFlowRecord_Data.setAuditflowState("2");
        }

        dkAuditFlowRecord_Data.setAuditflowOperationState(stepOperationState_d0);
        dkAuditFlowRecord_Data.setAuditflowOperateType(presentStep.getOperationType());


        TDkjbxxState tDkjbxxState = new TDkjbxxState();
        tDkjbxxState.setId(dkAuditFlowRecord_Data.getDkjdxxId());
        tDkjbxxState.setAuditflow(dkAuditFlowRecord_Data.getAuditflowType());
        tDkjbxxState.setStepCode(dkAuditFlowRecord_Data.getAuditflowStep());
        tDkjbxxState.setOperationBy(SecurityUtils.getUsername());
        tDkjbxxState.setOperationTime(DateUtils.getNowDate());
        tDkjbxxState.setOperationId(SecurityUtils.getUserId().toString());
        tDkjbxxState.setSuccessType(dkAuditFlowRecord_Data.getSuccessType());

        //下一级操作 0 成功 1失败
        if (tDkAuditFlowRecordsFull.isEmpty()) {
            //添加下一级时判断是否需要验证
            if ("0".equals(presentStep.getNextCheck())) {
                String previousId = dkAuditFlowRecord_Data.getPreviousId();
                TDkAuditflowRecord dkAuditFlowRecordQuery = new TDkAuditflowRecord();
                dkAuditFlowRecordQuery.setNextId(previousId);
                List<TDkAuditflowRecord> previousRecords = tDkAuditflowRecordMapper.selectTDkAuditflowRecordList(dkAuditFlowRecordQuery);
                for (TDkAuditflowRecord previousRecordObj : previousRecords) {
                    if ("1".equals(previousRecordObj.getSuccessType())) {
                        dkAuditFlowRecord_Data.setSuccessType("1");
                        tDkjbxxState.setSuccessType("1");
                    }
                    TDkAuditflowRecord updateTDkAuditflowRecord = new TDkAuditflowRecord();
                    updateTDkAuditflowRecord.setId(previousRecordObj.getId());
                    updateTDkAuditflowRecord.setAuditflowState("0");
                    tDkAuditflowRecordMapper.updateTDkAuditflowRecord(updateTDkAuditflowRecord);
                }
            }

            //下一级判断
            xyjAuditflowOrder++;
            TDkAuditflowRecord dkAuditFlowRecord_Data_Next = new TDkAuditflowRecord();
            dkAuditFlowRecord_Data_Next.setId(IdUtils.fastSimpleUUID());
            dkAuditFlowRecord_Data_Next.setDkjdxxId(dkAuditFlowRecord_Data.getDkjdxxId());
            dkAuditFlowRecord_Data_Next.setAuditflowType(dkAuditFlowRecord_Data.getAuditflowType());
            dkAuditFlowRecord_Data_Next.setAuditflowOrder(xyjAuditflowOrder);
            dkAuditFlowRecord_Data_Next.setAuditflowState("1");
            dkAuditFlowRecord_Data_Next.setPreviousId(dkAuditFlowRecord_Data.getNextId());
            dkAuditFlowRecord_Data_Next.setNextId(IdUtils.fastSimpleUUID());

            dkAuditFlowRecord_Data_Next.setAuditflowStep("-999");
            if ("0".equals(dkAuditFlowRecord_Data.getSuccessType())) {
                if (StringUtils.isNotEmpty(presentStep.getNextSuccess())) {
                    String[] nextSuccess = presentStep.getNextSuccess().split(",");
                    for (String s : nextSuccess) {
                        if (!StringUtils.isEmpty(s)) {
                            dkAuditFlowRecord_Data_Next.setId(IdUtils.fastSimpleUUID());
                            dkAuditFlowRecord_Data_Next.setAuditflowOrder(xyjAuditflowOrder);
                            dkAuditFlowRecord_Data_Next.setAuditflowStep(s);
                            if (!"-999".equals(s)) {
                                TDkAuditflowStep nestStepV0 = AuditFlowCacheUtil.getInstance().getAuditFlowTypePresentStep(dkAuditFlowRecord_Data.getAuditflowType(), s);
                                if (nestStepV0 == null) {
                                    throw new ServiceException("流程执行异常");
                                }
                                dkAuditFlowRecord_Data_Next.setAuditflowOperateType(nestStepV0.getOperationType());
                            } else {
                                dkAuditFlowRecord_Data_Next.setAuditflowOperateType("-999");
                            }
                            dkAuditFlowRecord_Data_Next.setAuditflowOperationState("2");
                            tDkAuditflowRecordMapper.insertTDkAuditflowRecord(dkAuditFlowRecord_Data_Next);
                            xyjAuditflowOrder++;
                            dkAuditFlowRecord_Data_Next.setId(IdUtils.fastSimpleUUID());
                        }
                    }
                } else {
                    tDkAuditflowRecordMapper.insertTDkAuditflowRecord(dkAuditFlowRecord_Data_Next);
                }
            } else {
                if (StringUtils.isNotEmpty(presentStep.getNextFull())) {
                    String[] nextSuccess = presentStep.getNextFull().split(",");
                    for (String s : nextSuccess) {
                        if (!StringUtils.isEmpty(s)) {
                            dkAuditFlowRecord_Data_Next.setAuditflowOrder(xyjAuditflowOrder);
                            dkAuditFlowRecord_Data_Next.setAuditflowStep(s);
                            TDkAuditflowStep nestStepV0 = AuditFlowCacheUtil.getInstance().getAuditFlowTypePresentStep(dkAuditFlowRecord_Data.getAuditflowType(), s);
                            if (nestStepV0 == null) {
                                throw new ServiceException("流程执行异常");
                            }
                            dkAuditFlowRecord_Data_Next.setAuditflowOperationState("2");
                            dkAuditFlowRecord_Data_Next.setAuditflowOperateType(nestStepV0.getOperationType());
                            tDkAuditflowRecordMapper.insertTDkAuditflowRecord(dkAuditFlowRecord_Data_Next);
                            xyjAuditflowOrder++;
                            dkAuditFlowRecord_Data_Next.setId(IdUtils.fastSimpleUUID());
                        }
                    }
                } else {
                    tDkAuditflowRecordMapper.insertTDkAuditflowRecord(dkAuditFlowRecord_Data_Next);
                }
            }
        }

        tDkAuditflowRecordMapper.updateTDkAuditflowRecord(dkAuditFlowRecord_Data);
        tDkjbxxStateMapper.updateTDkjbxxState(tDkjbxxState);
        retMap.put("current", dkAuditFlowRecord_Data.getId());
        return retMap;
    }

    private Map<String, String> refreshTDkAuditflowRecordV2(TDkAuditflowRecord tDkAuditflowRecord, LoginUser loginUser) {
        Map<String, String> retMap = new HashMap<>();
        //校验用户是否可以进行操作,校验分为两种，一种是角色校验，另外一种是方法校验
        //判断用户权限，是否可进行此操作 。暂时忽略 ，后期加上
        if (!loginUser.getSysUser().isAdmin()) {
            Set<String> auditFlows = loginUser.getAuditFlows();
            String auditFlowHandle = tDkAuditflowRecord.getAuditflowType() + ":" + tDkAuditflowRecord.getAuditflowStep();
            if (!auditFlows.contains(auditFlowHandle)) {
                throw new ServiceException("无权限执行");
            }
        }

        //获取当前流程的信息
        TDkAuditflowStep presentStep = AuditFlowCacheUtil.getInstance().getAuditFlowTypePresentStep(tDkAuditflowRecord.getAuditflowType(), tDkAuditflowRecord.getAuditflowStep());
        if (presentStep == null) {
            throw new ServiceException("流程执行异常");
        }

        //方法校验 做二次扩展 true为校验成功
        if (!checkBeanMethod(presentStep, tDkAuditflowRecord.getDkjdxxId(), loginUser)) {
            throw new ServiceException("流程执行异常");
        }

        //第三步校验是否可以进行多次执行
        //操作状态 0执行一次 1可重复操作 空默认执行一次
        String stepOperationState_d0 = presentStep.getOperationState();
        if (StringUtils.isEmpty(stepOperationState_d0)) {
            stepOperationState_d0 = "0";
        }

        //操作状态 0执行一次 1可重复操作 2未执行
        if ("0".equals(stepOperationState_d0)) {
            if ("0".equals(tDkAuditflowRecord.getAuditflowOperationState())) {
                throw new ServiceException("流程不可重复执行");
            }
            tDkAuditflowRecord.setAuditflowState("0");
        } else {
            stepOperationState_d0 = "1";
            tDkAuditflowRecord.setAuditflowState("2");
        }

        tDkAuditflowRecord.setAuditflowOperationState(stepOperationState_d0);
        tDkAuditflowRecord.setAuditflowOperateType(presentStep.getOperationType());

        TDkjbxxState tDkjbxxState = new TDkjbxxState();
        tDkjbxxState.setId(tDkAuditflowRecord.getDkjdxxId());
        tDkjbxxState.setAuditflow(tDkAuditflowRecord.getAuditflowType());
        tDkjbxxState.setStepCode(tDkAuditflowRecord.getAuditflowStep());
        tDkjbxxState.setOperationBy(SecurityUtils.getUsername());
        tDkjbxxState.setOperationTime(DateUtils.getNowDate());
        tDkjbxxState.setOperationId(SecurityUtils.getUserId().toString());
        tDkjbxxState.setSuccessType(tDkAuditflowRecord.getSuccessType());

        tDkAuditflowRecordMapper.updateTDkAuditflowRecord(tDkAuditflowRecord);
        tDkjbxxStateMapper.updateTDkjbxxState(tDkjbxxState);
        retMap.put("current", tDkAuditflowRecord.getId());
        return retMap;
    }

    private boolean checkBeanMethod(TDkAuditflowStep presentStep, String dkjdxxId, LoginUser loginUser) {
        String checkBean = presentStep.getCheckBean();
        String checkMethod = presentStep.getCheckMethod();
        if (StringUtils.isNotEmpty(checkBean)) {
            try {
                String userid = loginUser.getUserid().toString();
                List<Object> invokeMethodData = new ArrayList<>();
                invokeMethodData.add(dkjdxxId);
                invokeMethodData.add(userid);
                invokeMethodData.add(presentStep.getAuditflowCode());
                Object resultBean = BeanReflectionUtils.invokeMethodV1(checkBean, checkMethod, invokeMethodData);
                if (resultBean instanceof Boolean) {
                    boolean boolResult = (Boolean) resultBean;
                    if (!boolResult) {
                        return false;
                    }
                } else {
                    return false;
                }
            } catch (Exception e) {
                return false;
            }
        }
        return true;
    }

    /**
     * 修改地块核查流程
     *
     * @param tDkAuditflowRecord 地块核查流程
     * @return 结果
     */
    @Override
    public int updateTDkAuditflowRecord(TDkAuditflowRecord tDkAuditflowRecord) {
        return tDkAuditflowRecordMapper.updateTDkAuditflowRecord(tDkAuditflowRecord);
    }

    /**
     * 批量删除地块核查流程
     *
     * @param ids 需要删除的地块核查流程主键
     * @return 结果
     */
    @Override
    public int deleteTDkAuditflowRecordByIds(String[] ids) {
        return tDkAuditflowRecordMapper.deleteTDkAuditflowRecordByIds(ids);
    }

    /**
     * 删除地块核查流程信息
     *
     * @param id 地块核查流程主键
     * @return 结果
     */
    @Override
    public int deleteTDkAuditflowRecordById(String id) {
        return tDkAuditflowRecordMapper.deleteTDkAuditflowRecordById(id);
    }


    /**
     * 校驗用戶是否有權限操作，并返回当前操作
     *
     * @param auditflow
     * @return
     */
    @Override
    public boolean isCheckUserTDkAuditflowRecord(String dkid, String auditflow, String auditflowStep, String operationType) {
        if (StringUtils.isEmpty(dkid) || StringUtils.isEmpty(auditflow) || StringUtils.isEmpty(auditflowStep)) {
            return false;
        }
        LoginUser loginUser = SecurityUtils.getLoginUser();
        if (loginUser == null) {
            return false;
        }

        //验证当前的数据列席是否可进行下发
        TDkAuditflowRecord dkAuditFlowRecordQuery = new TDkAuditflowRecord();
        dkAuditFlowRecordQuery.setDkjdxxId(dkid);
        dkAuditFlowRecordQuery.setAuditflowType(auditflow);
        dkAuditFlowRecordQuery.setAuditflowStep(auditflowStep);
        dkAuditFlowRecordQuery.setAuditflowState("1");
        List<TDkAuditflowRecord> tDkAuditFlowRecords = selectTDkAuditflowRecordList(dkAuditFlowRecordQuery);
        if (tDkAuditFlowRecords == null || tDkAuditFlowRecords.isEmpty()) {
            dkAuditFlowRecordQuery.setAuditflowState("2");
            tDkAuditFlowRecords = selectTDkAuditflowRecordList(dkAuditFlowRecordQuery);
        }
        if (tDkAuditFlowRecords == null || tDkAuditFlowRecords.isEmpty()) {
            return false;
        }
        TDkAuditflowRecord dkAuditFlowRecord_Data = null;
        List<TDkAuditflowRecord> tDkAuditFlowRecordsFull = new ArrayList<>();
        for (TDkAuditflowRecord recordObj : tDkAuditFlowRecords) {
            if (auditflowStep.equals(recordObj.getAuditflowStep())) {
                dkAuditFlowRecord_Data = recordObj;
            }
        }

        if (dkAuditFlowRecord_Data == null) {
            return false;
        }

        //判断用户权限，是否可进行此操作 。暂时忽略 ，后期加上
        if (!loginUser.getSysUser().isAdmin()) {
            Set<String> auditFlows = loginUser.getAuditFlows();
            String auditFlowHandle = dkAuditFlowRecord_Data.getAuditflowType() + ":" + dkAuditFlowRecord_Data.getAuditflowStep();
            if (!auditFlows.contains(auditFlowHandle)) {
                return false;
            }
        }

        AuditFlowCacheUtil auditFlowCacheUtil = AuditFlowCacheUtil.getInstance();
        TDkAuditflowStep presentStep = auditFlowCacheUtil.getAuditFlowTypePresentStep(dkAuditFlowRecord_Data.getAuditflowType(), dkAuditFlowRecord_Data.getAuditflowStep());
        if (presentStep == null) {
            return false;
        }

        if (StringUtils.isNotEmpty(presentStep.getOperationType()) && StringUtils.isEmpty(operationType)) {
            return false;
        }

        if (StringUtils.isEmpty(presentStep.getOperationType()) && StringUtils.isNotEmpty(operationType)) {
            return false;
        }

        if (StringUtils.isNotEmpty(presentStep.getOperationType()) && StringUtils.isNotEmpty(operationType)) {
            if (!operationType.equals(presentStep.getOperationType())) {
                return false;
            }
        }

        return true;
    }

    /**
     * 校驗用戶是否有權限操作，并返回当前操作
     *
     * @param auditflow
     * @return
     */
    @Override
    public boolean isCheckDataUpdateTDkAuditflowRecord(String dkid, String auditflow) {
        if (StringUtils.isEmpty(dkid) || StringUtils.isEmpty(auditflow)) {
            return false;
        }

        //验证当前的数据列席是否可进行
        TDkAuditflowRecord dkAuditFlowRecordQuery = new TDkAuditflowRecord();
        dkAuditFlowRecordQuery.setDkjdxxId(dkid);
        dkAuditFlowRecordQuery.setAuditflowType(auditflow);
        List<TDkAuditflowRecord> tDkAuditFlowRecords = selectTDkAuditflowRecordList(dkAuditFlowRecordQuery);
        if (tDkAuditFlowRecords.isEmpty() || tDkAuditFlowRecords.size() <= 3) {
            return true;
        }
        return false;
    }

    ;
}
