package com.jeesite.modules.admin.wenjuan.service;

import java.util.List;

import com.jeesite.modules.admin.wenjuan.dao.AdminQuestionItemDao;
import com.jeesite.modules.admin.wenjuan.dao.AdminQuestionOptionDao;
import com.jeesite.modules.admin.wenjuan.entity.AdminQuestionItem;
import com.jeesite.modules.admin.wenjuan.entity.AdminQuestionOption;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.jeesite.common.entity.Page;
import com.jeesite.common.service.CrudService;

/**
 * 问卷调查项表Service
 *
 * @author wlc
 * @version 2022-06-21
 */
@Service
@Transactional(readOnly = true)
public class AdminQuestionItemService extends CrudService<AdminQuestionItemDao, AdminQuestionItem> {

    @Autowired
    private AdminQuestionOptionDao adminQuestionOptionDao;

    @Autowired
    private AdminQuestionItemDao adminQuestionItemDao;

    /**
     * 获取单条数据
     *
     * @param adminQuestionItem
     * @return
     */
    @Override
    public AdminQuestionItem get(AdminQuestionItem adminQuestionItem) {
        AdminQuestionItem entity = super.get(adminQuestionItem);
        if (entity != null) {
            AdminQuestionOption adminQuestionOption = new AdminQuestionOption(entity);
            adminQuestionOption.setStatus(AdminQuestionOption.STATUS_NORMAL);
            entity.setAdminQuestionOptionList(adminQuestionOptionDao.findList(adminQuestionOption));
        }
        return entity;
    }

    /**
     * 查询分页数据
     *
     * @param adminQuestionItem      查询条件
     * @param adminQuestionItem.page 分页对象
     * @return
     */
    @Override
    public Page<AdminQuestionItem> findPage(AdminQuestionItem adminQuestionItem) {
        return super.findPage(adminQuestionItem);
    }

    /**
     * 查询列表数据
     *
     * @param adminQuestionItem
     * @return
     */
    @Override
    public List<AdminQuestionItem> findList(AdminQuestionItem adminQuestionItem) {
        return super.findList(adminQuestionItem);
    }

    /**
     * 查询子表分页数据
     *
     * @param adminQuestionOption
     * @param adminQuestionOption.page 分页对象
     * @return
     */
    public Page<AdminQuestionOption> findSubPage(AdminQuestionOption adminQuestionOption) {
        Page<AdminQuestionOption> page = adminQuestionOption.getPage();
        page.setList(adminQuestionOptionDao.findList(adminQuestionOption));
        return page;
    }

    /**
     * 保存数据（插入或更新）
     *
     * @param adminQuestionItem
     */
    @Override
    @Transactional(readOnly = false)
    public void save(AdminQuestionItem adminQuestionItem) {
        super.save(adminQuestionItem);
        // 保存 AdminQuestionItem子表
        for (AdminQuestionOption adminQuestionOption : adminQuestionItem.getAdminQuestionOptionList()) {
            if (!AdminQuestionOption.STATUS_DELETE.equals(adminQuestionOption.getStatus())) {
                adminQuestionOption.setQuestionitemId(adminQuestionItem);
                if (adminQuestionOption.getIsNewRecord()) {
                    adminQuestionOptionDao.insert(adminQuestionOption);
                } else {
                    adminQuestionOptionDao.update(adminQuestionOption);
                }
            } else {
                adminQuestionOptionDao.delete(adminQuestionOption);
            }
        }
    }

    /**
     * 更新状态
     *
     * @param adminQuestionItem
     */
    @Override
    @Transactional(readOnly = false)
    public void updateStatus(AdminQuestionItem adminQuestionItem) {
        super.updateStatus(adminQuestionItem);
    }

    /**
     * 删除数据
     *
     * @param adminQuestionItem
     */
    @Override
    @Transactional(readOnly = false)
    public void delete(AdminQuestionItem adminQuestionItem) {
        super.delete(adminQuestionItem);
        AdminQuestionOption adminQuestionOption = new AdminQuestionOption();
        adminQuestionOption.setQuestionitemId(adminQuestionItem);
        adminQuestionOptionDao.deleteByEntity(adminQuestionOption);
    }

    /**
     * 根据问卷调查ID--->查询问卷调查项表
     * @param questionnaireSurveyId
     * @return
     */
    public List<AdminQuestionItem> selectQItemList(String questionnaireSurveyId) {
        return adminQuestionItemDao.selectQItemList(questionnaireSurveyId);
    }
}