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

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.lang.Validator;
import cn.hutool.core.util.StrUtil;
import com.zhongzheng.common.exception.CustomException;
import com.zhongzheng.common.utils.DateUtils;
import com.zhongzheng.common.utils.ServletUtils;
import com.zhongzheng.modules.base.bo.ApplyAreasAddBo;
import com.zhongzheng.modules.base.bo.ApplyAreasEditBo;
import com.zhongzheng.modules.base.bo.ApplyAreasQueryBo;
import com.zhongzheng.modules.base.domain.ApplyAreas;
import com.zhongzheng.modules.base.mapper.ApplyAreasMapper;
import com.zhongzheng.modules.base.service.IApplyAreasService;
import com.zhongzheng.modules.base.vo.ApplyAreasVo;
import com.zhongzheng.modules.course.domain.MajorCategory;
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.List;
import java.util.stream.Collectors;

/**
 * 报考地区Service业务层处理
 *
 * @author hjl
 * @date 2021-10-08
 */
@Service
public class ApplyAreasServiceImpl extends ServiceImpl<ApplyAreasMapper, ApplyAreas> implements IApplyAreasService {


    @Autowired
    private ApplyAreasMapper applyAreasMapper;

    @Override
    public ApplyAreasVo queryById(Long areaId){
        ApplyAreas db = this.baseMapper.selectById(areaId);
        return BeanUtil.toBean(db, ApplyAreasVo.class);
    }

    @Override
    public List<ApplyAreasVo> queryList(ApplyAreasQueryBo bo) {
        LambdaQueryWrapper<ApplyAreas> lqw = Wrappers.lambdaQuery();
        lqw.eq(StrUtil.isNotBlank(bo.getCode()), ApplyAreas::getCode, bo.getCode());
        lqw.like(StrUtil.isNotBlank(bo.getAreaName()), ApplyAreas::getAreaName, bo.getAreaName());
        lqw.like(StrUtil.isNotBlank(bo.getAreaFullName()), ApplyAreas::getAreaFullName, bo.getAreaFullName());
        lqw.eq(bo.getParentId() != null, ApplyAreas::getParentId, bo.getParentId());
        lqw.eq(bo.getAreaType() != null, ApplyAreas::getAreaType, bo.getAreaType());
        lqw.in(bo.getStatus() != null, ApplyAreas::getStatus, bo.getStatus());
        return entity2Vo(this.list(lqw));
    }

    @Override
    public List<ApplyAreas> selectCityList(ApplyAreasQueryBo bo) {
        return applyAreasMapper.selectCityList(bo);
    }

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

    @Override
    public Boolean insertByAddBo(ApplyAreasAddBo bo) {
        ApplyAreas add = BeanUtil.toBean(bo, ApplyAreas.class);
        add.setCode(ServletUtils.getEncoded("SS"));
        validEntityBeforeSave(add);
        add.setCreateTime(DateUtils.getNowTime());
        add.setUpdateTime(DateUtils.getNowTime());
        return this.save(add);
    }

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

    /**
     * 保存前的数据校验
     *
     * @param entity 实体类数据
     */
    private void validEntityBeforeSave(ApplyAreas entity){
        //TODO 做一些数据校验,如唯一约束
        if(checkNameUnique(entity)&&Validator.isEmpty(entity.getAreaId())){
            throw new CustomException("地区名重复");
        }
    }

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

    private boolean checkNameUnique(ApplyAreas entity) {
        ApplyAreas info = getOne(new LambdaQueryWrapper<ApplyAreas>()
                .eq(ApplyAreas::getAreaName,entity.getAreaName()).ne(ApplyAreas::getStatus,-1).last("limit 1"));
        if (Validator.isNotNull(info)) {
            return true;
        }
        return false;
    }
}
