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.VolunteerPageReqVO;
import cn.iocoder.yudao.module.system.controller.admin.biz.vo.VolunteerSaveReqVO;
import cn.iocoder.yudao.module.system.dal.dataobject.biz.ElderlyContractDO;
import cn.iocoder.yudao.module.system.dal.dataobject.biz.VolunteerDO;
import cn.iocoder.yudao.module.system.dal.mysql.biz.VolunteerMapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mzt.logapi.context.LogRecordContext;
import com.mzt.logapi.starter.annotation.LogRecord;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

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.bpm.enums.ErrorCodeConstants.VOLUNTEER_NOT_EXISTS;
import static cn.iocoder.yudao.module.system.enums.LogRecordConstants.*;

/**
 * @Author: jinyd
 * @CreateTime: 2025/3/10  9:05
 * @Description:
 * @Version: 4.0
 */

@Service
@Validated
public class VolunteerServiceImpl implements VolunteerService {

    @Resource
    private VolunteerMapper volunteerMapper;

    @Override
    @LogRecord(type = VOLUNTEER_TYPE, subType = VOLUNTEER_CREATE_SUB_TYPE, bizNo = "{{#id}}",
            success = VOLUNTEER_CREATE_SUCCESS)
    public Long create(VolunteerSaveReqVO createReqVO) {
        // 插入
        VolunteerDO volunteerDO  = BeanUtils.toBean(createReqVO, VolunteerDO.class);
        volunteerMapper.insert(volunteerDO);
        LogRecordContext.putVariable("id", volunteerDO.getId());
        // 返回
        return volunteerDO.getId();
    }

    @Override
    @LogRecord(type = VOLUNTEER_TYPE, subType = VOLUNTEER_UPDATE_SUB_TYPE, bizNo = "{{#updateReqVO.id}}",
            success = VOLUNTEER_UPDATE_SUCCESS)
    public void update(VolunteerSaveReqVO updateReqVO) {
        // 校验存在
        validateExists(updateReqVO.getId());
        // 更新
        VolunteerDO updateObj = BeanUtils.toBean(updateReqVO, VolunteerDO.class);
        volunteerMapper.updateById(updateObj);
    }

    @Override
    @LogRecord(type = VOLUNTEER_TYPE, subType = VOLUNTEER_DELETE_SUB_TYPE, bizNo = "{{#id}}",
            success = VOLUNTEER_DELETE_SUCCESS)
    public void delete(Long id) {
        // 校验存在
//        validateExists(id);

        VolunteerDO volunteerDO = volunteerMapper.selectById(id);
        if (volunteerDO == null) {
            throw exception(VOLUNTEER_NOT_EXISTS);
        }

        // 删除
        volunteerMapper.deleteById(id);

        LogRecordContext.putVariable("name", volunteerDO.getName());

    }

    private void validateExists(Long id) {
        if (volunteerMapper.selectById(id) == null) {
            throw exception(VOLUNTEER_NOT_EXISTS);
        }
    }

    @Override
    public VolunteerDO get(Long id) {
        return volunteerMapper.selectById(id);
    }

    @Override
    public PageResult<VolunteerDO> getPage(VolunteerPageReqVO pageReqVO) {
        Page<VolunteerDO> page = new Page<>(pageReqVO.getPageNo(), pageReqVO.getPageSize());
        List<VolunteerDO> list = volunteerMapper.selectListPage(page, pageReqVO);
        return new PageResult<>(list, page.getTotal());

    }


}
