package org.jeecg.modules.fbsBasedate.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.jeecg.modules.fbsBasedate.entity.FbsTechnologicalProcess;
import org.jeecg.modules.fbsBasedate.entity.FbsTechnologicalProcessItem;
import org.jeecg.modules.fbsBasedate.mapper.FbsTechnologicalProcessItemMapper;
import org.jeecg.modules.fbsBasedate.mapper.FbsTechnologicalProcessMapper;
import org.jeecg.modules.fbsBasedate.service.IFbsTechnologicalProcessItemService;
import org.jeecg.modules.fbsBasedate.service.IFbsTechnologicalProcessService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.util.List;

/**
 * @Description: 工艺流程档案
 * @author： jeecg-boot
 * @date：   2021-01-14
 * @version： V1.0
 */
@Service
public class FbsTechnologicalProcessServiceImpl extends ServiceImpl<FbsTechnologicalProcessMapper, FbsTechnologicalProcess> implements IFbsTechnologicalProcessService {
    @Autowired
    private FbsTechnologicalProcessMapper fbsTechnologicalProcessMapper;
    @Autowired
    private FbsTechnologicalProcessItemMapper fbsTechnologicalProcessItemMapper;
    @Autowired
    private IFbsTechnologicalProcessItemService iFbsTechnologicalProcessItemService;
    @Override
    public void addAll(FbsTechnologicalProcess fbsTechnologicalProcess) {
        //新增主表
        fbsTechnologicalProcessMapper.insert(fbsTechnologicalProcess);
        //新增子表数据
        if(fbsTechnologicalProcess.getFbsTechnologicalProcessItemList()!=null){
            addItem(fbsTechnologicalProcess);
        }
    }

    public void addItem(FbsTechnologicalProcess fbsTechnologicalProcess){
        for(FbsTechnologicalProcessItem fbsTechnologicalProcessItem:fbsTechnologicalProcess.getFbsTechnologicalProcessItemList()){
            fbsTechnologicalProcessItem.setId(null);
            fbsTechnologicalProcessItem.setFbsTechnologicalProcessId(fbsTechnologicalProcess.getId());
            fbsTechnologicalProcessItem.setCreateBy(null);
            fbsTechnologicalProcessItem.setCreateTime(null);
        }
        iFbsTechnologicalProcessItemService.saveBatch(fbsTechnologicalProcess.getFbsTechnologicalProcessItemList());
    }

    @Override
    public void updateAll(FbsTechnologicalProcess fbsTechnologicalProcess) {
        fbsTechnologicalProcessMapper.updateById(fbsTechnologicalProcess);
        if(fbsTechnologicalProcess.getFbsTechnologicalProcessItemList()!=null&&fbsTechnologicalProcess.getFbsTechnologicalProcessItemList().size()>0){
            //修改条件
            LambdaQueryWrapper<FbsTechnologicalProcessItem> lambdaQueryWrapper=new LambdaQueryWrapper<>();
            lambdaQueryWrapper.eq(FbsTechnologicalProcessItem::getDelFlag,"0");
            lambdaQueryWrapper.eq(FbsTechnologicalProcessItem::getFbsTechnologicalProcessId,fbsTechnologicalProcess.getId());
            //修改的值
            FbsTechnologicalProcessItem fbsTechnologicalProcessItem=new FbsTechnologicalProcessItem();
            fbsTechnologicalProcessItem.setDelFlag("1");
            fbsTechnologicalProcessItemMapper.update(fbsTechnologicalProcessItem,lambdaQueryWrapper);
            //新增明细
            addItem(fbsTechnologicalProcess);
        }
    }

    @Override
    public IPage<FbsTechnologicalProcess> findByPage(IPage<FbsTechnologicalProcess> page, QueryWrapper<FbsTechnologicalProcess> queryWrapper) {
        return this.baseMapper.findByPage(page,queryWrapper);
    }

    @Override
    public List<FbsTechnologicalProcess> getExport(FbsTechnologicalProcess fbsTechnologicalProcess) {
        return this.baseMapper.getExport(fbsTechnologicalProcess);
    }
}
