package com.qiaoba.module.system.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.qiaoba.api.system.entity.SysDictType;
import com.qiaoba.api.system.entity.param.SysDictTypeParam;
import com.qiaoba.api.auth.utils.SecurityUtil;
import com.qiaoba.common.base.code.DictErrorCode;
import com.qiaoba.common.base.exception.ServiceException;
import com.qiaoba.common.database.entity.PageQuery;
import com.qiaoba.common.database.entity.TableDataInfo;
import com.qiaoba.module.system.mapper.SysDictTypeMapper;
import com.qiaoba.module.system.service.SysDictTypeService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * 字典类型管理 服务层实现
 *
 * @author ailanyin
 * @version 1.0
 * @since 2023/5/31 10:31
 */
@Service
@RequiredArgsConstructor
public class SysDictTypeServiceImpl implements SysDictTypeService {

    private final SysDictTypeMapper sysDictTypeMapper;

    @Override
    public int insert(SysDictType sysDictType) {
        checkAllowInsertOrUpdate(sysDictType);
        sysDictType.setCreateTime(new Date());
        sysDictType.setCreateUser(SecurityUtil.getLoginUsername());
        return sysDictTypeMapper.insert(sysDictType);
    }

    @Override
    public int update(SysDictType sysDictType) {
        checkAllowInsertOrUpdate(sysDictType);
        sysDictType.setUpdateTime(new Date());
        sysDictType.setUpdateUser(SecurityUtil.getLoginUsername());
        return sysDictTypeMapper.updateById(sysDictType);
    }

    @Override
    public int deleteByIds(List<String> ids) {
        checkAllowDelete(ids);
        return sysDictTypeMapper.deleteBatchIds(ids);
    }

    @Override
    public SysDictType selectById(String dictId) {
        return sysDictTypeMapper.selectById(dictId);
    }

    @Override
    public TableDataInfo selectPageList(SysDictTypeParam param, PageQuery pageQuery) {
        return TableDataInfo.build(sysDictTypeMapper.selectPage(pageQuery.build(), param2Wrapper(param)));
    }

    @Override
    public List<SysDictType> selectList(SysDictTypeParam param) {
        return sysDictTypeMapper.selectList(param2Wrapper(param));
    }


    private void checkAllowInsertOrUpdate(SysDictType sysDictType) {
        String dictType = sysDictTypeMapper.checkDictTypeUnique(sysDictType);
        if (StrUtil.isNotBlank(dictType)) {
            throw new ServiceException(DictErrorCode.TYPE_EXIST.getCode(), StrUtil.format(DictErrorCode.TYPE_EXIST.getMsg(), dictType));
        }
    }

    private void checkAllowDelete(List<String> ids) {
        String typeName = sysDictTypeMapper.checkAllowDelete(ids);
        if (StrUtil.isNotBlank(typeName)) {
            throw new ServiceException(DictErrorCode.HAS_DATA.getCode(), StrUtil.format(DictErrorCode.HAS_DATA.getMsg(), typeName));
        }
    }

    private QueryWrapper<SysDictType> param2Wrapper(SysDictTypeParam param) {
        QueryWrapper<SysDictType> wrapper = new QueryWrapper<>();
        wrapper.lambda()
                .orderByDesc(SysDictType::getCreateTime)
                .like(StrUtil.isNotBlank(param.getDictName()), SysDictType::getDictName, param.getDictName())
                .like(StrUtil.isNotBlank(param.getDictType()), SysDictType::getDictType, param.getDictType())
                .eq(StrUtil.isNotBlank(param.getStatus()), SysDictType::getStatus, param.getStatus());
        return wrapper;
    }
}
