/*
 *      Copyright (c) 2018-2028, Chill Zhuang All rights reserved.
 *
 *  Redistribution and use in source and binary forms, with or without
 *  modification, are permitted provided that the following conditions are met:
 *
 *  Redistributions of source code must retain the above copyright notice,
 *  this list of conditions and the following disclaimer.
 *  Redistributions in binary form must reproduce the above copyright
 *  notice, this list of conditions and the following disclaimer in the
 *  documentation and/or other materials provided with the distribution.
 *  Neither the name of the dreamlu.net developer nor the names of its
 *  contributors may be used to endorse or promote products derived from
 *  this software without specific prior written permission.
 *  Author: Chill 庄骞 (smallchill@163.com)
 */
package org.springblade.ship.cable.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springblade.common.cache.DictCache;
import org.springblade.common.cache.SysCache;
import org.springblade.common.cache.UserCache;
import org.springblade.common.enums.DictEnum;
import org.springblade.core.tool.utils.BeanUtil;
import org.springblade.core.tool.utils.StringUtil;
import org.springblade.modules.system.entity.Region;
import org.springblade.modules.system.entity.User;
import org.springblade.modules.system.excel.CableExcel;
import org.springblade.modules.system.excel.UserExcel;
import org.springblade.ship.cable.entity.Cable;
import org.springblade.ship.cable.vo.CableVO;
import org.springblade.ship.cable.mapper.CableMapper;
import org.springblade.ship.cable.service.ICableService;
import org.springblade.core.mp.base.BaseServiceImpl;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.metadata.IPage;

import java.util.ArrayList;
import java.util.List;

/**
 *  服务实现类
 *
 * @author BladeX
 * @since 2021-11-15
 */
@Service
public class CableServiceImpl extends BaseServiceImpl<CableMapper, Cable> implements ICableService {

	@Override
	public IPage<CableVO> selectCablePage(IPage<CableVO> page, CableVO cable) {
		return page.setRecords(baseMapper.selectCablePage(page, cable));
	}

	@Override
	public void importCable(List<CableExcel> data, Boolean isCovered) {
		List<Cable> listAdd = new ArrayList<>();
		List<Cable> listUpdate = new ArrayList<>();
		data.forEach(cableExcel -> {
			Cable cable = BeanUtil.copy(cableExcel, Cable.class);

			String cable_category = DictCache.getKey("cable_category", cableExcel.getCategory());
			String cable_model = DictCache.getKey("cable_model", cableExcel.getModel());
			String ship_cabin = DictCache.getKey("ship_cabin", cableExcel.getBeginCabin());
			String ship_side = DictCache.getKey("ship_side", cableExcel.getBeginShipSide());
			String ship_cabin1 = DictCache.getKey("ship_cabin", cableExcel.getEndCabin());
			String ship_side1 = DictCache.getKey("ship_side", cableExcel.getEndShipSide());

			if (cableExcel.getLabel() != null && cableExcel.getLabel().length() < 24){
				String label = cableExcel.getLabel();
				String str = "%0" + (24 - label.length()) + "d";
				String format = String.format(str, 0);
				cable.setLabel(format + label);
			}
			if (cable_category != null){
				cable.setCategory(cable_category);
			}
			if (cable_model != null){
				cable.setModel(cable_model);
			}
			if (ship_cabin != null){
				cable.setBeginCabin(ship_cabin);
			}
			if (ship_side != null){
				cable.setBeginShipSide(ship_side);
			}
			if (ship_cabin1 != null){
				cable.setEndCabin(ship_cabin1);
			}
			if (ship_side1 != null){
				cable.setEndShipSide(ship_side1);
			}

			// 覆盖数据
			if (isCovered) {
				// 查询数据是否存在
				Cable old = this.getByAlias(cableExcel.getAlias());
				if (old != null && old.getId() != null) {
					cable.setId(old.getId());
				}
			}
			if (cable.getId() == null){
				listAdd.add(cable);
			}else {
				listUpdate.add(cable);
			}
		});
		this.saveBatch(listAdd);
		this.saveOrUpdateBatch(listUpdate);
	}

	@Override
	public Cable getByAlias(String alias) {
		return baseMapper.selectOne(new QueryWrapper<>(new Cable()).lambda().eq(Cable::getAlias, alias));
	}

	@Override
	public List<CableExcel> exportCable(QueryWrapper<Cable> queryWrapper) {
		List<CableExcel> list = new ArrayList<>();
		List<Cable> cableList = baseMapper.selectList(queryWrapper);
		cableList.forEach(cable -> {
			CableExcel cableExcel = new CableExcel();
			BeanUtil.copy(cable, cableExcel);

			String cable_category = DictCache.getValue("cable_category", cableExcel.getCategory());
			String cable_model = DictCache.getValue("cable_model", cableExcel.getModel());
			String ship_cabin = DictCache.getValue("ship_cabin", cableExcel.getBeginCabin());
			String ship_side = DictCache.getValue("ship_side", cableExcel.getBeginShipSide());
			String ship_cabin1 = DictCache.getValue("ship_cabin", cableExcel.getEndCabin());
			String ship_side1 = DictCache.getValue("ship_side", cableExcel.getEndShipSide());

			if (cable_category != null){
				cableExcel.setCategory(cable_category);
			}
			if (cable_model != null){
				cableExcel.setModel(cable_model);
			}
			if (ship_cabin != null){
				cableExcel.setBeginCabin(ship_cabin);
			}
			if (ship_side != null){
				cableExcel.setBeginShipSide(ship_side);
			}
			if (ship_cabin1 != null){
				cableExcel.setEndCabin(ship_cabin1);
			}
			if (ship_side1 != null){
				cableExcel.setEndShipSide(ship_side1);
			}

			list.add(cableExcel);
		});
		return list;
	}
}
