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

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.zhongzheng.common.utils.DateUtils;
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 com.zhongzheng.modules.polyv.bo.PolyvCataAddBo;
import com.zhongzheng.modules.polyv.bo.PolyvCataQueryBo;
import com.zhongzheng.modules.polyv.bo.PolyvCataEditBo;
import com.zhongzheng.modules.polyv.domain.PolyvCata;
import com.zhongzheng.modules.polyv.mapper.PolyvCataMapper;
import com.zhongzheng.modules.polyv.vo.PolyvCataVo;
import com.zhongzheng.modules.polyv.service.IPolyvCataService;

import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 保利威分类Service业务层处理
 *
 * @author ruoyi
 * @date 2022-01-11
 */
@Service
public class PolyvCataServiceImpl extends ServiceImpl<PolyvCataMapper, PolyvCata> implements IPolyvCataService {

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

    @Override
    public List<PolyvCataVo> queryList(PolyvCataQueryBo bo) {
        LambdaQueryWrapper<PolyvCata> lqw = Wrappers.lambdaQuery();
        lqw.eq(StrUtil.isNotBlank(bo.getText()), PolyvCata::getText, bo.getText());
        lqw.like(StrUtil.isNotBlank(bo.getCataname()), PolyvCata::getCataname, bo.getCataname());
        lqw.eq(bo.getCataid() != null, PolyvCata::getCataid, bo.getCataid());
        lqw.eq(bo.getParentid() != null, PolyvCata::getParentid, bo.getParentid());
        lqw.eq(StrUtil.isNotBlank(bo.getVideos()), PolyvCata::getVideos, bo.getVideos());
        return entity2Vo(this.list(lqw));
    }

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

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

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

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

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