package com.zhongzheng.modules.exam.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 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.exam.bo.ExamSiteAddBo;
import com.zhongzheng.modules.exam.bo.ExamSiteQueryBo;
import com.zhongzheng.modules.exam.bo.ExamSiteEditBo;
import com.zhongzheng.modules.exam.domain.ExamSite;
import com.zhongzheng.modules.exam.mapper.ExamSiteMapper;
import com.zhongzheng.modules.exam.vo.ExamSiteVo;
import com.zhongzheng.modules.exam.service.IExamSiteService;

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

/**
 * 考试地点Service业务层处理
 *
 * @author ruoyi
 * @date 2021-12-06
 */
@Service
public class ExamSiteServiceImpl extends ServiceImpl<ExamSiteMapper, ExamSite> implements IExamSiteService {

    @Override
    public ExamSiteVo queryById(Long siteId){
        ExamSite db = this.baseMapper.selectById(siteId);
        return BeanUtil.toBean(db, ExamSiteVo.class);
    }

    @Override
    public List<ExamSiteVo> queryList(ExamSiteQueryBo bo) {
        LambdaQueryWrapper<ExamSite> lqw = Wrappers.lambdaQuery();
        lqw.eq(StrUtil.isNotBlank(bo.getCode()), ExamSite::getCode, bo.getCode());
        lqw.eq(StrUtil.isNotBlank(bo.getSiteAddress()), ExamSite::getSiteAddress, bo.getSiteAddress());
        lqw.eq(bo.getPeople() != null, ExamSite::getPeople, bo.getPeople());
        lqw.in(CollectionUtils.isNotEmpty(bo.getStatus()), ExamSite::getStatus, bo.getStatus());
        lqw.orderByDesc(ExamSite::getCreateTime);
        return entity2Vo(this.list(lqw));
    }

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

    @Override
    public Boolean insertByAddBo(ExamSiteAddBo bo) {
        ExamSite add = BeanUtil.toBean(bo, ExamSite.class);
        LambdaQueryWrapper<ExamSite> lqw = Wrappers.lambdaQuery();
        lqw.eq(ExamSite::getSiteAddress, bo.getSiteAddress());
        lqw.eq(ExamSite::getStatus, 1);
        List<ExamSite> list = this.list(lqw);
        if (CollectionUtils.isNotEmpty(list)){
            throw new IllegalArgumentException("地址重复，请重新填写");
        }
        validEntityBeforeSave(add);
        add.setCode(ServletUtils.getEncoded("DD"));
        add.setCreateTime(DateUtils.getNowTime());
        add.setUpdateTime(DateUtils.getNowTime());
        return this.save(add);
    }

    @Override
    public Boolean updateByEditBo(ExamSiteEditBo bo) {
        ExamSite update = BeanUtil.toBean(bo, ExamSite.class);
        validEntityBeforeSave(update);
        update.setUpdateTime(DateUtils.getNowTime());
        if (bo.getStatus() != -1) {
            LambdaQueryWrapper<ExamSite> lqw = Wrappers.lambdaQuery();
            lqw.eq(ExamSite::getSiteAddress, bo.getSiteAddress());
            lqw.eq(ExamSite::getStatus, 1);
            lqw.eq(ExamSite::getSiteId, bo.getSiteId());
            List<ExamSite> list = this.list(lqw);
            if (CollectionUtils.isEmpty(list)) {
                lqw.clear();
                lqw.eq(ExamSite::getSiteAddress, bo.getSiteAddress());
                lqw.eq(ExamSite::getStatus, 1);
                List<ExamSite> examSiteList = this.list(lqw);
                if (CollectionUtils.isNotEmpty(examSiteList)){
                    throw new IllegalArgumentException("地址重复，请重新填写");
                }
            }
        }
        return this.updateById(update);
    }

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

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