package com.mvc.service.upgrade;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.mvc.constant.DeviceUpgradeStatus;
import com.mvc.constant.UpgradePlanStatus;
import com.mvc.dao.TUpgradePlanMapper;
import com.mvc.dao.TUpgradeResultsMapper;
import com.mvc.entity.TUpgradePlan;
import com.mvc.entity.TUpgradePlanExample;
import com.mvc.entity.TUpgradeResults;
import com.mvc.model.ResPageModel;
import com.mvc.model.upgrade.TUpgradePackageModel;
import com.mvc.model.upgrade.TUpgradePlanDetailModel;
import com.mvc.model.upgrade.TUpgradePlanModel;
import com.mvc.model.upgrade.TUpgradeResultsModel;
import com.mvc.netty.FzBaowenModel;
import com.mvc.service.taizhang.DeviceAccService;
import com.mvc.utils.DateUtil;
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.*;
import java.util.stream.Collectors;

@Service
@Transactional
public class TUpgradePlanService {

    @Resource
    private TUpgradePlanMapper tUpgradePlanMapper;

    @Resource
    private TUpgradePackageService tUpgradePackageService;

    @Resource
    private TUpgradePlanDetailService tUpgradePlanDetailService;

    @Resource
    private DeviceAccService deviceAccService;

    @Resource
    private TUpgradeResultsMapper upgradeResultsMapper;

    //失败次数限制(考虑NB升级功耗问题,统一为1次 2024-04-15 guozhao)
    private static final int FAIL_NUM = 1;

    /**
     * 查询全部记录
     *
     * @return
     */
    public List<TUpgradePlanModel> findAllTUpgradePlan() {
        List<TUpgradePlan> tUpgradePlans = tUpgradePlanMapper.selectByExample(null);
        List<TUpgradePlanModel> resList = new ArrayList<>();
        for (TUpgradePlan TUpgradePlan : tUpgradePlans) {
            TUpgradePlanModel model = new TUpgradePlanModel();
            BeanUtils.copyProperties(TUpgradePlan, model);
            resList.add(model);
        }
        return resList;
    }

    /**
     * 分页查询全部记录
     *
     * @param queryModel
     * @return
     */
    public ResPageModel findAllTUpgradePlan(TUpgradePlanModel queryModel) {
        TUpgradePlanExample example = new TUpgradePlanExample();
        TUpgradePlanExample.Criteria criteria = example.createCriteria();
        if (StringUtils.isNotEmpty(queryModel.getPlanName())) {
            criteria.andPlanNameLike(StringUtil.getLikeFormat(queryModel.getPlanName()));
        }
        if(StringUtils.isNotEmpty(queryModel.getPlanType())){
            criteria.andPlanTypeEqualTo(queryModel.getPlanType());
        }
        if(StringUtils.isNotEmpty(queryModel.getDeviceClass())){
            criteria.andDeviceClassEqualTo(queryModel.getDeviceClass());
        }
        if (StringUtils.isNotEmpty(queryModel.getPlanStatus())) {
            criteria.andPlanStatusEqualTo(queryModel.getPlanStatus());
        }
        if (StringUtils.isNotEmpty(queryModel.getCreateMan())) {
            criteria.andCreateManLike(StringUtil.getLikeFormat(queryModel.getCreateMan()));
        }
        if (StringUtils.isNotEmpty(queryModel.getPlanMemo())) {//借用备注来根据设备SN查询 2023-08-24 guozhao
            List<Integer> ids = tUpgradePlanDetailService.findPlanIdListByDeviceSn(queryModel.getPlanMemo());
            if (ids == null || ids.size() == 0) {//根据站代码未匹配出来计划,应该返回空
                ResPageModel empty = new ResPageModel(null, 0, 0, 0);
                return empty;
            }
            criteria.andIdIn(ids);
        }
        example.setOrderByClause(" CREATE_TIME desc ");
        Page<Object> pageObject = PageHelper.startPage(queryModel.getPageNum(), queryModel.getPageSize());
        List<TUpgradePlan> tUpgradePlans = tUpgradePlanMapper.selectByExample(example);
        List<TUpgradePlanModel> resList = new ArrayList<>();

        Date now = new Date();

        for (TUpgradePlan tUpgradePlan : tUpgradePlans) {
            TUpgradePlanModel model = new TUpgradePlanModel();
            BeanUtils.copyProperties(tUpgradePlan, model);
            setPackageModel(model);
            setDetails(model);
            model.setPlanTimeRange(new String[]{DateUtil.formateDate(model.getPlanStartTime(), DateUtil.PATTERN_DATA_NORMAL),
                    DateUtil.formateDate(model.getPlanEndTime(), DateUtil.PATTERN_DATA_NORMAL)});

            if (model.getPlanStartTime().compareTo(now) > 0) {
                //计划还未开始
                model.setEnableEdit(1);
                model.setEnableDel(1);
                model.setEnableTermination(0);
            } else {
                //计划已经开始但未结束
                if (model.getPlanStatus().equals(UpgradePlanStatus.INCOMPLETE.getFlag())) {
                    model.setEnableEdit(0);
                    model.setEnableDel(0);
                    model.setEnableTermination(1);
                } else {
                    //计划已经结束了
                    model.setEnableEdit(0);
                    model.setEnableDel(0);
                    model.setEnableTermination(0);
                }
            }

            resList.add(model);
        }
        ResPageModel resPageModel = new ResPageModel(resList, pageObject);
        return resPageModel;
    }

