package com.zjsgfa.project.zjsgfa.service.impl;

import java.util.List;
import java.util.stream.Collectors;

import com.zjsgfa.project.zjsgfa.domain.SjDjjc;
import com.zjsgfa.project.zjsgfa.domain.SjLjjw;
import com.zjsgfa.project.zjsgfa.mapper.SjDjjcMapper;
import com.zjsgfa.project.zt.domain.CommonParam;
import com.zjsgfa.project.zt.service.DjdcService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.zjsgfa.project.zjsgfa.mapper.SjLjfzqkMapper;
import com.zjsgfa.project.zjsgfa.domain.SjLjfzqk;
import com.zjsgfa.project.zjsgfa.service.ISjLjfzqkService;

/**
 * 邻井复杂情况Service业务层处理
 * 
 * @author ruoyi
 * @date 2026-02-04
 */
@Service
public class SjLjfzqkServiceImpl implements ISjLjfzqkService 
{
    @Autowired
    private SjLjfzqkMapper sjLjfzqkMapper;

    @Autowired
    private SjDjjcMapper sjDjjcMapper;


    @Autowired
    private DjdcService djdcService;

    /**
     * 查询邻井复杂情况
     * 
     * @param id 邻井复杂情况主键
     * @return 邻井复杂情况
     */
    @Override
    public SjLjfzqk selectSjLjfzqkById(Long id)
    {
        return sjLjfzqkMapper.selectSjLjfzqkById(id);
    }

    /**
     * 查询邻井复杂情况列表
     * 
     * @param sjLjfzqk 邻井复杂情况
     * @return 邻井复杂情况
     */
    @Override
    public List<SjLjfzqk> selectSjLjfzqkList(SjLjfzqk sjLjfzqk)
    {
        return sjLjfzqkMapper.selectSjLjfzqkList(sjLjfzqk);
    }

    /**
     * 新增邻井复杂情况
     * 
     * @param sjLjfzqk 邻井复杂情况
     * @return 结果
     */
    @Override
    public int insertSjLjfzqk(SjLjfzqk sjLjfzqk)
    {
        return sjLjfzqkMapper.insertSjLjfzqk(sjLjfzqk);
    }

    /**
     * 修改邻井复杂情况
     * 
     * @param sjLjfzqk 邻井复杂情况
     * @return 结果
     */
    @Override
    public int updateSjLjfzqk(SjLjfzqk sjLjfzqk)
    {
        return sjLjfzqkMapper.updateSjLjfzqk(sjLjfzqk);
    }

    /**
     * 批量删除邻井复杂情况
     * 
     * @param ids 需要删除的邻井复杂情况主键
     * @return 结果
     */
    @Override
    public int deleteSjLjfzqkByIds(Long[] ids)
    {
        return sjLjfzqkMapper.deleteSjLjfzqkByIds(ids);
    }

    /**
     * 删除邻井复杂情况信息
     * 
     * @param id 邻井复杂情况主键
     * @return 结果
     */
    @Override
    public int deleteSjLjfzqkById(Long id)
    {
        return sjLjfzqkMapper.deleteSjLjfzqkById(id);
    }

    @Override
    public int insertSjLjfzqkBatch(List<SjLjjw> list) {
        String jh = list.get(0).getJh();
        SjDjjc sjDjjc = sjDjjcMapper.selectSjDjjcByJh(jh);
        //先删除已有数据
        sjLjfzqkMapper.deleteSjLjfzqkByJh(sjDjjc.getId());
        CommonParam param1 = new CommonParam();
        param1.setJh(String.join(",",list.stream().map(SjLjjw::getLjjh).collect(Collectors.toList())));
        //复杂情况
        param1.setJkhzb(sjDjjc.getJkhzb());
        param1.setJkzzb(sjDjjc.getJkzzb());
        List<SjLjfzqk> jsqaList=djdcService.getJsqaListByJh(param1);
        if(jsqaList.size()>0){
            jsqaList.forEach(it->{
                it.setZbid(sjDjjc.getId());
                String fzqk = it.getFzqk() != null ? it.getFzqk() : "";
                String fsjgjyy = it.getFsjgjyy() != null ? it.getFsjgjyy() : "";
                it.setFzqk(fzqk + "：" + fsjgjyy);
            });
            sjLjfzqkMapper.insertSjLjfzqkBatch(jsqaList);
        }

        return 1;
    }

    @Override
    public int insertSjLjfzqkBatch2(List<SjLjfzqk> list) {
        String jh = list.get(0).getJh();
        SjDjjc sjDjjc = sjDjjcMapper.selectSjDjjcByJh(jh);
        //先删除已有数据
        sjLjfzqkMapper.deleteSjLjfzqkByJh(sjDjjc.getId());
        return sjLjfzqkMapper.insertSjLjfzqkBatch(list);
    }
}
