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

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.iocoder.yudao.module.system.controller.admin.extra.vo.SignatureProxyApplicationPageReqVO;
import cn.iocoder.yudao.module.system.dal.dataobject.extra.ElderlyExtraSignatureProxyApplicationDO;
import cn.iocoder.yudao.module.system.dal.mysql.extra.ElderlyExtraSignatureProxyApplicationMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

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

/**
 * (附件)签名代理申请 Service 实现
 *
 * @author 管理员
 */
@Service
@Slf4j
public class ElderlyExtraSignatureProxyApplicationServiceImpl implements ElderlyExtraSignatureProxyApplicationService {

    @Resource
    private ElderlyExtraSignatureProxyApplicationMapper mapper;

    @Override
    public Long create(ElderlyExtraSignatureProxyApplicationDO createReqVO) {
        mapper.insert(createReqVO);
        return createReqVO.getId();
    }

    @Override
    public void update(ElderlyExtraSignatureProxyApplicationDO updateReqVO) {
        validateExists(updateReqVO.getId());
        mapper.updateById(updateReqVO);
    }

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

    @Override
    public PageResult<ElderlyExtraSignatureProxyApplicationDO> findPage(SignatureProxyApplicationPageReqVO reqVO) {
        return mapper.selectPage(reqVO);
    }

    @Override
    public ElderlyExtraSignatureProxyApplicationDO getDetail(Long id) {
        return validateExists(id);
    }

    @Override
    public List<ElderlyExtraSignatureProxyApplicationDO> getListByElderId(Long elderId) {
        return mapper.selectList(
                new LambdaQueryWrapperX<ElderlyExtraSignatureProxyApplicationDO>()
                        .eqIfPresent(ElderlyExtraSignatureProxyApplicationDO::getElderId, elderId)
                        .orderByDesc(ElderlyExtraSignatureProxyApplicationDO::getCreateTime)
        );
    }

    private ElderlyExtraSignatureProxyApplicationDO validateExists(Long id) {
        ElderlyExtraSignatureProxyApplicationDO record = mapper.selectById(id);
        if (record == null) {
            throw exception(ELDERLY_EXTRA_SIGNATURE_PROXY_APPLICATION_NOT_EXISTS);
        }
        return record;
    }

}
