package org.linlinjava.jz.db.service;

import org.linlinjava.jz.db.dao.JzOrderGoodsMapper;
import org.linlinjava.jz.db.domain.JzOrderGoods;
import org.linlinjava.jz.db.domain.JzOrderGoodsExample;
import org.springframework.stereotype.Service;

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

/**
 * 商城 订单订购商品  1：N关系
 */
@Service
public class JzOrderGoodsService {

    @Resource
    private JzOrderGoodsMapper goodsMapper;


    public int add(JzOrderGoods orderGoods) {
        orderGoods.setAddTime(LocalDateTime.now());
        orderGoods.setUpdateTime(LocalDateTime.now());
        return goodsMapper.insertSelective(orderGoods);
    }

    public List<JzOrderGoods> queryByOid(Integer orderId) {
        JzOrderGoodsExample example = new JzOrderGoodsExample();
        example.or().andOrderIdEqualTo(orderId).andDeletedEqualTo(false);
        return goodsMapper.selectByExample(example);
    }

    public List<JzOrderGoods> findByOidAndGid(Integer orderId, Integer goodsId) {
        JzOrderGoodsExample example = new JzOrderGoodsExample();
        example.or().andOrderIdEqualTo(orderId).andGoodsIdEqualTo(goodsId).andDeletedEqualTo(false);
        return goodsMapper.selectByExample(example);
    }

    public JzOrderGoods findById(Integer id) {
        return goodsMapper.selectByPrimaryKey(id);
    }

    public void updateById(JzOrderGoods orderGoods) {
        orderGoods.setUpdateTime(LocalDateTime.now());
        goodsMapper.updateByPrimaryKeySelective(orderGoods);
    }

    public Short getComments(Integer orderId) {
        JzOrderGoodsExample example = new JzOrderGoodsExample();
        example.or().andOrderIdEqualTo(orderId).andDeletedEqualTo(false);
        long count = goodsMapper.countByExample(example);
        return (short) count;
    }

    public boolean checkExist(Integer goodsId) {
        JzOrderGoodsExample example = new JzOrderGoodsExample();
        example.or().andGoodsIdEqualTo(goodsId).andDeletedEqualTo(false);
        return goodsMapper.countByExample(example) != 0;
    }

    public void deleteByOrderId(Integer orderId) {
        JzOrderGoodsExample example = new JzOrderGoodsExample();
        example.or().andOrderIdEqualTo(orderId).andDeletedEqualTo(false);
        goodsMapper.logicalDeleteByExample(example);
    }
    
    public JzOrderGoods findByOrderId(Integer orderId) {
    	 JzOrderGoodsExample example = new JzOrderGoodsExample();
         example.or().andOrderIdEqualTo(orderId);
        return goodsMapper.selectOneByExample(example);
    }
}
