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

import com.steerinfo.baseinfo.meterbaseaddweight.mapper.MeterBaseAddWeightMapper;
import com.steerinfo.baseinfo.meterbaseaddweight.model.MeterBaseAddWeight;
import com.steerinfo.baseinfo.meterbaseaddweight.service.IMeterBaseAddWeightService;
import com.steerinfo.framework.mapper.IBaseMapper;
import com.steerinfo.framework.service.impl.BaseServiceImpl;
import com.steerinfo.framework.utils.base.StringUtils;
import com.steerinfo.util.CommonPage;
import com.steerinfo.util.FieldsCollector;
import org.springframework.stereotype.Service;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

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

    @Resource
    private MeterBaseAddWeightMapper meterBaseAddWeightMapper;

    @Override
    protected IBaseMapper<MeterBaseAddWeight, String> getMapper() {
        return meterBaseAddWeightMapper;
    }

    @Override
    public String checkandadd(MeterBaseAddWeight model) {
        String msg = check(model);
        if (StringUtils.isNotEmpty(msg)) {
            return msg;
        }
        HashMap<String, Object> obj = new HashMap<>();
        obj.put("utensilNo", model.getUtensilNo());
        obj.put("utensilName", model.getUtensilName());
        obj.put("utensilNameNo", model.getUtensilNameNo());
        List<MeterBaseAddWeight> com = meterBaseAddWeightMapper.selectByParameters(obj);
        if (!com.isEmpty()) {
            return model.getUtensilName() + "的" + model.getUtensilNo() + "编号已存在！";
        }
        String no = meterBaseAddWeightMapper.GetNewID();
        model.setId(new SimpleDateFormat("yyyyMMdd").format(new Date()) + no);
        model.setCreateTime(new Date());
        model.setValueFlag("0");
        meterBaseAddWeightMapper.insert(model);
        return "";
    }

    @Override
    public String Recovery(List<MeterBaseAddWeight> models) {
        try {
            if (models == null || models.size() < 1) {
                return "请传入参数!";
            }
            for (MeterBaseAddWeight model : models) {
                model.setId(model.getId());
                model.setValueFlag("0");
                model.setUpdateManName(model.getUpdateManName());
                model.setUpdateManNo(model.getUpdateManNo());
                model.setUpdateTime(new Date());
                meterBaseAddWeightMapper.updateByPrimaryKey(model);
            }
        } catch (Exception ex) {
            ex.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return "操作异常，请联系管理员";
        }
        return "";
    }

    @Override
    public String checkandupdate(String id, MeterBaseAddWeight model) {
        String msg = check(model);
        if (StringUtils.isNotEmpty(msg)) {
            return msg;
        }
        HashMap<String, Object> obj = new HashMap<>();
        obj.put("utensilNo", model.getUtensilNo());
        obj.put("utensilNameNo", model.getUtensilNameNo());
        obj.put("addWeightNo", id);
        List<MeterBaseAddWeight> com = meterBaseAddWeightMapper.selectForUpdate(obj);
        if (!com.isEmpty()) {
            return model.getUtensilName() + "的" + model.getUtensilNo() + "编号已存在！";
        }
        model.setUpdateTime(new Date());
        meterBaseAddWeightMapper.updateByPrimaryKey(model);
        return "";
    }

    @Override
    public String abolish(List<MeterBaseAddWeight> models) {
        try {
            if (models == null || models.size() < 1) {
                return "请传入参数!";
            }
            for (MeterBaseAddWeight model : models) {
                model.setId(model.getId());
                model.setValueFlag("1");
                model.setUpdateManName(model.getUpdateManName());
                model.setUpdateManNo(model.getUpdateManNo());
                model.setUpdateTime(new Date());
                meterBaseAddWeightMapper.updateByPrimaryKey(model);
            }
        } catch (Exception ex) {
            ex.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return "操作异常，请联系管理员";
        }
        return "";
    }

    /**
     * 查询待选器具
     *
     * @param cp 0有效  1无效
     * @return
     */
    public List<MeterBaseAddWeight> doQueryWf(CommonPage<MeterBaseAddWeight> cp) {
        MeterBaseAddWeight mbw = new MeterBaseAddWeight();
        if (cp.getParam() != null) {
            mbw=cp.getParam();
        }
        mbw.setValueFlag("0");

        Map<String, Object> map = FieldsCollector.beanToMap(mbw);
        return meterBaseAddWeightMapper.selectLikeByParameters(map);
    }


    private String check(MeterBaseAddWeight model) {
        if (model == null) {
            return "参数不能为空！";
        }
        if (StringUtils.isEmpty(model.getUtensilNo())) {
            return "器具编号不能为空";
        }
        if (StringUtils.isEmpty(model.getUtensilNameNo())) {
            return "器具名称编号不能为空";
        }
        if (StringUtils.isEmpty(model.getUtensilName())) {
            return "器具名称不能为空";
        }
        if (model.getUtensilWeight() == null) {
            return "器具重量不能为空";
        }
        if (StringUtils.isEmpty(model.getUtensilTypeNo())) {
            return "器具属性编号不能为空";
        }
        if (StringUtils.isEmpty(model.getUtensilUtensilName())) {
            return "器具属性名称不能为空";
        }
        if (StringUtils.isEmpty(model.getAddWeightNo())) {
            model.setCreateTime(new Date());
            model.setValueFlag("0");//有效
        } else {
            model.setUpdateTime(new Date());
        }
        return "";
    }
}
