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.vo.CourseProjectTypeVo;
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.CourseBusinessAddBo;
import com.zhongzheng.modules.course.bo.CourseBusinessQueryBo;
import com.zhongzheng.modules.course.bo.CourseBusinessEditBo;
import com.zhongzheng.modules.course.domain.CourseBusiness;
import com.zhongzheng.modules.course.mapper.CourseBusinessMapper;
import com.zhongzheng.modules.course.vo.CourseBusinessVo;
import com.zhongzheng.modules.course.service.ICourseBusinessService;
import org.springframework.util.CollectionUtils;

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 CourseBusinessServiceImpl extends ServiceImpl<CourseBusinessMapper, CourseBusiness> implements ICourseBusinessService {

    @Override
    public CourseBusinessVo queryById(Long id){
        CourseBusinessQueryBo courseBusinessQueryBo = new CourseBusinessQueryBo();
        courseBusinessQueryBo.setId(id);
        List<CourseBusinessVo> courseBusinessVos = entity2Vo(baseMapper.queryList(courseBusinessQueryBo));
        if (CollectionUtils.isEmpty(courseBusinessVos)){
            throw new IllegalArgumentException("请输入正确业务ID");
        }
        return BeanUtil.toBean(courseBusinessVos.get(0), CourseBusinessVo.class);
    }

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

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

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

    @Override
    public Boolean updateByEditBo(CourseBusinessEditBo bo) {
        CourseBusiness update = BeanUtil.toBean(bo, CourseBusiness.class);
        if (bo.getStatus() != -1) {
            CourseBusinessQueryBo courseBusinessQueryBo = new CourseBusinessQueryBo();
            courseBusinessQueryBo.setBusinessName(bo.getBusinessName());
            courseBusinessQueryBo.setProjectId(bo.getProjectId());
            List<Integer> longs = new ArrayList<>();
            longs.add(1);
            longs.add(0);
            courseBusinessQueryBo.setStatus(longs);
            courseBusinessQueryBo.setId(update.getId());
            Collection<CourseBusinessVo> courseBusinessVos = baseMapper.queryList(courseBusinessQueryBo);
            if (CollectionUtils.isEmpty(courseBusinessVos)) {
                validEntityBeforeSave(update);
            }
        }
        update.setUpdateTime(DateUtils.getNowTime());
        return this.updateById(update);
    }

    /**
     * 保存前的数据校验
     *
     * @param entity 实体类数据
     */
    private void validEntityBeforeSave(CourseBusiness entity){
        CourseBusinessQueryBo courseBusinessQueryBo = new CourseBusinessQueryBo();
        courseBusinessQueryBo.setBusinessName(entity.getBusinessName());
        courseBusinessQueryBo.setProjectId(entity.getProjectId());
        List<Integer> longs = new ArrayList<>();
        longs.add(1);
        longs.add(0);
        courseBusinessQueryBo.setStatus(longs);
        Collection<CourseBusinessVo> courseBusinessVos = baseMapper.queryList(courseBusinessQueryBo);
        if (!CollectionUtils.isEmpty(courseBusinessVos)){
            throw new RuntimeException("业务重复");
        }
        //TODO 做一些数据校验,如唯一约束
    }

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

    @Override
    public boolean editModel(CourseBusinessEditBo bo) {
        for (Long businessId : bo.getBusinessIds()) {
            CourseBusiness update =new CourseBusiness();
            update.setUpdateTime(DateUtils.getNowTime());
            update.setId(businessId);
            update.setTemplateStatus(bo.getTemplateStatus());
            this.updateById(update);
        }

        return true;
    }
}
