package org.dromara.payment.merchant.service.impl;

import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.mybatis.core.page.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.dromara.common.satoken.utils.LoginHelper;
import org.springframework.stereotype.Service;
import org.dromara.payment.merchant.domain.bo.BusMerchantProductSydBo;
import org.dromara.payment.merchant.domain.vo.BusMerchantProductSydVo;
import org.dromara.payment.merchant.domain.BusMerchantProductSyd;
import org.dromara.payment.merchant.mapper.BusMerchantProductSydMapper;
import org.dromara.payment.merchant.service.IBusMerchantProductSydService;

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

/**
 * 企业产品服务商Service业务层处理
 *
 * @author LionLi
 * @date 2024-04-25
 */
@RequiredArgsConstructor
@Service
public class BusMerchantProductSydServiceImpl implements IBusMerchantProductSydService {

    private final BusMerchantProductSydMapper baseMapper;

    /**
     * 查询企业产品服务商
     */
    @Override
    public BusMerchantProductSydVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询企业产品服务商列表
     */
    @Override
    public TableDataInfo<BusMerchantProductSydVo> queryPageList(BusMerchantProductSydBo bo, PageQuery pageQuery) {
        QueryWrapper<BusMerchantProductSyd> lqw = buildQueryWrapper(bo);
        Page<BusMerchantProductSydVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询企业产品服务商列表
     */
    @Override
    public List<BusMerchantProductSydVo> queryList(BusMerchantProductSydBo bo) {
        QueryWrapper<BusMerchantProductSyd> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private QueryWrapper<BusMerchantProductSyd> buildQueryWrapper(BusMerchantProductSydBo bo) {
        Map<String, Object> params = bo.getParams();
        QueryWrapper<BusMerchantProductSyd> lqw = new QueryWrapper<>();
        lqw.eq(bo.getMerId() != null, "mer_id", bo.getMerId());
        lqw.eq(bo.getSydId() != null, "syd_id", bo.getSydId());
        return lqw;
    }

    /**
     * 新增企业产品服务商
     */
    @Override
    public Boolean insertByBo(BusMerchantProductSydBo bo) {
        BusMerchantProductSyd add = MapstructUtils.convert(bo, BusMerchantProductSyd.class);
        validEntityBeforeSave(add);
        add.setCreateBy(LoginHelper.getUserId());
        add.setCreateTime(new Date());
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改企业产品服务商
     */
    @Override
    public Boolean updateByBo(BusMerchantProductSydBo bo) {
        BusMerchantProductSyd update = MapstructUtils.convert(bo, BusMerchantProductSyd.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(BusMerchantProductSyd entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除企业产品服务商
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
