package com.qmth.sop.business.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qmth.boot.api.exception.ApiException;
import com.qmth.sop.business.entity.SysDeviceModel;
import com.qmth.sop.business.entity.SysUser;
import com.qmth.sop.business.mapper.SysDeviceModelMapper;
import com.qmth.sop.business.service.SysDeviceBrandService;
import com.qmth.sop.business.service.SysDeviceModelService;
import com.qmth.sop.common.contant.SystemConstant;
import com.qmth.sop.common.enums.ExceptionResultEnum;
import com.qmth.sop.common.enums.FieldUniqueEnum;
import com.qmth.sop.common.util.ResultUtil;
import com.qmth.sop.common.util.ServletUtil;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Map;
import java.util.Objects;

/**
 * 设备品牌型号表 服务实现.
 *
 * @author: shudonghui
 * @date: 2023-08-25 10:29:08
 * @version: 1.0
 * @email: shudonghui@qmth.com.cn
 * @Company: www.qmth.com.cn
 */
@Service
public class SysDeviceModelServiceImpl extends ServiceImpl<SysDeviceModelMapper, SysDeviceModel> implements SysDeviceModelService {
	@Resource
	private SysDeviceBrandService sysDeviceBrandService;

	@Override
	public IPage<SysDeviceModel> query(IPage<Map> iPage, String query) {
		return this.baseMapper.query(iPage, query);
	}

	@Override
	@Transactional
	public Boolean saveSysDeviceModel(SysDeviceModel sysDeviceModel) {
		try {
			SysUser sysUser = (SysUser) ServletUtil.getRequestUser();
			if (Objects.isNull(sysDeviceModel.getId())) {// 新增
				sysDeviceModel.insertInfo(sysUser.getId());
			} else { // 修改
				sysDeviceModel.updateInfo(sysUser.getId());
			}
			return saveOrUpdate(sysDeviceModel);
		} catch (Exception e) {
			if (e instanceof DuplicateKeyException) {
				String errorColumn = e.getCause().toString();
				String columnStr = errorColumn.substring(errorColumn.lastIndexOf("key") + 3).replaceAll("'", "");
				throw ExceptionResultEnum.SQL_ERROR.exception("[" + FieldUniqueEnum.convertToTitle(columnStr) + "]数据不允许重复插入");
			} else if (e instanceof ApiException) {
				ResultUtil.error((ApiException) e, ((ApiException) e).getCode(), e.getMessage());
			} else {
				ResultUtil.error(e.getMessage());
			}
		}
		return null;

	}

	@Transactional
	@Override
	public SysDeviceModel findOrAddDeviceModel(String brand, String model) {
		Long brandId = sysDeviceBrandService.findOrAddDeviceBrand(brand).getId();
		SysDeviceModel sysDeviceModel = this.getOne(new QueryWrapper<SysDeviceModel>()
				.lambda()
				.eq(SysDeviceModel::getBrandId, brandId)
				.eq(SysDeviceModel::getModel, model)
				.last(SystemConstant.LIMIT1));
		if (Objects.isNull(sysDeviceModel)) {
			sysDeviceModel = new SysDeviceModel();
			sysDeviceModel.setBrandId(brandId);
			sysDeviceModel.setModel(model);
			this.saveSysDeviceModel(sysDeviceModel);
		}
		return sysDeviceModel;
	}

	@Override
	@Transactional
	public Boolean delete(Long id) {
		return this.removeById(id);
	}
}