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

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.zhongzheng.common.utils.DateUtils;
import com.zhongzheng.modules.user.bo.SchoolAddBo;
import com.zhongzheng.modules.user.bo.SchoolEditBo;
import com.zhongzheng.modules.user.bo.SchoolQueryBo;
import com.zhongzheng.modules.user.domain.School;
import com.zhongzheng.modules.user.mapper.SchoolMapper;
import com.zhongzheng.modules.user.mapper.UserSchoolInfoMapper;
import com.zhongzheng.modules.user.service.ISchoolService;
import com.zhongzheng.modules.user.vo.SchoolVo;
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 java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 高校Service业务层处理
 *
 * @author hjl
 * @date 2021-05-21
 */
@Service
public class SchoolServiceImpl extends ServiceImpl<SchoolMapper, School> implements ISchoolService {

    @Autowired
    private SchoolMapper schoolMapper;

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

    @Override
    public List<SchoolVo> queryList(SchoolQueryBo bo) {
        LambdaQueryWrapper<School> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getSchoolTypeId() != null, School::getSchoolTypeId, bo.getSchoolTypeId());
        lqw.like(StrUtil.isNotBlank(bo.getSchoolName()), School::getSchoolName, bo.getSchoolName());
        lqw.in(bo.getStatus() != null, School::getStatus, bo.getStatus());
        lqw.eq(StrUtil.isNotBlank(bo.getIntroduction()), School::getIntroduction, bo.getIntroduction());
        lqw.eq(StrUtil.isNotBlank(bo.getSchoolCity()), School::getSchoolCity, bo.getSchoolCity());
        lqw.eq(StrUtil.isNotBlank(bo.getAdvantageSpecialty()), School::getAdvantageSpecialty, bo.getAdvantageSpecialty());
        lqw.eq(StrUtil.isNotBlank(bo.getContact()), School::getContact, bo.getContact());
        lqw.eq(StrUtil.isNotBlank(bo.getPhone()), School::getPhone, bo.getPhone());
        lqw.eq(StrUtil.isNotBlank(bo.getAddress()), School::getAddress, bo.getAddress());
        lqw.eq(bo.getDictCode() != null, School::getDictCode, bo.getDictCode());
        return entity2Vo(this.list(lqw));
    }

    @Override
    public List<School> selectList(SchoolQueryBo bo) {
        return schoolMapper.selectList(bo);
    }

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

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

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

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

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