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

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
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.entity.ScDictData;
import com.sunyard.ssp.modules.sysconf.dict.mapper.ScDictDataMapper;
import com.sunyard.ssp.modules.sysconf.dict.mapper.ScDictMapper;
import com.sunyard.ssp.modules.sysconf.dict.service.IScDictDataService;
import com.sunyard.ssp.utils.CommonUtil;
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.ArrayList;
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 ScDictDataServiceImpl extends ServiceImpl<ScDictDataMapper, ScDictData> implements IScDictDataService {

    @Autowired
    private ScDictDataMapper scDictDataMapper;

    @Autowired
    private ScDictMapper scDictMapper;

    @Override
    public IPage<ScDictData> queryList(ScDictData scDictData){
        if(scDictData.getSort()==null || scDictData.getSort()==""){
            scDictData.setSort(SqlConstant.DB_SORT_COLUMN);
        }else {
            String sort = CommonUtil.underscoreName(scDictData.getSort());
            scDictData.setSort(sort);
        }
        Page<ScDictData> scDictDataPage = new Page<>(scDictData.getPageNumber(),scDictData.getPageSize());
        IPage<ScDictData> scDictDataIPage = scDictDataPage.setRecords(scDictDataMapper.queryList(scDictDataPage,scDictData));
        return scDictDataIPage;
    }

    @Override
    public List<ScDictData> selectByDictType(String type) {
        ScDict scDict = scDictMapper.selectByType(type);
        List<ScDictData> scDictDataList = new ArrayList<>();
        if(scDict != null){
            scDictDataList = scDictDataMapper.selectByDictId(scDict.getId());
        }
        return scDictDataList;
    }

    @Override
    public void deleteByDictId(Long dictId) {
        scDictDataMapper.deleteByDictId(dictId);
    }

    @Override
    public Map checkInputData(ScDictData scDictData) {
        Map result = new HashMap();

        //检测数据是否为空
        if(scDictData == null){
            result.put("status",false);
            result.put("message","传入数据不合法");
            return result;
        }

        //检测字段是否为空
        if(scDictData.getDictId()==null || scDictData.getTitle()== null || scDictData.getValue()==null){
            result.put("status",false);
            result.put("message","传入数据不合法");
            return result;
        }

        //检测字典标题是否超过上限
        if(scDictData.getTitle().length()>255){
            result.put("status",false);
            result.put("message","名称字符超过上限");
            return result;
        }

        //检测字典数据值是否超过上限
        if(scDictData.getValue().length()>255){
            result.put("status",false);
            result.put("message","数据值字符超过上限");
            return result;
        }

        //检测描述是否超过上限
        if(scDictData.getDescription()!=null) {
            if (scDictData.getDescription().length() > 255) {
                result.put("status", false);
                result.put("message", "备注字符超过上限");
                return result;
            }
        }

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


}
