package com.kym.service.admin.impl;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.github.pagehelper.PageHelper;
import com.kym.common.utils.CommUtil;
import com.kym.entity.admin.Platform;
import com.kym.entity.admin.queryParams.CommonQueryParam;
import com.kym.entity.common.PageBean;
import com.kym.mapper.admin.PlatformMapper;
import com.kym.service.admin.PlatformService;
import com.kym.service.mybatisplus.MyBaseServiceImpl;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 互联互通平台配置 服务实现类
 * </p>
 *
 * @author skyline
 */
@Service
@DS("db-admin")
public class PlatformServiceImpl extends MyBaseServiceImpl<PlatformMapper, Platform> implements PlatformService {

    @Value("${spring.profiles.active}")
    private String env;

    @Override
    public PageBean<Platform> listPlatform(CommonQueryParam params) {
        PageHelper.startPage(params.getPageNum(), params.getPageSize());
        var res = lambdaQuery().eq(Platform::getEnv, env).list();
        return new PageBean<>(res);
    }

    @Override
    public long createPlatform(Platform platform) {
        CommUtil.assertsNonNull(platform.getPlatformName(), "平台名称不能为空");
        CommUtil.assertsNonNull(platform.getCompanyName(), "公司名称能为空");
        Long count = lambdaQuery().eq(Platform::getPlatformName, platform.getPlatformName()).count();
        CommUtil.asserts(count == 0, "平台已存在");
        save(platform);
        return platform.getId();
    }

    @Override
    public void updatePlatform(Platform platform) {
        CommUtil.assertsNonNull(platform.getPlatformName(), "平台名称不能为空");
        CommUtil.assertsNonNull(platform.getCompanyName(), "公司名称能为空");
        Long count = lambdaQuery().eq(Platform::getPlatformName, platform.getPlatformName()).ne(Platform::getId, platform.getId()).count();
        CommUtil.asserts(count == 0, "平台已存在");
        updateById(platform);
    }

    @Override
    public Platform detail(long id) {
        return getById(id);
    }

    @Override
    public void removePlatform(long id) {
        //

    }
}
