package cn.iocoder.byzs.module.bjdx.service.reportmanage;

import cn.hutool.core.collection.CollUtil;
import cn.iocoder.byzs.module.bjdx.controller.admin.reportmanage.vo.ReportManagePageReqVO;
import cn.iocoder.byzs.module.bjdx.controller.admin.reportmanage.vo.ReportManageSaveReqVO;
import org.springframework.stereotype.Service;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import java.util.*;

import cn.iocoder.byzs.module.bjdx.dal.dataobject.reportmanage.ReportManageDO;
import cn.iocoder.byzs.framework.common.pojo.PageResult;
import cn.iocoder.byzs.framework.common.util.object.BeanUtils;

import cn.iocoder.byzs.module.bjdx.dal.mysql.reportmanage.ReportManageMapper;

import static cn.iocoder.byzs.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.byzs.module.bjdx.enums.ErrorCodeConstants.MANAGE_NOT_EXISTS;

/**
 * 评估报告-评语 Service 实现类
 *
 * @author lyb
 */
@Service
@Validated
public class ReportManageServiceImpl implements ReportManageService {

    @Resource
    private ReportManageMapper manageMapper;

    @Override
    public Long createManage(ReportManageSaveReqVO createReqVO) {
        // 插入
        ReportManageDO manage = BeanUtils.toBean(createReqVO, ReportManageDO.class);
        manageMapper.insert(manage);
        // 返回
        return manage.getBrcId();
    }

    @Override
    public void updateManage(ReportManageSaveReqVO updateReqVO) {
        // 校验存在
        validateManageExists(updateReqVO.getBrcId());
        // 更新
        ReportManageDO updateObj = BeanUtils.toBean(updateReqVO, ReportManageDO.class);
        manageMapper.updateById(updateObj);
    }

    @Override
    public void deleteManage(Long id) {
        // 校验存在
        validateManageExists(id);
        // 删除
        manageMapper.deleteById(id);
    }

    @Override
        public void deleteManageListByIds(List<Long> ids) {
        // 校验存在
        validateManageExists(ids);
        // 删除
        manageMapper.deleteByIds(ids);
        }

    private void validateManageExists(List<Long> ids) {
        List<ReportManageDO> list = manageMapper.selectByIds(ids);
        if (CollUtil.isEmpty(list) || list.size() != ids.size()) {
            throw exception(MANAGE_NOT_EXISTS);
        }
    }

    private void validateManageExists(Long id) {
        if (manageMapper.selectById(id) == null) {
            throw exception(MANAGE_NOT_EXISTS);
        }
    }

    @Override
    public ReportManageDO getManage(Long id) {
        return manageMapper.selectById(id);
    }

    @Override
    public PageResult<ReportManageDO> getManagePage(ReportManagePageReqVO pageReqVO) {
        return manageMapper.selectPage(pageReqVO);
    }

}