package com.mvc.service.dic;

import com.mvc.dao.DicChkplatCellMapper;
import com.mvc.entity.DicChkplatCell;
import com.mvc.entity.DicChkplatCellExample;
import com.mvc.model.DicChkplatCellModel;
import com.mvc.model.DicChkplatModel;
import com.mvc.utils.StringUtil;
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 DicChkplatCellService {

    @Autowired
    private DicChkplatCellMapper dicChkplatCellMapper;

    public List<DicChkplatCellModel> findAllDicChkplatCell(String platId) {
        List<DicChkplatCellModel> resList=new ArrayList<>();
        DicChkplatCellExample example=new DicChkplatCellExample();
        DicChkplatCellExample.Criteria criteria = example.createCriteria();
        criteria.andPlatIdEqualTo(platId);
        //example.setOrderByClause("CELL_NAME asc");
        //转换成数字排序 2022-10-18 guozhao
        example.setOrderByClause("CAST(regexp_substr(CELL_NAME, '[0-9]+') AS UNSIGNED)");
        List<DicChkplatCell> DicChkplatCells = dicChkplatCellMapper.selectByExample(example);
        for(DicChkplatCell cell:DicChkplatCells){
            DicChkplatCellModel model = new DicChkplatCellModel();
            BeanUtils.copyProperties(cell,model);
            resList.add(model);
        }
        return resList;
    }

    public DicChkplatCellModel saveDicChkplatCell(DicChkplatCellModel model) {
        model.setCellId(StringUtil.getUuid());
        DicChkplatCell cell = new DicChkplatCell();
        BeanUtils.copyProperties(model,cell);
        int insert = dicChkplatCellMapper.insert(cell);
        model.setId(cell.getId());
        return model;
    }

    public DicChkplatCellModel updateDicChkplatCell(DicChkplatCellModel model) {
        DicChkplatCell cell = dicChkplatCellMapper.selectByPrimaryKey(model.getId());
        BeanUtils.copyProperties(model, cell);
        int i = dicChkplatCellMapper.updateByPrimaryKey(cell);
        return model;
    }

    public int deleteDicChkplatCell(int id) {
        int i = dicChkplatCellMapper.deleteByPrimaryKey(id);
        return i;
    }

    public int deleteDicChkplatCellByPlanid(String plantId) {
        DicChkplatCellExample example=new DicChkplatCellExample();
        DicChkplatCellExample.Criteria criteria = example.createCriteria();
        criteria.andPlatIdEqualTo(plantId);
        int i = dicChkplatCellMapper.deleteByExample(example);
        return i;
    }

    public DicChkplatCellModel findCellModelbycellId(String  cellId) {
        DicChkplatCellExample example=new DicChkplatCellExample();
        DicChkplatCellExample.Criteria criteria = example.createCriteria();
        criteria.andCellIdEqualTo(cellId);
        List<DicChkplatCell> dicChkplatCells = dicChkplatCellMapper.selectByExample(example);
       if(dicChkplatCells.size()>0){
           DicChkplatCell cell = dicChkplatCells.get(0);
           DicChkplatCellModel resModel=new DicChkplatCellModel();
           BeanUtils.copyProperties(cell,resModel);
           return resModel;
       }else{
           return null;
       }
    }

    /**
     * 根据DicChkplatModel和Name创建CellModel
     * @param model
     * @param cellName
     * @return
     */
    public DicChkplatCellModel addCellModel(DicChkplatModel model,String cellName){
        DicChkplatCellModel cellmodel = new DicChkplatCellModel();
        cellmodel.setPlatId(model.getPlatId());
        cellmodel.setCellName(cellName);
        cellmodel.setStaticFlag(model.getStaticFlag());
        cellmodel.setStdValue(model.getStdValue());
        cellmodel.setUsedFlag(model.getUsedFlag());
        return saveDicChkplatCell(cellmodel);
    }
}
