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.TestPaperCatService;
import com.sundot.airport.exam.common.service.TestPaperService;
import com.sundot.airport.exam.domain.TestPaperCat;
import com.sundot.airport.exam.enums.CloudSchoolBizExceptionEnum;
import com.sundot.airport.exam.logic.manage.model.params.TestPaperCatAddParam;
import com.sundot.airport.exam.logic.manage.model.params.TestPaperCatDelParam;
import com.sundot.airport.exam.logic.manage.model.params.TestPaperCatPageParam;
import com.sundot.airport.exam.logic.manage.model.params.TestPaperCatUpdParam;
import com.sundot.airport.exam.logic.manage.model.result.TestPaperCatAddResult;
import com.sundot.airport.exam.logic.manage.model.result.TestPaperCatGetResult;
import com.sundot.airport.exam.logic.manage.model.result.TestPaperCatPageResult;
import com.sundot.airport.exam.logic.manage.service.TestPaperCatBizService;
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>TestPaperCatBizServiceImpl<br>
 * <b>说明：</b> 试卷分类业务类 <br>
 * <b>著作权：</b> Copyright (C) 2021 HUIFANEDU  CORPORATION<br>
 * <b>修改履历：
 *
 * @author 2021-06-21 liangxiao
 */
@Service
public class TestPaperCatBizServiceImpl implements TestPaperCatBizService {

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

    @Resource
    private TestPaperCatService testPaperCatService;

    @Resource
    private TestPaperService paperService;

    @Override
    public HttpResult<TestPaperCatAddResult> add(TestPaperCatAddParam param) {
        // 根据名称查询记录
        Optional<TestPaperCat> nameCheck = testPaperCatService.byNameCheckExistsGet(param.getTpcName());
        // 名称存在记录
        CheckUtil.boolCheck(nameCheck.isPresent(), CloudSchoolBizExceptionEnum.DUPLICATE_NAME);

        // 复制对象
        TestPaperCat entity = new TestPaperCat();
        BeanUtils.copyProperties(param, entity);
        // 保存数据
        testPaperCatService.insert(entity);
        // 封装结果
        TestPaperCatAddResult result = new TestPaperCatAddResult();
        result.setTpcId(entity.getTpcId());
        return HttpResult.success(result);
    }

    @Override
    public HttpResult<Boolean> delete(TestPaperCatDelParam param) {
        //删除试卷分类，将该试卷下的题目转到未分类
        paperService.byTpcIdUpdateTpcId(param.getTpcId(), "");
        return HttpResult.success(testPaperCatService.delete(param.getTpcId()));
    }

    @Override
    public HttpResult<Boolean> update(TestPaperCatUpdParam param) {
        log.info(String.format("TestPaperCatBizServiceImpl.update:修改试卷分类:param=%s", param.toString()));
        // 根据名称查询记录
        Optional<TestPaperCat> nameCheck = testPaperCatService.byNameCheckExistsGet(param.getTpcName());
        // 名称存在记录
        CheckUtil.boolCheck(nameCheck.map(i -> !i.getTpcId().equals(param.getTpcId())).orElse(false), CloudSchoolBizExceptionEnum.DUPLICATE_NAME);

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

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

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

        return HttpResult.success(pageData);
    }

}
