package org.dromara.mall.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.RequiredArgsConstructor;
import org.dromara.common.core.exception.ServiceException;
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.dromara.mall.product.domain.MallProduct;
import org.dromara.mall.product.domain.bo.MallProductBo;
import org.dromara.mall.product.domain.vo.MallProductVo;
import org.dromara.mall.product.mapper.MallProductMapper;
import org.dromara.mall.product.service.MallProductService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

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

/**
 * 商品Service业务层处理
 *
 * @author pengles
 */
@RequiredArgsConstructor
@Service
public class MallProductServiceImpl implements MallProductService {

    private final MallProductMapper baseMapper;

    /**
     * 查询商品
     */
    @Override
    public MallProductVo queryById(Long id) {
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询商品列表
     */
    @Override
    public TableDataInfo<MallProductVo> queryPageList(MallProductBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<MallProduct> lqw = buildQueryWrapper(bo);
        Page<MallProductVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询商品列表
     */
    @Override
    public List<MallProductVo> queryList(MallProductBo bo) {
        LambdaQueryWrapper<MallProduct> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<MallProduct> buildQueryWrapper(MallProductBo bo) {
        LambdaQueryWrapper<MallProduct> lqw = new LambdaQueryWrapper<>();
        lqw.like(StringUtils.isNotBlank(bo.getName()), MallProduct::getName, bo.getName());
        lqw.eq(bo.getCategoryId() != null, MallProduct::getCategoryId, bo.getCategoryId());
        lqw.eq(bo.getBrandId() != null, MallProduct::getBrandId, bo.getBrandId());
        lqw.eq(bo.getStatus() != null, MallProduct::getStatus, bo.getStatus());
        lqw.orderByDesc(MallProduct::getSort);
        lqw.orderByDesc(MallProduct::getId);
        return lqw;
    }

    /**
     * 新增商品
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean insertByBo(MallProductBo bo) {
        MallProduct add = MapstructUtils.convert(bo, MallProduct.class);
        validEntityBeforeSave(add);

        // 设置默认值
        if (add.getSort() == null) {
            add.setSort(0);
        }
        if (add.getStatus() == null) {
            add.setStatus(0);
        }
        if (add.getSalesCount() == null) {
            add.setSalesCount(0);
        }
        if (add.getViewCount() == null) {
            add.setViewCount(0);
        }

        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改商品
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean updateByBo(MallProductBo bo) {
        MallProduct update = MapstructUtils.convert(bo, MallProduct.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(MallProduct entity) {
        if (StringUtils.isBlank(entity.getName())) {
            throw new ServiceException("商品名称不能为空");
        }
    }

    /**
     * 批量删除商品
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if (isValid) {
            for (Long id : ids) {
                MallProduct product = baseMapper.selectById(id);
                if (product == null) {
                    throw new ServiceException("商品不存在");
                }
            }
        }
        return baseMapper.deleteByIds(ids) > 0;
    }
}