package com.mvc.service.upgrade;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.mvc.constant.BaseConstant;
import com.mvc.dao.TUpgradeResultsMapper;
import com.mvc.entity.TUpgradeResults;
import com.mvc.entity.TUpgradeResultsExample;
import com.mvc.model.ResPageModel;
import com.mvc.model.upgrade.TUpgradePlanModel;
import com.mvc.model.upgrade.TUpgradeResultsModel;
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 TUpgradeResultsService {

    @Resource
    private TUpgradeResultsMapper upgradeResultsMapper;

    @Resource
    private TUpgradePlanService upgradePlanService;

    /**
     * 查询全部记录
     *
     * @return
     */
    public ResPageModel findAllTUpgradeResults(TUpgradeResultsModel queryModel) {
        TUpgradeResultsExample example = new TUpgradeResultsExample();
        TUpgradeResultsExample.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.getStationCode())) {
            if (queryModel.getStationCode().length() == BaseConstant.LENGTH_STATION_CODE) {
                criteria.andStationCodeEqualTo(queryModel.getStationCode());
            } else {
                criteria.andStationCodeLike(StringUtil.getLikeFormat(queryModel.getStationCode()));
            }
        }
        if (StringUtils.isNotEmpty(queryModel.getVersionNum())) {
            criteria.andVersionNumEqualTo(queryModel.getVersionNum());
        }
        if (queryModel.getUpgradeResult() != null) {
            criteria.andUpgradeResultEqualTo(queryModel.getUpgradeResult());
        }
        example.setOrderByClause(" CREATE_TIME desc ");
        Page<Object> pageObject = PageHelper.startPage(queryModel.getPageNum(), queryModel.getPageSize());
        List<TUpgradeResults> tUpgradePlans = upgradeResultsMapper.selectByExample(example);
        List<TUpgradeResultsModel> resList = new ArrayList<>();
        for (TUpgradeResults upgradeResults : tUpgradePlans) {
            TUpgradeResultsModel model = new TUpgradeResultsModel();
            BeanUtils.copyProperties(upgradeResults, model);
            setPlanModel(model);
            resList.add(model);
        }
        ResPageModel resPageModel = new ResPageModel(resList, pageObject);
        return resPageModel;
    }

    /**
     * 赋值升级计划对象
     *
     * @param model
     */
    private void setPlanModel(TUpgradeResultsModel model) {
        if (model.getPlanId() != null) {
            TUpgradePlanModel planModel = upgradePlanService.findTUpgradePlanModelById(model.getPlanId());
            model.setPlanModel(planModel);
        }
    }

    /**
     * 保存新增对象
     *
     * @param model
     * @return
     */
    public TUpgradeResultsModel saveTUpgradeResults(TUpgradeResultsModel model) {
        if (model.getDeviceSn().length() >= 12) {
            model.setDeviceSn(model.getDeviceSn().substring(0, 11));
        }
        model.setCreateTime(new Date());
        TUpgradeResults tUpgradeResults = new TUpgradeResults();
        BeanUtils.copyProperties(model, tUpgradeResults);
        upgradeResultsMapper.insert(tUpgradeResults);
        model.setId(tUpgradeResults.getId());
        return model;
    }

    /**
     * 修改对象
     *
     * @param model
     * @return
     */
    public TUpgradeResultsModel updateTUpgradeResults(TUpgradeResultsModel model) {
        TUpgradeResults tUpgradeResults = upgradeResultsMapper.selectByPrimaryKey(model.getId());
        BeanUtils.copyProperties(model, tUpgradeResults);
        int i = upgradeResultsMapper.updateByPrimaryKey(tUpgradeResults);
        return model;
    }

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