package com.sundot.airport.exam.logic.manage.service.impl;

import com.github.pagehelper.PageInfo;
import com.sundot.airport.exam.common.CheckUtil;
import com.sundot.airport.exam.common.HttpResult;
import com.sundot.airport.exam.common.PageData;
import com.sundot.airport.exam.common.service.QuesCatService;
import com.sundot.airport.exam.common.service.QuesService;
import com.sundot.airport.exam.domain.QuesCat;
import com.sundot.airport.exam.enums.CloudSchoolBizExceptionEnum;
import com.sundot.airport.exam.logic.manage.model.params.QuesCatAddParam;
import com.sundot.airport.exam.logic.manage.model.params.QuesCatDelParam;
import com.sundot.airport.exam.logic.manage.model.params.QuesCatPageParam;
import com.sundot.airport.exam.logic.manage.model.params.QuesCatUpdParam;
import com.sundot.airport.exam.logic.manage.model.result.QuesCatAddResult;
import com.sundot.airport.exam.logic.manage.model.result.QuesCatGetResult;
import com.sundot.airport.exam.logic.manage.model.result.QuesCatPageResult;
import com.sundot.airport.exam.logic.manage.service.QuesCatBizService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * <b>功能名：</b>QuesCatBizServiceImpl<br>
 * <b>说明：</b> 题目分类业务类 <br>
 * <b>著作权：</b> Copyright (C) 2021 HUIFANEDU  CORPORATION<br>
 * <b>修改履历：
 *
 * @author 2021-06-17 liangxiao
 */
@Service
public class QuesCatBizServiceImpl implements QuesCatBizService {

    private static final Logger log = LoggerFactory.getLogger(QuesCatBizServiceImpl.class);

    @Resource
    private QuesCatService quesCatService;

    @Resource
    private QuesService quesService;

    @Override
    public HttpResult<QuesCatAddResult> add(QuesCatAddParam param) {
        log.info(String.format("QuesCatBizServiceImpl.add:新增分类:param=%s", param.toString()));
        // 根据名称查询记录
        Optional<QuesCat> nameCheck = quesCatService.byNameCheckExistsGet(param.getQcName());
        // 名称存在记录
        CheckUtil.boolCheck(nameCheck.isPresent(), CloudSchoolBizExceptionEnum.DUPLICATE_NAME);

        // 复制对象
        QuesCat entity = new QuesCat();
        BeanUtils.copyProperties(param, entity);
        // 保存数据
        quesCatService.insert(entity);
        // 封装结果
        QuesCatAddResult result = new QuesCatAddResult();
        result.setQcId(entity.getQcId());
        return HttpResult.success(result);
    }

    @Override
    public HttpResult<Boolean> delete(QuesCatDelParam param) {
        //删除题干分类，将该题干下的题目转到未分类
        quesService.byQcIdUpdateQcId(param.getQcId(), "");
        return HttpResult.success(quesCatService.delete(param.getQcId()));
    }

    @Override
    public HttpResult<Boolean> update(QuesCatUpdParam param) {
        log.info(String.format("QuesCatBizServiceImpl.update:修改分类:param=%s", param.toString()));
        // 根据名称查询记录
        Optional<QuesCat> nameCheck = quesCatService.byNameCheckExistsGet(param.getQcName());
        // 名称存在记录
        CheckUtil.boolCheck(nameCheck.map(i -> !i.getQcId().equals(param.getQcId())).orElse(false), CloudSchoolBizExceptionEnum.DUPLICATE_NAME);

        QuesCat entity = new QuesCat();
        BeanUtils.copyProperties(param, entity);
        return HttpResult.success(quesCatService.update(entity));
    }

    @Override
    public HttpResult<QuesCatGetResult> get(String qcId) {
        // 查询
        Optional<QuesCat> entity = quesCatService.get(qcId);
        CheckUtil.boolCheck(!entity.isPresent(), CloudSchoolBizExceptionEnum.OBJ_NOT_FOUND);
        // 封装结果
        QuesCatGetResult result = new QuesCatGetResult();
        BeanUtils.copyProperties(entity.get(), result);
        return HttpResult.success(result);
    }

    @Override
    public HttpResult<PageData<QuesCatPageResult>> pageList(QuesCatPageParam param) {
        // 分页条件查询
        PageInfo<com.sundot.airport.exam.common.model.result.QuesCatPageResult> pageInfo = quesCatService.pageList(param);
        // 组装分页数据
        PageData<QuesCatPageResult> pageData = new PageData<>(pageInfo);
        pageData.setList(pageInfo.getList().stream().map(res -> new QuesCatPageResult() {
            {
                BeanUtils.copyProperties(res, this);
            }
        }).collect(Collectors.toList()));

        return HttpResult.success(pageData);
    }

}
