package com.izouma.nineth.web;

import com.alibaba.fastjson.JSONObject;
import com.izouma.nineth.domain.IdentityAuth;
import com.izouma.nineth.dto.PageQuery;
import com.izouma.nineth.enums.AuthStatus;
import com.izouma.nineth.exception.BusinessException;
import com.izouma.nineth.repo.IdentityAuthRepo;
import com.izouma.nineth.service.IdentityAuthService;
import com.izouma.nineth.utils.IdentityUtil;
import com.izouma.nineth.utils.SecurityUtils;
import com.izouma.nineth.utils.excel.ExcelUtils;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.Page;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.Objects;

@RestController
@RequestMapping("/identityAuth")
@AllArgsConstructor
@Slf4j
public class IdentityAuthController extends BaseController {
    private IdentityAuthService identityAuthService;
    private IdentityAuthRepo    identityAuthRepo;

    private IdentityUtil identityUtil;

//    @PreAuthorize("hasAnyRole('ADMIN','OPERATOR')")
    @PostMapping("/all")
    public Page<IdentityAuth> all(@RequestBody PageQuery pageQuery) {
        return identityAuthService.all(pageQuery);
    }

    @PreAuthorize("hasRole('ADMIN')")
    @GetMapping("/get/{id}")
    public IdentityAuth get(@PathVariable Long id) {
        return identityAuthRepo.findById(id).orElseThrow(new BusinessException("认证无记录"));
    }

    @PreAuthorize("hasRole('ADMIN')")
    @PostMapping("/del/{id}")
    public void del(@PathVariable Long id) {
        identityAuthRepo.softDelete(id);
    }

    @PreAuthorize("hasRole('ADMIN')")
    @PostMapping("/excel")
    @ResponseBody
    public void excel(HttpServletResponse response, @RequestBody PageQuery pageQuery) throws IOException {
        List<IdentityAuth> data = all(pageQuery).getContent();
        ExcelUtils.export(response, data);
    }

    @PostMapping("/apply")
    public void apply(IdentityAuth identityAuth) {
        identityAuth.setUserId(SecurityUtils.getAuthenticatedUser().getId());
        identityAuthService.apply(identityAuth);
    }

    @PreAuthorize("hasRole('OPERATOR') or hasRole('ADMIN')")
    @PostMapping("/pass")
    public void audit(@RequestParam Long id) {
        identityAuthRepo.findByIdAndDelFalse(id).orElseThrow(new BusinessException("申请不存在"));
        identityAuthService.audit(id, AuthStatus.SUCCESS);
    }

    @PreAuthorize("hasRole('ADMIN')")
    @GetMapping("/oneButtonPass")
    public void oneButtonPass() {
        identityAuthService.oneButtonPass();
    }

    @PreAuthorize("hasRole('OPERATOR') or hasRole('ADMIN')")
    @PostMapping("/deny")
    public void deny(@RequestParam Long id) {
        identityAuthService.audit(id, AuthStatus.FAIL);
    }


    /**
     *
     * 实名验证，管理员可用
     */
    @PreAuthorize("hasRole('ADMIN')")
    @PostMapping("verifyInformation")
    public JSONObject verifyInformation(@RequestParam Map reqMap){
        String accountNo = Objects.toString(reqMap.get("accountNo"),"");
        String phone = Objects.toString(reqMap.get("phone"),"");
        String idCardCode = Objects.toString(reqMap.get("idCardCode"),"");
        String name = Objects.toString(reqMap.get("name"),"");
        log.info("手动执行了实名操作：" + reqMap);
        identityUtil.verifyInformation(accountNo, phone, idCardCode, name);
        JSONObject result = new JSONObject();
        result.put("status", "Success");
        return result;
    }


}

