package com.qianhe.service.impl;

import java.util.List;
import com.qianhe.common.utils.DateUtils;
import com.qianhe.domain.BzscYsjglggzsc;
import com.qianhe.domain.BzscYsjglggzscCb;
import com.qianhe.domain.BzscYsjglggzscZb;
import com.qianhe.mapper.BzscYsjglggzscCbMapper;
import com.qianhe.mapper.BzscYsjglggzscMapper;
import com.qianhe.mapper.BzscYsjglggzscZbMapper;
import com.qianhe.service.IBzscYsjglggzscService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.qianhe.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;


/**
 * 标准手册-油水井管理岗工作手册-总Service业务层处理
 *
 * @author qianhe
 * @date 2024-10-09
 */
@Service
public class BzscYsjglggzscServiceImpl implements IBzscYsjglggzscService
{
    @Autowired
    private BzscYsjglggzscMapper bzscYsjglggzscMapper;

    @Autowired
    private BzscYsjglggzscZbMapper bzscYsjglggzscZbMapper;

    @Autowired
    private BzscYsjglggzscCbMapper bzscYsjglggzscCbMapper;
    /**
     * 查询标准手册-油水井管理岗工作手册-总
     *
     * @param ID 标准手册-油水井管理岗工作手册-总主键
     * @return 标准手册-油水井管理岗工作手册-总
     */
    @Override
    public BzscYsjglggzsc selectBzscYsjglggzscByID(Long ID)
    {
        BzscYsjglggzsc bzscYsjglggzsc = bzscYsjglggzscMapper.selectBzscYsjglggzscByID(ID);
        BzscYsjglggzscZb bzscYsjglggzscZb = new BzscYsjglggzscZb();
        bzscYsjglggzscZb.setZbId(ID);
        List<BzscYsjglggzscZb> bzscYsjglggzscZbslist = new ArrayList<>();
        List<BzscYsjglggzscZb> bzscYsjglggzscZbs = bzscYsjglggzscZbMapper.selectBzscYsjglggzscZbList(bzscYsjglggzscZb);

        for(BzscYsjglggzscZb bzscYsjglggzscZb1 : bzscYsjglggzscZbs){
            BzscYsjglggzscCb bzscYsjglggzsccb = new BzscYsjglggzscCb();
            bzscYsjglggzsccb.setZbId(bzscYsjglggzscZb1.getID());
            List<BzscYsjglggzscCb> bzscYsjglggzscCbs = bzscYsjglggzscCbMapper.selectBzscYsjglggzscCbList(bzscYsjglggzsccb);
            bzscYsjglggzscZb1.setBzscYsjglggzscCbList(bzscYsjglggzscCbs);
            bzscYsjglggzscZbslist.add(bzscYsjglggzscZb1);
        }


        bzscYsjglggzsc.setBzscYsjglggzscZbList(bzscYsjglggzscZbslist);
        return bzscYsjglggzsc;
    }

    /**
     * 查询标准手册-油水井管理岗工作手册-总列表
     *
     * @param bzscYsjglggzsc 标准手册-油水井管理岗工作手册-总
     * @return 标准手册-油水井管理岗工作手册-总
     */
    @Override
    public List<BzscYsjglggzsc> selectBzscYsjglggzscList(BzscYsjglggzsc bzscYsjglggzsc)
    {
        return bzscYsjglggzscMapper.selectBzscYsjglggzscList(bzscYsjglggzsc);
    }

    /**
     * 新增标准手册-油水井管理岗工作手册-总
     *
     * @param bzscYsjglggzsc 标准手册-油水井管理岗工作手册-总
     * @return 结果
     */
    @Transactional
    @Override
    public int insertBzscYsjglggzsc(BzscYsjglggzsc bzscYsjglggzsc)
    {
        bzscYsjglggzsc.setCreateTime(DateUtils.getNowDate());
        int rows = bzscYsjglggzscMapper.insertBzscYsjglggzsc(bzscYsjglggzsc);
        insertBzscYsjglggzscZb(bzscYsjglggzsc);
        return rows;
    }



    /**
     * 修改标准手册-油水井管理岗工作手册-总
     *
     * @param bzscYsjglggzsc 标准手册-油水井管理岗工作手册-总
     * @return 结果
     */
    @Transactional
    @Override
    public int updateBzscYsjglggzsc(BzscYsjglggzsc bzscYsjglggzsc)
    {
        bzscYsjglggzsc.setUpdateTime(DateUtils.getNowDate());
        BzscYsjglggzscZb bzscYsjglggzscZb = new BzscYsjglggzscZb();
        bzscYsjglggzscZb.setZbId(bzscYsjglggzsc.getID());
        List<BzscYsjglggzscZb> bzscYsjglggzscZbs = bzscYsjglggzscZbMapper.selectBzscYsjglggzscZbList(bzscYsjglggzscZb);
        for(BzscYsjglggzscZb bzscYsjglggzscZb1 : bzscYsjglggzscZbs){
            bzscYsjglggzscZbMapper.deleteBzscYsjglggzscCbByZbId(bzscYsjglggzscZb1.getID());
        }

        bzscYsjglggzscMapper.deleteBzscYsjglggzscZbByZbId(bzscYsjglggzsc.getID());
        insertBzscYsjglggzscZb(bzscYsjglggzsc);
        return bzscYsjglggzscMapper.updateBzscYsjglggzsc(bzscYsjglggzsc);
    }

