package com.jisco.dynamicweight.dynamicbasereciever.service.impl;

import cn.hutool.core.lang.Assert;
import com.jisco.dynamicweight.dynamicbasereciever.mapper.DynamicBaseRecieverMapper;
import com.jisco.dynamicweight.dynamicbasereciever.model.DynamicBaseReciever;
import com.jisco.dynamicweight.dynamicbasereciever.service.IDynamicBaseRecieverService;
import com.jisco.dynamicweight.dynamicbasesender.model.DynamicBaseSender;
import com.jisco.dynamicweight.util.UserUtil;
import com.steerinfo.baseinfo.meterbasecalibrarelation.model.MeterBaseCalibraRelation;
import com.steerinfo.framework.mapper.IBaseMapper;
import com.steerinfo.framework.service.impl.BaseServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

/**
 * DynamicBaseReciever服务实现：
 * @author generator
 * @version 1.0-SNAPSHORT 2021-12-31 02:55
 * 类描述
 * 修订历史：
 * 日期：2021-12-31
 * 作者：generator
 * 参考：
 * 描述：DynamicBaseReciever服务实现
 * @see null
 * @Copyright 湖南视拓信息技术股份有限公司. All rights reserved.
 */
@Service(value = "dynamicBaseRecieverService")
public class DynamicBaseRecieverServiceImpl extends BaseServiceImpl<DynamicBaseReciever, String> implements IDynamicBaseRecieverService {

    @Autowired
    private DynamicBaseRecieverMapper dynamicBaseRecieverMapper;

    @Override
    protected IBaseMapper<DynamicBaseReciever, String> getMapper() {
        return dynamicBaseRecieverMapper;
    }

    @Override
    public List<MeterBaseCalibraRelation> queryall(HashMap parmas) {
        return dynamicBaseRecieverMapper.selectByParameters(parmas);
    }
    /**
     * 启用
     * @param id
     * @return
     */
    @Override
    public boolean enable(String id) {
        DynamicBaseReciever byId = this.getById(id);
        Assert.notNull(byId, "记录不存在");
        if (byId.getValidFlag().equals("1")) return false;
        DynamicBaseReciever config = new DynamicBaseReciever();
        config.setReceiverNo(id);
        setUpdateManInfo(config);
        return dynamicBaseRecieverMapper.enable(config) == 1;
    }

    /**
     * 禁用
     * @param id
     * @return
     */
    @Override
    public boolean disable(String id) {
        DynamicBaseReciever byId = this.getById(id);
        if (byId.getValidFlag() == "0") return false;
        DynamicBaseReciever config = new DynamicBaseReciever();
        config.setReceiverNo(id);
        setUpdateManInfo(config);
        return dynamicBaseRecieverMapper.disable(config) == 1;
    }

    private void setUpdateManInfo(DynamicBaseReciever config) {
        config.setUpdateManNo(UserUtil.getCurrentUserId());
        config.setUpdateManName(UserUtil.getCurrentUserName());
        config.setUpdateTime(new Date());
    }
}
