package com.mvc.service;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.mvc.constant.BaseConstant;
import com.mvc.dao.TServerCfgMapper;
import com.mvc.entity.TServerCfg;
import com.mvc.entity.TServerCfgExample;
import com.mvc.model.ResPageModel;
import com.mvc.model.TServerCfgModel;
import com.mvc.utils.StringUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
@Transactional
public class TServerCfgService {

    @Resource
    private TServerCfgMapper serverCfgMapper;

    /**
     * 查询全部记录
     *
     * @return
     */
    public ResPageModel findAllTServerCfg(TServerCfgModel queryModel) {
        TServerCfgExample example = new TServerCfgExample();
        TServerCfgExample.Criteria criteria = example.createCriteria();
        if (StringUtils.isNotEmpty(queryModel.getDeviceSn())) {
            if (queryModel.getDeviceSn().length() == BaseConstant.LENGTH_DEVICE_SN) {
                criteria.andDeviceSnEqualTo(queryModel.getDeviceSn());
            } else {
                criteria.andDeviceSnLike(StringUtil.getLikeFormat(queryModel.getDeviceSn()));
            }
        }

        if(StringUtils.isNotEmpty(queryModel.getCreateMan())){
            criteria.andCreateManLike(StringUtil.getLikeFormat(queryModel.getCreateMan()));
        }

        example.setOrderByClause(" CREATE_TIME desc ");
        Page<Object> pageObject = PageHelper.startPage(queryModel.getPageNum(), queryModel.getPageSize());
        List<TServerCfg> tUpgradePlans = serverCfgMapper.selectByExample(example);
        List<TServerCfgModel> resList = new ArrayList<>();
        for (TServerCfg upgradeResults : tUpgradePlans) {
            TServerCfgModel model = new TServerCfgModel();
            BeanUtils.copyProperties(upgradeResults, model);
            resList.add(model);
        }
        ResPageModel resPageModel = new ResPageModel(resList, pageObject);
        return resPageModel;
    }

    /**
     * 根据设备编号查询对象
     * @param deviceSn
     * @return
     */
    public TServerCfgModel findTServerCfgModelByDeviceSn(String deviceSn) {
        TServerCfgExample example = new TServerCfgExample();
        TServerCfgExample.Criteria criteria = example.createCriteria();
        criteria.andDeviceSnEqualTo(deviceSn);
        criteria.andUseFlagEqualTo(BaseConstant.VALID_FLAG);

        List<TServerCfg> tUpgradePlans = serverCfgMapper.selectByExample(example);
        if(tUpgradePlans.size() > 0){
            TServerCfg tServerCfg = tUpgradePlans.get(0);
            TServerCfgModel model = new TServerCfgModel();
            BeanUtils.copyProperties(tServerCfg, model);
            return model;
        }

        return null;
    }

    /**
     * 保存新增对象
     *
     * @param model
     * @return
     */
    public TServerCfgModel saveTServerCfg(TServerCfgModel model) {
        if (model.getDeviceSn().length() >= 12) {
            model.setDeviceSn(model.getDeviceSn().substring(0, 11));
        }

        //1.将原来有的数据置为无效
        TServerCfgExample example = new TServerCfgExample();
        TServerCfgExample.Criteria criteria = example.createCriteria();
        criteria.andDeviceSnEqualTo(model.getDeviceSn());
        criteria.andUseFlagEqualTo(BaseConstant.VALID_FLAG);
        TServerCfg record = new TServerCfg();
        record.setUseFlag(BaseConstant.INVALID_FLAG);
        serverCfgMapper.updateByExampleSelective(record, example);

        //2.保存新记录
        model.setCreateTime(new Date());
        model.setUseFlag(BaseConstant.VALID_FLAG);
        TServerCfg tServerCfg = new TServerCfg();
        BeanUtils.copyProperties(model, tServerCfg);
        serverCfgMapper.insert(tServerCfg);
        model.setId(tServerCfg.getId());
        return model;
    }



    /**
     * 修改对象
     *
     * @param model
     * @return
     */
    public TServerCfgModel updateTServerCfg(TServerCfgModel model) {
        TServerCfg tServerCfg = serverCfgMapper.selectByPrimaryKey(model.getId());
        BeanUtils.copyProperties(model, tServerCfg);
        int i = serverCfgMapper.updateByPrimaryKey(tServerCfg);
        return model;
    }

    /**
     * 删除对象
     *
     * @param id
     * @return
     */
    public int deleteTServerCfg(int id) {
        int i = serverCfgMapper.deleteByPrimaryKey(id);
        return i;
    }
}
