package com.czg.service.product.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import com.czg.exception.CzgException;
import com.czg.product.dto.ProdConsBindDTO;
import com.czg.product.dto.ProdConsRelationDTO;
import com.czg.product.entity.ProdConsRelation;
import com.czg.product.entity.Product;
import com.czg.product.service.ProdConsRelationService;
import com.czg.sa.StpKit;
import com.czg.service.product.mapper.ProdConsRelationMapper;
import com.czg.service.product.mapper.ProductMapper;
import com.czg.utils.PageUtil;
import com.czg.validator.ValidatorUtil;
import com.czg.validator.group.DefaultGroup;
import com.mybatisflex.core.query.QueryWrapper;
import com.mybatisflex.spring.service.impl.ServiceImpl;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 商品耗材绑定关系
 *
 * @author Tankaikai tankaikai@aliyun.com
 * @since 1.0 2025-02-20
 */
@Service
@AllArgsConstructor
public class ProdConsRelationServiceImpl extends ServiceImpl<ProdConsRelationMapper, ProdConsRelation> implements ProdConsRelationService {

    private final ProductMapper productMapper;

    private QueryWrapper buildQueryWrapper(ProdConsRelationDTO param) {
        QueryWrapper queryWrapper = PageUtil.buildSortQueryWrapper();
        Long shopId = StpKit.USER.getShopId();
        queryWrapper.eq(ProdConsRelation::getShopId, shopId);
        return queryWrapper;
    }

    @Override
    public List<ProdConsRelationDTO> getProdConsRelationList(ProdConsRelationDTO param) {
        QueryWrapper queryWrapper = buildQueryWrapper(param);
        return super.listAs(queryWrapper, ProdConsRelationDTO.class);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveProdConsRelation(ProdConsBindDTO dto) {
        Long shopId = StpKit.USER.getShopId();
        long count = productMapper.selectCountByQuery(query().eq(Product::getShopId, shopId).eq(Product::getId, dto.getId()));
        if (count == 0) {
            throw new CzgException("商品不存在");
        }
        super.remove(query().eq(ProdConsRelation::getShopId, shopId).eq(ProdConsRelation::getProductId, dto.getId()));
        if (CollUtil.isEmpty(dto.getConsList())) {
            return;
        }
        ValidatorUtil.validateEntityList(dto.getConsList(), DefaultGroup.class);
        List<ProdConsRelationDTO> consList = dto.getConsList();
        for (ProdConsRelationDTO con : consList) {
            con.setShopId(shopId);
            con.setProductId(dto.getId());
        }
        List<Long> list = consList.stream().map(ProdConsRelationDTO::getProductId).distinct().toList();
        if (CollUtil.isNotEmpty(list) && list.size() > 1) {
            throw new CzgException("只能绑定同一商品下的耗材");
        }
        if (!dto.getId().equals(list.getFirst())) {
            throw new CzgException("非法操作，绑定关系商品不一致");
        }
        List<ProdConsRelation> entityList = BeanUtil.copyToList(consList, ProdConsRelation.class);
        mapper.insertBatchSelective(entityList, 50);
    }

    @Override
    public List<ProdConsRelationDTO> selectListByProdId(Long prodId) {
        return mapper.selectListByProdId(prodId);
    }

    /**
     * 扣减库存 使用
     * @param prodId
     * @return
     */
    @Override
    public List<ProdConsRelationDTO> selectStockByProdId(Long prodId) {
        return mapper.selectStockByProdId(prodId);
    }

}