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

import com.steerinfo.baseinfo.dto.SelectMatterDemo;
import com.steerinfo.baseinfo.meterbasetare.mapper.MeterBaseTareMapper;
import com.steerinfo.baseinfo.meterbasetare.model.MeterBaseTare;
import com.steerinfo.baseinfo.meterbasetare.service.IMeterBaseTareService;
import com.steerinfo.framework.mapper.IBaseMapper;
import com.steerinfo.framework.service.impl.BaseServiceImpl;
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 java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

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

    @Autowired
    private MeterBaseTareMapper meterBaseTareMapper;
    @Override
    protected IBaseMapper<MeterBaseTare, String> getMapper() {
        return meterBaseTareMapper;
    }

    @Override
    public String checkandadd(MeterBaseTare model)
    {
        String msg=check(model);
        if (StringUtils.isNotEmpty(msg)){
            return msg;
        }
        HashMap<String, Object> obj = new HashMap<>();
        obj.put("matterNo", model.getMatterNo());
        obj.put("matterName", model.getMatterName());
        obj.put("meterTypeNo", model.getMeterTypeNo());
        obj.put("meterTypeName", model.getMeterTypeName());
        List<MeterBaseTare> list=meterBaseTareMapper.selectByParameters(obj);
        if( !list.isEmpty())
        {
            return model.getMeterTypeName()+"的"+model.getMatterName()+"的回皮时间已设置，请先删除或者修改！";
        }
        String no=meterBaseTareMapper.GetNewID();
        model.setTareNo(new SimpleDateFormat("yyyyMMdd").format(new Date()) +no);
        meterBaseTareMapper.insertSelective(model);
        return "";
    }

    @Override
    public String checkandupdate(String id,MeterBaseTare model)
    {
        String msg=check(model);
        if (StringUtils.isNotEmpty(msg)){
            return msg;
        }
        HashMap<String, Object> obj = new HashMap<>();
            obj.put("matterNo", model.getMatterNo());
            obj.put("matterName", model.getMatterName());
            obj.put("meterTypeNo", model.getMeterTypeNo());
            obj.put("meterTypeName", model.getMeterTypeName());
            obj.put("tareNo", model.getTareNo());
        List<MeterBaseTare> list=meterBaseTareMapper.selectForUpdate(obj);
        if( !list.isEmpty() )
        {
            return model.getMeterTypeName()+"的"+model.getMeterTypeName()+"的回皮时间已设置，请先删除或者修改！";
        }
        model.setUpdateTime(new Date());
        meterBaseTareMapper.updateByPrimaryKey(model);
        return "";
    }

    @Override
    public PageList<MeterBaseTare> listLike(SelectMatterDemo model){
        PageHelper.startPage(model.getPageNum(), model.getPageSize());
        HashMap<String, Object> obj = new HashMap<>();
        obj.put("matterName", model.getMatterName());
        List<MeterBaseTare> list = meterBaseTareMapper.selectLikeByParameters(obj);
        PageList pageInfo = new PageList(list);
        return (pageInfo);
    }

    private String check(MeterBaseTare model)
    {
        if( model == null){
            return "参数为空！！";
        }
        if (StringUtils.isEmpty(model.getMatterNo())){
            return "物料编号不能为空！！";
        }
        if (StringUtils.isEmpty(model.getMatterName())){
            return "物料名称不能为空！！";
        }
        if (StringUtils.isEmpty(model.getMeterTypeNo())){
            return "计量类型编号不能为空！！";
        }
        if (StringUtils.isEmpty(model.getMeterTypeName())){
            return "计量类型名称不能为空！！";
        }
        if (StringUtils.isEmpty(model.getTareTypeNo())){
            return "回皮时间类型编号不能为空！！";
        }
        if (StringUtils.isEmpty(model.getTareTypeName())){
            return "回皮时间类型名称不能为空！！";
        }
        if(StringUtils.isEmpty(model.getTareNo())){
            model.setCreateTime(new Date());
        }else {
            model.setUpdateTime(new Date());
        }
        return "";
    }
}
