package com.bonus.material.basic.service.impl;

import com.bonus.common.biz.enums.HttpCodeEnum;
import com.bonus.common.core.utils.DateUtils;
import com.bonus.common.core.web.domain.AjaxResult;
import com.bonus.common.security.utils.SecurityUtils;
import com.bonus.material.basic.domain.BmAssetAttributes;
import com.bonus.material.basic.mapper.BmAssetAttributesMapper;
import com.bonus.material.basic.service.BmAssetAttributesService;
import org.springframework.stereotype.Service;

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

/**
 * 资产属性表(BmAssetAttributes)表服务实现类
 *
 * @author makejava
 * @since 2024-10-15 10:39:02
 */
@Service("bmAssetAttributesService")
public class BmAssetAttributesServiceImpl implements BmAssetAttributesService {
    @Resource
    private BmAssetAttributesMapper bmAssetAttributesDao;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public BmAssetAttributes queryById(Long id) {
        return bmAssetAttributesDao.queryById(id);
    }

    /**
     * 分页查询
     *
     * @param bmAssetAttributes 筛选条件
     * @return 查询结果
     */
    @Override
    public List<BmAssetAttributes> queryByPage(BmAssetAttributes bmAssetAttributes) {
        return bmAssetAttributesDao.queryAllByLimit(bmAssetAttributes);
    }

    /**
     * 新增数据
     *
     * @param bmAssetAttributes 实例对象
     * @return 实例对象
     */
    @Override
    public AjaxResult insert(BmAssetAttributes bmAssetAttributes) {
        if (bmAssetAttributes == null) {
            return AjaxResult.error(HttpCodeEnum.TO_PARAM_NULL.getCode(), HttpCodeEnum.TO_PARAM_NULL.getMsg());
        }
        //先判断物资类型名称是否存在
        List<BmAssetAttributes> attributesList = bmAssetAttributesDao.selectBmAssetAttributesByAssetCode(bmAssetAttributes);
        if (attributesList.size() > 1) {
            return AjaxResult.error(HttpCodeEnum.NAME_DUPLICATE.getCode(), "资产类型名称或资产编码与库中重复");
        }
        if (attributesList.size() == 1) {
            if (!Objects.equals(attributesList.get(0).getId(), bmAssetAttributes.getId())) {
                return AjaxResult.error(HttpCodeEnum.NAME_DUPLICATE.getCode(), "资产类型名称或资产编码与库中重复");
            }
        }
        bmAssetAttributes.setCreateBy(SecurityUtils.getUserId().toString());
        bmAssetAttributes.setCreateTime(DateUtils.getNowDate());
        int result = bmAssetAttributesDao.insert(bmAssetAttributes);
        if (result > 0) {
            return AjaxResult.success();
        }
        return AjaxResult.error(HttpCodeEnum.FAIL.getCode(), HttpCodeEnum.FAIL.getMsg());
    }

    /**
     * 修改数据
     *
     * @param bmAssetAttributes 实例对象
     * @return 实例对象
     */
    @Override
    public AjaxResult update(BmAssetAttributes bmAssetAttributes) {
        if (bmAssetAttributes == null || bmAssetAttributes.getId() == null) {
            return AjaxResult.error(HttpCodeEnum.TO_PARAM_NULL.getCode(), HttpCodeEnum.TO_PARAM_NULL.getMsg());
        }
        //先判断物资类型名称是否存在
        List<BmAssetAttributes> attributesList = bmAssetAttributesDao.selectBmAssetAttributesByAssetCode(bmAssetAttributes);
        if (attributesList.size() > 1) {
            return AjaxResult.error(HttpCodeEnum.NAME_DUPLICATE.getCode(), "资产类型名称或资产编码与库中重复");
        }
        if (attributesList.size() == 1) {
            if (!Objects.equals(attributesList.get(0).getId(), bmAssetAttributes.getId())) {
                return AjaxResult.error(HttpCodeEnum.NAME_DUPLICATE.getCode(), "资产类型名称或资产编码与库中重复");
            }
        }
        bmAssetAttributes.setUpdateBy(SecurityUtils.getUserId().toString());
        bmAssetAttributes.setUpdateTime(DateUtils.getNowDate());
        int result = bmAssetAttributesDao.update(bmAssetAttributes);
        if (result > 0) {
            return AjaxResult.success();
        }
        return AjaxResult.error(HttpCodeEnum.FAIL.getCode(), HttpCodeEnum.FAIL.getMsg());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public AjaxResult deleteById(Long id) {
        int result = bmAssetAttributesDao.deleteById(id);
        if (result > 0) {
            return AjaxResult.success();
        }
        return AjaxResult.error(HttpCodeEnum.FAIL.getCode(), HttpCodeEnum.FAIL.getMsg());
    }
}
