package com.steerinfo.meterwork.meterworkcarrecover.service.impl;

import com.steerinfo.framework.mapper.IBaseMapper;
import com.steerinfo.framework.service.impl.BaseServiceImpl;
import com.steerinfo.framework.user.UserPayload;
import com.steerinfo.meterwork.meterworkcaractualfirst.model.MeterWorkCarActualFirst;
import com.steerinfo.meterwork.meterworkcarrecover.model.MeterWorkCarRecover;
import com.steerinfo.meterwork.meterworkcarrecover.mapper.MeterWorkCarRecoverMapper;
import com.steerinfo.meterwork.meterworkcarrecover.service.IMeterWorkCarRecoverService;
import com.steerinfo.util.FieldsCollector;
import com.steerinfo.util.PublicMethod;
import com.steerinfo.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

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

    @Autowired
    private MeterWorkCarRecoverMapper meterWorkCarRecoverMapper;

    @Override
    protected IBaseMapper<MeterWorkCarRecover, String> getMapper() {
        return meterWorkCarRecoverMapper;
    }

    /**
     * 新增
     *
     * @param first
     * @return
     */
    @Override
    public String insert(MeterWorkCarActualFirst first) {
        if (first == null || StringUtils.isEmpty(first.getCreateManNo()) || StringUtils.isEmpty(first.getCreateManName())) {
            return "复磅计量点名称及复磅计量点编号不允许为空！";
        }

        MeterWorkCarRecover judgeRepeat = new MeterWorkCarRecover();
        judgeRepeat.setCarNo(first.getCarNo());
        judgeRepeat.setValueFlag("0");
        Map<String, Object> map = FieldsCollector.beanToMap(judgeRepeat);
        List<MeterWorkCarRecover> recoverList = meterWorkCarRecoverMapper.selectByParameters(map);
        if (recoverList != null && recoverList.size() > 0) {
            return "车号【" + first.getCarNo() + "】已经存在一条计量点为【" + recoverList.get(0).getRecBaseSpotName() + "】未复磅的记录，请废除后，再重新添加复磅记录！";
        }

        String no = PublicMethod.IdCreate(2);
        MeterWorkCarRecover carRecover = new MeterWorkCarRecover();
        carRecover.setRecoverNo(no);
        carRecover.setCarNo(first.getCarNo()); // first.getFORWARDING_UNIT_NO
        carRecover.setValueFlag("0");
        carRecover.setTrailerNo(first.getTrailerNo());
        carRecover.setMatterNo(first.getMatterNo());
        carRecover.setMatterName(first.getMatterName());
        carRecover.setCustomerSupplierNo(first.getCustomerSupplierNo());
        carRecover.setCustomerSupplierName(first.getCustomerSupplierName());
        carRecover.setForwardingUnitNo(first.getForwardingUnitNo());
        carRecover.setForwardingUnitName(first.getForwardingUnitName());
        carRecover.setReceivingUintNo(first.getReceivingUintNo());
        carRecover.setReceivingUintName(first.getReceivingUintName());
        carRecover.setMeterTypeNo(first.getMatterNo());
        carRecover.setMeterTypeName(first.getMatterName());
        carRecover.setMeterWeight(first.getMeterWeight());//计量点（原始数据）
        carRecover.setMeterTime(first.getCreateTime()); //计量时间（原始数据）
        carRecover.setBaseSpotNo(first.getBaseSpotNo());//计量点编号（原始数据）
        carRecover.setBaseSpotName(first.getBaseSpotName());//计量点名称（原始数据）
        carRecover.setRecBaseSpotNo(first.getCreateManNo());//计量点名称（需复磅的计量点）
        carRecover.setRecBaseSpotName(first.getCreateManName());//计量点名称（需复磅的计量点）

        //用户名，用户账户处理
//        UserPayload payload = UserPayload.getCurrUser();
//        String userCode = payload.getUserCode();
//        String userName = payload.getUserName();
//        if (carRecover.getCreateManNo() == null) {
////            carRecover.setCreateTime(new Date());
//            carRecover.setCreateManNo(userCode);
//            carRecover.setCreateManName(userName);
//        }
        meterWorkCarRecoverMapper.insertSelective(carRecover);
        return "";
    }

    /**
     * 复磅计量
     *
     * @param model
     * @return
     */
    @Override
    public String doRecover(MeterWorkCarRecover model) {
        if (model == null || StringUtils.isEmpty(model.getCarNo())) {
            return "复磅时车号不允许为空！！";
        }
        MeterWorkCarRecover searchMap = new MeterWorkCarRecover();
        searchMap.setCarNo(model.getCarNo());
        searchMap.setValueFlag("0"); //状态（0=未复磅，1=已复磅，2=作废）
        Map<String, Object> map = FieldsCollector.beanToMap(searchMap);
        List<MeterWorkCarRecover> recoverList = meterWorkCarRecoverMapper.selectByParameters(map);
        if (recoverList == null || recoverList.size() == 0) {
            return "车号【" + model.getCarNo() + "】未查询到需要复磅的数据";
        }
        if (recoverList != null && recoverList.size() > 1) {
            return "车号【" + model.getCarNo() + "】存在多条需要复磅的数据，请联系计量员处理！";
        }
        model.setValueFlag("1");
        model.setRecoverNo(recoverList.get(0).getRecoverNo()); //主键
        model.setRecMeterTime(new Date()); //复磅时间
        model.setUpdateTime(new Date());
        model.setUpdateManNo("system");
        model.setUpdateManName("客户端复磅更新");
        meterWorkCarRecoverMapper.updateByPrimaryKeySelective(model);
        return "";
    }
}
