package com.mvc.service.dic;

import com.mvc.dao.DicGeneralMapper;
import com.mvc.entity.DicGeneral;
import com.mvc.entity.DicGeneralExample;
import com.mvc.model.DicGeneralModel;
import com.mvc.utils.StringUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
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.Arrays;
import java.util.List;

@Service
@Transactional
public class DicGeneralService {


    @Autowired
    private DicGeneralMapper dicGeneralMapper;

    public List<DicGeneralModel> findAllDicGeneral() {
        List<DicGeneral> DicGenerals = dicGeneralMapper.selectByExample(null);
        List<DicGeneralModel> resList = new ArrayList<>();
        for (DicGeneral DicGeneral : DicGenerals) {
            DicGeneralModel model = new DicGeneralModel();
            BeanUtils.copyProperties(DicGeneral, model);
            resList.add(model);
        }
        return resList;
    }

    /**
     * 根据字典类型和key获取对象
     * @param dicType
     * @return
     */
    public DicGeneralModel findADicGeneralModelByDicTypeAndKey(String dicType,String dicKey) {
        if(StringUtils.isEmpty(dicType)||StringUtils.isEmpty(dicKey)) return null;
        DicGeneralExample example=new DicGeneralExample();
        DicGeneralExample.Criteria criteria = example.createCriteria();
        criteria.andDicTypeEqualTo(dicType);
        criteria.andDicKeyEqualTo(dicKey);
        List<DicGeneral> dicGenerals = dicGeneralMapper.selectByExample(example);
        if(dicGenerals!=null&&dicGenerals.size()>0){
            DicGeneralModel model = new DicGeneralModel();
            BeanUtils.copyProperties(dicGenerals.get(0), model);
            return model;
        }
        return null;
    }

    /**
     * 根据字典类型字符串获取对象列表
     * @param dicTypes
     * @return
     */
    public List<DicGeneralModel> findADicGeneralModelByDicTypes(String dicTypes) {
        if(StringUtils.isEmpty(dicTypes)) return null;
        DicGeneralExample example=new DicGeneralExample();
        DicGeneralExample.Criteria criteria = example.createCriteria();
        if(dicTypes.contains(",")){
            criteria.andDicTypeIn(Arrays.asList(dicTypes.split(",")));
        }else {
            criteria.andDicTypeEqualTo(dicTypes);
        }

        List<DicGeneralModel> dicGeneralModels=new ArrayList<>();
        List<DicGeneral> dicGenerals = dicGeneralMapper.selectByExample(example);
        for(DicGeneral dicGeneral:dicGenerals){
            DicGeneralModel model = new DicGeneralModel();
            BeanUtils.copyProperties(dicGeneral, model);
            dicGeneralModels.add(model);
        }
        return dicGeneralModels;
    }

    public DicGeneralModel saveDicGeneral(DicGeneralModel model) {
        model.setDicId(StringUtil.getUuid());
        DicGeneral DicGeneral = new DicGeneral();
        BeanUtils.copyProperties(model, DicGeneral);
        int insert = dicGeneralMapper.insert(DicGeneral);
        model.setId(DicGeneral.getId());
        return model;
    }

    public DicGeneralModel updateDicGeneral(DicGeneralModel model) {
        DicGeneral DicGeneral = dicGeneralMapper.selectByPrimaryKey(model.getId());
        BeanUtils.copyProperties(model, DicGeneral);
        int i = dicGeneralMapper.updateByPrimaryKey(DicGeneral);
        return model;
    }

    public int deleteDicGeneral(int id) {
        int i = dicGeneralMapper.deleteByPrimaryKey(id);
        return i;
    }
}
