package org.dromara.zhishu.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.zhishu.domain.bo.ShopContextBo;
import org.dromara.zhishu.domain.vo.ShopContextVo;
import org.dromara.zhishu.domain.ShopContext;
import org.dromara.zhishu.mapper.ShopContextMapper;
import org.dromara.zhishu.service.IShopContextService;

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

/**
 * 店铺设置商品描述Service业务层处理
 *
 * @author yxy
 * @date 2025-09-30
 */
@RequiredArgsConstructor
@Service
public class ShopContextServiceImpl implements IShopContextService {

    private final ShopContextMapper baseMapper;

    /**
     * 查询店铺设置商品描述
     *
     * @param id 主键
     * @return 店铺设置商品描述
     */
    @Override
    public ShopContextVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    @Override
    public ShopContextVo selectByShopId(Long shopId){
        return baseMapper.selectByShopId(shopId);
    }

    /**
     * 分页查询店铺设置商品描述列表
     *
     * @param bo        查询条件
     * @param pageQuery 分页参数
     * @return 店铺设置商品描述分页列表
     */
    @Override
    public TableDataInfo<ShopContextVo> queryPageList(ShopContextBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<ShopContext> lqw = buildQueryWrapper(bo);
        Page<ShopContextVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询符合条件的店铺设置商品描述列表
     *
     * @param bo 查询条件
     * @return 店铺设置商品描述列表
     */
    @Override
    public List<ShopContextVo> queryList(ShopContextBo bo) {
        LambdaQueryWrapper<ShopContext> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<ShopContext> buildQueryWrapper(ShopContextBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<ShopContext> lqw = Wrappers.lambdaQuery();
        lqw.orderByAsc(ShopContext::getId);
        lqw.eq(bo.getShopId() != null, ShopContext::getShopId, bo.getShopId());
        lqw.eq(StringUtils.isNotBlank(bo.getContext()), ShopContext::getContext, bo.getContext());
        lqw.eq(StringUtils.isNotBlank(bo.getStatus()), ShopContext::getStatus, bo.getStatus());
        return lqw;
    }

    /**
     * 新增店铺设置商品描述
     *
     * @param bo 店铺设置商品描述
     * @return 是否新增成功
     */
    @Override
    public Boolean insertByBo(ShopContextBo bo) {
        ShopContext add = MapstructUtils.convert(bo, ShopContext.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改店铺设置商品描述
     *
     * @param bo 店铺设置商品描述
     * @return 是否修改成功
     */
    @Override
    public Boolean updateByBo(ShopContextBo bo) {
        ShopContext update = MapstructUtils.convert(bo, ShopContext.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

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

    /**
     * 校验并批量删除店铺设置商品描述信息
     *
     * @param ids     待删除的主键集合
     * @param isValid 是否进行有效性校验
     * @return 是否删除成功
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteByIds(ids) > 0;
    }
}
