package com.sy.coinage.workshop.service.impl;

import com.alibaba.fastjson2.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.sy.coinage.core.constants.CacheKey;
import com.sy.coinage.core.constants.Constants;
import com.sy.coinage.core.redis.RedisService;
import com.sy.coinage.core.vo.OptVO;
import com.sy.coinage.sys.model.Dept;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.sy.coinage.workshop.dao.ProductDao;
import com.sy.coinage.workshop.model.*;
import com.sy.coinage.workshop.service.IProductService;

import java.util.List;

/**
 * -产品表ServiceImpl
 */
@Service
public class ProductServiceImpl extends ServiceImpl<ProductDao, Product> implements IProductService {

    @Autowired
    private RedisService redisService;

    @Override
    public List<OptVO> opt() {
        LambdaQueryWrapper<Product> queryWrapper = new LambdaQueryWrapper<>();

        queryWrapper.eq(Product::getStatus, Constants.STATUS_USABLE);

        List<OptVO> resp = this.baseMapper.queryOptList(queryWrapper);
        return resp;
    }

    @Override
    public Product getByCache(Integer id) {
        Product resp = null;
        String key = CacheKey.PRODUCT + id;
        if (redisService.hasKey(key)) {
            String json = redisService.get(key);
            resp = JSON.parseObject(json, Product.class);
        } else {
            resp = this.getById(id);
            if (resp != null) {
                this.redisService.set(key, JSON.toJSONString(resp), CacheKey.EXPIRED_ONE_DAY);
            }
        }
        return resp;
    }

    // @Override
    // public IPage<ProductVO> queryPageList(ProductListReq req) {
    // QueryWrapper<ProductVO> queryWrapper = new QueryWrapper<>();
    // if (null != req.getUserId() && req.getUserId() > 0) {
    // queryWrapper.eq("user_id", userId);
    // }
    // if (StringUtils.isNotBlank(req.getRealName())) {
    // queryWrapper.like("sys_user.real_name", req.getRealName());
    // }
    // Page<ProductVO> page = new Page<>(req.getPage(), req.getRows());
    // IPage<ProductVO> resp = this.baseMapper.queryPageList(page, queryWrapper);
    // return resp;
    // }

}
