package org.dromara.system.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.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.dromara.system.domain.bo.TblMiniprogramBo;
import org.dromara.system.domain.vo.TblMiniprogramVo;
import org.dromara.system.domain.TblMiniprogram;
import org.dromara.system.mapper.TblMiniprogramMapper;
import org.dromara.system.service.ITblMiniprogramService;

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

/**
 * 小程序信息Service业务层处理
 *
 * @author Lion Li
 * @date 2024-07-18
 */
@RequiredArgsConstructor
@Service
public class TblMiniprogramServiceImpl implements ITblMiniprogramService {

    private final TblMiniprogramMapper baseMapper;

    /**
     * 查询小程序信息
     */
    @Override
    public TblMiniprogramVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询小程序信息列表
     */
    @Override
    public TableDataInfo<TblMiniprogramVo> queryPageList(TblMiniprogramBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<TblMiniprogram> lqw = buildQueryWrapper(bo);
        Page<TblMiniprogramVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询小程序信息列表
     */
    @Override
    public List<TblMiniprogramVo> queryList(TblMiniprogramBo bo) {
        LambdaQueryWrapper<TblMiniprogram> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<TblMiniprogram> buildQueryWrapper(TblMiniprogramBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<TblMiniprogram> lqw = Wrappers.lambdaQuery();
        lqw.like(StringUtils.isNotBlank(bo.getName()), TblMiniprogram::getName, bo.getName());
        lqw.eq(StringUtils.isNotBlank(bo.getLogo()), TblMiniprogram::getLogo, bo.getLogo());
        lqw.eq(StringUtils.isNotBlank(bo.getUrl()), TblMiniprogram::getUrl, bo.getUrl());
        lqw.eq(bo.getStatus() != null, TblMiniprogram::getStatus, bo.getStatus());
        lqw.eq(StringUtils.isNotBlank(bo.getRemark()), TblMiniprogram::getRemark, bo.getRemark());
        lqw.eq(StringUtils.isNotBlank(bo.getExt1()), TblMiniprogram::getExt1, bo.getExt1());
        lqw.eq(StringUtils.isNotBlank(bo.getExt2()), TblMiniprogram::getExt2, bo.getExt2());
        if(bo.getParams() != null && bo.getParams().get("type") != null) {
            lqw.apply(StringUtils.format(" ext1->'$.type' = '{}'", params.get("type").toString()));
        }else{
            lqw.apply(" ext1->'$.type' != '2' OR ext1->'$.type' IS NULL");
        }
        return lqw;
    }

    /**
     * 新增小程序信息
     */
    @Override
    public Boolean insertByBo(TblMiniprogramBo bo) {
        TblMiniprogram add = MapstructUtils.convert(bo, TblMiniprogram.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改小程序信息
     */
    @Override
    public Boolean updateByBo(TblMiniprogramBo bo) {
        TblMiniprogram update = MapstructUtils.convert(bo, TblMiniprogram.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

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

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