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

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

import com.zjsgfa.common.utils.bean.BeanUtils;
import com.zjsgfa.project.zjsgfa.domain.SjDjjc;
import com.zjsgfa.project.zjsgfa.domain.SjLjfzqk;
import com.zjsgfa.project.zjsgfa.domain.SjZtgjsj;
import com.zjsgfa.project.zjsgfa.mapper.SjDjjcMapper;
import com.zjsgfa.project.zjsgfa.mapper.SjLjfzqkMapper;
import com.zjsgfa.project.zjsgfa.mapper.SjZtgjsjMapper;
import com.zjsgfa.project.zt.domain.CommonParam;
import com.zjsgfa.project.zt.domain.DjZqsjfx;
import com.zjsgfa.project.zt.domain.Djjc;
import com.zjsgfa.project.zt.domain.Jsqa;
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.SjLjjwMapper;
import com.zjsgfa.project.zjsgfa.domain.SjLjjw;
import com.zjsgfa.project.zjsgfa.service.ISjLjjwService;

/**
 * 设计-邻井井位Service业务层处理
 * 
 * @author
 * @date 2025-07-22
 */
@Service
public class SjLjjwServiceImpl implements ISjLjjwService 
{
    @Autowired
    private SjLjjwMapper sjLjjwMapper;

    @Autowired
    private SjDjjcMapper sjDjjcMapper;
    @Autowired
    private DjdcService djdcService;

    @Autowired
    private SjZtgjsjMapper sjZtgjsjMapper;


    @Autowired
    private SjLjfzqkMapper sjLjfzqkMapper;

    /**
     * 查询设计-邻井井位
     * 
     * @param id 设计-邻井井位主键
     * @return 设计-邻井井位
     */
    @Override
    public SjLjjw selectSjLjjwById(Long id)
    {
        return sjLjjwMapper.selectSjLjjwById(id);
    }

    /**
     * 查询设计-邻井井位列表
     * 
     * @param sjLjjw 设计-邻井井位
     * @return 设计-邻井井位
     */
    @Override
    public List<SjLjjw> selectSjLjjwList(SjLjjw sjLjjw)
    {
        return sjLjjwMapper.selectSjLjjwList(sjLjjw);
    }

    /**
     * 新增设计-邻井井位
     * 
     * @param sjLjjw 设计-邻井井位
     * @return 结果
     */
    @Override
    public int insertSjLjjw(SjLjjw sjLjjw)
    {
        return sjLjjwMapper.insertSjLjjw(sjLjjw);
    }

    /**
     * 修改设计-邻井井位
     * 
     * @param sjLjjw 设计-邻井井位
     * @return 结果
     */
    @Override
    public int updateSjLjjw(SjLjjw sjLjjw)
    {
        return sjLjjwMapper.updateSjLjjw(sjLjjw);
    }

    /**
     * 批量删除设计-邻井井位
     * 
     * @param ids 需要删除的设计-邻井井位主键
     * @return 结果
     */
    @Override
    public int deleteSjLjjwByIds(Long[] ids)
    {
        return sjLjjwMapper.deleteSjLjjwByIds(ids);
    }

    /**
     * 删除设计-邻井井位信息
     * 
     * @param id 设计-邻井井位主键
     * @return 结果
     */
    @Override
    public int deleteSjLjjwById(Long id)
    {
        return sjLjjwMapper.deleteSjLjjwById(id);
    }

    @Override
    public int insertSjLjjwBatch(List<SjLjjw> list) {
        String jh = list.get(0).getJh();
        SjLjjw sjLjjw=new SjLjjw();
        sjLjjw.setJh(jh);
        sjLjjw.setLb1(list.get(0).getLb1());


        //钻头数据信息
        List<SjLjjw> sjLjjws = sjLjjwMapper.selectSjLjjwList(sjLjjw);

        Set<String> jhSet = sjLjjws.stream()
                .map(SjLjjw::getLjjh)
                .collect(Collectors.toSet());
        List<SjLjjw> difference = list.stream()
                .filter(item -> !jhSet.contains(item.getLjjh()))
                .collect(Collectors.toList());




        if(difference.size()>0){
            int i = sjLjjwMapper.insertSjLjjwBatch(difference);
            return i;
        }else {
            return 1;
        }




    }

    @Override
    public int deleteSjLjjwByJh(String jh) {
        return sjLjjwMapper.deleteSjLjjwByJh(jh);
    }

    @Override
    public int insertSjLjjwBatch2(List<SjLjjw> list) throws Exception {
        String jh = list.get(0).getJh();
        SjLjjw sjLjjw=new SjLjjw();
        sjLjjw.setJh(jh);
        sjLjjw.setLb1(list.get(0).getLb1());

//        List<SjLjjw> sjLjjws = sjLjjwMapper.selectSjLjjwList(sjLjjw);

//        Set<String> jhSet = sjLjjws.stream()
//                .map(SjLjjw::getLjjh)
//                .collect(Collectors.toSet());
//        List<SjLjjw> difference = list.stream()
//                .filter(item -> !jhSet.contains(item.getLjjh()))
//                .collect(Collectors.toList());
        SjDjjc sjDjjc = sjDjjcMapper.selectSjDjjcByJh(jh);
        //钻头数据信息
        CommonParam param1 = new CommonParam();
        param1.setJh(String.join(",",list.stream().map(SjLjjw::getLjjh).collect(Collectors.toList())));
        List<Djjc> djjcList = djdcService.getDjjcList(param1);
        List<SjZtgjsj> sjZtgjsjList=new ArrayList<>();

        djjcList.forEach(item->{
            SjZtgjsj sjZtgjsj=new SjZtgjsj();
            BeanUtils.copyProperties(item,sjZtgjsj);
            sjZtgjsj.setJh(jh);
            sjZtgjsj.setLjjh(item.getJh());
            sjZtgjsj.setSy(item.getPz());
            sjZtgjsj.setZjymd(item.getZjymd()!=null?item.getZjymd().toString():"");
            String kc  =item.getKc();
            if(kc.equals("1")){
                kc="一开";
            }else if(kc.equals("2")){
                kc="二开";
            }else if(kc.equals("3")){
                kc="三开";
            }else if(kc.equals("4")){
                kc="四开";
            }else if(kc.equals("5")){
                kc="五开";
            }
            sjZtgjsj.setKc(kc);
            sjZtgjsjList.add(sjZtgjsj);
        });
        if(sjZtgjsjList.size()>0){
            sjZtgjsjMapper.deleteSjZtgjsjByJh(jh);
            sjZtgjsjMapper.insertSjZtgjsjBatch(sjZtgjsjList);
        }
//        //复杂情况
//        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());});
//            sjLjfzqkMapper.deleteSjLjfzqkByJh(sjDjjc.getId());
//            sjLjfzqkMapper.insertSjLjfzqkBatch(jsqaList);
//        }



        if(list.size()>0){
            //删除
            sjLjjwMapper.deleteSjLjjwByJh(jh);
            int i = sjLjjwMapper.insertSjLjjwBatch(list);
            return i;
        }else {
            return 1;
        }

    }
}
