package com.ruoyi.bst.customerService.service.impl;

import java.util.List;

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

import com.github.pagehelper.PageHelper;
import com.ruoyi.bst.area.service.AreaService;
import com.ruoyi.bst.areaJoin.domain.enums.AreaJoinPermission;
import com.ruoyi.bst.customerService.domain.CustomerService;
import com.ruoyi.bst.customerService.domain.CustomerServiceQuery;
import com.ruoyi.bst.customerService.domain.CustomerServiceVO;
import com.ruoyi.bst.customerService.mapper.CustomerServiceMapper;
import com.ruoyi.bst.customerService.service.CustomerServiceService;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.collection.CollectionUtils;

/**
 * 客服Service业务层处理
 *
 * @author ruoyi
 * @date 2025-04-02
 */
@Service
public class CustomerServiceServiceImpl implements CustomerServiceService
{
    @Autowired
    private CustomerServiceMapper customerServiceMapper;

    @Autowired
    private AreaService areaService;

    /**
     * 查询客服
     *
     * @param id 客服主键
     * @return 客服
     */
    @Override
    public CustomerServiceVO selectCustomerServiceById(Long id, boolean scope)
    {
        if (id == null) {
            return null;
        }
        CustomerServiceQuery query = new CustomerServiceQuery();
        query.setId(id);
        query.setScope(scope);
        query.addAreaPermission(AreaJoinPermission.CUSTOMER_SERVICE_VIEW.getCode());
        return this.selectOne(query);
    }
    
    private CustomerServiceVO selectOne(CustomerServiceQuery query) {
        PageHelper.startPage(1, 1);
        List<CustomerServiceVO> list = this.selectCustomerServiceList(query);
        return CollectionUtils.firstElement(list);
    }

    /**
     * 查询客服列表
     * @param query 客服
     * @return 客服
     */
    @Override
    public List<CustomerServiceVO> selectCustomerServiceList(CustomerServiceQuery query)
    {
        query.addAreaPermission(AreaJoinPermission.CUSTOMER_SERVICE_VIEW.getCode());
        return customerServiceMapper.selectCustomerServiceList(query);
    }

    /**
     * 新增客服
     *
     * @param customerService 客服
     * @return 结果
     */
    @Override
    public int insertCustomerService(CustomerService customerService)
    {
        Long storeId = areaService.selectAreaById(customerService.getAreaId()).getUserId();
        customerService.setStoreId(storeId);
        customerService.setCreateTime(DateUtils.getNowDate());
        return customerServiceMapper.insertCustomerService(customerService);
    }

    /**
     * 修改客服
     *
     * @param customerService 客服
     * @return 结果
     */
    @Override
    public int updateCustomerService(CustomerService customerService)
    {
        return customerServiceMapper.updateCustomerService(customerService);
    }

    /**
     * 批量删除客服
     *
     * @param ids 需要删除的客服主键
     * @return 结果
     */
    @Override
    public int deleteCustomerServiceByIds(Long[] ids)
    {
        return customerServiceMapper.deleteCustomerServiceByIds(ids);
    }

    /**
     * 删除客服信息
     *
     * @param id 客服主键
     * @return 结果
     */
    @Override
    public int deleteCustomerServiceById(Long id)
    {
        return customerServiceMapper.deleteCustomerServiceById(id);
    }

    @Override
    public int logicDel(List<Long> ids) {
        return customerServiceMapper.logicDel(ids);
    }
}
