package com.aegis.web.controller.resource.expert;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
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.aegis.common.annotation.Log;
import com.aegis.common.core.controller.BaseController;
import com.aegis.common.core.domain.AjaxResult;
import com.aegis.common.core.page.TableDataInfo;
import com.aegis.common.enums.BusinessType;
import com.aegis.resource.expert.domain.Expert;
import com.aegis.resource.expert.service.IResourceExpertService;
import com.aegis.system.service.ISysDictTypeService;
import com.aegis.common.core.domain.entity.SysDictData;

/**
 * 专家Controller
 * 
 * @author aegis
 */
@RestController
@RequestMapping("/resources/experts")
public class ResourceExpertController extends BaseController {

    @Autowired
    private IResourceExpertService expertService;

    @Autowired
    private ISysDictTypeService dictTypeService;

    /**
     * 查询专家列表
     */
    @PreAuthorize("@ss.hasPermi('resource:expert:list')")
    @GetMapping("/list")
    public TableDataInfo list(Expert expert) {
        startPage();
        List<Expert> list = expertService.listByCondition(expert);
        return getDataTable(list);
    }

    /**
     * 根据ID获取专家详情
     */
    @PreAuthorize("@ss.hasPermi('resource:expert:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable Long id) {
        return success(expertService.getById(id));
    }

    /**
     * 根据专长领域查询专家列表
     */
    @PreAuthorize("@ss.hasPermi('resource:expert:list')")
    @GetMapping("/specialty/{specialty}")
    public AjaxResult listBySpecialty(@PathVariable String specialty) {
        List<Expert> list = expertService.listExpertsBySpecialty(specialty);
        return success(list);
    }

    /**
     * 获取专家专长字典列表
     */
    @GetMapping("/specialty/dict")
    public AjaxResult getSpecialtyDict() {
        List<SysDictData> dictList = dictTypeService.selectDictDataByType("expert_specialty");
        return success(dictList);
    }

    /**
     * 新增专家
     */
    @PreAuthorize("@ss.hasPermi('resource:expert:add')")
    @Log(title = "专家管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Validated @RequestBody Expert expert) {
        return toAjax(expertService.save(expert));
    }

    /**
     * 修改专家
     */
    @PreAuthorize("@ss.hasPermi('resource:expert:edit')")
    @Log(title = "专家管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody Expert expert) {
        return toAjax(expertService.updateById(expert));
    }

    /**
     * 删除专家
     */
    @PreAuthorize("@ss.hasPermi('resource:expert:remove')")
    @Log(title = "专家管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(expertService.removeByIds(Arrays.asList(ids)));
    }

    /**
     * 启用专家
     */
    @PreAuthorize("@ss.hasPermi('resource:expert:activate')")
    @Log(title = "专家管理", businessType = BusinessType.UPDATE)
    @PostMapping("/{id}/activate")
    public AjaxResult activate(@PathVariable Long id) {
        return toAjax(expertService.activateExpert(id));
    }

    /**
     * 停用专家
     */
    @PreAuthorize("@ss.hasPermi('resource:expert:deactivate')")
    @Log(title = "专家管理", businessType = BusinessType.UPDATE)
    @PostMapping("/{id}/deactivate")
    public AjaxResult deactivate(@PathVariable Long id) {
        return toAjax(expertService.deactivateExpert(id));
    }

    /**
     * 加入黑名单
     */
    @PreAuthorize("@ss.hasPermi('resource:expert:blacklist')")
    @Log(title = "专家管理", businessType = BusinessType.UPDATE)
    @PostMapping("/{id}/blacklist")
    public AjaxResult blacklist(@PathVariable Long id) {
        return toAjax(expertService.blacklistExpert(id));
    }

    /**
     * 解除黑名单
     */
    @PreAuthorize("@ss.hasPermi('resource:expert:blacklist')")
    @Log(title = "专家管理", businessType = BusinessType.UPDATE)
    @PostMapping("/{id}/unblacklist")
    public AjaxResult unblacklist(@PathVariable Long id) {
        return toAjax(expertService.unblacklistExpert(id));
    }

    /**
     * 评分（1-5）
     */
    @PreAuthorize("@ss.hasPermi('resource:expert:rate')")
    @Log(title = "专家管理", businessType = BusinessType.UPDATE)
    @PostMapping("/{id}/rate")
    public AjaxResult rate(@PathVariable Long id, @RequestBody Map<String, Object> params) {
        Integer rating = params.get("rating") != null ? Integer.valueOf(params.get("rating").toString()) : null;
        if (rating == null || rating < 1 || rating > 5) {
            return error("评分必须在1-5之间");
        }
        return toAjax(expertService.rateExpert(id, rating));
    }
}