package cn.aqroid.business.customer.service.impl;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.aqroid.business.customer.domain.Customer;
import cn.aqroid.business.customer.mapper.CustomerMapper;
import cn.aqroid.business.customer.service.ICustomerService;
import cn.aqroid.common.utils.DateUtils;

/**
 * 顾客Service业务层处理
 *
 * @author aqroid
 * @date 2026-01-12
 */
@Service
public class CustomerServiceImpl implements ICustomerService {

    @Autowired
    private CustomerMapper customerMapper;

    /**
     * 根据openid查询顾客
     */
    @Override
    public Customer selectByOpenid(String openid) {
        return customerMapper.selectByOpenid(openid);
    }

    /**
     * 根据ID查询顾客
     */
    @Override
    public Customer selectById(Long id) {
        return customerMapper.selectById(id);
    }

    /**
     * 根据手机号查询顾客
     */
    @Override
    public Customer selectByPhone(String phone) {
        return customerMapper.selectByPhone(phone);
    }

    /**
     * 查询顾客列表
     */
    @Override
    public List<Customer> selectCustomerList(Customer customer) {
        return customerMapper.selectCustomerList(customer);
    }

    /**
     * 新增顾客
     */
    @Override
    public int insertCustomer(Customer customer) {
        customer.setCreateTime(DateUtils.getNowDate());
        customer.setUpdateTime(DateUtils.getNowDate());
        if (customer.getStatus() == null) {
            customer.setStatus("0"); // 默认正常状态
        }
        return customerMapper.insertCustomer(customer);
    }

    /**
     * 更新顾客信息
     */
    @Override
    public int updateCustomer(Customer customer) {
        customer.setUpdateTime(DateUtils.getNowDate());
        return customerMapper.updateCustomer(customer);
    }

    /**
     * 更新最后登录时间
     */
    @Override
    public int updateLastLoginTime(Long id) {
        return customerMapper.updateLastLoginTime(id);
    }

    /**
     * 删除顾客
     */
    @Override
    public int deleteById(Long id) {
        return customerMapper.deleteById(id);
    }

    /**
     * 根据openid获取或创建顾客
     * 如果openid不存在则创建新顾客
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Customer getOrCreateByOpenid(String openid, String nickname, String avatar) {
        // 先查询是否存在
        Customer customer = customerMapper.selectByOpenid(openid);
        
        if (customer != null) {
            // 已存在，更新登录时间
            customerMapper.updateLastLoginTime(customer.getId());
            return customer;
        }
        
        // 不存在，创建新顾客
        customer = new Customer();
        customer.setOpenid(openid);
        customer.setNickname(nickname != null ? nickname : "微信用户");
        customer.setAvatar(avatar != null ? avatar : "");
        customer.setStatus("0");
        customer.setLastLoginTime(new Date());
        customer.setCreateTime(DateUtils.getNowDate());
        customer.setUpdateTime(DateUtils.getNowDate());
        
        customerMapper.insertCustomer(customer);
        
        return customer;
    }
}
