package com.steerinfo.pretrack.pretracktementrust.service.impl;

import com.steerinfo.baseinfo.meterbasebillprint.mapper.MeterBaseBillPrintMapper;
import com.steerinfo.baseinfo.meterbasebillprint.model.MeterBaseBillPrint;
import com.steerinfo.baseinfo.meterbasecustomersupplier.mapper.MeterBaseCustomerSupplierMapper;
import com.steerinfo.baseinfo.meterbasecustomersupplier.model.MeterBaseCustomerSupplier;
import com.steerinfo.baseinfo.meterbasematterinfo.mapper.MeterBaseMatterInfoMapper;
import com.steerinfo.baseinfo.meterbasematterinfo.model.MeterBaseMatterInfo;
import com.steerinfo.framework.mapper.IBaseMapper;
import com.steerinfo.framework.service.impl.BaseServiceImpl;
import com.steerinfo.meterwork.except.MarkerMetException;
import com.steerinfo.pretrack.pretracktementrust.model.PreTrackTemEntrust;
import com.steerinfo.pretrack.pretracktementrust.mapper.PreTrackTemEntrustMapper;
import com.steerinfo.pretrack.pretracktementrust.service.IPreTrackTemEntrustService;
import com.steerinfo.util.FieldsCollector;
import com.steerinfo.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

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

    @Autowired
    private PreTrackTemEntrustMapper preTrackTemEntrustMapper;

    //物料对应的打印张数
    @Autowired
    private MeterBaseBillPrintMapper meterBaseBillPrintMapper;

    //物料表
    @Autowired
    private MeterBaseMatterInfoMapper meterBaseMatterInfoMapper;

    //客商表
    @Autowired
    private MeterBaseCustomerSupplierMapper meterBaseCustomerSupplierMapper;

    @Override
    protected IBaseMapper<PreTrackTemEntrust, String> getMapper() {
        return preTrackTemEntrustMapper;
    }

    //插入数据
    @Override
    public PreTrackTemEntrust insert(PreTrackTemEntrust model) {
        try {

            if (model == null || StringUtils.isEmpty(model.getCreateManNo()) || StringUtils.isEmpty(model.getCreateManName())) {
                throw new MarkerMetException(500, "参数及创建人编码不允许为空!!");
            }
            if (StringUtils.isEmpty(model.getMatterName()) || StringUtils.isEmpty(model.getForwardingUnitName())
                    || StringUtils.isEmpty(model.getReceivingUintName())) {
                throw new MarkerMetException(500, "物资名称，收货单位，发货单位均不允许为空!!");
            }

            PreTrackTemEntrust temEntrustSearch = new PreTrackTemEntrust();
            temEntrustSearch.setReceivingUintName(model.getReceivingUintName());
            temEntrustSearch.setForwardingUnitName(model.getForwardingUnitName());
            temEntrustSearch.setMatterName(model.getMatterName());
            temEntrustSearch.setValueFlag("1");
            Map<String, Object> map = FieldsCollector.beanToMap(temEntrustSearch);
            List<PreTrackTemEntrust> temEntrustList = preTrackTemEntrustMapper.selectByParameters(map);
            if (temEntrustList != null && temEntrustList.size() > 0) {
                throw new MarkerMetException(500, model.getMatterName() + "，" + model.getReceivingUintName() + "，" + model.getForwardingUnitName() + "已经存在对应的临时委托，请查询数据!!");
            }

            String id = preTrackTemEntrustMapper.getMaxId();
            model.setEntrustNo(String.format("%09d", Integer.valueOf(id)));
            model.setCreateTime(new Date());

            //物料编码处理
            MeterBaseMatterInfo matterInfo = new MeterBaseMatterInfo();
            matterInfo.setMatterName(model.getMatterName());
            Map<String, Object> mapMatterInfo = FieldsCollector.beanToMap(matterInfo);
            List<MeterBaseMatterInfo> matterInfoList = meterBaseMatterInfoMapper.selectByParameters(mapMatterInfo);
            if (matterInfoList != null && matterInfoList.size() > 0) {
                model.setMatterNo(matterInfoList.get(0).getMatterNo());
                model.setMatterName(matterInfoList.get(0).getMatterName());
            } else {
                String matterNo = meterBaseMatterInfoMapper.GetNewID();
                matterInfo.setIndexCode("");
                matterInfo.setMatterNo(matterNo);
                matterInfo.setCreateTime(new Date());
                matterInfo.setValidFlag("0");//默认无效，不让在小程序选择
                matterInfo.setCreateTime(new Date());
                matterInfo.setCreateManNo("system");
                matterInfo.setCreateManName("临时委托新增");
                meterBaseMatterInfoMapper.insert(matterInfo);
                model.setMatterNo(matterNo);
                model.setMatterName(model.getMatterName());
            }

            //发货单位
            MeterBaseCustomerSupplier baseForwarde = new MeterBaseCustomerSupplier();
            baseForwarde.setCustomerSupplierName(model.getForwardingUnitName());
            Map<String, Object> mapCustomer = FieldsCollector.beanToMap(baseForwarde);
            List<MeterBaseCustomerSupplier> baseCustomerSupplierList = meterBaseCustomerSupplierMapper.selectByParameters(mapCustomer);
            if (baseCustomerSupplierList != null && baseCustomerSupplierList.size() > 0) {
                model.setForwardingUnitNo(baseCustomerSupplierList.get(0).getCustomerSupplierNo());
                model.setForwardingUnitName(baseCustomerSupplierList.get(0).getCustomerSupplierName());
            } else {
                String customerNo = meterBaseCustomerSupplierMapper.GetNewID();
                baseForwarde.setCustomerSupplierNo(customerNo);
                baseForwarde.setCustomerSupplierPassword("123456");
                baseForwarde.setCustomerSupplierLogin(customerNo);
                baseForwarde.setCustomerSupplierType("2"); //供应商/货带
                baseForwarde.setValidFlag("0"); //0=无效，1=有效；
                baseForwarde.setCheckFlag("0");//0=未审核，1=已审核；
                baseForwarde.setCreateTime(new Date());
                baseForwarde.setCreateManNo("system");
                baseForwarde.setCreateManName("临时委托新增");
                meterBaseCustomerSupplierMapper.insertSelective(baseForwarde);
                model.setForwardingUnitNo(customerNo);
                model.setForwardingUnitName(model.getForwardingUnitName());
            }

            //收货单位
            MeterBaseCustomerSupplier baseReceive = new MeterBaseCustomerSupplier();
            baseReceive.setCustomerSupplierName(model.getReceivingUintName());
            Map<String, Object> mapReceive = FieldsCollector.beanToMap(baseReceive);
            List<MeterBaseCustomerSupplier> baseReceiveList = meterBaseCustomerSupplierMapper.selectByParameters(mapReceive);
            if (baseReceiveList != null && baseReceiveList.size() > 0) {
                model.setReceivingUintNo(baseReceiveList.get(0).getCustomerSupplierNo());
                model.setReceivingUintName(baseReceiveList.get(0).getCustomerSupplierName());
            } else {
                String ReceiverNo = meterBaseCustomerSupplierMapper.GetNewID();
                baseReceive.setCustomerSupplierNo(ReceiverNo);
                baseReceive.setCustomerSupplierPassword("123456");
                baseReceive.setCustomerSupplierLogin(ReceiverNo);
                baseReceive.setCustomerSupplierType("2"); //供应商/货带
                baseReceive.setValidFlag("0"); //0=无效，1=有效；
                baseReceive.setCheckFlag("0");//0=未审核，1=已审核；
                baseReceive.setCreateTime(new Date());
                baseReceive.setCreateManNo("system");
                baseReceive.setCreateManName("临时委托新增");
                meterBaseCustomerSupplierMapper.insertSelective(baseReceive);
                model.setReceivingUintNo(ReceiverNo);
                model.setReceivingUintName(model.getReceivingUintName());
            }
            preTrackTemEntrustMapper.insertSelective(model);


            //默认打印一张小票
            MeterBaseBillPrint meterBaseBillPrint = new MeterBaseBillPrint();
            meterBaseBillPrint.setMatterNo(model.getMatterNo());
            Map<String, Object> mapPrint = FieldsCollector.beanToMap(meterBaseBillPrint);
            List<MeterBaseBillPrint> printList = meterBaseBillPrintMapper.selectByParameters(mapPrint);
            if (printList != null && printList.size() > 0) {
                return model;
            }
            meterBaseBillPrint.setMatterName(model.getMatterName());
            String no = meterBaseBillPrintMapper.GetNewID();
            meterBaseBillPrint.setBillPrintNo(new SimpleDateFormat("yyyyMMdd").format(new Date()) + no);
            meterBaseBillPrint.setWeightTypeNo("001008003");
            meterBaseBillPrint.setWeightTypeName("净重");
            meterBaseBillPrint.setPrintNum((short) 2);
            meterBaseBillPrint.setCreateManNo("system");
            meterBaseBillPrint.setCreateManName("临时委托自动创建");
            meterBaseBillPrint.setCreateTime(new Date());
            meterBaseBillPrintMapper.insertSelective(meterBaseBillPrint);
            return model;

        } catch (MarkerMetException e) {
            throw e;
        } catch (Exception ex) {
            ex.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
        }
        return null;
    }
}
