package cn.iocoder.yudao.module.system.service.evaluation;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.tenant.core.context.TenantContextHolder;
import cn.iocoder.yudao.module.system.controller.admin.evaluation.vo.FoodSatisfactionSurveyPageReqVO;
import cn.iocoder.yudao.module.system.controller.admin.evaluation.vo.FoodSatisfactionSurveyRespVO;
import cn.iocoder.yudao.module.system.controller.admin.evaluation.vo.FoodSatisfactionSurveySaveReqVO;
import cn.iocoder.yudao.module.system.dal.dataobject.evaluation.FoodSatisfactionSurveyDO;
import cn.iocoder.yudao.module.system.dal.mysql.evaluation.FoodSatisfactionSurveyMapper;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;

import java.time.LocalDateTime;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.system.enums.ErrorCodeConstants.COMMON_NOT_FOUND;

@Service
@Validated
public class FoodSatisfactionSurveyServiceImpl implements FoodSatisfactionSurveyService {

    @Resource
    private FoodSatisfactionSurveyMapper foodSatisfactionSurveyMapper;

    @Override
    public Long create(FoodSatisfactionSurveySaveReqVO createReqVO) {
        FoodSatisfactionSurveyDO survey = BeanUtils.toBean(createReqVO, FoodSatisfactionSurveyDO.class);
        if (survey.getTenantId() == null) {
            survey.setTenantId(TenantContextHolder.getTenantId());
        }
        survey.setCreateTime(LocalDateTime.now());
        foodSatisfactionSurveyMapper.insert(survey);
        return survey.getId();
    }

    @Override
    public void update(FoodSatisfactionSurveySaveReqVO updateReqVO) {
        validateExists(updateReqVO.getId());
        FoodSatisfactionSurveyDO updateObj = BeanUtils.toBean(updateReqVO, FoodSatisfactionSurveyDO.class);
        if (updateObj.getTenantId() == null) {
            updateObj.setTenantId(TenantContextHolder.getTenantId());
        }
        foodSatisfactionSurveyMapper.updateById(updateObj);
    }

    @Override
    public void delete(Long id) {
        validateExists(id);
        foodSatisfactionSurveyMapper.deleteById(id);
    }

    @Override
    public FoodSatisfactionSurveyRespVO get(Long id) {
        FoodSatisfactionSurveyDO survey = foodSatisfactionSurveyMapper.selectById(id);
        if (survey == null) {
            throw exception(COMMON_NOT_FOUND);
        }
        return BeanUtils.toBean(survey, FoodSatisfactionSurveyRespVO.class);
    }

    @Override
    public PageResult<FoodSatisfactionSurveyRespVO> getPage(FoodSatisfactionSurveyPageReqVO pageReqVO) {
        if (pageReqVO.getTenantId() == null) {
            pageReqVO.setTenantId(TenantContextHolder.getTenantId());
        }
        PageResult<FoodSatisfactionSurveyDO> pageResult = foodSatisfactionSurveyMapper.selectPage(pageReqVO);
        return BeanUtils.toBean(pageResult, FoodSatisfactionSurveyRespVO.class);
    }

    private void validateExists(Long id) {
        if (id == null || foodSatisfactionSurveyMapper.selectById(id) == null) {
            throw exception(COMMON_NOT_FOUND);
        }
    }
}
