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

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.jeecg.modules.projectManagement.entity.FdContract;
import org.jeecg.modules.sampleAndConsumableManagement.entity.FdStandardProducts;
import org.jeecg.modules.sampleAndConsumableManagement.entity.FdStandardProductsRecipientRecord;
import org.jeecg.modules.sampleAndConsumableManagement.mapper.FdStandardProductsMapper;
import org.jeecg.modules.projectManagement.service.IFdProjectFileService;
import org.jeecg.modules.sampleAndConsumableManagement.service.IFdStandardProductsRecipientRecordService;
import org.jeecg.modules.sampleAndConsumableManagement.service.IFdStandardProductsService;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Description: 标准品管理
 * @author： jeecg-boot
 * @date： 2019-06-12
 * @version： V1.0
 */
@Service
@Transactional
public class FdStandardProductsServiceImpl extends ServiceImpl<FdStandardProductsMapper, FdStandardProducts> implements IFdStandardProductsService {

    @Resource
    IFdStandardProductsService fdStandardProductsService;

    @Resource
    IFdStandardProductsRecipientRecordService fdStandardProductsRecipientRecordService;

    @Resource
    FdStandardProductsMapper fdStandardProductsMapper;


    /**
     * @return void
     * @Author gurui
     * @Description //TODO 添加标准品管理 同时添加 标准品管理领用记录
     * @Date 13:53 2019/6/12
     * @Param [fdStandardProducts]
     **/
    @Override
    public void addAll(FdStandardProducts fdStandardProducts) {
        if (fdStandardProducts.getId() == null || fdStandardProducts.getId().equals("")){
            fdStandardProducts.setDelFlag("0");
            fdStandardProductsService.save(fdStandardProducts);
        }else{
            fdStandardProductsService.updateById(fdStandardProducts);
            //删除标准品管理领用记录
            LambdaQueryWrapper<FdStandardProductsRecipientRecord> queryWrapper2 = new LambdaQueryWrapper<>();
            queryWrapper2.eq(FdStandardProductsRecipientRecord::getPkStandardProducts,fdStandardProducts.getId());
            fdStandardProductsRecipientRecordService.remove(queryWrapper2);
        }
        for (FdStandardProductsRecipientRecord fdStandardProductsRecipientRecord:fdStandardProducts.getFdStandardProductsRecipientRecordList()){
            fdStandardProductsRecipientRecord.setId("");
            fdStandardProductsRecipientRecord.setPkStandardProducts(fdStandardProducts.getId());
        }
        //批量添加
        fdStandardProductsRecipientRecordService.saveBatch(fdStandardProducts.getFdStandardProductsRecipientRecordList());

    }

    @Override
    public FdStandardProducts queryStandardByConId(String id) {
        return fdStandardProductsMapper.queryStandardByConId(id);
    }

    @Override
    public boolean deleteAll(String id) {
        boolean result = true;
        //删除工时记录
        try {
            fdStandardProductsService.removeById(id);
            LambdaQueryWrapper<FdStandardProductsRecipientRecord> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(FdStandardProductsRecipientRecord::getPkStandardProducts,id);
            fdStandardProductsRecipientRecordService.remove(queryWrapper);
        }catch (Exception e){
            result=false;
            e.printStackTrace();
        }
        return result;
    }

    @Override
    public List<FdContract> getFileContractList() {
        return fdStandardProductsMapper.getFileContractList();
    }
}
