package com.siwei.rsmonitoring.service.impl;

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

import com.alibaba.fastjson2.JSON;
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.uuid.IdUtils;
import com.siwei.common.security.utils.SecurityUtils;
import com.siwei.rsmonitoring.cache.AuditFlowCacheUtil;
import com.siwei.rsmonitoring.config.StaticConfig;
import com.siwei.rsmonitoring.domain.*;
import com.siwei.rsmonitoring.service.ITDkAuditflowRecordService;
import com.siwei.rsmonitoring.service.ITDkjbxxService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.siwei.rsmonitoring.mapper.TDkjbxxTjMapper;
import com.siwei.rsmonitoring.service.ITDkjbxxTjService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 地块基本信息提交Service业务层处理
 *
 * @author siwei
 * @date 2025-02-20
 */
@Service
public class TDkjbxxTjServiceImpl implements ITDkjbxxTjService {
    @Autowired
    private TDkjbxxTjMapper tDkjbxxTjMapper;
    @Autowired
    private ITDkjbxxService itDkjbxxService;
    @Autowired
    private ITDkAuditflowRecordService dkAuditflowRecordService;

    /**
     * 查询地块基本信息提交
     *
     * @param id 地块基本信息提交主键
     * @return 地块基本信息提交
     */
    @Override
    public TDkjbxxTj selectTDkjbxxTjById(String id) {
        return tDkjbxxTjMapper.selectTDkjbxxTjById(id);
    }

    @Override
    public TDkjbxxTj selectTDkjbxxTjInfo(String dkid, String auditflowStep, String auditflowId) {
        if (StringUtils.isEmpty(dkid) || StringUtils.isEmpty(auditflowStep)) {
            return null;
        }
        TDkAuditflowRecord tDkAuditflowRecord = new TDkAuditflowRecord();
        tDkAuditflowRecord.setDkjdxxId(dkid);
        tDkAuditflowRecord.setAuditflowStep(auditflowStep);
        if (StringUtils.isNotEmpty(auditflowId)) {
            tDkAuditflowRecord.setId(auditflowId);
        }
        List<TDkAuditflowRecord> tDkAuditflowRecords = dkAuditflowRecordService.selectTDkAuditflowRecordList(tDkAuditflowRecord);
        if (tDkAuditflowRecords.isEmpty()) {
            return null;
        }
        TDkAuditflowRecord qureyData0 = tDkAuditflowRecords.get(0);
        if (!StaticConfig.AUDIT_FLOW_STEP_OPERATION_TYPE_003.equals(qureyData0.getAuditflowOperateType())) {
            return null;
        }
        TDkjbxxTj tDkjbxxTjQuery = new TDkjbxxTj();
        tDkjbxxTjQuery.setDkjbxxId(dkid);
        tDkjbxxTjQuery.setAuditflowStep(auditflowStep);
        tDkjbxxTjQuery.setAuditflowId(qureyData0.getId());
        List<TDkjbxxTj> list = tDkjbxxTjMapper.selectTDkjbxxTjList(tDkjbxxTjQuery);
        if (list.isEmpty() && "1".equals(qureyData0.getAuditflowState())) {
            TDkAuditflowRecord tDkAuditflowRecord2 = new TDkAuditflowRecord();
            tDkAuditflowRecord2.setDkjdxxId(dkid);
            tDkAuditflowRecord2.setNextId(qureyData0.getPreviousId());
            List<TDkAuditflowRecord> tDkAuditflowRecords2 = dkAuditflowRecordService.selectTDkAuditflowRecordList(tDkAuditflowRecord2);
            if (tDkAuditflowRecords.isEmpty()) {
                return null;
            }

            AuditFlowCacheUtil auditFlowCacheUtil = AuditFlowCacheUtil.getInstance();
            String decide = "0";
            List<Map<String, String>> notes = new ArrayList<>();
            for (TDkAuditflowRecord obj_v1 : tDkAuditflowRecords2) {
                TDkAuditflowStep dkAuditflowStep = auditFlowCacheUtil.getAuditFlowTypePresentStep(obj_v1.getAuditflowType(), obj_v1.getAuditflowStep());
                if (null == dkAuditflowStep) {
                    return null;
                }
                Map<String, String> data = new HashMap<>();
                data.put("stepValue", dkAuditflowStep.getStepValue());
                data.put("stepCode", dkAuditflowStep.getStepCode());
                if ("1".equals(obj_v1.getSuccessType())) {
                    decide = "1";
                    data.put("notes", "操作执行完成");
                } else {
                    data.put("notes", "操作执行完成");
                }
                notes.add(data);
            }
            TDkjbxxTj ret = new TDkjbxxTj();
            ret.setDkjbxxId(dkid);
            ret.setDecide(decide);
            ret.setNotes(JSON.toJSONString(notes));
            return ret;
        } else if (!list.isEmpty()) {
            return list.get(0);
        } else {
            return null;
        }
    }

