package org.agt.module.license.service.customer;

import cn.hutool.core.util.StrUtil;
import jakarta.annotation.Resource;
import org.agt.framework.common.pojo.PageResult;
import org.agt.framework.common.util.object.BeanUtils;
import org.agt.module.license.controller.admin.customer.vo.CustomerPageReqVO;
import org.agt.module.license.controller.admin.customer.vo.CustomerSaveReqVO;
import org.agt.module.license.dal.dataobject.customer.CustomerDO;
import org.agt.module.license.dal.mysql.customer.CustomerMapper;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import java.util.List;

import static org.agt.framework.common.exception.util.ServiceExceptionUtil.exception;
import static org.agt.module.license.enums.ErrorCodeConstants.CUSTOMER_CODE_DUPLICATE;
import static org.agt.module.license.enums.ErrorCodeConstants.CUSTOMER_NAME_DUPLICATE;
import static org.agt.module.license.enums.ErrorCodeConstants.CUSTOMER_NOT_EXISTS;

/**
 * 客户 Service 实现类
 *
 * @author 管理员
 */
@Service
@Validated
public class CustomerServiceImpl implements CustomerService {

    @Resource
    private CustomerMapper customerMapper;

    @Override
    public Long createCustomer(CustomerSaveReqVO createReqVO) {
        // 校验客户名称和编号是否唯一
        validateCustomerNameAndCodeUnique(createReqVO.getName(), createReqVO.getCode(), createReqVO.getId());
        // 插入
        CustomerDO customer = BeanUtils.toBean(createReqVO, CustomerDO.class);
        customerMapper.insert(customer);
        // 返回
        return customer.getId();
    }

    @Override
    public void updateCustomer(CustomerSaveReqVO updateReqVO) {
        // 校验客户名称和编号是否唯一
        validateCustomerNameAndCodeUnique(updateReqVO.getName(), updateReqVO.getCode(), updateReqVO.getId());
        // 校验存在
        validateCustomerExists(updateReqVO.getId());
        // 更新
        CustomerDO updateObj = BeanUtils.toBean(updateReqVO, CustomerDO.class);
        customerMapper.updateById(updateObj);
    }

    @Override
    public void deleteCustomer(Long id) {
        // 校验存在
        validateCustomerExists(id);
        // 删除
        customerMapper.deleteById(id);
    }

    private void validateCustomerExists(Long id) {
        if (customerMapper.selectById(id) == null) {
            throw exception(CUSTOMER_NOT_EXISTS);
        }
    }

    private void validateCustomerNameAndCodeUnique(String name, String code, Long id) {
        Boolean isNameUnique = validateCustomerNameUnique(name, id);
        if (!isNameUnique) {
            throw exception(CUSTOMER_NAME_DUPLICATE, name);
        }
        Boolean isCodeUnique = validateCustomerCodeUnique(code, id);
        if (!isCodeUnique) {
            throw exception(CUSTOMER_CODE_DUPLICATE, code);
        }
    }

    @Override
    public CustomerDO getCustomer(Long id) {
        return customerMapper.selectById(id);
    }

    @Override
    public PageResult<CustomerDO> getCustomerPage(CustomerPageReqVO pageReqVO) {
        return customerMapper.selectPage(pageReqVO);
    }

    @Override
    public List<CustomerDO> getCustomerList() {
        return customerMapper.selectList();
    }

    @Override
    public Boolean validateCustomerNameUnique(String name, Long id) {
        if (StrUtil.isBlank(name)) {
            return true;
        }
        CustomerDO customer = customerMapper.selectByName(name);
        if (customer == null) {
            return true;
        }
        // 如果 id 为空，说明不用比较是否为相同 id 的客户
        if (id == null) {
            return false;
        }
        if (!customer.getId().equals(id)) {
            return false;
        }
        return true;
    }

    @Override
    public Boolean validateCustomerCodeUnique(String code, Long id) {
        if (StrUtil.isBlank(code)) {
            return true;
        }
        CustomerDO customer = customerMapper.selectByCode(code);
        if (customer == null) {
            return true;
        }
        // 如果 id 为空，说明不用比较是否为相同 id 的客户
        if (id == null) {
            return false;
        }
        if (!customer.getId().equals(id)) {
            return false;
        }
        return true;
    }

    @Override
    public Integer selectMaxCode() {
        Integer maxCode = customerMapper.selectMaxCode();
        return maxCode == null || maxCode < 2000 ? 2000 : maxCode + 1;
    }

}