package com.ruoyi.web.controller.idcard;

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

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.web.service.TokenService;
import com.ruoyi.system.domain.idcard.SysUserIdcard;
import com.ruoyi.system.service.ISysUserService;
import com.ruoyi.system.service.idcard.ISysUserIdcardService;
import org.springframework.security.access.prepost.PreAuthorize;
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.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 用户身份证信息Controller
 *
 * @author boman
 * @date 2024-04-22
 */
@RestController
@RequestMapping("/idCard")
public class SysUserIdcardController extends BaseController {
    @Autowired
    private ISysUserIdcardService sysUserIdcardService;

    @Autowired
    private ISysUserService userService;

    @Autowired
    private TokenService tokenService;

    /**
     * 查询用户身份证信息列表
     */
    @PreAuthorize("@ss.hasPermi('idcard:idcard:list')")
    @GetMapping("/list")
    public TableDataInfo list(SysUserIdcard sysUserIdcard) {
        startPage();
        List<SysUserIdcard> list = sysUserIdcardService.selectSysUserIdcardList(sysUserIdcard);
        return getDataTable(list);
    }

    /**
     * 导出用户身份证信息列表
     */
    @PreAuthorize("@ss.hasPermi('idcard:idcard:export')")
    @Log(title = "用户身份证信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysUserIdcard sysUserIdcard) {
        List<SysUserIdcard> list = sysUserIdcardService.selectSysUserIdcardList(sysUserIdcard);
        ExcelUtil<SysUserIdcard> util = new ExcelUtil<SysUserIdcard>(SysUserIdcard.class);
        util.exportExcel(response, list, "用户身份证信息数据");
    }

    /**
     * 获取用户身份证信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('idcard:idcard:query')")
    @GetMapping(value = "/{userId}")
    public AjaxResult getInfo(@PathVariable("userId") Long userId) {
        return success(sysUserIdcardService.selectSysUserIdcardByUserId(userId));
    }

    /**
     * 新增用户身份证信息
     */
    @PreAuthorize("@ss.hasPermi('idcard:idcard:add')")
    @Log(title = "用户身份证信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SysUserIdcard sysUserIdcard) {
        LoginUser loginUser = getLoginUser();
        SysUser currentUser = loginUser.getUser();
        currentUser.setInitFace("Y");
        userService.checkUserAllowed(currentUser);
        userService.checkUserDataScope(currentUser.getUserId());
        currentUser.setUpdateBy(getUsername());
        if (userService.updateUserStatus(currentUser) > 0) {
            currentUser.setInitFace("Y");
            // 更新缓存用户信息
            tokenService.setLoginUser(loginUser);
            return toAjax(sysUserIdcardService.insertSysUserIdcard(sysUserIdcard));
        }
        return  error("人脸认证更新失败");
    }

    /**
     * 修改用户身份证信息
     */
    @PreAuthorize("@ss.hasPermi('idcard:idcard:edit')")
    @Log(title = "用户身份证信息", businessType = BusinessType.UPDATE)
    @PostMapping("/put")
    public AjaxResult edit(@RequestBody SysUserIdcard sysUserIdcard) {
        return toAjax(sysUserIdcardService.updateSysUserIdcard(sysUserIdcard));
    }

    /**
     * 删除用户身份证信息
     */
    @PreAuthorize("@ss.hasPermi('idcard:idcard:remove')")
    @Log(title = "用户身份证信息", businessType = BusinessType.DELETE)
    @GetMapping("/delete/{userIds}")
    public AjaxResult remove(@PathVariable Long[] userIds) {
        return toAjax(sysUserIdcardService.deleteSysUserIdcardByUserIds(userIds));
    }
}
