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

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.zhongzheng.common.utils.DateUtils;
import com.zhongzheng.common.utils.ServletUtils;
import com.zhongzheng.modules.base.domain.ApplyAreas;
import com.zhongzheng.modules.course.bo.CourseEducationTierAddBo;
import com.zhongzheng.modules.course.domain.CourseEducationTier;
import com.zhongzheng.modules.course.service.ICourseEducationTierService;
import com.zhongzheng.modules.course.vo.CourseEducationTierVo;
import com.zhongzheng.modules.pay.domain.PayAisle;
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.CourseEducationTypeAddBo;
import com.zhongzheng.modules.course.bo.CourseEducationTypeQueryBo;
import com.zhongzheng.modules.course.bo.CourseEducationTypeEditBo;
import com.zhongzheng.modules.course.domain.CourseEducationType;
import com.zhongzheng.modules.course.mapper.CourseEducationTypeMapper;
import com.zhongzheng.modules.course.vo.CourseEducationTypeVo;
import com.zhongzheng.modules.course.service.ICourseEducationTypeService;
import org.springframework.util.StringUtils;

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-08
 */
@Service
public class CourseEducationTypeServiceImpl extends ServiceImpl<CourseEducationTypeMapper, CourseEducationType> implements ICourseEducationTypeService {

    @Autowired
    private ICourseEducationTierService courseEducationTierService;

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

    @Override
    public List<CourseEducationTypeVo> queryList(CourseEducationTypeQueryBo bo) {
        LambdaQueryWrapper<CourseEducationType> lqw = Wrappers.lambdaQuery();
        lqw.like(StrUtil.isNotBlank(bo.getEducationName()), CourseEducationType::getEducationName, bo.getEducationName());
        lqw.eq(StrUtil.isNotBlank(bo.getEncoder()), CourseEducationType::getEncoder, bo.getEncoder());
        lqw.in(bo.getStatus() != null, CourseEducationType::getStatus, bo.getStatus());
        lqw.orderByDesc(CourseEducationType::getUpdateTime);
        return entity2Vo(this.list(lqw));
    }

    /**
    * 实体类转化成视图对象
    *
    * @param collection 实体类集合
    * @return
    */
    private List<CourseEducationTypeVo> entity2Vo(Collection<CourseEducationType> collection) {
        List<CourseEducationTypeVo> voList = collection.stream()
                .map(any -> BeanUtil.toBean(any, CourseEducationTypeVo.class))
                .collect(Collectors.toList());
        if (collection instanceof Page) {
            Page<CourseEducationType> page = (Page<CourseEducationType>)collection;
            Page<CourseEducationTypeVo> pageVo = new Page<>();
            BeanUtil.copyProperties(page,pageVo);
            pageVo.addAll(voList);
            voList = pageVo;
        }
        for (CourseEducationTypeVo courseEducationTypeVo : voList) {
            LambdaQueryWrapper<CourseEducationTier> lqw = Wrappers.lambdaQuery();
            lqw.eq(CourseEducationTier::getEducationId,courseEducationTypeVo.getId());
            List<CourseEducationTier> list = courseEducationTierService.list(lqw);
            if (!CollectionUtils.isEmpty(list)) {
                courseEducationTypeVo.setTireStatus(list.get(0).getTireStatus());
            }
        }
        return voList;
    }

    @Override
    public Boolean insertByAddBo(CourseEducationTypeAddBo bo) {

        LambdaQueryWrapper<CourseEducationType> lqw = Wrappers.lambdaQuery();
        lqw.like(StrUtil.isNotBlank(bo.getEducationName()), CourseEducationType::getEducationName, bo.getEducationName());
        List<CourseEducationType> list = this.list(lqw);
        if (CollectionUtils.isNotEmpty(list)){
            throw new IllegalArgumentException("教育类型名称重复");
        }

        CourseEducationType add = BeanUtil.toBean(bo, CourseEducationType.class);
        validEntityBeforeSave(add);
        add.setEncoder(ServletUtils.getEncoded("JY"));
        add.setCreateTime(DateUtils.getNowTime());
        add.setUpdateTime(DateUtils.getNowTime());
        boolean save = this.save(add);
        CourseEducationTierAddBo courseEducationTierAddBo = new CourseEducationTierAddBo();
        courseEducationTierAddBo.setTireStatus(bo.getTireStatus());
        courseEducationTierAddBo.setEducationId(add.getId());
        Boolean aBoolean = courseEducationTierService.insertByAddBo(courseEducationTierAddBo);
        return aBoolean;
    }

    @Override
    public Boolean updateByEditBo(CourseEducationTypeEditBo bo) {

        LambdaQueryWrapper<CourseEducationType> lqw = Wrappers.lambdaQuery();
        lqw.like(StrUtil.isNotBlank(bo.getEducationName()), CourseEducationType::getEducationName, bo.getEducationName());
        lqw.like(bo.getId()!= null, CourseEducationType::getId, bo.getId());
        List<CourseEducationType> list = this.list(lqw);
        if (CollectionUtils.isEmpty(list)){
            lqw.clear();
            lqw.eq(StrUtil.isNotBlank(bo.getEducationName()), CourseEducationType::getEducationName, bo.getEducationName());
            List<Integer> status = new ArrayList<>();
            status.add(1);
            status.add(0);
            lqw.in(bo.getStatus() != null, CourseEducationType::getStatus, status);
            List<CourseEducationType> list1 = this.list(lqw);
            if (CollectionUtils.isNotEmpty(list1)){
                throw new IllegalArgumentException("教育类型名称重复");
            }
        }

        CourseEducationType update = BeanUtil.toBean(bo, CourseEducationType.class);
        validEntityBeforeSave(update);
        update.setUpdateTime(DateUtils.getNowTime());

        LambdaQueryWrapper<CourseEducationTier> lqwTier = Wrappers.lambdaQuery();
        lqwTier.eq(CourseEducationTier::getEducationId,bo.getId());
        courseEducationTierService.remove(lqwTier);
        if (!StringUtils.isEmpty(bo.getTireStatus())) {
            CourseEducationTierAddBo courseEducationTierAddBo = new CourseEducationTierAddBo();
            courseEducationTierAddBo.setTireStatus(bo.getTireStatus());
            courseEducationTierAddBo.setEducationId(bo.getId());
            Boolean aBoolean = courseEducationTierService.insertByAddBo(courseEducationTierAddBo);
        }
        return this.updateById(update);
    }

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

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