package com.seer.teach.mall.good.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.seer.teach.common.PageListBean;
import com.seer.teach.common.enums.GoodsAttributeScopeEnum;
import com.seer.teach.common.enums.ResultCodeEnum;
import com.seer.teach.common.utils.AssertUtils;
import com.seer.teach.common.utils.PageConverterUtils;
import com.seer.teach.mall.good.service.IAdminGoodsAttributeValueService;
import com.seer.teach.mall.convert.GoodsAttributeValueConvert;
import com.seer.teach.mall.entity.GoodsAttributeValueEntity;
import com.seer.teach.mall.mapper.GoodsAttributeValueMapper;
import com.seer.teach.mall.request.GoodsAttributeValuePageReq;
import com.seer.teach.mall.request.GoodsAttributeValueReq;
import com.seer.teach.mall.response.GoodsAttributeValueResp;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;

@Service
@RequiredArgsConstructor
public class AdminGoodsAttributeValueServiceImpl extends ServiceImpl<GoodsAttributeValueMapper, GoodsAttributeValueEntity>
        implements IAdminGoodsAttributeValueService {

    @Override
    public PageListBean<GoodsAttributeValueResp> getPage(GoodsAttributeValuePageReq pageReq) {
        // 构建分页参数
        Page<GoodsAttributeValueEntity> page = new Page<>(pageReq.getPageNo(), pageReq.getPageSize());

        LambdaQueryWrapper<GoodsAttributeValueEntity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(pageReq.getAttributeId() != null,
                        GoodsAttributeValueEntity::getAttributeId, pageReq.getAttributeId())
                .like(pageReq.getAttributeValue() != null,
                        GoodsAttributeValueEntity::getAttributeValue, pageReq.getAttributeValue())
                .eq(pageReq.getStatus() != null,
                        GoodsAttributeValueEntity::getStatus, pageReq.getStatus());

        // 如果指定了scope，则按指定scope查询
        if (pageReq.getScope() != null) {
            queryWrapper.eq(GoodsAttributeValueEntity::getScope, pageReq.getScope());
        }
        // 如果指定了merchantId，则添加merchantId过滤条件
        if (pageReq.getMerchantId() != null) {
            queryWrapper.and(wrapper -> wrapper.eq(GoodsAttributeValueEntity::getScope, GoodsAttributeScopeEnum.PLATFORM)
                    .or()
                    .eq(GoodsAttributeValueEntity::getMerchantId, pageReq.getMerchantId()));
        }
        
        queryWrapper.orderByDesc(GoodsAttributeValueEntity::getCreateTime);

        IPage<GoodsAttributeValueEntity> entityPage = super.page(page, queryWrapper);

        return PageConverterUtils.convertPageListBean(entityPage, GoodsAttributeValueConvert.INSTANCE::entityListToRespList);
    }

    @Override
    public GoodsAttributeValueResp getById(Integer id) {
        GoodsAttributeValueEntity entity = super.getById(id);
        AssertUtils.notNull(entity, ResultCodeEnum.GOODS_ATTRIBUTE_VALUE_NOT_FOUND);
        return GoodsAttributeValueConvert.INSTANCE.entityToResp(entity);
    }

    @Override
    public boolean save(GoodsAttributeValueReq req) {
        // 检查同一属性下值是否已存在
        long count = this.count(new LambdaQueryWrapper<GoodsAttributeValueEntity>()
                .eq(GoodsAttributeValueEntity::getAttributeId, req.getAttributeId())
                .eq(GoodsAttributeValueEntity::getAttributeValue, req.getAttributeValue()));

        AssertUtils.isTrue(count == 0, ResultCodeEnum.GOODS_ATTRIBUTE_VALUE_ALREADY_EXISTS);

        GoodsAttributeValueEntity entity = GoodsAttributeValueConvert.INSTANCE.reqToEntity(req);
        return this.save(entity);
    }

    @Override
    public boolean update(GoodsAttributeValueReq req) {
        AssertUtils.notNull(req.getId(), ResultCodeEnum.GOODS_ATTRIBUTE_VALUE_ID_REQUIRED);

        // 检查记录是否存在
        GoodsAttributeValueEntity existing = super.getById(req.getId());
        AssertUtils.notNull(existing, ResultCodeEnum.GOODS_ATTRIBUTE_VALUE_NOT_FOUND);

        // 检查同一属性下值是否与其他记录重复
        long count = this.count(new LambdaQueryWrapper<GoodsAttributeValueEntity>()
                .eq(GoodsAttributeValueEntity::getAttributeId, req.getAttributeId())
                .eq(GoodsAttributeValueEntity::getAttributeValue, req.getAttributeValue())
                .ne(GoodsAttributeValueEntity::getId, req.getId()));

        AssertUtils.isTrue(count == 0, ResultCodeEnum.GOODS_ATTRIBUTE_VALUE_ALREADY_EXISTS);

        LambdaUpdateWrapper<GoodsAttributeValueEntity> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(GoodsAttributeValueEntity::getId, req.getId())
                .set(req.getAttributeId() != null,
                        GoodsAttributeValueEntity::getAttributeId, req.getAttributeId())
                .set(req.getAttributeValue() != null,
                        GoodsAttributeValueEntity::getAttributeValue, req.getAttributeValue())
                .set(req.getStatus() != null,
                        GoodsAttributeValueEntity::getStatus, req.getStatus())
                .set(req.getRemark() != null,
                        GoodsAttributeValueEntity::getRemark, req.getRemark());

        return super.update(updateWrapper);
    }

    @Override
    public boolean delete(List<Integer> ids) {
        AssertUtils.notEmpty(ids, ResultCodeEnum.GOODS_ATTRIBUTE_VALUE_IDS_REQUIRED);

        // 检查是否被商品使用 TODO

        return super.removeByIds(ids);
    }
}