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

import java.util.List;

import net.lab1024.sa.admin.module.business.base.area.domain.entity.AreaEntity;
import net.lab1024.sa.admin.module.business.base.customer.dao.CustomerDao;
import net.lab1024.sa.admin.module.business.base.customer.domain.entity.CustomerEntity;
import net.lab1024.sa.admin.module.business.base.customer.domain.form.CustomerAddForm;
import net.lab1024.sa.admin.module.business.base.customer.domain.form.CustomerUpdateForm;
import net.lab1024.sa.admin.module.business.base.customer.manager.CustomerManager;
import net.lab1024.sa.base.common.code.UserErrorCode;
import net.lab1024.sa.base.common.util.SmartBeanUtil;
import net.lab1024.sa.base.common.domain.ResponseDTO;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;

import jakarta.annotation.Resource;
import org.springframework.transaction.annotation.Transactional;

/**
 * 客户管理 Service
 *
 * @author 霍锦
 * @since 2025-03-25 10:42:33
 * copyright 友仓
 */

@Service
public class CustomerService {

    @Resource
    private CustomerDao customerDao;

    @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);
        customerDao.insert(customerEntity);
        //更新缓存
        customerManager.removeCache();
        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);
        customerDao.updateById(customerEntity);
        //更新缓存
        customerManager.removeCache();
        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.removeBatchByIds(idList);
        //更新缓存
        customerManager.removeCache();
        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());
        }

        customerDao.deleteById(customerId);
        //更新缓存
        customerManager.removeCache();
        return ResponseDTO.ok();
    }
}
