package cn.com.ty.lift.ud.tableDict.service.impl;

import cn.com.ty.lift.ud.tableDict.mapper.TableDictMapper;
import cn.com.ty.lift.ud.tableDict.mapper.entity.TableDictDto;
import cn.com.ty.lift.ud.tableDict.mapper.entity.TableDictEntity;
import cn.com.ty.lift.ud.tableDict.mapper.entity.TableDictVo;
import cn.com.ty.lift.ud.tableDict.service.ITableDictService;
import cn.com.ty.lift.ud.userInfo.service.IUserInfoService;
import cn.com.xwy.util.collection.ListUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
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.List;

/**
 * 数据字典表 -ServiceImpl
 *
 * @author Xson
 * @date 2020-05-07
 */
@Service
@Transactional
public class TableDictServiceImpl extends ServiceImpl<TableDictMapper,TableDictEntity>
    implements ITableDictService {


    @Autowired
    private  TableDictMapper tableDictMapper;

    @Autowired
    private IUserInfoService iUserInfoService;

    /**
     * 根据表名和字段查询常量
     * @param request
     */
    @Override
    public List<TableDictVo> getConstantByTableAndColumn(TableDictDto request) {
        List<TableDictEntity> resultList = tableDictMapper.getConstantByTableAndColumn(request);
        return getList(resultList);
    }


    /**
     * 返回List格式
     * @param list
     * @return
     */
    private List<TableDictVo> getList(List<TableDictEntity> list){
        List<TableDictVo> tdList = new ArrayList<>();
        if(ListUtil.isNotEmpty(list)){
            for (TableDictEntity tdEntity:list) {
                TableDictVo tableDictVo = new TableDictVo();
                tableDictVo.setKey(tdEntity.getCode());
                tableDictVo.setValue(tdEntity.getVal());
                tableDictVo.setDescr(tdEntity.getDescr());
                tdList.add(tableDictVo);
            }
        }
        return tdList;
    }


    @Override
    public String findOneByTableAndColumnAndCode(String tableName, String field, String code) {
        return tableDictMapper.findOneByTableAndColumnAndCode(tableName, field, code);
    }
}
