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

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import ink.wgink.common.base.DefaultBaseService;
import ink.wgink.exceptions.SearchException;
import ink.wgink.module.examine.dao.question.IQuestionTypeDao;
import ink.wgink.module.examine.pojo.bos.question.QuestionTypeBO;
import ink.wgink.module.examine.pojo.dtos.question.QuestionTypeDTO;
import ink.wgink.module.examine.pojo.pos.question.QuestionTypePO;
import ink.wgink.module.examine.pojo.vos.question.QuestionTypeBatchVO;
import ink.wgink.module.examine.pojo.vos.question.QuestionTypeVO;
import ink.wgink.module.examine.service.question.IQuestionTypeService;
import ink.wgink.pojo.ListPage;
import ink.wgink.pojo.dtos.ZTreeDTO;
import ink.wgink.pojo.result.SuccessResultList;
import ink.wgink.util.ReflectUtil;
import ink.wgink.util.UUIDUtil;
import ink.wgink.util.map.HashMapUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

/**
 * @ClassName: QuestionTypeServiceImpl
 * @Description: 试题类型
 * @Author: CodeFactory
 * @Date: 2021-04-04 11:40:13
 * @Version: 3.0
 **/
@Service
public class QuestionTypeServiceImpl extends DefaultBaseService implements IQuestionTypeService {

    @Autowired
    private IQuestionTypeDao questionTypeDao;

    @Override
    public void save(QuestionTypeVO questionTypeVO) throws ReflectUtil.ReflectException {
        saveReturnId(questionTypeVO);
    }

    @Override
    public void saveBatch(QuestionTypeBatchVO questionTypeBatchVO) {
        List<String> typeNames = Arrays.asList(questionTypeBatchVO.getTypeNames().split(","));
        String userId = securityComponent.getCurrentUser().getUserId();
        Map<String, Object> params = getHashMap(8);
        params.put("questionTypeParentId", questionTypeBatchVO.getQuestionTypeParentId());
        setSaveInfo(params);
        for (String typeName : typeNames) {
            typeName = typeName.trim().replaceAll("\\s", "");
            if (getPO(userId, typeName) != null) {
                continue;
            }
            String questionTypeId = UUIDUtil.getUUID();
            params.put("questionTypeId", questionTypeId);
            params.put("typeName", typeName);
            params.put("typeSummary", typeName);
            questionTypeDao.save(params);
        }
    }

    @Override
    public void save(String token, QuestionTypeVO questionTypeVO) throws ReflectUtil.ReflectException {
        saveReturnId(token, questionTypeVO);
    }

    @Override
    public String saveReturnId(QuestionTypeVO questionTypeVO) throws ReflectUtil.ReflectException {
        return saveReturnId(null, questionTypeVO);
    }

    @Override
    public String saveReturnId(String token, QuestionTypeVO questionTypeVO) throws ReflectUtil.ReflectException {
        String questionTypeId = UUIDUtil.getUUID();
        Map<String, Object> params = HashMapUtil.beanToMap(questionTypeVO);
        params.put("questionTypeId", questionTypeId);
        String userId;
        if (StringUtils.isBlank(token)) {
            userId = securityComponent.getCurrentUser().getUserId();
            setSaveInfo(params);
        } else {
            userId = securityComponent.getAppTokenUser(token).getId();
            setAppSaveInfo(token, params);
        }
        if (getPO(userId, questionTypeVO.getTypeName()) != null) {
            throw new SearchException("类型已经存在");
        }
        questionTypeDao.save(params);
        return questionTypeId;
    }

    @Override
    public void remove(List<String> ids) {
        remove(null, ids);
    }

    @Override
    public void remove(String token, List<String> ids) {
        Map<String, Object> params = getHashMap(2);
        params.put("questionTypeIds", ids);
        if (StringUtils.isBlank(token)) {
            setUpdateInfo(params);
        } else {
            setAppUpdateInfo(token, params);
        }
        questionTypeDao.remove(params);
    }

