package com.ruoyi.web.app;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.ruoyi.bst.area.domain.AreaQuery;
import com.ruoyi.bst.area.domain.AreaVO;
import com.ruoyi.bst.area.service.AreaService;
import com.ruoyi.bst.customerService.domain.CustomerServiceQuery;
import com.ruoyi.bst.customerService.domain.dto.CustomerServiceIndexQuery;
import com.ruoyi.bst.customerService.domain.enums.CustomerServiceStatus;
import com.ruoyi.bst.customerService.service.CustomerServiceService;
import com.ruoyi.common.annotation.Anonymous;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.vo.UserVO;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.system.user.service.UserService;

import io.swagger.annotations.ApiOperation;


@RestController
@RequestMapping("/app/customerService")
public class AppCustomerServiceController extends BaseController {


    @Autowired
    private CustomerServiceService customerServiceService;

    @Autowired
    private UserService userService;

    @Autowired
    private AreaService areaService;

    @ApiOperation("获取客服列表")
    @Anonymous
    @GetMapping("/list")
    public TableDataInfo list(CustomerServiceQuery query) {
        startPage();
        return getDataTable(customerServiceService.selectCustomerServiceList(query));
    }

    @ApiOperation("首页客服")
    @Anonymous
    @GetMapping("/indexList")
    public AjaxResult indexList(CustomerServiceIndexQuery query) {
        AjaxResult ajax = AjaxResult.success();

        // 售后客服
        UserVO user = userService.selectUserById(getUserId());
        Long areaId = user.getAreaId();
        if (areaId != null) {
            CustomerServiceQuery afterQuery = new CustomerServiceQuery();
            afterQuery.setAreaId(areaId);
            afterQuery.setIsEnabled(CustomerServiceStatus.ENABLE.getStatus());
            ajax.put("after", customerServiceService.selectCustomerServiceList(afterQuery));
        }

        // 售前客服
        if (query.getRadius() != null && query.getCenter() != null && query.getCenter().size() >= 2) {
            // 查询运营区
            AreaQuery areaQuery = new AreaQuery();
            areaQuery.setCenter(query.getCenter());
            areaQuery.setRadius(query.getRadius());
            areaQuery.setId(user.getAreaId());
            AreaVO area = areaService.selectNearBy(areaQuery);
            if (area != null) {
                // 查询运营区客服
                CustomerServiceQuery beforeQuery = new CustomerServiceQuery();
                beforeQuery.setAreaId(area.getId());
                beforeQuery.setIsEnabled(CustomerServiceStatus.ENABLE.getStatus());
                ajax.put("before", customerServiceService.selectCustomerServiceList(beforeQuery));
            }
        }

        return ajax;
    }


}
