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.MallAttributeTemplate;
import org.dromara.mall.product.domain.bo.MallAttributeTemplateBo;
import org.dromara.mall.product.domain.vo.MallAttributeTemplateVo;
import org.dromara.mall.product.mapper.MallAttributeTemplateMapper;
import org.dromara.mall.product.service.MallAttributeTemplateService;
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 MallAttributeTemplateServiceImpl implements MallAttributeTemplateService {

    private final MallAttributeTemplateMapper baseMapper;

    /**
     * 查询属性模板
     */
    @Override
    public MallAttributeTemplateVo queryById(Long id) {
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询属性模板列表
     */
    @Override
    public TableDataInfo<MallAttributeTemplateVo> queryPageList(MallAttributeTemplateBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<MallAttributeTemplate> lqw = buildQueryWrapper(bo);
        Page<MallAttributeTemplateVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询属性模板列表
     */
    @Override
    public List<MallAttributeTemplateVo> queryList(MallAttributeTemplateBo bo) {
        LambdaQueryWrapper<MallAttributeTemplate> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

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

    /**
     * 新增属性模板
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean insertByBo(MallAttributeTemplateBo bo) {
        MallAttributeTemplate add = MapstructUtils.convert(bo, MallAttributeTemplate.class);
        validEntityBeforeSave(add);

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

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

    /**
     * 修改属性模板
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean updateByBo(MallAttributeTemplateBo bo) {
        MallAttributeTemplate update = MapstructUtils.convert(bo, MallAttributeTemplate.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(MallAttributeTemplate entity) {
        if (StringUtils.isBlank(entity.getName())) {
            throw new ServiceException("模板名称不能为空");
        }
        // 检查模板名称是否重复
        LambdaQueryWrapper<MallAttributeTemplate> lqw = new LambdaQueryWrapper<>();
        lqw.eq(MallAttributeTemplate::getName, entity.getName());
        if (entity.getId() != null) {
            lqw.ne(MallAttributeTemplate::getId, entity.getId());
        }
        Long count = baseMapper.selectCount(lqw);
        if (count > 0) {
            throw new ServiceException("模板名称已存在");
        }
    }

    /**
     * 批量删除属性模板
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if (isValid) {
            for (Long id : ids) {
                MallAttributeTemplate template = baseMapper.selectById(id);
                if (template == null) {
                    throw new ServiceException("属性模板不存在");
                }
                // 可以后续添加属性表关联查询，检查是否有关联的属性
            }
        }
        return baseMapper.deleteByIds(ids) > 0;
    }
}
