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

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.zhongzheng.common.utils.DateUtils;
import com.zhongzheng.common.utils.ServletUtils;
import com.zhongzheng.modules.course.bo.CourseProjectTypeQueryBo;
import com.zhongzheng.modules.course.domain.Major;
import com.zhongzheng.modules.course.mapper.MajorMapper;
import com.zhongzheng.modules.course.service.IMajorService;
import com.zhongzheng.modules.course.vo.CourseProjectTypeVo;
import com.zhongzheng.modules.course.vo.MajorVo;
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.course.bo.MajorLabelAddBo;
import com.zhongzheng.modules.course.bo.MajorLabelQueryBo;
import com.zhongzheng.modules.course.bo.MajorLabelEditBo;
import com.zhongzheng.modules.course.domain.MajorLabel;
import com.zhongzheng.modules.course.mapper.MajorLabelMapper;
import com.zhongzheng.modules.course.vo.MajorLabelVo;
import com.zhongzheng.modules.course.service.IMajorLabelService;

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

/**
 * 标签Service业务层处理
 *
 * @author ruoyi
 * @date 2021-10-09
 */
@Service
public class MajorLabelServiceImpl extends ServiceImpl<MajorLabelMapper, MajorLabel> implements IMajorLabelService {

    @Autowired
    private IMajorService majorService;



    @Override
    public MajorLabelVo queryById(Long id){
        MajorLabel db = this.baseMapper.selectById(id);
        MajorLabelVo majorLabelVo = BeanUtil.toBean(db, MajorLabelVo.class);
        LambdaQueryWrapper<Major> lqw = Wrappers.lambdaQuery();
        lqw.eq( Major::getLabelId, majorLabelVo.getId());
        List<Major> list = majorService.list(lqw);
        majorLabelVo.setMajorVos(entityMajorVo(list));
        return majorLabelVo;
    }

    @Override
    public List<MajorLabelVo> queryList(MajorLabelQueryBo bo) {
        return entity2Vo(baseMapper.queryList(bo));
    }

    private List<MajorVo> entityMajorVo(Collection<Major> collection) {
        List<MajorVo> voList = collection.stream()
                .map(any -> BeanUtil.toBean(any, MajorVo.class))
                .collect(Collectors.toList());
        if (collection instanceof Page) {
            Page<Major> page = (Page<Major>)collection;
            Page<MajorVo> pageVo = new Page<>();
            BeanUtil.copyProperties(page,pageVo);
            pageVo.addAll(voList);
            voList = pageVo;
        }
        return voList;
    }

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

    @Override
    public Boolean insertByAddBo(MajorLabelAddBo bo) {
        MajorLabel add = BeanUtil.toBean(bo, MajorLabel.class);
        validEntityBeforeSave(add);
        add.setCreateTime(DateUtils.getNowTime());
        add.setUpdateTime(DateUtils.getNowTime());
        add.setEncoder(ServletUtils.getEncoded("BQ"));
        return this.save(add);
    }

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

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

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

    @Override
    public List<MajorLabelVo> listMajor(MajorLabelQueryBo bo) {
        return entity2Vo(baseMapper.listMajor(bo));
    }

    @Override
    public boolean editMajor(MajorLabelEditBo bo) {
        Major major = new Major();
        major.setId(bo.getId());
        major.setLabelId(0L);
        major.setUpdateTime(DateUtils.getNowTime());
        return majorService.updateById(major);
    }
}
