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.ZhishuShopGoodsDetailBo;
import org.dromara.zhishu.domain.vo.ZhishuShopGoodsDetailVo;
import org.dromara.zhishu.domain.ZhishuShopGoodsDetail;
import org.dromara.zhishu.mapper.ZhishuShopGoodsDetailMapper;
import org.dromara.zhishu.service.IZhishuShopGoodsDetailService;

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

/**
 * 商品信息补全表Service业务层处理
 *
 * @author yxy
 * @date 2025-06-13
 */
@RequiredArgsConstructor
@Service
public class ZhishuShopGoodsDetailServiceImpl implements IZhishuShopGoodsDetailService {

    private final ZhishuShopGoodsDetailMapper baseMapper;

    /**
     * 查询商品信息补全表
     *
     * @param id 主键
     * @return 商品信息补全表
     */
    @Override
    public ZhishuShopGoodsDetailVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    @Override
    public ZhishuShopGoodsDetailVo selectByPid(Long pid){
        return baseMapper.selectByPid(pid);
    }

    /**
     * 分页查询商品信息补全表列表
     *
     * @param bo        查询条件
     * @param pageQuery 分页参数
     * @return 商品信息补全表分页列表
     */
    @Override
    public TableDataInfo<ZhishuShopGoodsDetailVo> queryPageList(ZhishuShopGoodsDetailBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<ZhishuShopGoodsDetail> lqw = buildQueryWrapper(bo);
        Page<ZhishuShopGoodsDetailVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询符合条件的商品信息补全表列表
     *
     * @param bo 查询条件
     * @return 商品信息补全表列表
     */
    @Override
    public List<ZhishuShopGoodsDetailVo> queryList(ZhishuShopGoodsDetailBo bo) {
        LambdaQueryWrapper<ZhishuShopGoodsDetail> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<ZhishuShopGoodsDetail> buildQueryWrapper(ZhishuShopGoodsDetailBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<ZhishuShopGoodsDetail> lqw = Wrappers.lambdaQuery();
        lqw.orderByAsc(ZhishuShopGoodsDetail::getId);
        lqw.eq(bo.getPid() != null, ZhishuShopGoodsDetail::getPid, bo.getPid());
        lqw.eq(StringUtils.isNotBlank(bo.getAuthor()), ZhishuShopGoodsDetail::getAuthor, bo.getAuthor());
        lqw.eq(StringUtils.isNotBlank(bo.getPublisher()), ZhishuShopGoodsDetail::getPublisher, bo.getPublisher());
        lqw.eq(StringUtils.isNotBlank(bo.getPublishertime()), ZhishuShopGoodsDetail::getPublishertime, bo.getPublishertime());
        lqw.eq(StringUtils.isNotBlank(bo.getFormat()), ZhishuShopGoodsDetail::getFormat, bo.getFormat());
        lqw.eq(StringUtils.isNotBlank(bo.getWordage()), ZhishuShopGoodsDetail::getWordage, bo.getWordage());
        lqw.eq(StringUtils.isNotBlank(bo.getUnifiedisbn()), ZhishuShopGoodsDetail::getUnifiedisbn, bo.getUnifiedisbn());
        lqw.eq(StringUtils.isNotBlank(bo.getStatus()), ZhishuShopGoodsDetail::getStatus, bo.getStatus());
        return lqw;
    }

    /**
     * 新增商品信息补全表
     *
     * @param bo 商品信息补全表
     * @return 是否新增成功
     */
    @Override
    public Boolean insertByBo(ZhishuShopGoodsDetailBo bo) {
        ZhishuShopGoodsDetail add = MapstructUtils.convert(bo, ZhishuShopGoodsDetail.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改商品信息补全表
     *
     * @param bo 商品信息补全表
     * @return 是否修改成功
     */
    @Override
    public Boolean updateByBo(ZhishuShopGoodsDetailBo bo) {
        ZhishuShopGoodsDetail update = MapstructUtils.convert(bo, ZhishuShopGoodsDetail.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

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

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