package com.jojubanking.boot.module.system.service.label;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.jojubanking.boot.framework.common.exception.util.ServiceExceptionUtil;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import java.util.*;
import com.jojubanking.boot.module.system.controller.admin.label.vo.*;
import com.jojubanking.boot.module.system.dal.dataobject.label.LabelDO;
import com.jojubanking.boot.framework.common.pojo.PageResult;

import com.jojubanking.boot.module.system.convert.label.LabelConvert;
import com.jojubanking.boot.module.system.dal.mysql.label.LabelMapper;

import static com.jojubanking.boot.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.jojubanking.boot.module.system.enums.ErrorCodeConstants.*;

/**
 * 信息 Service 实现类
 *
 * @author admin
 */
@Service
@Validated
public class LabelServiceImpl implements LabelService {

    @Resource
    private LabelMapper labelMapper;

    @Override
    public Integer createLabel(LabelCreateReqVO createReqVO) {
        QueryWrapper query = new QueryWrapper<>();
        query.eq("name",createReqVO.getName());
        long count = labelMapper.selectCount(query);
        if(count !=0 ){
            throw ServiceExceptionUtil.exception(LABEL_EXISTS);
        }
        // 插入
        LabelDO label = LabelConvert.INSTANCE.convert(createReqVO);
        labelMapper.insert(label);
        // 返回
        return label.getId();
    }

    @Override
    public void updateLabel(LabelUpdateReqVO updateReqVO) {
        // 校验存在
        this.validateLabelExists(updateReqVO.getId());
        // 更新
        LabelDO updateObj = LabelConvert.INSTANCE.convert(updateReqVO);
        labelMapper.updateById(updateObj);
    }

    @Override
    public void deleteLabel(Integer id) {
        // 校验存在
        this.validateLabelExists(id);
        // 删除
        labelMapper.deleteById(id);
    }

    private void validateLabelExists(Integer id) {
        if (labelMapper.selectById(id) == null) {
            throw exception(LABEL_NOT_EXISTS);
        }
    }

    @Override
    public LabelDO getLabel(Integer id) {
        return labelMapper.selectById(id);
    }

    @Override
    public List<LabelDO> getLabelList(Collection<Integer> ids) {
        return labelMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<LabelDO> getLabelPage(LabelPageReqVO pageReqVO) {
        return labelMapper.selectPage(pageReqVO);
    }

    @Override
    public List<LabelDO> getLabelList(LabelExportReqVO exportReqVO) {
        return labelMapper.selectList(exportReqVO);
    }

}
