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

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.zhongzheng.common.utils.DateUtils;
import com.zhongzheng.modules.goods.bo.GoodsAuditionConfigAddBo;
import com.zhongzheng.modules.goods.bo.GoodsAuditionConfigEditBo;
import com.zhongzheng.modules.goods.bo.GoodsAuditionConfigQueryBo;
import com.zhongzheng.modules.goods.domain.GoodsAuditionConfig;
import com.zhongzheng.modules.goods.mapper.GoodsAuditionConfigMapper;
import com.zhongzheng.modules.goods.service.IGoodsAuditionConfigService;
import com.zhongzheng.modules.goods.vo.GoodsAuditionConfigVo;
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-02
 */
@Service
public class GoodsAuditionConfigServiceImpl extends ServiceImpl<GoodsAuditionConfigMapper, GoodsAuditionConfig> implements IGoodsAuditionConfigService {

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

    @Override
    public List<GoodsAuditionConfigVo> queryList(GoodsAuditionConfigQueryBo bo) {
        LambdaQueryWrapper<GoodsAuditionConfig> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getGoodsId() != null, GoodsAuditionConfig::getGoodsId, bo.getGoodsId());
        lqw.eq(bo.getSectionId() != null, GoodsAuditionConfig::getSectionId, bo.getSectionId());
        lqw.eq(bo.getAuditionMinute() != null, GoodsAuditionConfig::getAuditionMinute, bo.getAuditionMinute());
        lqw.eq(bo.getCourseId() != null, GoodsAuditionConfig::getCourseId, bo.getCourseId());
        return entity2Vo(this.list(lqw));
    }

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

    @Override
    public Boolean insertByAddBo(GoodsAuditionConfigAddBo bo) {
        GoodsAuditionConfig add = BeanUtil.toBean(bo, GoodsAuditionConfig.class);
        validEntityBeforeSave(add);
        return this.save(add);
    }

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

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

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