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

import com.steerinfo.framework.mapper.IBaseMapper;
import com.steerinfo.framework.service.impl.BaseServiceImpl;
import com.steerinfo.baseinfo.meterbasescalepoint.model.MeterBaseScalePoint;
import com.steerinfo.baseinfo.meterbasescalepoint.mapper.MeterBaseScalePointMapper;
import com.steerinfo.baseinfo.meterbasescalepoint.service.IMeterBaseScalePointService;
import com.steerinfo.framework.service.pagehelper.PageHelper;
import com.steerinfo.framework.service.pagehelper.PageList;
import com.steerinfo.framework.utils.base.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.interceptor.TransactionAspectSupport;
import org.springframework.web.bind.annotation.RequestBody;

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

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

    @Resource
    private MeterBaseScalePointMapper meterBaseScalePointMapper;

    @Override
    protected IBaseMapper<MeterBaseScalePoint, String> getMapper() {
        return meterBaseScalePointMapper;
    }

    @Override
    public String checkandadd(MeterBaseScalePoint model)
    {
            String msg=check(model);
            if (StringUtils.isNotEmpty(msg)){
                return msg;
            }
            HashMap<String, Object> obj = new HashMap<>();
            obj.put("scalePointName", model.getScalePointName());
            List<MeterBaseScalePoint> com1=meterBaseScalePointMapper.selectByParameters(obj);
            if(com1.size()==0) {
                String no=meterBaseScalePointMapper.getNewID(model.getBaseSpotNo());
                model.setScalePointNo(model.getBaseSpotNo()+no);
                model.setCreateTime(new Date());
                model.setValidFlag("1");
                int num=meterBaseScalePointMapper.insert(model);
                if(num!=0){return "";}
            }else{
                return "该秤点已存在";
            }
            return "";
    }

    @Override
    public String Recovery(List<MeterBaseScalePoint> models){
        if (models == null || models.size() < 1) {
            return "请传入参数!";
        }
        for (MeterBaseScalePoint model:models) {
            model.setScalePointNo(model.getScalePointNo());
            model.setValidFlag("1");
            model.setUpdateManName(model.getUpdateManName());
            model.setUpdateManNo(model.getUpdateManNo());
            model.setUpdateTime(new Date());
            meterBaseScalePointMapper.updateByPrimaryKey(model);}
        return "";
    }

    @Override
    public String abolish(List<MeterBaseScalePoint> models){
        if (models == null || models.size() < 1) {
            return "请传入参数!";
        }
        for (MeterBaseScalePoint model:models) {
            model.setScalePointNo(model.getScalePointNo());
            model.setValidFlag("0");
            model.setUpdateManName(model.getUpdateManName());
            model.setUpdateManNo(model.getUpdateManNo());
            model.setUpdateTime(new Date());
            meterBaseScalePointMapper.updateByPrimaryKey(model);}
        return "";
    }

    @Override
    public String checkandupdate(String id,MeterBaseScalePoint model)
    {
        String msg=check(model);
        if (StringUtils.isNotEmpty(msg)){
            return msg;
        }
        HashMap<String, Object> obj = new HashMap<>();
        obj.put("scalePointName", model.getScalePointName());
        obj.put("scalePointNo", model.getScalePointNo());
        List<MeterBaseScalePoint> com1=meterBaseScalePointMapper.selectForUpdate(obj);
        if(  com1.isEmpty())
        {
            model.setUpdateTime(new Date());
            int num=meterBaseScalePointMapper.updateByPrimaryKey(model);
            if(num!=0){return "";}
        }else{
            return "秤点名称已存在";
        }
        return "";
    }

    @Override
    public List<MeterBaseScalePoint> queryAll(Map<String,Object> parmas)
    {
        return meterBaseScalePointMapper.selectByParameters(parmas);
    }

    @Override
    public PageList<MeterBaseScalePoint> notIncludedLonXi(Map<String, Object> params, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<MeterBaseScalePoint> rows = meterBaseScalePointMapper.notIncludedLonXi(params);
        PageList pageInfo = new PageList(rows);
        return pageInfo;
    }

    /**
     * 客户端：数据更新
     * @param model
     * @return
     */
    @Override
    public String doUpdateWf(MeterBaseScalePoint model) {
        if (StringUtils.isEmpty(model.getScalePointNo())) {
            return "计量点主键不能为空";
        }
        model.setUpdateTime(new Date());
        meterBaseScalePointMapper.updateByPrimaryKeySelective(model);
        return "";
    }

    private String check(MeterBaseScalePoint model)
    {
        if (StringUtils.isEmpty(model.getScalePointName())) {
            return "秤点名称不能为空";
        }
        if (StringUtils.isEmpty(model.getBaseSpotNo())) {
            return "计量点编号不能为空";
        }
        if (StringUtils.isEmpty(model.getBaseSpotName())) {
            return "计量点名称不能为空";
        }
        if(StringUtils.isEmpty(model.getScalePointNo())){
            model.setCreateTime(new Date());
            model.setValidFlag("1");
        }else{
            model.setUpdateTime(new Date());
        }
        return "";
    }
}
