package com.mvc.service.dic;


import com.mvc.dao.DicStkroomMapper;
import com.mvc.entity.DicStkroom;
import com.mvc.entity.DicStkroomExample;
import com.mvc.model.DicStkroomModel;
import com.mvc.utils.PinyinUtil;
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.List;

@Service
@Transactional
public class DicStkroomService {
    @Autowired
    private DicStkroomMapper dicStkroomMapper;

    public List<DicStkroomModel> findAllDicStkroom() {
        List<DicStkroom> DicStkrooms = dicStkroomMapper.selectByExample(null);
        List<DicStkroomModel> resList=new ArrayList<>();
        for(DicStkroom DicStkroom:DicStkrooms){
            DicStkroomModel model = new DicStkroomModel();
            BeanUtils.copyProperties(DicStkroom,model);
            resList.add(model);
        }
        return resList;
    }

    public DicStkroomModel saveDicStkroom(DicStkroomModel model) {
        DicStkroom DicStkroom = new DicStkroom();
        model.setStkroomId(StringUtil.getUuid());
        if(StringUtils.isEmpty(model.getStkroomPy())){//拼音码空时,生成拼音码
            //model.setStkroomPy(ChineseToEn.getAllFirstLetter(model.getStkroomName()));
            model.setStkroomPy(PinyinUtil.getPinYinHeadChar(model.getStkroomName(),true));
        }

        BeanUtils.copyProperties(model,DicStkroom);
        int insert = dicStkroomMapper.insert(DicStkroom);
        model.setId(DicStkroom.getId());
        return model;
    }

    public DicStkroomModel updateDicStkroom(DicStkroomModel model) {
        DicStkroom DicStkroom = dicStkroomMapper.selectByPrimaryKey(model.getId());
        if(StringUtils.isEmpty(model.getStkroomPy())){//拼音码空时,生成拼音码
            //model.setStkroomPy(ChineseToEn.getAllFirstLetter(model.getStkroomName()));
            model.setStkroomPy(PinyinUtil.getPinYinHeadChar(model.getStkroomName(),true));
        }
        BeanUtils.copyProperties(model,DicStkroom);
        int i = dicStkroomMapper.updateByPrimaryKey(DicStkroom);
        return model;
    }

    public int deleteDicStkroom(int id) {
        int i = dicStkroomMapper.deleteByPrimaryKey(id);
        return i;
    }

    /**
     * 根据库房名称获取对象
     * @param stkroomName
     * @return
     */
    public DicStkroomModel findStkroomByStkroomName(String stkroomName){
        DicStkroomExample example=new DicStkroomExample();
        DicStkroomExample.Criteria criteria = example.createCriteria();
        criteria.andStkroomNameEqualTo(stkroomName);
        List<DicStkroom> dicStkrooms=dicStkroomMapper.selectByExample(example);
        if(dicStkrooms!=null&&dicStkrooms.size()>0){
            DicStkroomModel model=new DicStkroomModel();
            BeanUtils.copyProperties(dicStkrooms.get(0),model);
            return model;
        }
        return null;
    }

}
