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

import com.steerinfo.baseinfo.meterbasedriver.model.MeterBaseDriver;
import com.steerinfo.baseinfo.meterbasematterinfo.model.MeterBaseMatterInfo;
import com.steerinfo.framework.mapper.IBaseMapper;
import com.steerinfo.framework.service.impl.BaseServiceImpl;
import com.steerinfo.baseinfo.meterbasematterinfodirect.model.MeterBaseMatterInfoDirect;
import com.steerinfo.baseinfo.meterbasematterinfodirect.mapper.MeterBaseMatterInfoDirectMapper;
import com.steerinfo.baseinfo.meterbasematterinfodirect.service.IMeterBaseMatterInfoDirectService;
import com.steerinfo.framework.user.UserPayload;
import com.steerinfo.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.Date;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;

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

    @Autowired
    private MeterBaseMatterInfoDirectMapper meterBaseMatterInfoDirectMapper;

    @Override
    protected IBaseMapper<MeterBaseMatterInfoDirect, String> getMapper() {
        return meterBaseMatterInfoDirectMapper;
    }

    @Override
    public String checkandadd(MeterBaseMatterInfoDirect model) {
        if (StringUtils.isEmpty(model.getMatterName())) {
            return "物料名称不能为空";
        }
        if (StringUtils.isNotEmpty(model.getMeterCode()) && model.getMeterCode().contains("000")) {
            return "物料的计量码不允许包含【000】，此编号已被临时委托使用！";
        }
        if (StringUtils.isNotEmpty(model.getMeterCode())) {
            HashMap<String, Object> obj2 = new HashMap<>();
            obj2.put("meterCode", model.getMeterCode());
            List<MeterBaseMatterInfoDirect> com2 = meterBaseMatterInfoDirectMapper.selectByParameters(obj2);
            if (com2 != null && com2.size() > 0) {
                return "该计量码已存在！";
            }
        }
        HashMap<String, Object> obj1 = new HashMap<>();
        obj1.put("matterName", model.getMatterName());
        List<MeterBaseMatterInfoDirect> com1 = meterBaseMatterInfoDirectMapper.selectByParameters(obj1);
        if (!com1.isEmpty()) {
            return "该物料名称已存在";
        }
        String no = meterBaseMatterInfoDirectMapper.GetNewID();
        model.setMatterNo(no);
        model.setCreateTime(new Date());
        model.setValidFlag("1");//有效
        meterBaseMatterInfoDirectMapper.insert(model);
        return "";
    }

    @Override
    public String checkandupdate(String id, MeterBaseMatterInfoDirect model) {
        if (model.getMatterName() == null || model.getMatterName().equals("")) {
            return "物料名称不能为空";
        }
        if (StringUtils.isNotEmpty(model.getMeterCode()) && model.getMeterCode().contains("000")) {
            return "物料的计量码不允许包含【000】，此编号已被临时委托使用！";
        }
        if (StringUtils.isNotEmpty(model.getMeterCode())) {
            HashMap<String, Object> obj = new HashMap<>();
            obj.put("meterCode", model.getMeterCode());
            obj.put("matterNo", id);
            List<MeterBaseMatterInfo> com = meterBaseMatterInfoDirectMapper.selectForUpdate(obj);
            if (!com.isEmpty() && com.size() > 1) {
                return "该计量码已存在！";
            }
            if (!com.isEmpty() && com.size() == 1 && !com.get(0).getMatterNo().equals(id)) {
                return "该计量码已存在！";
            }
        }

        HashMap<String, Object> obj1 = new HashMap<>();
        obj1.put("matterName", model.getMatterName());
        obj1.put("matterNo", id);
        List<MeterBaseMatterInfo> com = meterBaseMatterInfoDirectMapper.selectForUpdate(obj1);
        if (!com.isEmpty() && com.size() > 1) {
            return "该物料名称已存在！";
        }
        if (!com.isEmpty() && com.size() == 1 && !com.get(0).getMatterNo().equals(id)) {
            return "该物料名称已存在！";
        }
        model.setUpdateTime(new Date());
        meterBaseMatterInfoDirectMapper.updateByPrimaryKey(model);
        return "";
    }
}
