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

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

import com.zjsgfa.project.zjsgfa.domain.SjDjjc;
import com.zjsgfa.project.zjsgfa.domain.SjLjfzqk;
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.SjLjzjyfzqkMapper;
import com.zjsgfa.project.zjsgfa.domain.SjLjzjyfzqk;
import com.zjsgfa.project.zjsgfa.service.ISjLjzjyfzqkService;

/**
 * 邻井复杂情况-钻井液Service业务层处理
 * 
 * @author ruoyi
 * @date 2026-02-24
 */
@Service
public class SjLjzjyfzqkServiceImpl implements ISjLjzjyfzqkService 
{
    @Autowired
    private SjLjzjyfzqkMapper sjLjzjyfzqkMapper;



    @Autowired
    private SjDjjcMapper sjDjjcMapper;


    @Autowired
    private DjdcService djdcService;

    /**
     * 查询邻井复杂情况-钻井液
     * 
     * @param id 邻井复杂情况-钻井液主键
     * @return 邻井复杂情况-钻井液
     */
    @Override
    public SjLjzjyfzqk selectSjLjzjyfzqkById(Long id)
    {
        return sjLjzjyfzqkMapper.selectSjLjzjyfzqkById(id);
    }

    /**
     * 查询邻井复杂情况-钻井液列表
     * 
     * @param sjLjzjyfzqk 邻井复杂情况-钻井液
     * @return 邻井复杂情况-钻井液
     */
    @Override
    public List<SjLjzjyfzqk> selectSjLjzjyfzqkList(SjLjzjyfzqk sjLjzjyfzqk)
    {
        return sjLjzjyfzqkMapper.selectSjLjzjyfzqkList(sjLjzjyfzqk);
    }

    /**
     * 新增邻井复杂情况-钻井液
     * 
     * @param sjLjzjyfzqk 邻井复杂情况-钻井液
     * @return 结果
     */
    @Override
    public int insertSjLjzjyfzqk(SjLjzjyfzqk sjLjzjyfzqk)
    {
        return sjLjzjyfzqkMapper.insertSjLjzjyfzqk(sjLjzjyfzqk);
    }

    /**
     * 修改邻井复杂情况-钻井液
     * 
     * @param sjLjzjyfzqk 邻井复杂情况-钻井液
     * @return 结果
     */
    @Override
    public int updateSjLjzjyfzqk(SjLjzjyfzqk sjLjzjyfzqk)
    {
        return sjLjzjyfzqkMapper.updateSjLjzjyfzqk(sjLjzjyfzqk);
    }

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

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

    @Override
    public int insertSjLjzjyfzqkBatch(List<SjLjjw> list) {

        String jh = list.get(0).getJh();
        SjDjjc sjDjjc = sjDjjcMapper.selectSjDjjcByJh(jh);
        //先删除已有数据
        sjLjzjyfzqkMapper.deleteSjLjzjyfzqkByZbid(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());});
            sjLjzjyfzqkMapper.insertSjLjzjyfzqkBatch(jsqaList);
        }

        return 1;
    }

    @Override
    public int pladd3(List<SjLjzjyfzqk> list) {
        String jh = list.get(0).getJh();
        SjDjjc sjDjjc = sjDjjcMapper.selectSjDjjcByJh(jh);
        //先删除已有数据
        sjLjzjyfzqkMapper.deleteSjLjzjyfzqkByZbid(sjDjjc.getId());
        sjLjzjyfzqkMapper.insertSjLjzjyfzqkBatch2(list);
        return 1;
    }
}
