package com.siwei.rsmonitoring.service.impl;

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

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.siwei.common.core.utils.DateUtils;
import com.siwei.common.core.utils.StringUtils;
import com.siwei.rsmonitoring.domain.TDkYwypXlsxDetails;
import com.siwei.rsmonitoring.service.ITDkYwypXlsxDetailsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.siwei.rsmonitoring.mapper.TDkGeomDbDetailsMapper;
import com.siwei.rsmonitoring.domain.TDkGeomDbDetails;
import com.siwei.rsmonitoring.service.ITDkGeomDbDetailsService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 地块入库信息空间详情Service业务层处理
 *
 * @author siwei
 * @date 2025-02-07
 */
@Service
public class TDkGeomDbDetailsServiceImpl implements ITDkGeomDbDetailsService {
    @Autowired
    private TDkGeomDbDetailsMapper tDkGeomDbDetailsMapper;
    @Autowired
    private ITDkYwypXlsxDetailsService itDkYwypXlsxDetailsService;

    /**
     * 查询地块入库信息空间详情
     *
     * @param id 地块入库信息空间详情主键
     * @return 地块入库信息空间详情
     */
    @Override
    public TDkGeomDbDetails selectTDkGeomDbDetailsById(String id) {
        TDkGeomDbDetails ret = tDkGeomDbDetailsMapper.selectTDkGeomDbDetailsById(id);
        if (ret != null && StringUtils.isNotEmpty(ret.getSheetJson())) {
            JSONArray jsonArray = JSON.parseArray(ret.getSheetJson());
            JSONArray retjsonArray = new JSONArray();
            for (int i = 0; i < jsonArray.size(); i++) {
                JSONObject jsonObject = jsonArray.getJSONObject(i);
                String[] bhj = jsonObject.getString("bhj").split(",", -1);
                String[] mjj = jsonObject.getString("mjj").split(",", -1);
                String uploadid = jsonObject.getString("upload");
                String sheet = jsonObject.getString("sheet");
                List<TDkYwypXlsxDetails> data_list = new ArrayList<>();
                for (int j = 0; j < bhj.length; j++) {
                    String objv1 = bhj[j];
                    String mjjv1 = mjj[j];
                    if (StringUtils.isNotEmpty(objv1)) {
                        TDkYwypXlsxDetails dkYwypXlsxDetails = itDkYwypXlsxDetailsService.selectTDkYwypXlsxDetailsBySheet(uploadid, sheet, objv1);
                        dkYwypXlsxDetails.setYzmj(mjjv1);
                        data_list.add(dkYwypXlsxDetails);
                    }
                }
                jsonObject.put("data_list", data_list);
                retjsonArray.add(jsonObject);
            }
            ret.setSheetJson(retjsonArray.toString());
        }
        return ret;
    }

    /**
     * 查询地块入库信息空间详情列表
     *
     * @param tDkGeomDbDetails 地块入库信息空间详情
     * @return 地块入库信息空间详情
     */
    @Override
    public List<TDkGeomDbDetails> selectTDkGeomDbDetailsList(TDkGeomDbDetails tDkGeomDbDetails) {
        return tDkGeomDbDetailsMapper.selectTDkGeomDbDetailsList(tDkGeomDbDetails);
    }

    /**
     * 新增地块入库信息空间详情
     *
     * @param tDkGeomDbDetails 地块入库信息空间详情
     * @return 结果
     */
    @Override
    public int insertTDkGeomDbDetails(TDkGeomDbDetails tDkGeomDbDetails) {
        tDkGeomDbDetails.setCreateTime(DateUtils.getNowDate());
        return tDkGeomDbDetailsMapper.insertTDkGeomDbDetails(tDkGeomDbDetails);
    }

    /**
     * 修改地块入库信息空间详情
     *
     * @param tDkGeomDbDetails 地块入库信息空间详情
     * @return 结果
     */
    @Override
    public int updateTDkGeomDbDetails(TDkGeomDbDetails tDkGeomDbDetails) {
        tDkGeomDbDetails.setUpdateTime(DateUtils.getNowDate());
        return tDkGeomDbDetailsMapper.updateTDkGeomDbDetails(tDkGeomDbDetails);
    }

    /**
     * 批量删除地块入库信息空间详情
     *
     * @param ids 需要删除的地块入库信息空间详情主键
     * @return 结果
     */
    @Override
    public int deleteTDkGeomDbDetailsByIds(String[] ids) {
        return tDkGeomDbDetailsMapper.deleteTDkGeomDbDetailsByIds(ids);
    }

    @Override
    public int deleteTDkGeomDbDetailsByUploadIds(String[] ids) {
        return tDkGeomDbDetailsMapper.deleteTDkGeomDbDetailsByUploadIds(ids);
    }

    /**
     * 删除地块入库信息空间详情信息
     *
     * @param id 地块入库信息空间详情主键
     * @return 结果
     */
    @Override
    public int deleteTDkGeomDbDetailsById(String id) {
        return tDkGeomDbDetailsMapper.deleteTDkGeomDbDetailsById(id);
    }
}
