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

import com.steerinfo.baseinfo.dto.SelectMatterDemo;
import com.steerinfo.baseinfo.meterbasebillprint.mapper.MeterBaseBillPrintMapper;
import com.steerinfo.baseinfo.meterbasebillprint.model.MeterBaseBillPrint;
import com.steerinfo.baseinfo.meterbasebillprint.service.IMeterBaseBillPrintService;
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;

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

    @Autowired
    private MeterBaseBillPrintMapper meterBaseBillPrintMapper;

    @Override
    protected IBaseMapper<MeterBaseBillPrint, String> getMapper() {
        return meterBaseBillPrintMapper;
    }

    @Override
    public String checkandadd(MeterBaseBillPrint model)
    {
        String msg=check(model);
        if (StringUtils.isNotEmpty(msg)){
            return msg;
        }
        HashMap<String, Object> obj = new HashMap<>();
            obj.put("matterNo", model.getMatterNo());
            obj.put("baseSpotNo", model.getMatterNo());
            obj.put("meterTypeNo", model.getMeterTypeNo());
            obj.put("loadPointNo", model.getWeightTypeNo());
            List<MeterBaseBillPrint> com=meterBaseBillPrintMapper.selectByParameters(obj);
        if (! com.isEmpty()) {
            return model.getMeterTypeName()+"中"+model.getMatterName()+"的物料已配置，请直接修改！！";
        }
        String no=meterBaseBillPrintMapper.GetNewID();
        model.setBillPrintNo(new SimpleDateFormat("yyyyMMdd").format(new Date()) +no);
        meterBaseBillPrintMapper.insertSelective(model);
        return "";
    }

    @Override
    public String checkandupdate(String id,MeterBaseBillPrint model)
    {
        String msg=check(model);
        if (StringUtils.isNotEmpty(msg)){
            return msg;
        }
        HashMap<String, Object> obj = new HashMap<>();
        obj.put("matterNo", model.getMatterNo());
        obj.put("baseSpotNo", model.getMatterNo());
        obj.put("meterTypeNo", model.getMeterTypeNo());
        obj.put("loadPointNo", model.getWeightTypeNo());
        obj.put("billPrintNo", model.getBillPrintNo());
        List<MeterBaseBillPrint> com=meterBaseBillPrintMapper.selectForUpdate(obj);
        if (! com.isEmpty()) {
            return model.getMeterTypeName()+"中"+model.getMatterName()+"的物料已配置，请直接修改！！";
        }
        meterBaseBillPrintMapper.updateByPrimaryKeySelective(model);
        return "";
    }

    private String check(MeterBaseBillPrint model)
    {
        if( model == null){
            return "参数为空！！";
        }
        if (StringUtils.isEmpty(model.getMatterNo())){
            return "物料编号不能为空！！";
        }
        if (StringUtils.isEmpty(model.getMatterName())){
            return "物料名称不能为空！！";
        }
        if (StringUtils.isEmpty(model.getWeightTypeNo())){
            return "重量类型编号不能为空！！";
        }
        if (StringUtils.isEmpty(model.getWeightTypeName())){
            return "重量类型名称不能为空！！";
        }
        if (model.getPrintNum() == null ||  model.getPrintNum().equals("")){
            return "打印数量不能为空！！";
        }
        if(StringUtils.isEmpty(model.getBillPrintNo())){
            model.setCreateTime(new Date());
        }else {
            model.setUpdateTime(new Date());
        }
        return "";
    }

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