package com.hotwj.platform.resourceManagement.company.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hotwj.platform.resourceManagement.company.domain.HotCompanyThirdPlatform;
import com.hotwj.platform.resourceManagement.company.domain.bo.HotCompanyThirdPlatformBo;
import com.hotwj.platform.resourceManagement.company.domain.vo.HotCompanyThirdPlatformVo;
import com.hotwj.platform.resourceManagement.company.mapper.HotCompanyThirdPlatformMapper;
import com.hotwj.platform.resourceManagement.company.service.IHotCompanyThirdPlatformService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * 公司第三方平台账号Service业务层处理
 *
 * @author shihongwei
 * @date 2025-12-08
 */
@Slf4j
@RequiredArgsConstructor
@Service
public class HotCompanyThirdPlatformServiceImpl implements IHotCompanyThirdPlatformService {

    private final HotCompanyThirdPlatformMapper baseMapper;

    /**
     * 查询公司第三方平台账号
     *
     * @param id 主键
     * @return 公司第三方平台账号
     */
    @Override
    public HotCompanyThirdPlatformVo queryById(Long id) {
        return baseMapper.selectVoById(id);
    }

    /**
     * 分页查询公司第三方平台账号列表
     *
     * @param bo        查询条件
     * @param pageQuery 分页参数
     * @return 公司第三方平台账号分页列表
     */
    @Override
    public TableDataInfo<HotCompanyThirdPlatformVo> queryPageList(HotCompanyThirdPlatformBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<HotCompanyThirdPlatform> lqw = buildQueryWrapper(bo);
        Page<HotCompanyThirdPlatformVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询符合条件的公司第三方平台账号列表
     *
     * @param bo 查询条件
     * @return 公司第三方平台账号列表
     */
    @Override
    public List<HotCompanyThirdPlatformVo> queryList(HotCompanyThirdPlatformBo bo) {
        LambdaQueryWrapper<HotCompanyThirdPlatform> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<HotCompanyThirdPlatform> buildQueryWrapper(HotCompanyThirdPlatformBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<HotCompanyThirdPlatform> lqw = Wrappers.lambdaQuery();
        lqw.orderByAsc(HotCompanyThirdPlatform::getId);
        lqw.eq(bo.getCompanyId() != null, HotCompanyThirdPlatform::getCompanyId, bo.getCompanyId());
        lqw.like(StringUtils.isNotBlank(bo.getPlatformName()), HotCompanyThirdPlatform::getPlatformName, bo.getPlatformName());
        lqw.eq(StringUtils.isNotBlank(bo.getAccount()), HotCompanyThirdPlatform::getAccount, bo.getAccount());
        lqw.eq(StringUtils.isNotBlank(bo.getPassword()), HotCompanyThirdPlatform::getPassword, bo.getPassword());
        lqw.eq(StringUtils.isNotBlank(bo.getLoginUrl()), HotCompanyThirdPlatform::getLoginUrl, bo.getLoginUrl());
        lqw.eq(StringUtils.isNotBlank(bo.getSsoConfig()), HotCompanyThirdPlatform::getSsoConfig, bo.getSsoConfig());
        lqw.eq(bo.getIsDeleted() != null, HotCompanyThirdPlatform::getIsDeleted, bo.getIsDeleted());
        return lqw;
    }

    /**
     * 新增公司第三方平台账号
     *
     * @param bo 公司第三方平台账号
     * @return 是否新增成功
     */
    @Override
    public Boolean insertByBo(HotCompanyThirdPlatformBo bo) {
        HotCompanyThirdPlatform add = MapstructUtils.convert(bo, HotCompanyThirdPlatform.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改公司第三方平台账号
     *
     * @param bo 公司第三方平台账号
     * @return 是否修改成功
     */
    @Override
    public Boolean updateByBo(HotCompanyThirdPlatformBo bo) {
        HotCompanyThirdPlatform update = MapstructUtils.convert(bo, HotCompanyThirdPlatform.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(HotCompanyThirdPlatform entity) {
    }

    /**
     * 校验并批量删除公司第三方平台账号信息
     *
     * @param ids     待删除的主键集合
     * @param isValid 是否进行有效性校验
     * @return 是否删除成功
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if (isValid) {
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteByIds(ids) > 0;
    }
}
