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.ExamApply;
import com.zhongzheng.modules.exam.service.IExamApplyService;
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.ExamApplyGoodsAddBo;
import com.zhongzheng.modules.exam.bo.ExamApplyGoodsQueryBo;
import com.zhongzheng.modules.exam.bo.ExamApplyGoodsEditBo;
import com.zhongzheng.modules.exam.domain.ExamApplyGoods;
import com.zhongzheng.modules.exam.mapper.ExamApplyGoodsMapper;
import com.zhongzheng.modules.exam.vo.ExamApplyGoodsVo;
import com.zhongzheng.modules.exam.service.IExamApplyGoodsService;

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 ExamApplyGoodsServiceImpl extends ServiceImpl<ExamApplyGoodsMapper, ExamApplyGoods> implements IExamApplyGoodsService {

    @Autowired
    private IExamApplyService examApplyService;

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

    @Override
    public List<ExamApplyGoodsVo> queryList(ExamApplyGoodsQueryBo bo) {
        LambdaQueryWrapper<ExamApplyGoods> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getApplyId() != null, ExamApplyGoods::getApplyId, bo.getApplyId());
        lqw.eq(bo.getGoodsId() != null, ExamApplyGoods::getGoodsId, bo.getGoodsId());
        return entity2Vo(this.list(lqw));
    }

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

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

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

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

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

    @Override
    public boolean addGoods(ExamApplyGoodsAddBo bo) {
        ExamApply examApply = new ExamApply();
        examApply.setApplyId(bo.getApplyId());
        examApply.setEducationTypeId(bo.getEducationTypeId());
        examApply.setProjectId(bo.getProjectId());
        examApply.setUpdateTime(DateUtils.getNowTime());
        examApply.setBusinessId(bo.getBusinessId());
        examApply.setGoodsType(bo.getGoodsType());
        examApplyService.updateById(examApply);
        LambdaQueryWrapper<ExamApplyGoods> lqw = Wrappers.lambdaQuery();
        lqw.eq(ExamApplyGoods::getApplyId, bo.getApplyId());
        this.remove(lqw);
        for (Long goodsId : bo.getGoodsId()) {
            ExamApplyGoods add = new ExamApplyGoods();
            add.setCreateTime(DateUtils.getNowTime());
            add.setUpdateTime(DateUtils.getNowTime());
            add.setApplyId(bo.getApplyId());
            add.setGoodsId(goodsId);
            this.save(add);
        }
        return true;
    }
}
