package com.ym.mec.biz.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ym.mec.biz.dal.dao.TenantProductInfoDao;
import com.ym.mec.biz.dal.dto.TenantProductInfoDto;
import com.ym.mec.biz.dal.entity.TenantProductInfo;
import com.ym.mec.biz.service.PlatformServeDetailService;
import com.ym.mec.biz.service.PlatformServeService;
import com.ym.mec.biz.service.TenantProductInfoService;
import com.ym.mec.common.exception.BizException;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Optional;

/**
 * @author hgw
 * Created by 2021-12-02
 */
@Service
public class TenantProductInfoServiceImpl extends ServiceImpl<TenantProductInfoDao, TenantProductInfo> implements TenantProductInfoService {

    @Autowired
    private PlatformServeService platformServeService;
    @Autowired
    private PlatformServeDetailService platformServeDetailService;

    /**
     * 添加机构选购的产品信息
     */
    @Override
    public void addTenantProduct(TenantProductInfoDto dto) {
        checkServe(dto.getServeId(), dto.getServeDetailId());
        TenantProductInfo info = new TenantProductInfo();
        info.setUsing(0);
        BeanUtils.copyProperties(dto, info);
        baseMapper.insert(info);
    }

    @Override
    public void updateTenantProduct(TenantProductInfoDto dto) {
        checkServe(dto.getServeId(), dto.getServeDetailId());
        TenantProductInfo info = new TenantProductInfo();
        BeanUtils.copyProperties(dto, info);
        baseMapper.updateById(info);
    }

    /**
     * 根据机构id修改正在使用的机构产品信息
     */
    @Override
    public void updateByTenantId(TenantProductInfo obj) {
        baseMapper.updateByTenantId(obj);
    }

    private void checkServe(Integer id, Integer detailId) {
        Optional.ofNullable(id)
                .map(platformServeService::getById)
                .orElseThrow(() -> new BizException("未查询到该服务！"));

        Optional.ofNullable(detailId)
                .map(platformServeDetailService::getById)
                .orElseThrow(() -> new BizException("未查询到该服务模式！"));
    }


}
