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.OrderBillGoodsAddBo;
import com.zhongzheng.modules.order.bo.OrderBillGoodsEditBo;
import com.zhongzheng.modules.order.bo.OrderBillGoodsQueryBo;
import com.zhongzheng.modules.order.domain.OrderBillGoods;
import com.zhongzheng.modules.order.domain.OrderGoods;
import com.zhongzheng.modules.order.mapper.OrderBillGoodsMapper;
import com.zhongzheng.modules.order.service.IOrderBillGoodsService;
import com.zhongzheng.modules.order.vo.OrderBillGoodsVo;
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 hjl
 * @date 2021-11-09
 */
@Service
public class OrderBillGoodsServiceImpl extends ServiceImpl<OrderBillGoodsMapper, OrderBillGoods> implements IOrderBillGoodsService {

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

    @Override
    public OrderBillGoodsVo selectDetail(OrderBillGoodsQueryBo bo) {
        OrderBillGoods db = this.getOne(new LambdaQueryWrapper<OrderBillGoods>().eq(OrderBillGoods::getSheetSn, bo.getSheetSn()).eq(OrderBillGoods::getGoodsId,bo.getGoodsId()));
        return BeanUtil.toBean(db, OrderBillGoodsVo.class);
    }

    @Override
    public List<OrderBillGoodsVo> queryList(OrderBillGoodsQueryBo bo) {
        LambdaQueryWrapper<OrderBillGoods> lqw = Wrappers.lambdaQuery();
        lqw.eq(StrUtil.isNotBlank(bo.getSheetSn()), OrderBillGoods::getSheetSn, bo.getSheetSn());
        lqw.eq(bo.getGoodsId() != null, OrderBillGoods::getGoodsId, bo.getGoodsId());
        lqw.eq(StrUtil.isNotBlank(bo.getNatureCharges()), OrderBillGoods::getNatureCharges, bo.getNatureCharges());
        lqw.eq(StrUtil.isNotBlank(bo.getCostType()), OrderBillGoods::getCostType, bo.getCostType());
        lqw.eq(bo.getAmount() != null, OrderBillGoods::getAmount, bo.getAmount());
        lqw.eq(StrUtil.isNotBlank(bo.getDescription()), OrderBillGoods::getDescription, bo.getDescription());
        lqw.eq(StrUtil.isNotBlank(bo.getOrderSn()), OrderBillGoods::getOrderSn, bo.getOrderSn());
        lqw.eq(bo.getStatus() != null, OrderBillGoods::getStatus, bo.getStatus());
        return entity2Vo(this.list(lqw));
    }

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

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

    @Override
    public Boolean updateByEditBo(OrderBillGoodsEditBo bo) {
        OrderBillGoods update = BeanUtil.toBean(bo, OrderBillGoods.class);
        validEntityBeforeSave(update);
        update.setUpdateTime(DateUtils.getNowTime());
        return this.updateById(update);
    }

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

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