package cn.iocoder.yudao.module.member.service.storesalegoods;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.iocoder.yudao.module.member.controller.app.store.dto.StoreGoodsDto;
import cn.iocoder.yudao.module.member.controller.app.store.dto.StoreSaleGoodsDto;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import java.util.stream.Collectors;

import cn.iocoder.yudao.module.member.controller.admin.storesalegoods.vo.*;
import cn.iocoder.yudao.module.member.dal.dataobject.storesalegoods.StoreSaleGoodsDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import cn.iocoder.yudao.module.member.dal.mysql.storesalegoods.StoreSaleGoodsMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.member.enums.ErrorCodeConstants.*;

/**
 * 售卖商品 Service 实现类
 *
 * @author 管理员
 */
@Service
@Validated
public class StoreSaleGoodsServiceImpl implements StoreSaleGoodsService {

    @Resource
    private StoreSaleGoodsMapper storeSaleGoodsMapper;

    @Override
    public Long createStoreSaleGoods(StoreSaleGoodsSaveReqVO createReqVO) {
        // 插入
        StoreSaleGoodsDO storeSaleGoods = BeanUtils.toBean(createReqVO, StoreSaleGoodsDO.class);
        storeSaleGoodsMapper.insert(storeSaleGoods);
        // 返回
        return storeSaleGoods.getId();
    }

    @Override
    public void updateStoreSaleGoods(StoreSaleGoodsSaveReqVO updateReqVO) {
        // 校验存在
        validateStoreSaleGoodsExists(updateReqVO.getId());
        // 更新
        StoreSaleGoodsDO updateObj = BeanUtils.toBean(updateReqVO, StoreSaleGoodsDO.class);
        storeSaleGoodsMapper.updateById(updateObj);
    }

    @Override
    public void deleteStoreSaleGoods(Long id) {
        // 校验存在
        validateStoreSaleGoodsExists(id);
        // 删除
        storeSaleGoodsMapper.deleteById(id);
    }

    private void validateStoreSaleGoodsExists(Long id) {
        if (storeSaleGoodsMapper.selectById(id) == null) {
            throw exception(STORE_SALE_GOODS_NOT_EXISTS);
        }
    }

    @Override
    public StoreSaleGoodsDO getStoreSaleGoods(Long id) {
        return storeSaleGoodsMapper.selectById(id);
    }

    @Override
    public PageResult<StoreSaleGoodsDO> getStoreSaleGoodsPage(StoreSaleGoodsPageReqVO pageReqVO) {
        return storeSaleGoodsMapper.selectPage(pageReqVO);
    }

    @Override
    public Boolean bindGoods(StoreSaleGoodsDto dto) {

        if(CollectionUtil.isEmpty(dto.getEquipmentGoodsCustoms())){
            throw exception(STORE_SALE_GOODS_NOT_NULL);
        }
        //清空
        storeSaleGoodsMapper.delete(new LambdaQueryWrapper<StoreSaleGoodsDO>().eq(StoreSaleGoodsDO::getEquipmentCode, dto.getEquipmentCode()));
        ArrayList<StoreSaleGoodsDO> list = new ArrayList<>();
        for (StoreGoodsDto storeGoodsDto : dto.getEquipmentGoodsCustoms()) {
            StoreSaleGoodsDO storeSaleGoodsDO = new StoreSaleGoodsDO();
            BeanUtil.copyProperties(storeGoodsDto, storeSaleGoodsDO);
            storeSaleGoodsDO.setEquipmentCode(dto.getEquipmentCode());
            list.add(storeSaleGoodsDO);
        }
        return storeSaleGoodsMapper.insertBatch(list);
    }

    @Override
    public List<Integer> getGoodsIds(String equipmentCode) {
        List<StoreSaleGoodsDO> storeSaleGoodsDOS = storeSaleGoodsMapper.selectList(new LambdaQueryWrapper<StoreSaleGoodsDO>().eq(StoreSaleGoodsDO::getEquipmentCode, equipmentCode));
        if (CollectionUtil.isEmpty(storeSaleGoodsDOS)){
            return Collections.emptyList();
        }else {
            return storeSaleGoodsDOS.stream().map(StoreSaleGoodsDO::getGoodsId).collect(Collectors.toList());
        }
    }
}