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.framework.tenant.core.aop.TenantIgnore;
import cn.iocoder.yudao.module.system.controller.admin.biz.vo.VolunteerTeamPageReqVO;
import cn.iocoder.yudao.module.system.controller.admin.biz.vo.VolunteerTeamSaveReqVO;
import cn.iocoder.yudao.module.system.dal.dataobject.biz.VolunteerTeamDO;
import cn.iocoder.yudao.module.system.dal.mysql.biz.VolunteerTeamMapper;
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 static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.bpm.enums.ErrorCodeConstants.VOLUNTEER_TEAM_NOT_EXISTS;
import static cn.iocoder.yudao.module.system.enums.LogRecordConstants.*;

/**
 * @Author: jinyd
 * @CreateTime: 2025/3/7  11:36
 * @Description:
 * @Version: 4.0
 */


@Service
@Validated
public class VolunteerTeamServiceImpl implements VolunteerTeamService {

    @Resource
    private VolunteerTeamMapper teamMapper;

    @Override
    @TenantIgnore
    @LogRecord(type = VOLUNTEER_TEAM_TYPE, subType = VOLUNTEER_TEAM_CREATE_SUB_TYPE, bizNo = "{{#id}}",
            success = VOLUNTEER_TEAM_CREATE_SUCCESS)
    public Long createTeam(VolunteerTeamSaveReqVO createReqVO) {
        // 插入
        VolunteerTeamDO team = BeanUtils.toBean(createReqVO, VolunteerTeamDO.class);
        teamMapper.insert(team);
        LogRecordContext.putVariable("id", team.getId());
        // 返回
        return team.getId();
    }

    @Override
    @TenantIgnore
    @LogRecord(type = VOLUNTEER_TEAM_TYPE, subType = VOLUNTEER_TEAM_UPDATE_SUB_TYPE, bizNo = "{{#updateReqVO.id}}",
            success = VOLUNTEER_TEAM_UPDATE_SUCCESS)
    public void updateTeam(VolunteerTeamSaveReqVO updateReqVO) {
        // 校验存在
        validateTeamExists(updateReqVO.getId());
        // 更新
        VolunteerTeamDO updateObj = BeanUtils.toBean(updateReqVO, VolunteerTeamDO.class);
        teamMapper.updateById(updateObj);
    }

    @Override
    @TenantIgnore
    @LogRecord(type = VOLUNTEER_TEAM_TYPE, subType = VOLUNTEER_TEAM_DELETE_SUB_TYPE, bizNo = "{{#id}}",
            success = VOLUNTEER_TEAM_DELETE_SUCCESS)
    public void deleteTeam(Long id) {
        // 校验存在
//        validateTeamExists(id);
        VolunteerTeamDO volunteerTeamDO = teamMapper.selectById(id);
        if (volunteerTeamDO == null) {
            throw exception(VOLUNTEER_TEAM_NOT_EXISTS);
        }

        // 删除
        teamMapper.deleteById(id);
        LogRecordContext.putVariable("teamName", volunteerTeamDO.getTeamName());
    }

    private void validateTeamExists(Long id) {
        if (teamMapper.selectById(id) == null) {
            throw exception(VOLUNTEER_TEAM_NOT_EXISTS);
        }
    }

    @Override
    @TenantIgnore
    public VolunteerTeamDO getTeam(Long id) {
        return teamMapper.selectById(id);
    }

    @Override
    @TenantIgnore
    public PageResult<VolunteerTeamDO> getTeamPage(VolunteerTeamPageReqVO pageReqVO) {
        return teamMapper.selectPage(pageReqVO);
    }


}
