package com.xueyi.system.staff.controller;

import cn.hutool.core.text.CharSequenceUtil;
import com.xueyi.common.core.utils.core.ObjectUtil;
import com.xueyi.common.core.utils.file.FileTypeUtil;
import com.xueyi.common.core.utils.file.MimeTypeUtil;
import com.xueyi.common.core.web.result.AjaxResult;
import com.xueyi.common.core.web.result.R;
import com.xueyi.common.core.web.validate.V_A;
import com.xueyi.common.core.web.validate.V_E;
import com.xueyi.common.log.annotation.Log;
import com.xueyi.common.log.enums.BusinessType;
import com.xueyi.common.security.annotation.RequiresPermissions;
import com.xueyi.common.web.entity.controller.BaseController;
import com.xueyi.file.api.domain.SysFile;
import com.xueyi.file.api.feign.RemoteFileService;
import com.xueyi.system.api.digitalmans.domain.dto.DmVisitorsDto;
import com.xueyi.system.staff.domain.query.DmVisitorsQuery;
import com.xueyi.system.staff.service.IDmVisitorsService;
import org.springframework.beans.factory.annotation.Autowired;
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.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.Serializable;
import java.util.Arrays;
import java.util.List;

/**
 * 访客管理 业务处理
 *
 * @author xueyi
 */
@RestController
@RequestMapping("/visitors")
public class DmVisitorsController extends BaseController<DmVisitorsQuery, DmVisitorsDto, IDmVisitorsService> {

    @Autowired
    private RemoteFileService remoteFileService;

    /** 定义节点名称 */
    @Override
    protected String getNodeName() {
        return "访客" ;
    }

    /**
     * 查询访客列表
     */
    @Override
    @GetMapping("/list")
    @RequiresPermissions(Auth.DM_VISITORS_LIST)
    public AjaxResult list(DmVisitorsQuery dmVisitors) {
        return super.list(dmVisitors);
    }

    /**
     * 查询访客详细
     */
    @Override
    @GetMapping(value = "/{id}")
    @RequiresPermissions(Auth.DM_VISITORS_SINGLE)
    public AjaxResult getInfo(@PathVariable Serializable id) {
        return super.getInfo(id);
    }

    /**
     * 访客新增
     */
    @Override
    @PostMapping
    @RequiresPermissions(Auth.DM_VISITORS_ADD)
    @Log(title = "访客管理", businessType = BusinessType.INSERT)
    public AjaxResult add(@Validated({V_A.class}) @RequestBody DmVisitorsDto dmVisitors) {
        return super.add(dmVisitors);
    }

    /**
     * 访客修改
     */
    @Override
    @PutMapping
    @RequiresPermissions(Auth.DM_VISITORS_EDIT)
    @Log(title = "访客管理", businessType = BusinessType.UPDATE)
    public AjaxResult edit(@Validated({V_E.class}) @RequestBody DmVisitorsDto dmVisitors) {
        return super.edit(dmVisitors);
    }

    /**
     * 访客批量删除
     */
    @Override
    @DeleteMapping("/batch/{idList}")
    @RequiresPermissions(Auth.DM_VISITORS_DEL)
    @Log(title = "访客管理", businessType = BusinessType.DELETE)
    public AjaxResult batchRemove(@PathVariable List<Long> idList) {
        return super.batchRemove(idList);
    }

    /**
     * 获取访客选择框列表
     */
    @Override
    @GetMapping("/option")
    public AjaxResult option() {
        return super.option();
    }

    interface Auth {
        /** 系统 - 访客管理 - 列表 */
        String DM_VISITORS_LIST = "visitor:visitors:list";
        /** 系统 - 访客管理 - 详情 */
        String DM_VISITORS_SINGLE = "visitor:visitors:single";
        /** 系统 - 访客管理 - 新增 */
        String DM_VISITORS_ADD = "visitor:visitors:add";
        /** 系统 - 访客管理 - 修改 */
        String DM_VISITORS_EDIT = "visitor:visitors:edit";
        /** 系统 - 访客管理 - 删除 */
        String DM_VISITORS_DEL = "visitor:visitors:delete";
    }
    /**
     * 头像上传
     */
    @PostMapping("/upload")
    @Log(title = "访客管理 - 上传头像", businessType = BusinessType.UPDATE)
    public AjaxResult upload(@RequestParam("file") MultipartFile file) {
        if (!file.isEmpty()) {
            String extension = FileTypeUtil.getExtension(file);
            if (!CharSequenceUtil.equalsAnyIgnoreCase(extension, MimeTypeUtil.IMAGE_EXTENSION)) {
                return error("文件格式不正确，请上传" + Arrays.toString(MimeTypeUtil.IMAGE_EXTENSION) + "格式");
            }
            R<SysFile> fileResult = remoteFileService.uploadFace(file);
            if (ObjectUtil.isNull(fileResult) || ObjectUtil.isNull(fileResult.getData()))
                return error("文件服务异常，请联系管理员！");
            String url = fileResult.getData().getUrl();
            // 预留人脸识别与图像质量监测接口，如果监测异常返回错误

            AjaxResult ajax = success();
            ajax.put(AjaxResult.URL_TAG, url);
            return ajax;
        }
        return error("上传图片异常，请联系管理员！");
    }
}
