package com.ruoyi.web.bst;

import com.ruoyi.bst.area.domain.AreaVO;
import com.ruoyi.bst.area.service.AreaService;
import com.ruoyi.bst.area.service.AreaValidator;
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.service.CustomerServiceService;
import com.ruoyi.bst.customerService.service.CustomerServiceValidator;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.ServiceUtil;
import com.ruoyi.common.utils.poi.ExcelUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 客服Controller
 *
 * @author ruoyi
 * @date 2025-04-02
 */
@RestController
@RequestMapping("/bst/customerService")
public class CustomerServiceController extends BaseController
{
    @Autowired
    private CustomerServiceService customerServiceService;

    @Autowired
    private CustomerServiceValidator customerServiceValidator;

    @Autowired
    private AreaService areaService;

    @Autowired
    private AreaValidator areaValidator;

    /**
     * 查询客服列表
     */
    @PreAuthorize("@ss.hasPermi('bst:customerService:list')")
    @GetMapping("/list")
    public TableDataInfo list(CustomerServiceQuery query)
    {
        startPage();
        startOrderBy();
        query.setScope(true);
        List<CustomerServiceVO> list = customerServiceService.selectCustomerServiceList(query);
        return getDataTable(list);
    }

    /**
     * 导出客服列表
     */
    @PreAuthorize("@ss.hasPermi('bst:customerService:export')")
    @Log(title = "客服", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, CustomerServiceQuery query)
    {
        List<CustomerServiceVO> list = customerServiceService.selectCustomerServiceList(query);
        ExcelUtil<CustomerServiceVO> util = new ExcelUtil<CustomerServiceVO>(CustomerServiceVO.class);
        util.exportExcel(response, list, "客服数据");
    }

    /**
     * 获取客服详细信息
     */
    @PreAuthorize("@ss.hasPermi('bst:customerService:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(customerServiceService.selectCustomerServiceById(id));
    }

    /**
     * 新增客服
     */
    @PreAuthorize("@ss.hasPermi('bst:customerService:add')")
    @Log(title = "客服", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody CustomerService customerService)
    {
        if (customerService.getAreaId() != null) {
            AreaVO area = areaService.selectAreaById(customerService.getAreaId());
            ServiceUtil.assertion(area==null, "当前运营区不存在");
            ServiceUtil.assertion(!areaValidator.canAddCustomerService(customerService.getAreaId()), "您无权插入客服");
        }
        return toAjax(customerServiceService.insertCustomerService(customerService));
    }

    /**
     * 修改客服
     */
    @PreAuthorize("@ss.hasPermi('bst:customerService:edit')")
    @Log(title = "客服", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody CustomerService customerService)
    {
        if (customerService.getAreaId() != null) {
            ServiceUtil.assertion(!areaValidator.canAddCustomerService(customerService.getAreaId()), "您无权修改该运营区下的信息");
        }

        if (!customerServiceValidator.canEdit(customerService.getId())) {
            return AjaxResult.error("您没有权限修改ID为" + customerService.getId() + "的客服信息");
        }
        customerService.setStoreId(areaService.selectAreaById(customerService.getAreaId()).getUserId());
        return toAjax(customerServiceService.updateCustomerService(customerService));
    }

    /**
     * 删除客服
     */
    @PreAuthorize("@ss.hasPermi('bst:customerService:remove')")
    @Log(title = "客服", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable List<Long> ids)
    {
        if (!customerServiceValidator.canDeleteAll(ids)) {
            return AjaxResult.error("您没有权限删除ID为" + ids + "的客服信息");
        }
        return toAjax(customerServiceService.logicDel(ids));
    }
}
