package net.lab1024.sa.admin.module.business.wms.base.customer.service.impl;

import jakarta.annotation.Resource;
import net.lab1024.sa.admin.module.business.wms.base.customer.domain.entity.CustomerEntity;
import net.lab1024.sa.admin.module.business.wms.base.customer.domain.form.CustomerAddForm;
import net.lab1024.sa.admin.module.business.wms.base.customer.domain.form.CustomerUpdateForm;
import net.lab1024.sa.admin.module.business.wms.base.customer.manager.CustomerManager;
import net.lab1024.sa.admin.module.business.wms.base.customer.service.CustomerQueryService;
import net.lab1024.sa.admin.module.business.wms.base.customer.service.CustomerService;
import net.lab1024.sa.base.common.code.UserErrorCode;
import net.lab1024.sa.base.common.domain.ResponseDTO;
import net.lab1024.sa.base.common.util.SmartBeanUtil;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service

public class CustomerServiceImpl implements CustomerService {

    @Resource
    private CustomerManager customerManager;

    @Resource
    private CustomerQueryService customerQueryService;

    /**
     * 添加
     *
     * @param addForm 添加参数
     * @return ResponseDTO<String>
     */
    @Transactional(rollbackFor = Exception.class)
    public ResponseDTO<String> add(CustomerAddForm addForm) {
        CustomerEntity existingCustomer = customerQueryService.queryByCustomerName(addForm.getCustomerName());
        if (existingCustomer != null) {
            return ResponseDTO.error(UserErrorCode.ALREADY_EXIST, UserErrorCode.ALREADY_EXIST.getMsg());
        }
        CustomerEntity customerEntity = SmartBeanUtil.copy(addForm, CustomerEntity.class);
        customerManager.save(customerEntity);
        return ResponseDTO.ok();
    }

    /**
     * 更新
     *
     * @param updateForm 更新参数
     * @return ResponseDTO<String>
     */
    @Transactional(rollbackFor = Exception.class)
    public ResponseDTO<String> update(CustomerUpdateForm updateForm) {
        CustomerEntity existingCustomer = customerQueryService.queryByCustomerName(updateForm.getCustomerName());
        if (existingCustomer != null && !existingCustomer.getCustomerId().equals(updateForm.getCustomerId())) {
            return ResponseDTO.error(UserErrorCode.ALREADY_EXIST, UserErrorCode.ALREADY_EXIST.getMsg());
        }
        CustomerEntity customerEntity = SmartBeanUtil.copy(updateForm, CustomerEntity.class);
        //更新并更新缓存
        customerManager.update(customerEntity);
        return ResponseDTO.ok();
    }

    /**
     * 批量删除
     *
     * @param idList id集合
     * @return ResponseDTO<String>
     */
    @Transactional(rollbackFor = Exception.class)
    public ResponseDTO<String> batchDelete(List<Long> idList) {
        if (CollectionUtils.isEmpty(idList)) {
            return ResponseDTO.userErrorParam(UserErrorCode.PARAM_ERROR.getMsg());
        }
        //批量删除并删除缓存
        customerManager.batchDelete(idList);
        return ResponseDTO.ok();
    }

    /**
     * 单个删除
     *
     * @param customerId id
     * @return ResponseDTO<String>
     */
    @Transactional(rollbackFor = Exception.class)
    public ResponseDTO<String> delete(Long customerId) {
        if (null == customerId) {
            return ResponseDTO.userErrorParam(UserErrorCode.PARAM_ERROR.getMsg());
        }
        //更新缓存
        customerManager.deleteById(customerId);
        return ResponseDTO.ok();
    }
}