    /**
     * 查询地块基本信息提交列表
     *
     * @param tDkjbxxTj 地块基本信息提交
     * @return 地块基本信息提交
     */
    @Override
    public List<TDkjbxxTj> selectTDkjbxxTjList(TDkjbxxTj tDkjbxxTj) {
        return tDkjbxxTjMapper.selectTDkjbxxTjList(tDkjbxxTj);
    }

    /**
     * 新增地块基本信息提交
     *
     * @param tDkjbxxTj 地块基本信息提交
     * @return 结果
     */
    @Override
    public int insertTDkjbxxTj(TDkjbxxTj tDkjbxxTj) {
        tDkjbxxTj.setCreateTime(DateUtils.getNowDate());
        return tDkjbxxTjMapper.insertTDkjbxxTj(tDkjbxxTj);
    }

    /**
     * 新增地块基本信息提交
     *
     * @param tDkjbxxTj 地块基本信息提交
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int addTDkjbxxTj(TDkjbxxTj tDkjbxxTj) {
        if (null == tDkjbxxTj) {
            return 0;
        }
        String dkid = tDkjbxxTj.getDkjbxxId();
        if (StringUtils.isEmpty(dkid)) {
            return 0;
        }
        String auditflowStep = tDkjbxxTj.getAuditflowStep();
        if (StringUtils.isEmpty(auditflowStep)) {
            return 0;
        }
        if (StringUtils.isEmpty(tDkjbxxTj.getDecide())) {
            return 0;
        }
        TDkjbxx dkjbxx = itDkjbxxService.selectTDkjbxxById(dkid);
        if (dkjbxx == null) {
            return 0;
        }
        if (!dkAuditflowRecordService.isCheckUserTDkAuditflowRecord(dkjbxx.getId(), dkjbxx.getAuditflow(), auditflowStep, StaticConfig.AUDIT_FLOW_STEP_OPERATION_TYPE_003)) {
            return 0;
        }
        tDkjbxxTj.setCreateBy(SecurityUtils.getUsername());
        tDkjbxxTj.setId(IdUtils.fastSimpleUUID());
        tDkjbxxTj.setCreateTime(DateUtils.getNowDate());
        tDkjbxxTjMapper.insertTDkjbxxTj(tDkjbxxTj);


        //更新流程
        TDkAuditflowRecord tDkAuditflowRecord = new TDkAuditflowRecord();
        tDkAuditflowRecord.setDkjdxxId(dkid);
        tDkAuditflowRecord.setAuditflowType(dkjbxx.getAuditflow());
        tDkAuditflowRecord.setCreateBy(SecurityUtils.getUsername());
        tDkAuditflowRecord.setAuditflowStep(auditflowStep);

        Map<String, String> recordData = null;
        if ("1".equals(tDkjbxxTj.getDecide())) {
            tDkAuditflowRecord.setSuccessType("1");
        } else {
            tDkAuditflowRecord.setSuccessType("0");
        }
        recordData = dkAuditflowRecordService.refreshTDkAuditflowRecord(tDkAuditflowRecord);
        if (recordData.isEmpty()) {
            throw new ServiceException("流程失败");
        }
        String currentId = recordData.get("current");
        deleteAuditflowId(currentId);

        TDkjbxxTj tDkjbxxTjUpdate = new TDkjbxxTj();
        tDkjbxxTjUpdate.setId(tDkjbxxTj.getId());
        tDkjbxxTjUpdate.setAuditflowId(currentId);
        tDkjbxxTjMapper.updateTDkjbxxTj(tDkjbxxTjUpdate);
        return 1;
    }

    private void deleteAuditflowId(String auditflowId) {
        TDkjbxxTj query = new TDkjbxxTj();
        query.setAuditflowId(auditflowId);
        List<TDkjbxxTj> removerList = tDkjbxxTjMapper.selectTDkjbxxTjList(query);
        for (TDkjbxxTj hgx : removerList) {
            tDkjbxxTjMapper.deleteTDkjbxxTjById(hgx.getId());
        }
    }

    /**
     * 修改地块基本信息提交
     *
     * @param tDkjbxxTj 地块基本信息提交
     * @return 结果
     */
    @Override
    public int updateTDkjbxxTj(TDkjbxxTj tDkjbxxTj) {
        return tDkjbxxTjMapper.updateTDkjbxxTj(tDkjbxxTj);
    }

    /**
     * 批量删除地块基本信息提交
     *
     * @param ids 需要删除的地块基本信息提交主键
     * @return 结果
     */
    @Override
    public int deleteTDkjbxxTjByIds(String[] ids) {
        return tDkjbxxTjMapper.deleteTDkjbxxTjByIds(ids);
    }

    /**
     * 删除地块基本信息提交信息
     *
     * @param id 地块基本信息提交主键
     * @return 结果
     */
    @Override
    public int deleteTDkjbxxTjById(String id) {
        return tDkjbxxTjMapper.deleteTDkjbxxTjById(id);
    }
}