    /**
     * 赋值升级包对象
     *
     * @param model
     */
    private void setPackageModel(TUpgradePlanModel model) {
        if (model.getPackageId() != null) {
            TUpgradePackageModel packageModel = tUpgradePackageService.findTUpgradePackageById(model.getPackageId());
            model.setPackageModel(packageModel);
        }
    }

    /**
     * 赋值升级计划明细
     *
     * @param model
     */
    private void setDetails(TUpgradePlanModel model) {
        List<TUpgradePlanDetailModel> details = tUpgradePlanDetailService.findTUpgradePlanDetailsByPlanId(model.getId());
        model.setDetails(details);
    }

    /**
     * 保存新增对象
     *
     * @param model
     * @return
     */
    public TUpgradePlanModel saveTUpgradePlan(TUpgradePlanModel model) {
        model.setCreateTime(new Date());
        model.setPlanStatus("0");
        if (model.getPlanTimeRange() != null && model.getPlanTimeRange().length == 2) {
            model.setPlanStartTime(DateUtil.parseTimeStr(model.getPlanTimeRange()[0], DateUtil.PATTERN_DATA_NORMAL));
            model.setPlanEndTime(DateUtil.parseTimeStr(model.getPlanTimeRange()[1], DateUtil.PATTERN_DATA_NORMAL));
        }

        TUpgradePackageModel packageModel = null;
        if (model.getPackageId() != null) {
            packageModel = tUpgradePackageService.findTUpgradePackageById(model.getPackageId());
            if (packageModel != null) {
                model.setPlanName(packageModel.getVersionNum());
            }
        }

        TUpgradePlan tUpgradePlan = new TUpgradePlan();
        BeanUtils.copyProperties(model, tUpgradePlan);
        int insert = tUpgradePlanMapper.insert(tUpgradePlan);
        model.setId(tUpgradePlan.getId());

        if (model.getMatchMethod().equals("1")) {//指定设备SN
            if (model.getDetails() != null && model.getDetails().size() > 0 && insert > 0) {
                for (TUpgradePlanDetailModel detail : model.getDetails()) {//保存明细
                    detail.setPlanId(model.getId());
                    tUpgradePlanDetailService.saveTUpgradePlanDetail(detail);
                }
            }
        } else if (model.getMatchMethod().equals("0")) {//指定版本号
            if (packageModel != null) {
                generateDetailsByPackageModel(packageModel.getMatchVersionNum(), model.getId());
            }
        }

        return model;
    }

    /**
     * 修改对象
     *
     * @param model
     * @return
     */
    public TUpgradePlanModel updateTUpgradePlan(TUpgradePlanModel model) {
        if (model.getPlanTimeRange() != null && model.getPlanTimeRange().length == 2) {
            model.setPlanStartTime(DateUtil.parseTimeStr(model.getPlanTimeRange()[0], DateUtil.PATTERN_DATA_NORMAL));
            model.setPlanEndTime(DateUtil.parseTimeStr(model.getPlanTimeRange()[1], DateUtil.PATTERN_DATA_NORMAL));
        }
        TUpgradePlan tUpgradePlan = tUpgradePlanMapper.selectByPrimaryKey(model.getId());
        BeanUtils.copyProperties(model, tUpgradePlan);
        tUpgradePlanMapper.updateByPrimaryKey(tUpgradePlan);

        //明细改为不可更新,判断较为麻烦,不如重新创建 2024-04-12 guozhao
        /*tUpgradePlanDetailService.deleteTUpgradePlanDetailsByPlanId(tUpgradePlan.getId());
        for (TUpgradePlanDetailModel detail : model.getDetails()) {//保存明细
            detail.setPlanId(model.getId());
            tUpgradePlanDetailService.saveTUpgradePlanDetail(detail);
        }*/
        return model;
    }

    /**
     * 删除对象
     *
     * @param id
     * @return
     */
    public int deleteTUpgradePlan(int id) {
        int i = tUpgradePlanMapper.deleteByPrimaryKey(id);
        if (i > 0) {//删除明细
            tUpgradePlanDetailService.deleteTUpgradePlanDetailsByPlanId(id);
        }
        return i;
    }

    /**
     * 根据deviceSn匹配升级计划
     *
     * @param deviceSn
     * @return
     */
    public TUpgradePlanModel findTUpgradePlanByDeviceSn(String deviceSn, boolean isCheckNum) {
        if (StringUtils.isEmpty(deviceSn)) {
            return null;
        }

        TUpgradePlanModel resModel = null;
        TUpgradePlanExample example = new TUpgradePlanExample();
        TUpgradePlanExample.Criteria criteria = example.createCriteria();
        Date now = new Date();
        criteria.andPlanStartTimeLessThanOrEqualTo(now);
        criteria.andPlanEndTimeGreaterThanOrEqualTo(now);
        criteria.andPlanStatusEqualTo(UpgradePlanStatus.INCOMPLETE.getFlag());
        example.setOrderByClause(" CREATE_TIME desc ");
        PageHelper.startPage(1, 50);
        List<TUpgradePlan> tUpgradePlans = tUpgradePlanMapper.selectByExample(example);
        for (TUpgradePlan tUpgradePlan : tUpgradePlans) {
            TUpgradePlanModel model = new TUpgradePlanModel();
            BeanUtils.copyProperties(tUpgradePlan, model);
            setDetails(model);
            //List<TUpgradePlanDetailModel> details = model.getDetails().stream().filter(s -> s.getStationCode().equals(stationCode)).collect(Collectors.toList());
            List<TUpgradePlanDetailModel> details = model.getDetails().stream().filter(s -> s.getDeviceSn().equals(deviceSn)).collect(Collectors.toList());
            if (details != null && details.size() > 0) {
                if (StringUtils.isNotEmpty(details.get(0).getUpgradeStatus())) {
                    //已经升级成功的和版本不匹配的不再升级 2023-06-13 guozhao
                    if (details.get(0).getUpgradeStatus().equals(DeviceUpgradeStatus.SUCCESS.getFlag())
                            || details.get(0).getUpgradeStatus().equals(DeviceUpgradeStatus.MISMATCH.getFlag())) {
                        return null;
                    }
                }
                if (isCheckNum && details.get(0).getFailNum() != null && details.get(0).getFailNum() >= FAIL_NUM) {//失败过n次了,不再升级 2023-07-13 guozhao
                    return null;
                }
                setPackageModel(model);
                resModel = model;
                break;
            }
        }
        return resModel;
    }

    /**
     * 更新升级状态
     *
     * @param partModel
     * @param resultsModel
     */
    public TUpgradePlanDetailModel updateUpgradeStatus(FzBaowenModel partModel, TUpgradeResultsModel resultsModel) {
        TUpgradePlanDetailModel detailModel = null;
        TUpgradePlanModel planModel = findTUpgradePlanByDeviceSn(partModel.getSn(), false);

        if (planModel == null) {
            System.out.println("版本回执:[" + partModel.getSn() + "]没有匹配到升级计划");
            return null;
        }

        //此计划已经执行完成,不可再匹配
        if (!planModel.getPlanStatus().equals(UpgradePlanStatus.INCOMPLETE.getFlag())) {
            return null;
        }

        List<TUpgradePlanDetailModel> details = planModel.getDetails().stream().filter(s -> s.getDeviceSn().equals(partModel.getSn())).collect(Collectors.toList());
        if (details.size() > 0) {
            TUpgradePlanDetailModel detail = details.get(0);

            //设备已经执行成功了
            if (detail.getUpgradeStatus().equals(DeviceUpgradeStatus.SUCCESS.getFlag())) {
                return null;
            }

            detailModel = detail;
            if (planModel.getPlanType().equals("1")) {
                //升级
                String newVersion = planModel.getPackageModel().getVersionNum();//计划的版本号
                String oldVersion = planModel.getPackageModel().getMatchVersionNum();//计划中匹配的版本号
                String curVersion = resultsModel.getVersionNum();//当前设备上报的版本号
                if (curVersion.equals(newVersion)) {
                    //与新版本号匹配,升级成功了
                    detailModel.setUpgradeStatus(DeviceUpgradeStatus.SUCCESS.getFlag());
                } else if (curVersion.equals(oldVersion)) {
                    //与旧版本号匹配,升级失败了
                    detailModel.setUpgradeStatus(DeviceUpgradeStatus.FAIL.getFlag());
                } else {
                    //差分包版本不匹配
                    detailModel.setUpgradeStatus(DeviceUpgradeStatus.MISMATCH.getFlag());
                }
            } else {
                //回退(不需要匹配版本号,收到ED后就代表回退成功,MCU中只切换标记,没有读写,常规下不会失败)
                detailModel.setUpgradeStatus(DeviceUpgradeStatus.SUCCESS.getFlag());
            }
            detailModel.setUpgradeTime(new Date());
            tUpgradePlanDetailService.updateTUpgradePlanDetail(detailModel);
        }

        updatePlanStatus(planModel, detailModel);

        return detailModel;
    }

    /**
     * 更新计划状态
     *
     * @param planModel
     * @param curDetail
     */
    public void updatePlanStatus(TUpgradePlanModel planModel, TUpgradePlanDetailModel curDetail) {
        int sucNum = 0;
        int failNum = 0;
        String status = "";
        for (TUpgradePlanDetailModel detail : planModel.getDetails()) {
            status = detail.getId() == curDetail.getId() ? curDetail.getUpgradeStatus() : detail.getUpgradeStatus();
            DeviceUpgradeStatus deviceUpgradeStatus = DeviceUpgradeStatus.getDeviceUpgradeStatusByFlag(status);
            if (deviceUpgradeStatus == null) continue;

            switch (deviceUpgradeStatus) {
                case SUCCESS:
                    sucNum++;
                    break;
                case FAIL:
                    if (detail.getFailNum() >= FAIL_NUM) {
                        failNum++;
                    }
                    break;
                case MISMATCH:
                    failNum++;
                    break;
            }
        }

        boolean isCompleted = true;

        //全部执行完了(含失败不再执行的,超次和版本不匹配的)
        if (planModel.getDetails().size() == sucNum) {//全部成功
            planModel.setPlanStatus(UpgradePlanStatus.SUCCESS.getFlag());
        } else if (planModel.getDetails().size() == failNum) {//全部失败
            planModel.setPlanStatus(UpgradePlanStatus.MISMATCH.getFlag());
        } else if (planModel.getDetails().size() == (sucNum + failNum)) {//部分成功
            planModel.setPlanStatus(UpgradePlanStatus.FAIL.getFlag());
        } else {//未执行完,不更新状态
            isCompleted = false;
        }

        if (isCompleted) {
            //注意不可更新明细 2024-03-14 guozhao
            TUpgradePlan tUpgradePlan = tUpgradePlanMapper.selectByPrimaryKey(planModel.getId());
            tUpgradePlan.setPlanStatus(planModel.getPlanStatus());
            tUpgradePlanMapper.updateByPrimaryKey(tUpgradePlan);
        }
    }

    /**
     * 根据ID获取升级计划对象
     *
     * @param id
     * @return
     */
    public TUpgradePlanModel findTUpgradePlanModelById(int id) {
        TUpgradePlan plan = tUpgradePlanMapper.selectByPrimaryKey(id);
        if (plan != null) {
            TUpgradePlanModel model = new TUpgradePlanModel();
            BeanUtils.copyProperties(plan, model);
            return model;
        }
        return null;
    }

    /**
     * 根据TUpgradePlanModel和deviceSn更新设备的执行次数
     *
     * @param planModel
     * @param deviceSn
     */
    public void updateExecNum(TUpgradePlanModel planModel, String deviceSn) {
        Optional<TUpgradePlanDetailModel> optional = planModel.getDetails().stream().filter(s -> s.getDeviceSn().equals(deviceSn)).findFirst();
        if (optional.isPresent()) {
            TUpgradePlanDetailModel detailModel = optional.get();
            detailModel.setFailNum(detailModel.getFailNum() == null ? 1 : detailModel.getFailNum() + 1);
            tUpgradePlanDetailService.updateTUpgradePlanDetail(detailModel);
        }
    }

    /**
     * 处理设备多次升级失败的情况
     * 更新设备升级状态及计划状态
     */
    public void handleMultipleFailedDevices() {
        List<TUpgradePlan> plans = selectIncompletePlan();
        Date now = new Date();
        for (TUpgradePlan plan : plans) {
            List<TUpgradePlanDetailModel> details = tUpgradePlanDetailService.findTUpgradePlanDetailsByPlanId(plan.getId());
            int sucNum = 0;
            int failNum = 0;

            boolean isOverdue = now.compareTo(plan.getPlanEndTime()) > 0;

            for (TUpgradePlanDetailModel detail : details) {
                boolean isUpdated = false;
                if (detail.getUpgradeStatus().equals(DeviceUpgradeStatus.UNEXECUTED.getFlag())) {
                    if (isOverdue) {
                        isUpdated = isOverdue;
                    } /*else if (detail.getFailNum() >= FAIL_NUM && detail.getUpgradeTime() != null) {
                        //将尝试数次改为1次后,引发此问题 2024-04-15 guozhao
                        //判断时间是为了防止还未收到设备回复的升级结果报文时,此任务已经将状态更新为失败
                        long mins = DateUtil.getMinsbetweenTime(detail.getUpgradeTime(), now);
                        if (mins > 15) {
                            isUpdated = true;
                        } else {
                            break;
                        }
                    }*/ //注释掉了,有的设备升级成功后,连接失败,要等下次离散才可能上报成功,如果直接判定失败,是不合理的 --2024-04-15 guozhao
                    if (isUpdated) {
                        detail.setUpgradeStatus(isOverdue ? DeviceUpgradeStatus.OVERDUE.getFlag() : DeviceUpgradeStatus.FAIL.getFlag());
                        detail.setUpgradeTime(now);
                        tUpgradePlanDetailService.updateTUpgradePlanDetail(detail);
                        failNum++;
                    }
                } else if (detail.getUpgradeStatus().equals(DeviceUpgradeStatus.SUCCESS.getFlag())) {
                    sucNum++;
                } else {
                    failNum++;
                }
            }
            //如果处理完超过3次的,整批设备都已经执行过了,则更新计划状态
            if (details.size() == (sucNum + failNum)) {
                //全部执行完了(含失败不再执行的,超次和版本不匹配的)
                if (details.size() == sucNum) {//全部成功
                    plan.setPlanStatus(UpgradePlanStatus.SUCCESS.getFlag());
                } else if (details.size() == failNum) {//全部失败
                    plan.setPlanStatus(UpgradePlanStatus.MISMATCH.getFlag());
                } else if (details.size() == (sucNum + failNum)) {//部分成功
                    plan.setPlanStatus(UpgradePlanStatus.FAIL.getFlag());
                }
                tUpgradePlanMapper.updateByPrimaryKey(plan);
            }
        }
    }


    /**
     * 查询未完成的升级计划
     *
     * @return
     */
    private List<TUpgradePlan> selectIncompletePlan() {
        TUpgradePlanExample example = new TUpgradePlanExample();
        example.createCriteria().andPlanStatusEqualTo(UpgradePlanStatus.INCOMPLETE.getFlag());
        List<TUpgradePlan> plans = tUpgradePlanMapper.selectByExample(example);
        return plans;
    }

    /**
     * 根据版本号生成设备升级明细
     *
     * @param versionNum
     * @param planId     (=0时,不入库,只做查询)
     * @return
     */
    private List<TUpgradePlanDetailModel> generateDetailsByPackageModel(String versionNum, int planId) {
        List<TUpgradePlanDetailModel> detailModels = new ArrayList<>();
        List<TUpgradeResults> upgradeResultsList = upgradeResultsMapper.getLatestUpgradeResultByVersionNum(versionNum);

        if (upgradeResultsList.size() > 0) {
            //去重,并忽略掉是0的SN号
            List<String> deviceSns = new ArrayList<>();
            deviceSns.add("00000000000");
            for (TUpgradeResults upgradeResults : upgradeResultsList) {
                if (StringUtils.isEmpty(upgradeResults.getDeviceSn()) || deviceSns.contains(upgradeResults.getDeviceSn())) {
                    continue;
                }
                TUpgradePlanDetailModel detailModel = new TUpgradePlanDetailModel();

                detailModel.setDeviceSn(upgradeResults.getDeviceSn());
                if (planId > 0) {
                    detailModel.setPlanId(planId);
                    TUpgradePlanDetailModel resModel = tUpgradePlanDetailService.saveTUpgradePlanDetail(detailModel);
                    detailModels.add(resModel);
                } else {
                    detailModels.add(detailModel);
                }
                deviceSns.add(upgradeResults.getDeviceSn());
            }
        }
        return detailModels;
    }

    /**
     * 验证设备是否存在于台账中
     *
     * @param planModel
     * @return
     */
    public String validateDeviceExistenceInPlan(TUpgradePlanModel planModel) {
        if (planModel.getMatchMethod().equals("0")) {
            TUpgradePackageModel packageModel = null;
            if (planModel.getPackageId() != null) {
                packageModel = tUpgradePackageService.findTUpgradePackageById(planModel.getPackageId());
                if(packageModel==null){
                    return "升级文件已不存在";
                }
                List<TUpgradePlanDetailModel> detailModels = generateDetailsByPackageModel(packageModel.getMatchVersionNum(), 0);
                if (detailModels.size() == 0) {
                    return "当前版本未匹配到任何设备";
                }
            }
        } else {
            if (planModel.getDetails() == null || planModel.getDetails().size() == 0) {
                return "未添加任何设备";
            }
        }

        // 初始时，没有发现不存在的设备
        StringBuilder sb = new StringBuilder();
        if (planModel.getDetails() != null) {
            try {
                // 使用Stream API优化设备存在性检查
                String nonExistentDevices = planModel.getDetails().stream()
                        .filter(detailModel -> deviceAccService.findDeviceAccBysn(detailModel.getDeviceSn()) == null)
                        .map(TUpgradePlanDetailModel::getDeviceSn)
                        .collect(Collectors.joining(","));

                if (!nonExistentDevices.isEmpty()) {
                    // 如果存在不存在的设备，构建相应的错误消息
                    sb.append("设备未登记台账:");
                    sb.append(nonExistentDevices);
                    sb.append(";");

                    return sb.toString();
                }

                List<TUpgradePlan> list = selectIncompletePlan();
                for (TUpgradePlan plan : list) {
                    List<TUpgradePlanDetailModel> details = tUpgradePlanDetailService.findTUpgradePlanDetailsByPlanId(plan.getId());
                    for (TUpgradePlanDetailModel detail : details) {
                        if (!detail.getUpgradeStatus().equals(DeviceUpgradeStatus.UNEXECUTED.getFlag())) {
                            continue;
                        }

                        Optional<TUpgradePlanDetailModel> optional = planModel.getDetails().stream().filter(s -> s.getDeviceSn().equals(detail.getDeviceSn())).findFirst();
                        if (optional.isPresent()) {
                            sb.append(detail.getDeviceSn());
                            sb.append(",");
                        }
                    }
                }
                if (sb.length() > 0) {
                    sb.deleteCharAt(sb.length() - 1);
                    sb.insert(0, "设备已存在升级计划中:");
                }
            } catch (Exception e) {
                // 处理可能的异常，例如日志记录、错误回报等
                // 这里只是简单的打印异常堆栈，实际应用中可能需要更复杂的处理逻辑
                e.printStackTrace();
            }
        }
        return sb.toString();
    }

    /**
     * 根据packageId查询升级计划
     *
     * @param packageId
     * @return
     */
    public List<TUpgradePlan> findTUpgradePlanByPackageId(int packageId) {
        TUpgradePlanExample example = new TUpgradePlanExample();
        example.createCriteria().andPackageIdEqualTo(packageId);
        List<TUpgradePlan> list = tUpgradePlanMapper.selectByExample(example);
        return list;
    }

    /**
     * 根据升级包id删除升级计划
     *
     * @param packageId
     * @return
     */
    public int deleteTUpgradePlanByPackageId(int packageId) {
        TUpgradePlanExample example = new TUpgradePlanExample();
        example.createCriteria().andPackageIdEqualTo(packageId);
        return tUpgradePlanMapper.deleteByExample(example);
    }
}
