package com.dcsoft.system.controller;

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

import com.dcsoft.common.core.utils.StringUtils;
import com.dcsoft.system.uniubi.service.ISysApiService;
import com.dcsoft.system.visitor.domain.Visitor;
import com.dcsoft.system.visitor.service.IVisitorService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.dcsoft.common.log.annotation.Log;
import com.dcsoft.common.log.enums.BusinessType;
import com.dcsoft.common.security.annotation.RequiresPermissions;
import com.dcsoft.system.domain.SysBlackList;
import com.dcsoft.system.service.ISysBlackListService;
import com.dcsoft.common.core.web.controller.BaseController;
import com.dcsoft.common.core.web.domain.AjaxResult;
import com.dcsoft.common.core.utils.poi.ExcelUtil;
import com.dcsoft.common.core.web.page.TableDataInfo;

/**
 * 黑名单管理Controller
 *
 * @author nichun
 * @date 2023-07-03
 */
@RestController
@RequestMapping("/blackList")
public class SysBlackListController extends BaseController
{
    @Autowired
    private ISysBlackListService sysBlackListService;

    @Autowired
    private IVisitorService visitorService;

    @Autowired
    private ISysApiService sysApiService;

    /**
     * 查询黑名单管理列表
     */
    @RequiresPermissions("system:blackList:list")
    @GetMapping("/list")
    public TableDataInfo list(SysBlackList sysBlackList)
    {
        startPage();
        List<SysBlackList> list = sysBlackListService.selectSysBlackListList(sysBlackList);
        return getDataTable(list);
    }

    /**
     * 导出黑名单管理列表
     */
    @RequiresPermissions("system:blackList:export")
    @Log(title = "黑名单管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysBlackList sysBlackList)
    {
        List<SysBlackList> list = sysBlackListService.selectSysBlackListList(sysBlackList);
        ExcelUtil<SysBlackList> util = new ExcelUtil<SysBlackList>(SysBlackList.class);
        util.exportExcel(response, list, "黑名单管理数据");
    }

    /**
     * 获取黑名单管理详细信息
     */
    @RequiresPermissions("system:blackList:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(sysBlackListService.selectSysBlackListById(id));
    }

    /**
     * 新增黑名单管理
     */
    @RequiresPermissions("system:blackList:add")
    @Log(title = "黑名单管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SysBlackList sysBlackList)
    {
        //查询符合条件的访客人员并将注册人员信息清空并下发设备
        Visitor visitor = visitorService.selectVisitorByPhone(sysBlackList.getPhone());
//        if(StringUtils.isNotEmpty(visitor.getGuid())){
//            String data=sysApiService.admitDelete(visitor.getGuid());
//        }
//        visitor.setGuid("");
//        visitor.setFaceGuid("");
        if(visitor != null) {
            visitorService.updateVisitor(visitor);
        }

        return toAjax(sysBlackListService.insertSysBlackList(sysBlackList));
    }

    /**
     * 修改黑名单管理
     */
    @RequiresPermissions("system:blackList:edit")
    @Log(title = "黑名单管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SysBlackList sysBlackList)
    {
        //查询符合条件的访客人员并将注册人员信息清空并下发设备
        Visitor visitor = visitorService.selectVisitorByPhone(sysBlackList.getPhone());
//        if(StringUtils.isNotEmpty(visitor.getGuid())){
//            String data=sysApiService.admitDelete(visitor.getGuid());
//        }
//        visitor.setGuid("");
//        visitor.setFaceGuid("");
        if(visitor != null) {
            visitorService.updateVisitor(visitor);

        }
        return toAjax(sysBlackListService.updateSysBlackList(sysBlackList));
    }

    /**
     * 删除黑名单管理
     */
    @RequiresPermissions("system:blackList:remove")
    @Log(title = "黑名单管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(sysBlackListService.deleteSysBlackListByIds(ids));
    }
}