    /**
     * 批量删除标准手册-油水井管理岗工作手册-总
     *
     * @param IDs 需要删除的标准手册-油水井管理岗工作手册-总主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteBzscYsjglggzscByIDs(Long[] IDs)
    {

        bzscYsjglggzscMapper.deleteBzscYsjglggzscZbByZbIds(IDs);
        return bzscYsjglggzscMapper.deleteBzscYsjglggzscByIDs(IDs);
    }

    /**
     * 删除标准手册-油水井管理岗工作手册-总信息
     *
     * @param ID 标准手册-油水井管理岗工作手册-总主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteBzscYsjglggzscByID(Long ID)
    {
        BzscYsjglggzscZb bzscYsjglggzscZb = new BzscYsjglggzscZb();
        bzscYsjglggzscZb.setZbId(ID);
        List<BzscYsjglggzscZb> bzscYsjglggzscZbs = bzscYsjglggzscZbMapper.selectBzscYsjglggzscZbList(bzscYsjglggzscZb);
        for(BzscYsjglggzscZb bzscYsjglggzscZb1 : bzscYsjglggzscZbs){
            bzscYsjglggzscZbMapper.deleteBzscYsjglggzscCbByZbId(bzscYsjglggzscZb1.getID());
        }
        bzscYsjglggzscMapper.deleteBzscYsjglggzscZbByZbId(ID);
        return bzscYsjglggzscMapper.deleteBzscYsjglggzscByID(ID);
    }

    /**
     * 新增标准手册-油水井管理岗工作手册-主信息
     *
     * @param bzscYsjglggzsc 标准手册-油水井管理岗工作手册-总对象
     */
    public void insertBzscYsjglggzscZb(BzscYsjglggzsc bzscYsjglggzsc)
    {
        List<BzscYsjglggzscZb> bzscYsjglggzscZbList = bzscYsjglggzsc.getBzscYsjglggzscZbList();
        Long ID = bzscYsjglggzsc.getID();
        if (StringUtils.isNotNull(bzscYsjglggzscZbList))
        {
            List<BzscYsjglggzscZb> list = new ArrayList<BzscYsjglggzscZb>();
            for (BzscYsjglggzscZb bzscYsjglggzscZb : bzscYsjglggzscZbList)
            {

                bzscYsjglggzscZb.setZbId(ID);
                bzscYsjglggzscZb.setCreateTime(DateUtils.getNowDate());

                int rows = bzscYsjglggzscZbMapper.insertBzscYsjglggzscZb(bzscYsjglggzscZb);

                    insertBzscYsjglggzscCb(bzscYsjglggzscZb);


            }
//            if (list.size() > 0)
//            {
//                bzscYsjglggzscMapper.batchBzscYsjglggzscZb(list);
//            }
        }
    }

    /**
     * 新增标准手册-油水井管理岗工作手册-主
     *
     * @param bzscYsjglggzscZb 标准手册-油水井管理岗工作手册-主
     * @return 结果
     */
    public void insertBzscYsjglggzscZbcb(BzscYsjglggzscZb bzscYsjglggzscZb)
    {
        bzscYsjglggzscZb.setCreateTime(DateUtils.getNowDate());
        bzscYsjglggzscZbMapper.insertBzscYsjglggzscZb(bzscYsjglggzscZb);
        insertBzscYsjglggzscCb(bzscYsjglggzscZb);
    }

    /**
     * 新增标准手册-油水井管理岗工作手册-从信息
     *
     * @param bzscYsjglggzscZb 标准手册-油水井管理岗工作手册-主对象
     */
    public void insertBzscYsjglggzscCb(BzscYsjglggzscZb bzscYsjglggzscZb)
    {
        List<BzscYsjglggzscCb> bzscYsjglggzscCbList = bzscYsjglggzscZb.getBzscYsjglggzscCbList();
        Long ID = bzscYsjglggzscZb.getID();
        if (StringUtils.isNotNull(bzscYsjglggzscCbList))
        {
            List<BzscYsjglggzscCb> list = new ArrayList<BzscYsjglggzscCb>();
            for (BzscYsjglggzscCb bzscYsjglggzscCb : bzscYsjglggzscCbList)
            {
                bzscYsjglggzscCb.setZbId(ID);
                bzscYsjglggzscCb.setCreateTime(DateUtils.getNowDate());
                list.add(bzscYsjglggzscCb);
            }
            if (list.size() > 0)
            {
                bzscYsjglggzscZbMapper.batchBzscYsjglggzscCb(list);
            }
        }
    }






}
