package cn.iocoder.yudao.module.member.service.customertraffic;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.module.member.controller.admin.customertraffic.vo.CustomerTrafficPageReqVO;
import cn.iocoder.yudao.module.member.controller.admin.customertraffic.vo.CustomerTrafficSaveReqVO;
import cn.iocoder.yudao.module.member.dal.dataobject.customertraffic.CustomerTrafficDO;
import cn.iocoder.yudao.module.member.dal.mysql.customertraffic.CustomerTrafficMapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.member.enums.ErrorCodeConstants.CUSTOMER_TRAFFIC_NOT_EXISTS;

/**
 * 顾客流量 Service 实现类
 *
 * @author 开发账号
 */
@Service
@Validated
public class CustomerTrafficServiceImpl implements CustomerTrafficService {

    @Resource
    private CustomerTrafficMapper customerTrafficMapper;

    @Override
    public Long createCustomerTraffic(CustomerTrafficSaveReqVO createReqVO) {
        // 插入
        CustomerTrafficDO customerTraffic = BeanUtils.toBean(createReqVO, CustomerTrafficDO.class);
        customerTrafficMapper.insert(customerTraffic);
        // 返回
        return customerTraffic.getId();
    }

    @Override
    public void updateCustomerTraffic(CustomerTrafficSaveReqVO updateReqVO) {
        // 校验存在
        validateCustomerTrafficExists(updateReqVO.getId());
        // 更新
        CustomerTrafficDO updateObj = BeanUtils.toBean(updateReqVO, CustomerTrafficDO.class);
        customerTrafficMapper.updateById(updateObj);
    }

    @Override
    public void deleteCustomerTraffic(Long id) {
        // 校验存在
        validateCustomerTrafficExists(id);
        // 删除
        customerTrafficMapper.deleteById(id);
    }

    private void validateCustomerTrafficExists(Long id) {
        if (customerTrafficMapper.selectById(id) == null) {
            throw exception(CUSTOMER_TRAFFIC_NOT_EXISTS);
        }
    }

    @Override
    public CustomerTrafficDO getCustomerTraffic(Long id) {
        return customerTrafficMapper.selectById(id);
    }

    @Override
    public PageResult<CustomerTrafficDO> getCustomerTrafficPage(CustomerTrafficPageReqVO pageReqVO) {
        return customerTrafficMapper.selectPage(pageReqVO);
    }

    @Override
    public void insertOne(CustomerTrafficDO customerTrafficDO) {
        customerTrafficMapper.insert(customerTrafficDO);
    }

    @Override
    public List<CustomerTrafficDO> getData() {
        LocalDateTime now = LocalDateTime.now();
        List<CustomerTrafficDO> customerTrafficDOS = customerTrafficMapper.selectList(Wrappers.<CustomerTrafficDO>lambdaQuery()
                .lt(CustomerTrafficDO::getCreateTime, now)
                .orderByDesc(CustomerTrafficDO::getCustomerNum)
                .last("limit 7"));
        return customerTrafficDOS;
    }
}