package com.siwei.rsmonitoring.service.impl;

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

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.config.StaticConfig;
import com.siwei.rsmonitoring.domain.*;
import com.siwei.rsmonitoring.service.ITDkAuditflowFjService;
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.TDkjbxxScMapper;
import com.siwei.rsmonitoring.service.ITDkjbxxScService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 地块基本信息审查Service业务层处理
 *
 * @author siwei
 * @date 2025-02-12
 */
@Service
public class TDkjbxxScServiceImpl implements ITDkjbxxScService {
    @Autowired
    private TDkjbxxScMapper tDkjbxxScMapper;
    @Autowired
    private ITDkjbxxService itDkjbxxService;
    @Autowired
    private ITDkAuditflowRecordService dkAuditflowRecordService;
    @Autowired
    private ITDkAuditflowFjService itDkAuditflowFjService;

    /**
     * 查询地块基本信息审查
     *
     * @param id 地块基本信息审查主键
     * @return 地块基本信息审查
     */
    @Override
    public TDkjbxxSc selectTDkjbxxScById(String id) {
        return tDkjbxxScMapper.selectTDkjbxxScById(id);
    }

    @Override
    public TDkjbxxSc selectTDkjbxxScInfo(String dkid, String auditflowStep, String auditflowId) {
        if (StringUtils.isEmpty(dkid) || StringUtils.isEmpty(auditflowStep)) {
            return null;
        }
        TDkjbxxSc tDkjbxxScQuery = new TDkjbxxSc();
        tDkjbxxScQuery.setDkjbxxId(dkid);
        tDkjbxxScQuery.setAuditflowStep(auditflowStep);
        if (StringUtils.isNotEmpty(auditflowId)) {
            tDkjbxxScQuery.setAuditflowId(auditflowId);
        }
        List<TDkjbxxSc> list = tDkjbxxScMapper.selectTDkjbxxScList(tDkjbxxScQuery);
        if (list.isEmpty()) {
            return null;
        }
        TDkjbxxSc dkjbxxSc = list.get(0);

        TDkAuditflowFj tDkAuditflowFj = new TDkAuditflowFj();
        tDkAuditflowFj.setAuditflowId(dkjbxxSc.getAuditflowId());
        dkjbxxSc.setFjs(itDkAuditflowFjService.selectTDkAuditflowFjList(tDkAuditflowFj));
        return dkjbxxSc;
    }

    /**
     * 查询地块基本信息审查列表
     *
     * @param tDkjbxxSc 地块基本信息审查
     * @return 地块基本信息审查
     */
    @Override
    public List<TDkjbxxSc> selectTDkjbxxScList(TDkjbxxSc tDkjbxxSc) {
        return tDkjbxxScMapper.selectTDkjbxxScList(tDkjbxxSc);
    }

    /**
     * 新增地块基本信息审查
     *
     * @param tDkjbxxSc 地块基本信息审查
     * @return 结果
     */
    @Override
    public int insertTDkjbxxSc(TDkjbxxSc tDkjbxxSc) {
        tDkjbxxSc.setCreateTime(DateUtils.getNowDate());
        return tDkjbxxScMapper.insertTDkjbxxSc(tDkjbxxSc);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int addTDkjbxxSc(TDkjbxxSc tDkjbxxSc) {
        if (null == tDkjbxxSc) {
            return 0;
        }
        String dkid = tDkjbxxSc.getDkjbxxId();
        if (StringUtils.isEmpty(dkid)) {
            return 0;
        }
        String auditflowStep = tDkjbxxSc.getAuditflowStep();
        if (StringUtils.isEmpty(auditflowStep)) {
            return 0;
        }
        if (StringUtils.isEmpty(tDkjbxxSc.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_002)) {
            return 0;
        }
        tDkjbxxSc.setCreateBy(SecurityUtils.getUsername());
        tDkjbxxSc.setId(IdUtils.fastSimpleUUID());
        tDkjbxxSc.setCreateTime(DateUtils.getNowDate());
        tDkjbxxScMapper.insertTDkjbxxSc(tDkjbxxSc);

        List<String> fjUpdateList = new ArrayList<>();
        //附件
        for (TDkAuditflowFj fjObj : tDkjbxxSc.getFjs()) {
            fjObj.setDkjbxxId(dkid);
            fjObj.setId(IdUtils.fastSimpleUUID());
            fjObj.setCreateBy(SecurityUtils.getUsername());
            fjObj.setCreateTime(DateUtils.getNowDate());
            itDkAuditflowFjService.insertTDkAuditflowFj(fjObj);
            fjUpdateList.add(fjObj.getId());
        }

        //更新流程
        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(tDkjbxxSc.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);

        //更新标志
        for (String idObj : fjUpdateList) {
            TDkAuditflowFj tDkAuditflowFj = new TDkAuditflowFj();
            tDkAuditflowFj.setId(idObj);
            tDkAuditflowFj.setAuditflowId(currentId);
            itDkAuditflowFjService.updateTDkAuditflowFj(tDkAuditflowFj);
        }

        TDkjbxxSc tDkjbxxScUpdate = new TDkjbxxSc();
        tDkjbxxScUpdate.setId(tDkjbxxSc.getId());
        tDkjbxxScUpdate.setAuditflowId(currentId);
        tDkjbxxScMapper.updateTDkjbxxSc(tDkjbxxScUpdate);
        return 1;
    }

    private void deleteAuditflowId(String auditflowId) {
        //删除之前上传的数据
        itDkAuditflowFjService.deleteTDkAuditflowFjByAuditflowId(auditflowId);

        TDkjbxxSc query = new TDkjbxxSc();
        query.setAuditflowId(auditflowId);
        List<TDkjbxxSc> removerList = tDkjbxxScMapper.selectTDkjbxxScList(query);
        for (TDkjbxxSc hgx : removerList) {
            tDkjbxxScMapper.deleteTDkjbxxScById(hgx.getId());
        }

    }

    /**
     * 修改地块基本信息审查
     *
     * @param tDkjbxxSc 地块基本信息审查
     * @return 结果
     */
    @Override
    public int updateTDkjbxxSc(TDkjbxxSc tDkjbxxSc) {
        return tDkjbxxScMapper.updateTDkjbxxSc(tDkjbxxSc);
    }

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

    /**
     * 删除地块基本信息审查信息
     *
     * @param id 地块基本信息审查主键
     * @return 结果
     */
    @Override
    public int deleteTDkjbxxScById(String id) {
        return tDkjbxxScMapper.deleteTDkjbxxScById(id);
    }
}
