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

import com.steerinfo.baseinfo.meterbasecarriercustomer.mapper.MeterBaseCarrierCustomerMapper;
import com.steerinfo.baseinfo.meterbasecarriercustomer.model.MeterBaseCarrierCustomer;
import com.steerinfo.baseinfo.meterbasecarriercustomer.service.IMeterBaseCarrierCustomerService;
import com.steerinfo.framework.mapper.IBaseMapper;
import com.steerinfo.framework.service.impl.BaseServiceImpl;
import com.steerinfo.framework.user.UserPayload;
import com.steerinfo.meterwork.except.MarkerMetException;
import com.steerinfo.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

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

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

    @Autowired
    private MeterBaseCarrierCustomerMapper meterBaseCarrierCustomerMapper;

    @Override
    protected IBaseMapper<MeterBaseCarrierCustomer, String> getMapper() {
        return meterBaseCarrierCustomerMapper;
    }

    @Override
    @Transactional
    public int relation(List<MeterBaseCarrierCustomer> models){
        if (models == null || models.size() <= 0){
            throw new MarkerMetException(500, "参数为空!!");
        }
        int num = 0;
        for(MeterBaseCarrierCustomer model : models){
            check(model);
            SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd");
            String afl = formatter.format(new Date());
            String no = meterBaseCarrierCustomerMapper.getNewID(afl);
            model.setCarrierCustomerNo(afl + no);
            meterBaseCarrierCustomerMapper.insertSelective(model);
            num ++;
        }
        return  num;
    }

    @Override
    @Transactional
    public int relieve(List<MeterBaseCarrierCustomer> models){
        List<String> list = new ArrayList<>();
        for (MeterBaseCarrierCustomer model : models){
            list.add(model.getCarrierCustomerNo());
        }
        int num = meterBaseCarrierCustomerMapper.batchDelete(list);
        return num;
    }


    private MeterBaseCarrierCustomer check (MeterBaseCarrierCustomer model){
        if (StringUtils.isEmpty(model.getCarrierUnitNo()) || StringUtils.isEmpty(model.getCarrierUnitName()) ) {
            throw new MarkerMetException(500, "承运单位为空!!");
        }
        if (StringUtils.isEmpty(model.getCustomerSupplierNo()) || StringUtils.isEmpty(model.getCustomerSupplierName()) ) {
            throw new MarkerMetException(500, "供应商为空!!");
        }
        UserPayload payload = UserPayload.getCurrUser();
        String userId = payload.getUserCode();
        String userName = payload.getUserName();
        if(StringUtils.isEmpty(model.getCarrierCustomerNo())){
            model.setCreateTime(new Date());
            model.setCreateManNo(userId);
            model.setCreateManName(userName);
        }else {
            model.setUpdateTime(new Date());
            model.setUpdateManNo(userId);
            model.setUpdateManName(userName);
        }
        return model;
    }

}
