package com.fuyuanshen.customer.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fuyuanshen.common.core.domain.model.LoginUser;
import com.fuyuanshen.common.mybatis.core.page.TableDataInfo;
import com.fuyuanshen.common.satoken.utils.LoginHelper;
import com.fuyuanshen.customer.domain.Customer;
import com.fuyuanshen.customer.domain.query.UserQueryCriteria;
import com.fuyuanshen.customer.domain.vo.ConsumerVo;
import com.fuyuanshen.customer.mapper.CustomerMapper;
import com.fuyuanshen.customer.service.CustomerService;
import io.undertow.util.BadRequestException;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Set;

/**
 * @author: 默苍璃
 * @date: 2025-07-0114:31
 */
@Service
@RequiredArgsConstructor
public class CustomerServiceImpl extends ServiceImpl<CustomerMapper, Customer> implements CustomerService {

    private final CustomerMapper customerMapper;


    /**
     * 分页查询客户
     *
     * @param criteria
     * @param page
     * @return
     */
    @Override
    public TableDataInfo<ConsumerVo> queryCustomers(UserQueryCriteria criteria, Page<Customer> page) {
        criteria.setOffset(page.offset());
        criteria.setPid(LoginHelper.getUserId());
        List<Customer> users = customerMapper.findCustomers(criteria);
        List<ConsumerVo> consumerVoList = BeanUtil.copyToList(users, ConsumerVo.class);
        Long total = customerMapper.countCustomers(criteria);
        // return PageUtil.toPage(consumerVoList, total);
        return new TableDataInfo<ConsumerVo>(consumerVoList, total);
    }


    /**
     * 查询所有客户
     *
     * @param criteria
     * @return
     */
    @Override
    public List<Customer> queryAllCustomers(UserQueryCriteria criteria) {
        List<Customer> users = customerMapper.queryAllCustomers(criteria);
        return users;
    }


    /**
     * 新增客户
     *
     * @param customer /
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void addCustomer(Customer customer) throws BadRequestException {

        LoginUser loginUser = LoginHelper.getLoginUser();

        UserQueryCriteria userQueryCriteria = new UserQueryCriteria();
        userQueryCriteria.setCustomerName(customer.getUserName());
        List<Customer> customers = customerMapper.queryCustomers(userQueryCriteria);
        if (!customers.isEmpty()) {
            throw new BadRequestException("用户名已存在！！！");
        }
        customer.setUserLevel((byte) (loginUser.getUserLevel() + 1));
        customer.setPid(loginUser.getUserId());

        save(customer);
    }


    /**
     * 修改客户
     *
     * @param resources /
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateCustomer(Customer resources) throws Exception {
        saveOrUpdate(resources);
    }


    /**
     * 删除客户
     *
     * @param ids /
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delete(Set<Long> ids) {
        customerMapper.deleteByIds(ids);
    }


}
