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

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.zhongzheng.common.utils.DateUtils;
import com.zhongzheng.modules.exam.domain.ExamApplyGoods;
import com.zhongzheng.modules.exam.domain.ExamBefore;
import com.zhongzheng.modules.exam.service.IExamBeforeService;
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 com.zhongzheng.modules.exam.bo.ExamBeforeGoodsAddBo;
import com.zhongzheng.modules.exam.bo.ExamBeforeGoodsQueryBo;
import com.zhongzheng.modules.exam.bo.ExamBeforeGoodsEditBo;
import com.zhongzheng.modules.exam.domain.ExamBeforeGoods;
import com.zhongzheng.modules.exam.mapper.ExamBeforeGoodsMapper;
import com.zhongzheng.modules.exam.vo.ExamBeforeGoodsVo;
import com.zhongzheng.modules.exam.service.IExamBeforeGoodsService;

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

/**
 * 前培绑定商品Service业务层处理
 *
 * @author ruoyi
 * @date 2021-12-07
 */
@Service
public class ExamBeforeGoodsServiceImpl extends ServiceImpl<ExamBeforeGoodsMapper, ExamBeforeGoods> implements IExamBeforeGoodsService {

    @Autowired
    private IExamBeforeService examBeforeService;

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

    @Override
    public List<ExamBeforeGoodsVo> queryList(ExamBeforeGoodsQueryBo bo) {
        LambdaQueryWrapper<ExamBeforeGoods> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getBeforeId() != null, ExamBeforeGoods::getBeforeId, bo.getBeforeId());
        lqw.eq(bo.getGoodsId() != null, ExamBeforeGoods::getGoodsId, bo.getGoodsId());
        return entity2Vo(this.list(lqw));
    }

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

    @Override
    public Boolean insertByAddBo(ExamBeforeGoodsAddBo bo) {
        ExamBefore examBefore = new ExamBefore();
        examBefore.setBeforeId(bo.getBeforeId());
        examBefore.setEducationTypeId(bo.getEducationTypeId());
        examBefore.setProjectId(bo.getProjectId());
        examBefore.setUpdateTime(DateUtils.getNowTime());
        examBefore.setBusinessId(bo.getBusinessId());
        examBefore.setGoodsType(bo.getGoodsType());
        examBeforeService.updateById(examBefore);
        LambdaQueryWrapper<ExamBeforeGoods> lqw = Wrappers.lambdaQuery();
        lqw.eq(ExamBeforeGoods::getBeforeId, bo.getBeforeId());
        this.remove(lqw);
        for (Long goodsId : bo.getGoodsId()) {
            ExamBeforeGoods add = new ExamBeforeGoods();
            add.setCreateTime(DateUtils.getNowTime());
            add.setUpdateTime(DateUtils.getNowTime());
            add.setGoodsId(goodsId);
            add.setBeforeId(bo.getBeforeId());
            return this.save(add);
        }
        return true;
    }

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

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

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