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

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.module.system.controller.admin.biz.vo.riskdisclosurestatement.ElderlyRiskDisclosureStatementPageReqVO;
import cn.iocoder.yudao.module.system.controller.admin.biz.vo.riskdisclosurestatement.ElderlyRiskDisclosureStatementRespVO;
import cn.iocoder.yudao.module.system.controller.admin.biz.vo.riskdisclosurestatement.ElderlyRiskDisclosureStatementSaveReqVO;
import cn.iocoder.yudao.module.system.dal.dataobject.biz.ElderlyRiskDisclosureStatementDO;
import cn.iocoder.yudao.module.system.dal.mysql.biz.ElderlyRiskDisclosureStatementMapper;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;

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 ElderlyRiskDisclosureStatementServiceImpl implements ElderlyRiskDisclosureStatementService {

    @Resource
    private ElderlyRiskDisclosureStatementMapper elderlyRiskDisclosureStatementMapper;

    @Override
    public Long create(ElderlyRiskDisclosureStatementSaveReqVO createReqVO) {
        ElderlyRiskDisclosureStatementDO record = BeanUtils.toBean(createReqVO, ElderlyRiskDisclosureStatementDO.class);
        elderlyRiskDisclosureStatementMapper.insert(record);
        return record.getId();
    }

    @Override
    public void update(ElderlyRiskDisclosureStatementSaveReqVO updateReqVO) {
        validateExists(updateReqVO.getId());
        ElderlyRiskDisclosureStatementDO updateObj = BeanUtils.toBean(updateReqVO, ElderlyRiskDisclosureStatementDO.class);
        elderlyRiskDisclosureStatementMapper.updateById(updateObj);
    }

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

    @Override
    public ElderlyRiskDisclosureStatementRespVO get(Long id) {
        ElderlyRiskDisclosureStatementRespVO respVO = elderlyRiskDisclosureStatementMapper.selectByIdWithElderInfo(id);
        if (respVO == null) {
            throw exception(COMMON_NOT_FOUND);
        }
        return respVO;
    }

    @Override
    public PageResult<ElderlyRiskDisclosureStatementRespVO> getPage(ElderlyRiskDisclosureStatementPageReqVO pageReqVO) {
        return elderlyRiskDisclosureStatementMapper.selectPageWithElderInfo(pageReqVO);
    }

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