package com.gxwebsoft.shop.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gxwebsoft.shop.entity.Merchant;
import com.gxwebsoft.shop.mapper.UserCollectionMapper;
import com.gxwebsoft.shop.service.MerchantService;
import com.gxwebsoft.shop.service.OrderGoodsService;
import com.gxwebsoft.shop.service.OrderService;
import com.gxwebsoft.shop.service.UserCollectionService;
import com.gxwebsoft.shop.entity.UserCollection;
import com.gxwebsoft.shop.param.UserCollectionParam;
import com.gxwebsoft.common.core.web.PageParam;
import com.gxwebsoft.common.core.web.PageResult;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * 我的收藏Service实现
 *
 * @author 科技小王子
 * @since 2024-10-03 01:51:46
 */
@Service
public class UserCollectionServiceImpl extends ServiceImpl<UserCollectionMapper, UserCollection> implements UserCollectionService {
    @Resource
    private MerchantService merchantService;
    @Resource
    private OrderGoodsService orderGoodsService;

    @Override
    public PageResult<UserCollection> pageRel(UserCollectionParam param) {
        PageParam<UserCollection, UserCollectionParam> page = new PageParam<>(param);
        page.setDefaultOrder("sort_number asc, create_time desc");
        List<UserCollection> list = baseMapper.selectPageRel(page, param);
        return new PageResult<>(list, page.getTotal());
    }

    @Override
    public List<UserCollection> listRel(UserCollectionParam param) {
        List<UserCollection> list = list(
                new LambdaQueryWrapper<UserCollection>()
                        .eq(UserCollection::getUserId, param.getUserId())
                        .eq(UserCollection::getType, param.getType())
                        .orderByDesc(UserCollection::getCreateTime)
        );
        for (UserCollection userCollection : list) {
            if (userCollection.getType().equals(0)) {
                Merchant merchant = merchantService.getById(userCollection.getTid());
                merchant.setOrderNum(orderGoodsService.countByMerchant(merchant.getMerchantId()));
                userCollection.setMerchant(merchant);
            }
        }
        return list;
    }

    @Override
    public UserCollection getByIdRel(Integer id) {
        UserCollectionParam param = new UserCollectionParam();
        param.setId(id);
        return param.getOne(baseMapper.selectListRel(param));
    }

    @Override
    public UserCollection check(Integer userId, Integer type, Integer tid) {
        UserCollectionParam param = new UserCollectionParam();
        param.setUserId(userId);
        param.setType(type);
        param.setTid(tid);
        return param.getOne(baseMapper.selectListRel(param));
    }

}
