package com.sunyard.ssp.modules.sysconf.dict.serviceimpl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sunyard.ssp.common.constant.SqlConstant;
import com.sunyard.ssp.modules.sysconf.dict.entity.ScDict;
import com.sunyard.ssp.modules.sysconf.dict.mapper.ScDictMapper;
import com.sunyard.ssp.modules.sysconf.dict.service.IScDictService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author fyc
 * @since 2020-03-13
 */
@Slf4j
@Service
@Transactional
public class ScDictServiceImpl extends ServiceImpl<ScDictMapper, ScDict> implements IScDictService {

    @Autowired
    private ScDictMapper scDictMapper;

    @Override
    public List<ScDict> selectByKey(String key) {
        QueryWrapper queryWrapper = new QueryWrapper<ScDict>().like("TITLE",key).orderByAsc(SqlConstant.DB_SORT_COLUMN);
        List<ScDict> dicts = scDictMapper.selectList(queryWrapper);
        return dicts;
    }

    @Override
    public Map checkInputForm(ScDict scDict) {
        Map result = new HashMap();
        //是否为空检测
        if(scDict == null){
            result.put("status",false);
            result.put("message","传入数据不合法");
            return result;
        }

        //字典名称长度检测
        if(scDict.getTitle().length()>255){
            result.put("status",false);
            result.put("message","字典名称字符超出上限");
            return result;
        }
        //字典类型长度检测
        if(scDict.getType().length()>255){
            result.put("status",false);
            result.put("message","字典类型字符超出上限");
            return result;
        }

        //备注字符长度检测
        if(scDict.getDescription()!=null) {
            if (scDict.getDescription().length() > 255) {
                result.put("status", false);
                result.put("message", "备注字符超出上限");
                return result;
            }
        }

        result.put("status",true);
        return result;
    }
}
