package com.ruoyi.web.bst;

import java.util.List;

import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.ruoyi.bst.area.service.AreaService;
import com.ruoyi.bst.area.service.AreaValidator;
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.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.enums.LogBizType;
import com.ruoyi.common.utils.poi.ExcelUtil;

/**
 * 客服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);
        query.addAreaPermission(AreaJoinPermission.CUSTOMER_SERVICE_VIEW.getCode());
        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)
    {
        query.setScope(true);
        query.addAreaPermission(AreaJoinPermission.CUSTOMER_SERVICE_VIEW.getCode());
        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, true));
    }

    /**
     * 新增客服
     */
    @PreAuthorize("@ss.hasPermi('bst:customerService:add')")
    @Log(title = "新增客服", businessType = BusinessType.INSERT, bizIdName = "arg0", bizType = LogBizType.CUSTOMER_SERVICE)
    @PostMapping
    public AjaxResult add(@RequestBody CustomerService customerService)
    {
        if (customerService.getAreaId() != null && !areaValidator.canAddCustomerService(customerService.getAreaId())) {
            return error("无权为ID为" + customerService.getAreaId() + "的运营区新增客服");
        }
        return toAjax(customerServiceService.insertCustomerService(customerService));
    }

    /**
     * 修改客服
     */
    @PreAuthorize("@ss.hasPermi('bst:customerService:edit')")
    @Log(title = "修改客服", businessType = BusinessType.UPDATE, bizIdName = "arg0", bizType = LogBizType.CUSTOMER_SERVICE)
    @PutMapping
    public AjaxResult edit(@RequestBody CustomerService customerService)
    {
        if (!customerServiceValidator.canEdit(customerService.getId())) {
            return error("您没有权限修改ID为" + customerService.getId() + "的客服信息");
        }
        if (customerService.getAreaId() != null && !areaValidator.canAddCustomerService(customerService.getAreaId())) {
            return error("无权修改ID为" + customerService.getAreaId() + "的运营区下的客服信息");
        }
        customerService.setStoreId(areaService.selectAreaById(customerService.getAreaId()).getUserId());
        return toAjax(customerServiceService.updateCustomerService(customerService));
    }

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