package com.steerinfo.appservice.appmeterbasecussupcar.service.impl;

import com.steerinfo.appservice.appmeterbasecussupcar.mapper.AppMeterBaseCusSupCarMapper;
import com.steerinfo.appservice.appmeterbasecussupcar.model.AppMeterBaseCusSupCar;
import com.steerinfo.appservice.appmeterbasecussupcar.service.IAppMeterBaseCusSupCarService;
import com.steerinfo.baseinfo.meterbasecarblacelist.mapper.MeterBaseCarBlacelistMapper;
import com.steerinfo.baseinfo.meterbasecarblacelist.model.MeterBaseCarBlacelist;
import com.steerinfo.baseinfo.meterbasecarriercustomer.mapper.MeterBaseCarrierCustomerMapper;
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.context.annotation.Primary;
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;

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

    @Resource
    private AppMeterBaseCusSupCarMapper appMeterBaseCusSupCarMapper;

    @Resource
    private MeterBaseCarBlacelistMapper meterBaseCarBlacelistMapper;

    @Autowired
    private MeterBaseCarrierCustomerMapper meterBaseCarrierCustomerMapper;


    @Override
    protected IBaseMapper<AppMeterBaseCusSupCar, String> getMapper() {
        return appMeterBaseCusSupCarMapper;
    }

    @Override
    public String checkandadd(AppMeterBaseCusSupCar model) {
        String msg = check(model);
        if (StringUtils.isNotEmpty(msg)) {
            return msg;
        }
        msg = checkCarNo(model.getCusSupCarNo());
        if (StringUtils.isNotEmpty(msg)) {
            return msg;
        }
        //2021年2月28日 测试GIT代码提交1234
        HashMap<String, Object> obj = new HashMap<>();
        obj.put("cusSupCarNo", model.getCusSupCarNo());
        obj.put("customerSupplierNo", model.getCustomerSupplierNo()); //2021年2月24日，杨秀东新增【客商编号】用于车辆信息重复判断
        List<AppMeterBaseCusSupCar> com = appMeterBaseCusSupCarMapper.selectByParameters(obj);
        if (!com.isEmpty()) {
            return "该车号已存在！";
        }
        String no = appMeterBaseCusSupCarMapper.GetNewID();
        model.setId(new SimpleDateFormat("yyyyMMdd").format(new Date())+no);
        appMeterBaseCusSupCarMapper.insert(model);
        return "";
    }

    @Override
    public String checkandupdate(String id, AppMeterBaseCusSupCar model) {
        String msg = check(model);
        if (StringUtils.isNotEmpty(msg)) {
            return msg;
        }
        msg = checkCarNo(model.getCusSupCarNo());
        if (StringUtils.isNotEmpty(msg)) {
            return msg;
        }
        //2021年6月23日 注释
        /*HashMap<String, Object> obj = new HashMap<>();
        obj.put("cusSupCarNo", model.getCusSupCarNo());
        obj.put("pkNo", id);
        List<AppMeterBaseCusSupCar> com =appMeterBaseCusSupCarMapper.selectForUpdate(obj);
        if(!com.isEmpty()){
            return "该车号已存在！";
        }*/
        appMeterBaseCusSupCarMapper.updateByPrimaryKeySelective(model);
        return "";
    }

    private String check(AppMeterBaseCusSupCar model) {
        if (model == null) {
            return "参数不能为空！！";
        }
        if (StringUtils.isEmpty(model.getCusSupCarNo())) {
            return "车号不能为空！！";
        }
        if (StringUtils.isEmpty(model.getDriverName())) {
            return "司机姓名不能为空！！";
        }
        if (StringUtils.isEmpty(model.getIdNum())) {
            return "身份证号不能为空！！";
        }
        if (StringUtils.isEmpty(model.getCustomerSupplierNo())) {
            return "客商编号不能为空！！";
        }
        if (StringUtils.isEmpty(model.getCustomerSupplierName())) {
            return "客商名称不能为空！！";
        }
        if (StringUtils.isEmpty(model.getPkNo())) {
            model.setCreateTime(new Date());
            model.setValidFlag("0");//服役
        } else {
            model.setUpdateTime(new Date());
        }
        return "";
    }

    //判断车号是否为黑名单
    private String checkCarNo(String carNo) {
        HashMap<String, Object> obj = new HashMap<>();
        obj.put("carNo", carNo);
        obj.put("valueFlag", "0");
        List<MeterBaseCarBlacelist> list = meterBaseCarBlacelistMapper.selectByParameters(obj);
        if (list.size() > 1) {
            return "该车在黑名单中，不允许新增或修改!!";
        }
        return "";
    }

    @Override
    public List<AppMeterBaseCusSupCar> selectCar(HashMap parmas) {
        Object customerSupplierNoObj = parmas.get("customerSupplierNo");
        if (customerSupplierNoObj == null) {
            return null;
        }
        String no = customerSupplierNoObj.toString();
        List<String> ids = meterBaseCarrierCustomerMapper.selectHD(no);
        ids.add(no);
        List<AppMeterBaseCusSupCar> list = appMeterBaseCusSupCarMapper.selectList(ids);
        return list;
    }

    @Override
    public List<AppMeterBaseCusSupCar> selectAllCar(HashMap parmas) {
        Object customerSupplierNoObj = parmas.get("customerSupplierNo");
        if (customerSupplierNoObj == null) {
            return null;
        }
        String no = customerSupplierNoObj.toString();
        List<String> ids = meterBaseCarrierCustomerMapper.selectHD(no);
        ids.add(no);
        List<AppMeterBaseCusSupCar> list = appMeterBaseCusSupCarMapper.selectAllCar(ids);
        return list;
    }

}
