package com.inkasso.management.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.inkasso.adapter.util.BeanUtilsProxy;
import com.inkasso.management.user.dao.BackendUserDAO;
import com.inkasso.management.user.entity.BackendUserDO;
import com.inkasso.management.user.model.dto.BackendUserDTO;
import com.inkasso.management.user.model.request.AddBackendUserRequest;
import com.inkasso.management.user.model.request.EditBackendUserRequest;
import com.inkasso.management.user.service.BackendUserService;
import com.inkasso.adapter.exception.ServiceException;
import com.inkasso.adapter.exception.enums.ExceptionEnum;
import org.springframework.stereotype.Service;

/**
* @author 张超
* @date  2023-08-24
*/
@Service
public class BackendUserServiceImpl extends ServiceImpl<BackendUserDAO, BackendUserDO> implements BackendUserService {


    @Override
    public void addBackendUser(AddBackendUserRequest addBackendUserRequest) {
    BackendUserDO backendUser = BeanUtilsProxy.copyPropertiesIgnoreNull(addBackendUserRequest, BackendUserDO.class);
    this.baseMapper.insert(backendUser);
    }

    @Override
    public void editBackendUser(EditBackendUserRequest editBackendUserRequest) {

    BackendUserDO backendUser = super.getById(editBackendUserRequest.getId());
    if (backendUser == null) {
    throw new ServiceException(400, ExceptionEnum.SYSTEM_DATE_ERROR);
    }
    BeanUtilsProxy.copyPropertiesNotIgnoreNull(editBackendUserRequest,backendUser);
    this.updateById(backendUser);
    }

    @Override
    public BackendUserDTO getBackendUserDtoById(Long id) {
    BackendUserDO backendUser = super.getById(id);
    BackendUserDTO backendUserDto = BeanUtilsProxy.copyProperties(backendUser,BackendUserDTO.class);
    return backendUserDto;
    }

    @Override
    public void deleteBackendUser(Long id) {

    this.removeById(id);
    }

    @Override
    public BackendUserDTO getBackendUserDtoByUsername(String username) {

        LambdaQueryWrapper<BackendUserDO> lambdaQueryWrapper = new LambdaQueryWrapper();
        lambdaQueryWrapper.eq(BackendUserDO::getUsername,username);
        BackendUserDO backendUserDO = this.baseMapper.selectOne(lambdaQueryWrapper);
        if(backendUserDO!=null){
             return BeanUtilsProxy.copyProperties(backendUserDO,BackendUserDTO.class);
        }
        return null;
    }
}

