package digital.laboratory.platform.reagent.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import digital.laboratory.platform.reagent.dto.SupplierInformationDTO;
import digital.laboratory.platform.reagent.entity.EvaluationForm;
import digital.laboratory.platform.reagent.entity.SupplierInformation;
import digital.laboratory.platform.reagent.mapper.SupplierInformationMapper;
import digital.laboratory.platform.reagent.service.EvaluationFormService;
import digital.laboratory.platform.reagent.service.ProvideServicesOrSuppliesService;
import digital.laboratory.platform.reagent.service.SupplierInformationService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * (服务商/供应商信息)服务实现类
 *
 * @author Zhang Xiaolong created at  2023-03-10
 * @describe (服务商 / 供应商信息) 服务实现类
 */
@Service
public class SupplierInformationServiceImpl extends ServiceImpl<SupplierInformationMapper, SupplierInformation> implements SupplierInformationService {

    @Autowired
    private SupplierInformationService supplierInformationService;

    @Autowired
    private EvaluationFormService evaluationFormService;

    @Autowired
    private ProvideServicesOrSuppliesService provideServicesOrSuppliesService;

    @Override//新增供应商信息
    @Transactional
    public Boolean addInfoById(SupplierInformationDTO supplierInformationDTO, SupplierInformation supplierInformation) {

        LambdaQueryWrapper<SupplierInformation> supplierInformationQueryWrapper = new LambdaQueryWrapper();

        List<SupplierInformation> list = supplierInformationService.list(supplierInformationQueryWrapper);

        for (SupplierInformation information : list) {
            if (information.getSupplierName().equals(supplierInformationDTO.getSupplierName())) {
                throw new RuntimeException(String.format("该供应商信息已存在"));
            }
        }

        BeanUtils.copyProperties(supplierInformationDTO, supplierInformation);

        supplierInformation.setSupplierInformationId(IdWorker.get32UUID().toUpperCase());

        EvaluationForm evaluationForm = evaluationFormService.addFormById(supplierInformation.getSupplierInformationId());

        provideServicesOrSuppliesService.addById(evaluationForm.getEvaluationFormId());

        return supplierInformationService.save(supplierInformation);
    }

    @Override
    public SupplierInformation editInfoById(SupplierInformationDTO supplierInformationDTO) {

        SupplierInformation byId = supplierInformationService.getById(supplierInformationDTO.getSupplierInformationId());

        BeanUtils.copyProperties(supplierInformationDTO, byId);

        if (supplierInformationService.updateById(byId)) {
            return byId;
        } else {
            return null;
        }
    }

}
