package ink.wgink.module.examine.service.paper.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import ink.wgink.common.base.DefaultBaseService;
import ink.wgink.exceptions.RemoveException;
import ink.wgink.exceptions.SearchException;
import ink.wgink.module.examine.dao.paper.IPaperDao;
import ink.wgink.module.examine.pojo.dtos.paper.PaperDTO;
import ink.wgink.module.examine.pojo.vos.paper.PaperVO;
import ink.wgink.module.examine.service.paper.IPaperService;
import ink.wgink.pojo.ListPage;
import ink.wgink.pojo.result.SuccessResultList;
import ink.wgink.util.UUIDUtil;
import ink.wgink.util.map.HashMapUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * @ClassName: PaperServiceImpl
 * @Description: 试卷
 * @Author: WenG
 * @Date: 2020-09-11 11:50
 * @Version: 1.0
 **/
@Service
public class PaperServiceImpl extends DefaultBaseService implements IPaperService {

    @Autowired
    private IPaperDao paperDao;

    @Override
    public void save(PaperVO paperVO) throws Exception {
        saveReturnId(paperVO);
    }

    @Override
    public String saveReturnId(PaperVO paperVO) throws Exception {
        String paperId = UUIDUtil.getUUID();
        Map<String, Object> params = HashMapUtil.beanToMap(paperVO);
        params.put("paperId", paperId);
        params.put("paperNumber", "SJ-" + System.currentTimeMillis());
        setSaveInfo(params);
        paperDao.save(params);
        return paperId;
    }

    @Override
    public void remove(List<String> paperIds) throws RemoveException {
        // 已经使用的试卷无法修改
        Map<String, Object> params = getHashMap(3);
        params.put("paperIds", paperIds);
        setUpdateInfo(params);
        paperDao.remove(params);
    }

    @Override
    public void delete(List<String> paperIds) {
        // 已经使用的试卷无法修改
        Map<String, Object> params = getHashMap(3);
        params.put("paperIds", paperIds);
        paperDao.delete(params);
    }

    @Override
    public void update(String paperId, PaperVO paperVO) throws Exception {
        // 已经使用的试卷无法修改
        Map<String, Object> params = HashMapUtil.beanToMap(paperVO);
        params.put("paperId", paperId);
        setUpdateInfo(params);
        paperDao.update(params);
    }

    @Override
    public PaperDTO getById(String paperId) throws SearchException {
        Map<String, Object> params = super.getHashMap(2);
        params.put("paperId", paperId);
        return paperDao.get(params);
    }

    @Override
    public List<PaperDTO> list(Map<String, Object> params) throws SearchException {
        return paperDao.list(params);
    }

    @Override
    public SuccessResultList<List<PaperDTO>> listPage(ListPage page) throws SearchException {
        PageHelper.startPage(page.getPage(), page.getRows());
        List<PaperDTO> paperDTOs = paperDao.list(page.getParams());
        PageInfo<PaperDTO> pageInfo = new PageInfo<>(paperDTOs);
        return new SuccessResultList<>(paperDTOs, pageInfo.getPageNum(), pageInfo.getTotal());
    }

    @Override
    public Integer count(Map<String, Object> params) throws SearchException {
        Integer count = paperDao.count(params);
        return count == null ? 0 : count;
    }

}