/**
 * BladeX Commercial License Agreement
 * Copyright (c) 2018-2099, https://bladex.cn. All rights reserved.
 * <p>
 * Use of this software is governed by the Commercial License Agreement
 * obtained after purchasing a license from BladeX.
 * <p>
 * 1. This software is for development use only under a valid license
 * from BladeX.
 * <p>
 * 2. Redistribution of this software's source code to any third party
 * without a commercial license is strictly prohibited.
 * <p>
 * 3. Licensees may copyright their own code but cannot use segments
 * from this software for such purposes. Copyright of this software
 * remains with BladeX.
 * <p>
 * Using this software signifies agreement to this License, and the software
 * must not be used for illegal purposes.
 * <p>
 * THIS SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY. The author is
 * not liable for any claims arising from secondary or illegal development.
 * <p>
 * Author: Chill Zhuang (bladejava@qq.com)
 */
package org.springblade.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.springblade.core.log.exception.ServiceException;
import org.springblade.core.mp.base.BaseServiceImpl;
import org.springblade.core.tool.utils.Func;
import org.springblade.system.mapper.CustomerTypeMapper;
import org.springblade.system.pojo.entity.CustomerType;
import org.springblade.system.pojo.vo.CustomerTypeVO;
import org.springblade.system.service.ICustomerTypeService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Objects;

/**
 * 客商类型 服务实现类
 *
 * @author Chill
 */
@Service
public class CustomerTypeServiceImpl extends BaseServiceImpl<CustomerTypeMapper, CustomerType> implements ICustomerTypeService {

	private static final int STATUS_ENABLED = 1;
	private static final int STATUS_DISABLED = 2;
	private static final int NAME_MAX_LENGTH = 50;
	private static final int ENGLISH_NAME_MAX_LENGTH = 100;

	@Override
	public IPage<CustomerTypeVO> selectCustomerTypePage(IPage<CustomerTypeVO> page, CustomerTypeVO customerType) {
		return page.setRecords(baseMapper.selectCustomerTypePage(page, customerType));
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public boolean submit(CustomerType customerType) {
		prepare(customerType);
		validate(customerType);
		return saveOrUpdate(customerType);
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public boolean changeStatus(Long id, Integer status) {
		if (Func.isEmpty(id)) {
			throw new ServiceException("主键不能为空");
		}
		CustomerType customerType = getById(id);
		if (Func.isEmpty(customerType)) {
			throw new ServiceException("客商类型不存在");
		}
		if (!Objects.equals(status, STATUS_ENABLED) && !Objects.equals(status, STATUS_DISABLED)) {
			throw new ServiceException("启停状态不正确");
		}
		CustomerType update = new CustomerType();
		update.setId(id);
		update.setStatus(status);
		return updateById(update);
	}

	private void prepare(CustomerType customerType) {
		customerType.setName(trimToEmpty(customerType.getName()));
		customerType.setEnglishName(trimToNull(customerType.getEnglishName()));
		if (Func.isEmpty(customerType.getStatus())) {
			customerType.setStatus(STATUS_ENABLED);
		}
	}

	private void validate(CustomerType customerType) {
		if (Func.isEmpty(customerType.getName())) {
			throw new ServiceException("中文名称不能为空");
		}
		if (customerType.getName().length() > NAME_MAX_LENGTH) {
			throw new ServiceException("中文名称不能超过50字");
		}
		if (Func.isNotEmpty(customerType.getEnglishName()) && customerType.getEnglishName().length() > ENGLISH_NAME_MAX_LENGTH) {
			throw new ServiceException("英文名称不能超过100字");
		}
		validateUniqueName(customerType);
	}

	private void validateUniqueName(CustomerType customerType) {
		LambdaQueryWrapper<CustomerType> queryWrapper = Wrappers.<CustomerType>lambdaQuery()
			.eq(CustomerType::getName, customerType.getName())
			.eq(CustomerType::getIsDeleted, 0);
		if (Func.isNotEmpty(customerType.getId())) {
			queryWrapper.ne(CustomerType::getId, customerType.getId());
		}
		if (count(queryWrapper) > 0L) {
			throw new ServiceException("该中文名称已存在");
		}
	}

	private String trimToEmpty(String value) {
		return value == null ? "" : value.trim();
	}

	private String trimToNull(String value) {
		String trimValue = trimToEmpty(value);
		return trimValue.isEmpty() ? null : trimValue;
	}

}
