package com.srs.web.controller.stureg;

import com.alibaba.druid.util.Utils;
import com.srs.common.annotation.Log;
import com.srs.common.config.SrsConfig;
import com.srs.common.constant.Constants;
import com.srs.common.core.controller.BaseController;
import com.srs.common.core.domain.AjaxResult;
import com.srs.common.core.domain.entity.SysUser;
import com.srs.common.core.page.TableDataInfo;
import com.srs.common.enums.BusinessType;
import com.srs.common.utils.SecurityUtils;
import com.srs.common.utils.StringUtils;
import com.srs.common.utils.bean.BeanUtils;
import com.srs.common.utils.poi.ExcelUtil;
import com.srs.stureg.domain.SrsRegInfo;
import com.srs.stureg.service.ISrsRegInfoService;
import com.srs.system.service.ISysRoleService;
import com.srs.system.service.ISysUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

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

/**
 * 注册信息
Controller
 * 
 * @author srs
 * @date 2023-05-19
 */
@Api("用户报到管理")
@RestController
@RequestMapping("/stureg/intenetin")
public class SrsRegInfoController extends BaseController
{
    @Autowired
    private ISrsRegInfoService srsRegInfoService;

    @Autowired
    private ISysUserService userService;

    @Autowired
    private ISysRoleService roleService;




    /**
     * 查询注册信息
列表
     */
//    @PreAuthorize("@ss.hasPermi('stureg:intenetin:list')")
    @GetMapping("/list")
    public TableDataInfo list(SrsRegInfo srsRegInfo)
    {
        startPage();
        List<SrsRegInfo> list = srsRegInfoService.selectSrsRegInfoList(srsRegInfo);
        return getDataTable(list);
    }


    /**
     * 导出注册信息列表
     */
//    @PreAuthorize("@ss.hasPermi('stureg:intenetin:export')")
    @Log(title = "注册信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SrsRegInfo srsRegInfo)
    {
        List<SrsRegInfo> list = srsRegInfoService.selectSrsRegInfoList(srsRegInfo);
        ExcelUtil<SrsRegInfo> util = new ExcelUtil<SrsRegInfo>(SrsRegInfo.class);
        util.exportExcel(response, list, "注册信息数据");
    }

    /**
     * 获取注册信 详细信息
     */
//    @PreAuthorize("@ss.hasPermi('stureg:intenetin:query')")
    @GetMapping(value = "/{registerId}")
    public AjaxResult getInfo(@PathVariable("registerId") Long registerId)
    {
        return success(srsRegInfoService.selectSrsRegInfoByRegisterId(registerId));
    }

    @ApiOperation("用户注册")
    @RequestMapping("/userRegister")
    public AjaxResult regUserInfo(@RequestBody SrsRegInfo srsRegInfo){
        SysUser user=new SysUser();
        user.setPassword(srsRegInfo.getPassword());
        user.setUserName(srsRegInfo.getUserId());
        user.setNickName(srsRegInfo.getUserName());
        Long[] l={2L};
        user.setRoleIds(l);
        srsRegInfo.setIsReg(1L);
        if (!userService.checkUserNameUnique(user))
        {
            return error("新增用户'" + user.getUserName() + "'失败，登录账号已存在");
        }
        SrsRegInfo sri=new SrsRegInfo();
        sri.setIdentyNo(srsRegInfo.getIdentyNo());
        List<SrsRegInfo> list=srsRegInfoService.selectSrsRegInfoList(sri);
        if(list.size()<1){
            return error("用户'" + user.getUserName() + "'注册失败，系统未找到你的录取信息");
        }
        else{
            sri=list.get(0);
            if (sri.getIsReg()!=null&&sri.getIsReg()==1){
                return error("用户'" + user.getUserName() + "注册失败，你的身份信息已注册，请你通过邮箱找回账号！");
            }
        }
        user.setPassword(SecurityUtils.encryptPassword(user.getPassword()));
        int i=userService.insertUser(user);
        BeanUtils.copyBeanProp(sri,srsRegInfo);
        //int j=srsRegInfoService.insertSrsRegInfo(srsRegInfo);
        int j=srsRegInfoService.updateSrsRegInfo(sri);
        return  toAjax(i).put("msg","注册成功！");
    }
    /**
     * 新增注册信息

     */
//    @PreAuthorize("@ss.hasPermi('stureg:intenetin:add')")
    @Log(title = "注册信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SrsRegInfo srsRegInfo)
    {


        return toAjax(srsRegInfoService.insertSrsRegInfo(srsRegInfo));
    }

    /**
     * 修改注册信息

     */
//    @PreAuthorize("@ss.hasPermi('stureg:intenetin:edit')")
    @Log(title = "注册信息", businessType = BusinessType.UPDATE)
    @PostMapping("/update")
    public AjaxResult edit(@RequestBody SrsRegInfo srsRegInfo)
    {
        return toAjax(srsRegInfoService.updateSrsRegInfo(srsRegInfo));
    }

    /**
     * 删除注册信息

     */
//    @PreAuthorize("@ss.hasPermi('stureg:intenetin:remove')")
    @Log(title = "注册信息", businessType = BusinessType.DELETE)
	@PostMapping("/delete/{registerIds}")
    public AjaxResult remove(@PathVariable Long[] registerIds)
    {
        return toAjax(srsRegInfoService.deleteSrsRegInfoByRegisterIds(registerIds));
    }

    @Log(title = "用户管理", businessType = BusinessType.IMPORT)
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception
    {
        ExcelUtil<SrsRegInfo> util = new ExcelUtil<SrsRegInfo>(SrsRegInfo.class);
        List<SrsRegInfo> userList = util.importExcel(file.getInputStream());
        String operName = getUsername();
        String message = srsRegInfoService.importRegUser(userList);
        return success(message);
    }

    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response)
    {
        ExcelUtil<SrsRegInfo> util = new ExcelUtil<SrsRegInfo>(SrsRegInfo.class);
        util.importTemplateExcel(response, "用户数据");
    }

    @ApiOperation("识别测试")
    @PostMapping("/facevetify")
    public  AjaxResult faceImageTest(@RequestBody SrsRegInfo srsRegInfo){

        SrsRegInfo sri=new SrsRegInfo();
        sri.setIdentyNo(srsRegInfo.getIdentyNo());
        List<SrsRegInfo> list=srsRegInfoService.selectSrsRegInfoList(sri);
        if(list.size()>0){
            sri=list.get(0);
            String localPath=SrsConfig.getProfile();
            String pathSourece=localPath + StringUtils.substringAfter(sri.getFaceImage(), Constants.RESOURCE_PREFIX);
            String pathLogin=localPath+StringUtils.substringAfter(srsRegInfo.getFaceImage(), Constants.RESOURCE_PREFIX);
            boolean b=srsRegInfoService.faceVetify(pathSourece,pathLogin);
            return success().put("msg",b);
        }
        return  success().put("msg","系统未找到对应账号信息！");
    }
}