    @Override
    public void delete(List<String> ids) {
        Map<String, Object> params = getHashMap(2);
        params.put("questionTypeIds", ids);
        questionTypeDao.delete(params);
    }

    @Override
    public void update(String questionTypeId, QuestionTypeVO questionTypeVO) throws ReflectUtil.ReflectException {
        update(null, questionTypeId, questionTypeVO);
    }

    @Override
    public void update(String token, String questionTypeId, QuestionTypeVO questionTypeVO) throws ReflectUtil.ReflectException {
        Map<String, Object> params = HashMapUtil.beanToMap(questionTypeVO);
        params.put("questionTypeId", questionTypeId);
        String userId;
        if (StringUtils.isBlank(token)) {
            userId = securityComponent.getCurrentUser().getUserId();
            setUpdateInfo(params);
        } else {
            userId = securityComponent.getAppTokenUser(token).getId();
            setAppUpdateInfo(token, params);
        }
        QuestionTypePO questionTypePO = getPO(userId, questionTypeVO.getTypeName());
        if (questionTypePO != null &&
                StringUtils.equals(questionTypePO.getCreator(), userId) &&
                !StringUtils.equals(questionTypePO.getQuestionTypeId(), questionTypeId)) {
            throw new SearchException("类型已经存在");
        }
        questionTypeDao.update(params);
    }

    @Override
    public QuestionTypeDTO get(Map<String, Object> params) {
        return questionTypeDao.get(params);
    }

    @Override
    public QuestionTypeDTO get(String questionTypeId) {
        Map<String, Object> params = super.getHashMap(2);
        params.put("questionTypeId", questionTypeId);
        return get(params);
    }

    @Override
    public QuestionTypeBO getBO(Map<String, Object> params) {
        return questionTypeDao.getBO(params);
    }

    @Override
    public QuestionTypeBO getBO(String questionTypeId) {
        Map<String, Object> params = super.getHashMap(2);
        params.put("questionTypeId", questionTypeId);
        return getBO(params);
    }

    @Override
    public QuestionTypePO getPO(Map<String, Object> params) {
        return questionTypeDao.getPO(params);
    }

    @Override
    public QuestionTypePO getPO(String questionTypeId) {
        Map<String, Object> params = super.getHashMap(2);
        params.put("questionTypeId", questionTypeId);
        return getPO(params);
    }

    private QuestionTypePO getPO(String userId, String typeName) {
        Map<String, Object> params = getHashMap(4);
        params.put("creator", userId);
        params.put("typeName", typeName);
        return getPO(params);
    }

    @Override
    public List<QuestionTypeDTO> list(Map<String, Object> params) {
        return questionTypeDao.list(params);
    }

    @Override
    public List<QuestionTypeDTO> list(List<String> questionTypeIds) {
        Map<String, Object> params = getHashMap(2);
        params.put("questionTypeIds", questionTypeIds);
        return list(params);
    }

    @Override
    public List<QuestionTypeBO> listBO(Map<String, Object> params) {
        return questionTypeDao.listBO(params);
    }

    @Override
    public List<QuestionTypePO> listPO(Map<String, Object> params) {
        return questionTypeDao.listPO(params);
    }

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

    @Override
    public SuccessResultList<List<QuestionTypeDTO>> listPage(String questionTypeParentId, ListPage page) {
        page.getParams().put("questionTypeParentId", questionTypeParentId);
        page.getParams().put("creator", securityComponent.getCurrentUser().getUserId());
        return listPage(page);
    }

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

    @Override
    public List<ZTreeDTO> listZTree(Map<String, Object> params) {
        params.put("creator", securityComponent.getCurrentUser().getUserId());
        List<ZTreeDTO> zTreeDTOs = questionTypeDao.listZTree(params);
        for (ZTreeDTO zTreeDTO : zTreeDTOs) {
            params.put("questionTypeParentId", zTreeDTO.getId());
            Integer subCount = questionTypeDao.countByParentId(params);
            setZTreeInfo(zTreeDTO, subCount);
        }
        return zTreeDTOs;
    }

}