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

import com.steerinfo.baseinfo.constants.DbConstants;
import com.steerinfo.framework.mapper.IBaseMapper;
import com.steerinfo.framework.service.impl.BaseServiceImpl;
import com.steerinfo.baseinfo.meterbasehistarecount.model.MeterBaseHisTareCount;
import com.steerinfo.baseinfo.meterbasehistarecount.mapper.MeterBaseHisTareCountMapper;
import com.steerinfo.baseinfo.meterbasehistarecount.service.IMeterBaseHisTareCountService;
import com.steerinfo.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

/**
 * MeterBaseHisTareCount服务实现：
 *
 * @author generator
 * @version 1.0-SNAPSHORT 2020-06-28 09:33
 * 类描述
 * 修订历史：
 * 日期：2020-06-28
 * 作者：generator
 * 参考：
 * 描述：MeterBaseHisTareCount服务实现
 * @Copyright 湖南视拓信息技术股份有限公司. All rights reserved.
 * @see null
 */
@Service(value = "meterBaseHisTareCountService")
public class MeterBaseHisTareCountServiceImpl extends BaseServiceImpl<MeterBaseHisTareCount, String> implements IMeterBaseHisTareCountService {

    @Autowired
    private MeterBaseHisTareCountMapper meterBaseHisTareCountMapper;

    @Override
    protected IBaseMapper<MeterBaseHisTareCount, String> getMapper() {
        return meterBaseHisTareCountMapper;
    }

    @Override
    public String checkandadd(MeterBaseHisTareCount model) {
        String msg = check(model);
        if (StringUtils.isNotEmpty(msg)) {
            return msg;
        }
        HashMap<String, Object> obj = new HashMap<>();
        if (StringUtils.isNotEmpty(model.getTypeNo())) {
            obj.put("valueFlag", "0");//有效
            obj.put("typeNo", model.getTypeNo());//同一类型的只能有一条数据有效
            List<MeterBaseHisTareCount> list = meterBaseHisTareCountMapper.selectByParameters(obj);
            for (MeterBaseHisTareCount list1 : list) {
                list1.setCountNo(list1.getCountNo());
                list1.setValueFlag("1");//无效
                meterBaseHisTareCountMapper.updateByPrimaryKeySelective(list1);
            }
        }

        String no = meterBaseHisTareCountMapper.GetNewID();
        model.setCountNo(new SimpleDateFormat("yyyyMMdd").format(new Date()) + no);
        model.setCreateTime(new Date());
        model.setValueFlag("0");
        model.setCountEdition((float) 1.0);
        meterBaseHisTareCountMapper.insertSelective(model);
        return "";
    }

    @Override
    public String Recovery(MeterBaseHisTareCount model) {
        try {
            if (model == null) {
                return "请传入参数!";
            }
            HashMap<String, Object> obj = new HashMap<>();
            obj.put("valueFlag", DbConstants.VALID);
            List<MeterBaseHisTareCount> list = meterBaseHisTareCountMapper.selectByParameters(obj);
            for (MeterBaseHisTareCount list1 : list) {
                list1.setCountNo(list1.getCountNo());
                list1.setValueFlag(DbConstants.INVALID);
                meterBaseHisTareCountMapper.updateByPrimaryKeySelective(list1);
            }
            model.setCountNo(model.getCountNo());
            model.setValueFlag("0");
            model.setUpdateManName(model.getUpdateManName());
            model.setUpdateManNo(model.getUpdateManNo());
            model.setUpdateTime(new Date());
            meterBaseHisTareCountMapper.updateByPrimaryKey(model);
        } catch (Exception ex) {
            ex.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return "操作异常，请联系管理员";
        }
        return "";
    }

    @Override
    public String checkandupdate(String id, MeterBaseHisTareCount model) {
        String msg = check(model);
        if (StringUtils.isNotEmpty(msg)) {
            return msg;
        }
        model.setUpdateTime(new Date());
        model.setCountEdition((float) (model.getCountEdition() + 0.1));
        meterBaseHisTareCountMapper.updateByPrimaryKey(model);
        return "";
    }

    @Override
    public String abolish(MeterBaseHisTareCount model) {
        try {
            if (model == null) {
                return "请传入参数!";
            }
            model.setCountNo(model.getCountNo());
            model.setValueFlag("1");
            model.setUpdateManName(model.getUpdateManName());
            model.setUpdateManNo(model.getUpdateManNo());
            model.setUpdateTime(new Date());
            meterBaseHisTareCountMapper.updateByPrimaryKey(model);
        } catch (Exception ex) {
            ex.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return "操作异常，请联系管理员";
        }
        return "";
    }


    private String check(MeterBaseHisTareCount model) {
        if (model == null) {
            return "数据为空";
        }
        if (StringUtils.isEmpty(model.getContrastTypeNo())) {
            return "对比类型编号不能为空！";
        }
        if (StringUtils.isEmpty(model.getContrastTypeName())) {
            return "对比类型名称不能为空！";
        }
        if (StringUtils.isEmpty(model.getCountTypeNo())) {
            return "计算方式编号不能为空！";
        }
        if (StringUtils.isEmpty(model.getCountTypeName())) {
            return "计算方式名称不能为空！";
        }
        return "";
    }


    /**
     * 按编码查询历史皮重配置子表的数据
     *
     * @param model
     * @return
     */
    @Override
    public List<MeterBaseHisTareCount> doQuerySpecWf(MeterBaseHisTareCount model) {
        if (model == null || StringUtils.isEmpty(model.getCountTypeNo())) {
            return null;
        }
        String countTypeNo = model.getCountTypeNo();
        List<MeterBaseHisTareCount> tareCountList = meterBaseHisTareCountMapper.doQuerySpecWf(countTypeNo);
        return tareCountList;
    }
}
