package com.siwei.rsmonitoring.service.impl;

import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import com.alibaba.druid.sql.visitor.functions.If;
import com.siwei.common.core.constant.SecurityConstants;
import com.siwei.common.core.domain.R;
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.security.utils.SecurityUtils;
import com.siwei.rsmonitoring.domain.TDkAuditflowRecord;
import com.siwei.rsmonitoring.domain.vo.DataCountVo;
import com.siwei.rsmonitoring.service.ITDkAuditflowRecordService;
import com.siwei.system.api.RemoteDeptService;
import com.siwei.system.api.domain.SysDept;
import com.siwei.system.api.model.LoginUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import com.siwei.rsmonitoring.mapper.TDkjbxxMapper;
import com.siwei.rsmonitoring.domain.TDkjbxx;
import com.siwei.rsmonitoring.service.ITDkjbxxService;

/**
 * 地块基本信息（DKJBXX）Service业务层处理
 *
 * @author siwei
 * @date 2025-01-20
 */
@Service
public class TDkjbxxServiceImpl implements ITDkjbxxService {
    @Autowired
    private TDkjbxxMapper tDkjbxxMapper;
    @Autowired
    private ITDkAuditflowRecordService itDkAuditflowRecordService;
    @Autowired
    private RemoteDeptService remoteDeptService;


    /**
     * 输出坐标系
     */
    @Value("${shp.out_srid}")
    private Integer shpOutSRID;

    /**
     * 查询地块基本信息（DKJBXX）
     *
     * @param id 地块基本信息（DKJBXX）主键
     * @return 地块基本信息（DKJBXX）
     */
    @Override
    public TDkjbxx selectTDkjbxxById(String id) {
        return tDkjbxxMapper.selectTDkjbxxById(id, shpOutSRID);
    }

    @Override
    public TDkjbxx selectTDkjbxxByDkbh(String dkbh) {
        return tDkjbxxMapper.selectTDkjbxxByDkbh(dkbh, shpOutSRID);
    }

    /**
     * 查询地块基本信息（DKJBXX）列表
     *
     * @param tDkjbxx 地块基本信息（DKJBXX）
     * @return 地块基本信息（DKJBXX）
     */
    @Override
    public List<TDkjbxx> selectTDkjbxxList(TDkjbxx tDkjbxx) {
        tDkjbxx.setShpOutSRID(shpOutSRID);
        String dkmjbs = tDkjbxx.getDkmjbs();
        if (StringUtils.isEmpty(dkmjbs) || "=".equals(dkmjbs)) {
            tDkjbxx.setDkmjbs("=");
        } else if ("&gt;".equals(dkmjbs)) {
            tDkjbxx.setDkmjbs(">");
        } else if ("&gt;=".equals(dkmjbs)) {
            tDkjbxx.setDkmjbs(">=");
        } else if ("&lt;".equals(dkmjbs)) {
            tDkjbxx.setDkmjbs("<");
        } else if ("&lt;=".equals(dkmjbs)) {
            tDkjbxx.setDkmjbs("<=");
        } else {
            throw new ServiceException("参数有误");
        }
        String descValue = tDkjbxx.getDescValue();
        if (StringUtils.isEmpty(descValue) || "create_time".equals(descValue)) {
            tDkjbxx.setDescValue("create_time");
        } else if ("dkbh".equals(descValue)) {
            tDkjbxx.setDescValue("dkbh");
        } else if ("dkmj".equals(descValue)) {
            tDkjbxx.setDescValue("dkmj::NUMERIC");
        } else {
            throw new ServiceException("参数有误");
        }
        return tDkjbxxMapper.selectTDkjbxxList(tDkjbxx);
    }

    /**
     * 新增地块基本信息（DKJBXX）
     *
     * @param tDkjbxx 地块基本信息（DKJBXX）
     * @return 结果
     */
    @Override
    public int insertTDkjbxx(TDkjbxx tDkjbxx) {
        tDkjbxx.setCreateTime(DateUtils.getNowDate());
        tDkjbxxMapper.insertTDkjbxx(tDkjbxx);
        //初始化
        TDkAuditflowRecord tDkAuditflowRecord = new TDkAuditflowRecord();
        tDkAuditflowRecord.setAuditflowType(tDkjbxx.getAuditflow());
        tDkAuditflowRecord.setCreateBy(tDkjbxx.getCreateBy());
        tDkAuditflowRecord.setDkjdxxId(tDkjbxx.getId());
        if (itDkAuditflowRecordService.initDkAuditflowRecord(tDkAuditflowRecord).isEmpty()) {
            throw new ServiceException("流程失败");
        }
        return 1;
    }

    /**
     * 修改地块基本信息（DKJBXX）
     *
     * @param tDkjbxx 地块基本信息（DKJBXX）
     * @return 结果
     */
    @Override
    public int updateTDkjbxx(TDkjbxx tDkjbxx) {
        tDkjbxx.setUpdateBy(SecurityUtils.getUsername());
        tDkjbxx.setUpdateTime(DateUtils.getNowDate());
        return tDkjbxxMapper.updateTDkjbxx(tDkjbxx);
    }

    @Override
    public int updateTDkjbxxByHgx(TDkjbxx tDkjbxx) {
        tDkjbxx.setUpdateTime(DateUtils.getNowDate());
        tDkjbxx.setUpdateBy(SecurityUtils.getUsername());
        return tDkjbxxMapper.updateTDkjbxxByHgx(tDkjbxx);
    }

    /**
     * 批量删除地块基本信息（DKJBXX）
     *
     * @param ids 需要删除的地块基本信息（DKJBXX）主键
     * @return 结果
     */
    @Override
    public int deleteTDkjbxxByIds(String[] ids) {
        return tDkjbxxMapper.deleteTDkjbxxByIds(ids);
    }

    /**
     * 删除地块基本信息（DKJBXX）信息
     *
     * @param id 地块基本信息（DKJBXX）主键
     * @return 结果
     */
    @Override
    public int deleteTDkjbxxById(String id) {
        return tDkjbxxMapper.deleteTDkjbxxById(id);
    }


    /**
     * 统计地块汇总信息
     *
     * @param dataCountVo
     * @return
     */
    @Override
    public DataCountVo selectTDkjbxxCctbCount(DataCountVo dataCountVo) {
        return tDkjbxxMapper.selectTDkjbxxCctbCount(dataCountVo);
    }


    @Override
    public boolean checkDkjbxxUpdate(String dkbh) {
        TDkjbxx queryDate = selectTDkjbxxByDkbh(dkbh);
        //不存在可以直接添加
        if (null == queryDate) {
            return true;
        }
        return itDkAuditflowRecordService.isCheckDataUpdateTDkAuditflowRecord(queryDate.getId(), queryDate.getAuditflow());
    }

    @Override
    public Set<String> selectXzqdmUserList(Long checkXzqdm) {
        R<List<SysDept>> r1 = remoteDeptService.childByDeptId(checkXzqdm, false, SecurityConstants.INNER);
        if (R.isError(r1)) {
            throw new ServiceException(r1.getMsg());
        }
        List<SysDept> list = r1.getData();
        if (list.isEmpty()) {
            throw new ServiceException("用户查询失败");
        }
        Set<String> xzqhdm = new HashSet<>();
        for (SysDept obj : list) {
            xzqhdm.add(obj.getDistrict());
        }
        return xzqhdm;
    }

}
