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

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import com.zhongzheng.common.utils.DateUtils;
import com.zhongzheng.modules.exam.bo.ExamApplySiteTimeAddBo;
import com.zhongzheng.modules.exam.domain.ExamApplySiteTime;
import com.zhongzheng.modules.exam.service.IExamApplySiteTimeService;
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.exam.bo.ExamApplySiteAddBo;
import com.zhongzheng.modules.exam.bo.ExamApplySiteQueryBo;
import com.zhongzheng.modules.exam.bo.ExamApplySiteEditBo;
import com.zhongzheng.modules.exam.domain.ExamApplySite;
import com.zhongzheng.modules.exam.mapper.ExamApplySiteMapper;
import com.zhongzheng.modules.exam.vo.ExamApplySiteVo;
import com.zhongzheng.modules.exam.service.IExamApplySiteService;

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

/**
 * 考试安排地点Service业务层处理
 *
 * @author ruoyi
 * @date 2021-12-07
 */
@Service
public class ExamApplySiteServiceImpl extends ServiceImpl<ExamApplySiteMapper, ExamApplySite> implements IExamApplySiteService {

    @Autowired
    private IExamApplySiteTimeService examApplySiteTimeService;

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

    @Override
    public List<ExamApplySiteVo> queryList(ExamApplySiteQueryBo bo) {
        LambdaQueryWrapper<ExamApplySite> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getSiteId() != null, ExamApplySite::getSiteId, bo.getSiteId());
        lqw.eq(bo.getApplyId() != null, ExamApplySite::getApplyId, bo.getApplyId());
        lqw.eq(bo.getStatus() != null, ExamApplySite::getStatus, bo.getStatus());
        return entity2Vo(this.list(lqw));
    }

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

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

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

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

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

    @Override
    public boolean addSite(List<ExamApplySiteAddBo> bo) {
        LambdaQueryWrapper<ExamApplySite> lqw = Wrappers.lambdaQuery();
        lqw.eq( ExamApplySite::getApplyId,bo.get(0).getApplyId());
        lqw.eq( ExamApplySite::getStatus,bo.get(0).getStatus());
        this.remove(lqw);
        for (ExamApplySiteAddBo examApplySiteAddBo : bo) {
            ExamApplySite add = BeanUtil.toBean(examApplySiteAddBo, ExamApplySite.class);
            validEntityBeforeSave(add);
            add.setCreateTime(DateUtils.getNowTime());
            add.setUpdateTime(DateUtils.getNowTime());
            this.save(add);
            for (ExamApplySiteTimeAddBo examApplySiteTimeAddBo : examApplySiteAddBo.getExamApplySiteTime()) {
                ExamApplySiteTime examApplySiteTime = BeanUtil.toBean(examApplySiteTimeAddBo, ExamApplySiteTime.class);
                examApplySiteTime.setApplySiteId(add.getId());
                examApplySiteTime.setApplyId(add.getApplyId());
                examApplySiteTime.setSiteTime(JSONUtil.toJsonStr(examApplySiteTimeAddBo.getExamApplySiteTimeTwo()));
                examApplySiteTime.setCreateTime(DateUtils.getNowTime());
                examApplySiteTime.setUpdateTime(DateUtils.getNowTime());
                examApplySiteTimeService.save(examApplySiteTime);
            }
        }
        return true;
    }
}
