All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.nxyfan.framework.survey.service.impl.WjAnswerServiceImpl Maven / Gradle / Ivy

The newest version!
package com.nxyfan.framework.survey.service.impl;

import java.util.List;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.nxyfan.framework.common.enums.CommonSortOrderEnum;
import com.nxyfan.framework.common.enums.CommonUseMarkEnum;
import com.nxyfan.framework.common.exception.CommonException;
import com.nxyfan.framework.common.page.CommonPageRequest;
import com.nxyfan.framework.survey.entity.WjAnswer;
import com.nxyfan.framework.survey.entity.WjSurvey;
import com.nxyfan.framework.survey.enums.SurveyStateEnum;
import com.nxyfan.framework.survey.mapper.WjAnswerMapper;
import com.nxyfan.framework.survey.param.AnswerPageParam;
import com.nxyfan.framework.survey.param.AnswerSubmitParam;
import com.nxyfan.framework.survey.result.AnswerDetailResult;
import com.nxyfan.framework.survey.service.IWjAnswerService;
import com.nxyfan.framework.survey.service.IWjSurveyService;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.crypto.digest.DigestUtil;

/** 
 *
 * Describe: 调查问卷回答表 服务实现类
 * Author: caoyang  
 * Create Time: 2023年2月23日 上午9:21:27 
 * Copyright @ 2023 51LIFE  
 */
@Service
public class WjAnswerServiceImpl extends ServiceImpl implements IWjAnswerService {

	@Resource
    private IWjSurveyService surveyService;
	
	@Override
	@Transactional(rollbackFor = Exception.class)
	public String answer(AnswerSubmitParam answerParam) {
		WjSurvey survey = this.surveyService.queryEntity(answerParam.getSurveyFlow());
		if(!SurveyStateEnum.START.getValue().equals(survey.getState())) {
			throw CommonException.bizException("该调查问卷还未发放!");
		}
		if(!CommonUseMarkEnum.Y.toString().equals(survey.getUseMark())) {
			throw CommonException.bizException("该调查问卷还未启用!");
		}
		String answerTime = DateUtil.now();
		String answerFlow = DigestUtil.md5Hex(answerParam.getSurveyFlow() + answerParam.getAnswerUserFlow() + answerTime);
		List answerList = answerParam.getAnswerList();
		for(AnswerSubmitParam.Answer answer: answerList) {
			WjAnswer entity = new WjAnswer();
			entity.setAnswerFlow(answerFlow);
			entity.setSurveyFlow(answerParam.getSurveyFlow());
			entity.setQuestionFlow(answer.getQuestionFlow());
			entity.setAnswerContent(answer.getAnswerContent());
			entity.setAnswerUserName(answerParam.getAnswerUserName());
			entity.setAnswerUserPhone(answerParam.getAnswerUserPhone());
			entity.setAnswerUserSex(answerParam.getAnswerUserSex());
			entity.setCreateUserFlow(answerParam.getAnswerUserFlow());
			entity.setCreateDateTime(answerTime);
			this.save(entity);
		}
		// 更新问卷的回答数量
		WjSurvey entity = new WjSurvey();
		entity.setSurveyFlow(survey.getSurveyFlow());
		entity.setAnswerNumber(survey.getAnswerNumber() + 1);
		entity.setUpdateDateTime(answerTime);
		this.surveyService.updateById(entity);
		return answerFlow;
	}

	@Override
	public Page page(AnswerPageParam pageParam) {
		QueryWrapper queryWrapper = new QueryWrapper<>();
		queryWrapper.select("distinct ANSWER_USER_NAME,ANSWER_USER_PHONE,CREATE_DATE_TIME,SURVEY_FLOW,ANSWER_FLOW");
        if(ObjectUtil.isNotEmpty(pageParam.getSurveyFlow())) {
            queryWrapper.lambda().eq(WjAnswer::getSurveyFlow, pageParam.getSurveyFlow());
        }
        if(ObjectUtil.isNotEmpty(pageParam.getStartTime()) && ObjectUtil.isNotEmpty(pageParam.getEndTime())) {
        	queryWrapper.lambda().between(WjAnswer::getCreateDateTime, pageParam.getStartTime(), pageParam.getEndTime());
        }else if(ObjectUtil.isNotEmpty(pageParam.getStartTime())) {
        	queryWrapper.lambda().ge(WjAnswer::getCreateDateTime, pageParam.getStartTime());
        }else if(ObjectUtil.isNotEmpty(pageParam.getEndTime())) {
        	queryWrapper.lambda().le(WjAnswer::getCreateDateTime, pageParam.getEndTime());
        }
        if(ObjectUtil.isAllNotEmpty(pageParam.getSortField(), pageParam.getSortOrder())) {
            CommonSortOrderEnum.validate(pageParam.getSortOrder());
            queryWrapper.orderBy(true, pageParam.getSortOrder().equals(CommonSortOrderEnum.ASC.getValue()), StrUtil.toUnderlineCase(pageParam.getSortField()));
        } else {
            queryWrapper.lambda().orderByAsc(WjAnswer::getCreateDateTime);
        }
        return this.page(CommonPageRequest.defaultPage(pageParam, null), queryWrapper);
	}

	@Override
	public AnswerDetailResult detail(String surveyFlow, String answerFlow) {
		WjSurvey survey = this.surveyService.queryEntity(surveyFlow);
		AnswerDetailResult result = BeanUtil.toBean(survey, AnswerDetailResult.class);
		List answerList = this.list(new LambdaQueryWrapper().eq(WjAnswer::getAnswerFlow, answerFlow).orderByAsc(WjAnswer::getSeq));
		result.setAnswerList(answerList);
		return result;
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy