package com.steerinfo.baseinfo.meterbaseversions.service.impl;

import com.steerinfo.framework.controller.RESTfulResult;
import com.steerinfo.framework.mapper.IBaseMapper;
import com.steerinfo.framework.service.impl.BaseServiceImpl;
import com.steerinfo.baseinfo.meterbaseversions.model.MeterBaseVersions;
import com.steerinfo.baseinfo.meterbaseversions.mapper.MeterBaseVersionsMapper;
import com.steerinfo.baseinfo.meterbaseversions.service.IMeterBaseVersionsService;
import com.steerinfo.framework.user.UserPayload;
import com.steerinfo.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.math.BigDecimal;
import java.util.HashMap;

/**
 * MeterBaseVersions服务实现：
 * @author generator
 * @version 1.0-SNAPSHORT 2022-03-08 03:59
 * 类描述
 * 修订历史：
 * 日期：2022-03-08
 * 作者：generator
 * 参考：
 * 描述：MeterBaseVersions服务实现
 * @see null
 * @Copyright 湖南视拓信息技术股份有限公司. All rights reserved.
 */
@Service(value = "meterBaseVersionsService")
public class MeterBaseVersionsServiceImpl extends BaseServiceImpl<MeterBaseVersions, String> implements IMeterBaseVersionsService {

    @Autowired
    private MeterBaseVersionsMapper meterBaseVersionsMapper;

    @Override
    protected IBaseMapper<MeterBaseVersions, String> getMapper() {
        return meterBaseVersionsMapper;
    }

    @Override
    public RESTfulResult checkAdd(MeterBaseVersions model) {
        RESTfulResult rm = new RESTfulResult();
        rm.setFailed();
        if (StringUtils.isEmpty(model.getVersionsInfo())) {
            rm.setMessage("版本信息不能为空");
        }
        if (StringUtils.isEmpty(model.getDownloadUrl())) {
            rm.setMessage("下载地址不能为空");
        }
        //获取最新的版本对象
        MeterBaseVersions versionModel = meterBaseVersionsMapper.selectNewVersion();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
        model.setVersionsNo(sdf.format(new Date()));
        //首次新增版本信息，版本号默认为1、后续新增版本信息，版本号加1
        if(versionModel == null){
            model.setVersionsNumber("1");
        }else{
            Long num = Long.parseLong(versionModel.getVersionsNumber()) + 1;
            model.setVersionsNumber(String.valueOf(num));
        }
        model.setValidFlag("1");
        UserPayload payload = UserPayload.getCurrUser();
        String userId = payload.getUserCode();
        String userName = payload.getUserName();
        /*String userId = "admins";
        String userName = "admins";*/
        model.setCreateManNo(userId);
        model.setCreateManName(userName);
        model.setCreateTime(new Date());
        int i = meterBaseVersionsMapper.insert(model);
        if( i >= 1){
            rm.setSucceed();
            rm.setCode("200");
            rm.setData(model);
            rm.setMessage("操作成功！！");
        }else{
            rm.setMessage("操作失败！！");
        }
        return rm;
    }

    @Override
    public RESTfulResult selectByVersionNo(String versionNo) {
        RESTfulResult rm = new RESTfulResult();
        rm.setSucceed();
        rm.setCode("200");
        MeterBaseVersions versionModel = meterBaseVersionsMapper.selectNewVersion();
        Long num = Long.parseLong(versionNo);
        Long versionNum = Long.parseLong(versionModel.getVersionsNumber());
        if(num >= versionNum) {
            rm.setMessage("当前版本已是最新版本，无需更新！！");
        }else{
            rm.setMessage("当前版本不是最新版本，请更新！！");
            rm.setData(versionModel);
        }
        return rm;
    }

    @Override
    public RESTfulResult updateValueFlag(String versionNo) {
        RESTfulResult rm = new RESTfulResult();
        rm.setSucceed();
        rm.setCode("200");
        MeterBaseVersions versionModel = meterBaseVersionsMapper.selectByPrimaryKey(versionNo);
        versionModel.setValidFlag("0");
        UserPayload payload = UserPayload.getCurrUser();
        String userId = payload.getUserCode();
        String userName = payload.getUserName();
        /*String userId = "admins";
        String userName = "admins";*/
        versionModel.setUpdateManNo(userId);
        versionModel.setCreateManName(userName);
        versionModel.setUpdateTime(new Date());
        int i = meterBaseVersionsMapper.updateByPrimaryKey(versionModel);
        if(i >= 0) {
            rm.setMessage("废除版本完成！！");
        }else {
            rm.setMessage("废除版本失败！！");
        }
        return rm;
    }
}
