package com.zhongzheng.modules.order.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.zhongzheng.common.utils.DateUtils;
import com.zhongzheng.modules.order.bo.OrderGoodsAddBo;
import com.zhongzheng.modules.order.bo.OrderGoodsEditBo;
import com.zhongzheng.modules.order.bo.OrderGoodsQueryBo;
import com.zhongzheng.modules.order.domain.OrderGoods;
import com.zhongzheng.modules.order.mapper.OrderGoodsMapper;
import com.zhongzheng.modules.order.mapper.OrderMapper;
import com.zhongzheng.modules.order.service.IOrderGoodsService;
import com.zhongzheng.modules.order.vo.OrderGoodsVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.github.pagehelper.Page;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 订单商品Service业务层处理
 *
 * @author ruoyi
 * @date 2021-06-30
 */
@Service
public class OrderGoodsServiceImpl extends ServiceImpl<OrderGoodsMapper, OrderGoods> implements IOrderGoodsService {

    @Autowired
    private OrderGoodsMapper orderGoodsMapper;

    @Override
    public OrderGoodsVo queryById(Long id){
        OrderGoods db = this.baseMapper.selectById(id);
        return BeanUtil.toBean(db, OrderGoodsVo.class);
    }

    @Override
    public List<OrderGoodsVo> queryList(OrderGoodsQueryBo bo) {
        LambdaQueryWrapper<OrderGoods> lqw = Wrappers.lambdaQuery();
        lqw.eq(StrUtil.isNotBlank(bo.getOrderSn()), OrderGoods::getOrderSn, bo.getOrderSn());
        lqw.eq(bo.getGoodsType() != null, OrderGoods::getGoodsType, bo.getGoodsType());
        lqw.eq(bo.getGoodsId() != null, OrderGoods::getGoodsId, bo.getGoodsId());
        lqw.eq(bo.getGoodsPrice() != null, OrderGoods::getGoodsPrice, bo.getGoodsPrice());
        lqw.eq(bo.getNum() != null, OrderGoods::getNum, bo.getNum());
        return entity2Vo(this.list(lqw));
    }

    @Override
    public List<OrderGoods> queryOrderGoodsList(OrderGoodsQueryBo bo) {
        return orderGoodsMapper.queryOrderGoodsList(bo);
    }

    /**
    * 实体类转化成视图对象
    *
    * @param collection 实体类集合
    * @return
    */
    private List<OrderGoodsVo> entity2Vo(Collection<OrderGoods> collection) {
        List<OrderGoodsVo> voList = collection.stream()
                .map(any -> BeanUtil.toBean(any, OrderGoodsVo.class))
                .collect(Collectors.toList());
        if (collection instanceof Page) {
            Page<OrderGoods> page = (Page<OrderGoods>)collection;
            Page<OrderGoodsVo> pageVo = new Page<>();
            BeanUtil.copyProperties(page,pageVo);
            pageVo.addAll(voList);
            voList = pageVo;
        }
        return voList;
    }

    @Override
    public Boolean insertByAddBo(OrderGoodsAddBo bo) {
        OrderGoods add = BeanUtil.toBean(bo, OrderGoods.class);
        validEntityBeforeSave(add);
        add.setCreateTime(DateUtils.getNowTime());
        return this.save(add);
    }

    @Override
    public Boolean updateByEditBo(OrderGoodsEditBo bo) {
        OrderGoods update = BeanUtil.toBean(bo, OrderGoods.class);
        validEntityBeforeSave(update);
        return this.updateById(update);
    }

    /**
     * 保存前的数据校验
     *
     * @param entity 实体类数据
     */
    private void validEntityBeforeSave(OrderGoods entity){
        //TODO 做一些数据校验,如唯一约束
    }

    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return this.removeByIds(ids);
    }
}
