package org.dromara.zhishu.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import jakarta.transaction.Transactional;
import lombok.RequiredArgsConstructor;
import org.apache.ibatis.annotations.Param;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.satoken.utils.LoginHelper;
import org.dromara.zhishu.domain.ShopGoodsPublished;
import org.dromara.zhishu.domain.ShopGoodsPublishedLog;
import org.dromara.zhishu.domain.ShopGoodsRejection;
import org.dromara.zhishu.domain.vo.ShopVo;
import org.dromara.zhishu.mapper.ShopGoodsPublishedLogMapper;
import org.dromara.zhishu.mapper.ShopGoodsRejectionMapper;
import org.dromara.zhishu.service.IShopService;
import org.dromara.zhishu.service.ShopGoodsPublishedLogService;
import org.dromara.zhishu.service.ShopGoodsRejectionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.awt.print.Pageable;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@RequiredArgsConstructor
public class ShopGoodsRejectionServiceImpl extends ServiceImpl<ShopGoodsRejectionMapper, ShopGoodsRejection> implements ShopGoodsRejectionService {

    @Autowired
    private ShopGoodsRejectionMapper shopGoodsRejectionMapper;

    private final IShopService shopService;
    @Override
    @Transactional
    public boolean save(ShopGoodsRejection shopGoodsRejection) {
        if (shopGoodsRejection.getCreateTime() == null) {
            shopGoodsRejection.setCreateTime(new java.util.Date());
        }
        return shopGoodsRejectionMapper.insert(shopGoodsRejection) > 0;
    }

    @Override
    @Transactional
    public boolean delete(Long id) {
        return shopGoodsRejectionMapper.deleteById(id) > 0;
    }

    @Override
    @Transactional
    public boolean update(ShopGoodsRejection shopGoodsRejection) {
        return shopGoodsRejectionMapper.update(shopGoodsRejection) > 0;
    }

    @Override
    public ShopGoodsRejection findById(Long id) {
        return shopGoodsRejectionMapper.selectById(id);
    }

    @Override
    public List<ShopGoodsRejection> findAll() {
        return shopGoodsRejectionMapper.selectAll();
    }

    @Override
    public List<ShopGoodsRejection> findByShopId(String shopId) {
        return shopGoodsRejectionMapper.selectByShopId(shopId);
    }

    @Override
    public List<ShopGoodsRejection> findByPlatformId(String platformId) {
        return shopGoodsRejectionMapper.selectByPlatformId(platformId);
    }

    @Override
    public List<ShopGoodsRejection> search(ShopGoodsRejection condition) {
        return shopGoodsRejectionMapper.selectByCondition(condition);
    }

    @Override
    public TableDataInfo<ShopGoodsRejection> searchWithPage(ShopGoodsRejection bo, PageQuery pageQuery) {
        Long userId = LoginHelper.getUserId();
        if(userId != 1){
            bo.setUserId(userId);
        }
        Page<ShopGoodsRejection> result =shopGoodsRejectionMapper.selectByConditionWithPage(pageQuery.build(),bo);
        for (ShopGoodsRejection shopGoodsRejection : result.getRecords()){
          ShopVo shopVo =  shopService.queryById(shopGoodsRejection.getShopId());
            shopGoodsRejection.setShopName(shopVo.getShopName());
        }
        return TableDataInfo.build(result);
    }

    @Override
    public int count(ShopGoodsRejection condition) {
        return shopGoodsRejectionMapper.countByCondition(condition);
    }
}
