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

import com.steerinfo.baseinfo.meterbaseunitinfo.mapper.MeterBaseUnitInfoMapper;
import com.steerinfo.baseinfo.meterbaseunitinfo.model.MeterBaseUnitInfo;
import com.steerinfo.baseinfo.meterbaseunitinfo.service.IMeterBaseUnitInfoService;
import com.steerinfo.framework.controller.RESTfulResult;
import com.steerinfo.framework.mapper.IBaseMapper;
import com.steerinfo.framework.service.impl.BaseServiceImpl;
import com.steerinfo.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

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

    @Resource
    private MeterBaseUnitInfoMapper meterBaseUnitInfoMapper;

    @Override
    protected IBaseMapper<MeterBaseUnitInfo, String> getMapper() {
        return meterBaseUnitInfoMapper;
    }

    @Override
    public List<MeterBaseUnitInfo> queryBynoPage(Map<String, Object> parmas) {
        List<MeterBaseUnitInfo> list = meterBaseUnitInfoMapper.queryBynoPage(parmas);
        return list;
    }

    @Override
    public RESTfulResult addModel(MeterBaseUnitInfo model) {
        RESTfulResult rm = new RESTfulResult();
        rm.setFailed();

        try{
            if(model.getForwardingUnitName() == null || model.getReceivingUnitName() == null) {
                rm.setMessage("收发货单位头不能为空，请确认！！");
                return rm;
            }
            HashMap<String, Object> map =  new HashMap<>();
            map.put("forwardingUnitNo", model.getForwardingUnitNo());
            map.put("receivingUnitNo", model.getReceivingUnitNo());
            map.put("matterNo", model.getMatterNo());
            if(model.getCarNo() != null && StringUtils.isNotEmpty(model.getCarNo())) {
                String carNo = model.getCarNo();
                if(StringUtils.isNotEmpty(carNo)) {
                    map.put("carNo", model.getCarNo());
                }
            }
            List<MeterBaseUnitInfo> models = meterBaseUnitInfoMapper.selectByParameters(map);
            if(models.size() == 1) {
                rm.setMessage("配置的收货单位、发货单位、物料、车号存在相同的数据信息，请确认！！！");
                return rm;
            }
            SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
            String no = meterBaseUnitInfoMapper.getNewID(sdf.format(new Date()));
            model.setUnitNo(sdf.format(new Date()) + no);
            model.setCreateTime(new Date());
            int i = meterBaseUnitInfoMapper.insertSelective(model);
            if(i >= 1){
                rm.setSucceed();
                rm.setCode("200");
                rm.setMessage("操作成功！！");
                rm.setData(model);
                return rm;
            }
            rm.setMessage("未配置成功，请确认！！");
            return rm;
        }catch(Exception e) {
            e.printStackTrace();
            rm.setMessage("操作异常！！" + e.getMessage());
            return rm;
        }
    }

    @Override
    public List<MeterBaseUnitInfo> getUnitInfo(MeterBaseUnitInfo model) {
        boolean isFlag = false;
        List<MeterBaseUnitInfo> list = meterBaseUnitInfoMapper.getUnitInfo(model);
        for (MeterBaseUnitInfo models:list) {
            if(models.getAllowedTareWeight().equals("0")){
                isFlag = true;
            }
        }
        if(isFlag){
            list = null;
        }
        return list;
    }
}
