package com.cweb.controller;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hfkj.common.exception.ErrorCode;
import com.hfkj.common.exception.ErrorHelp;
import com.hfkj.common.exception.SysCode;
import com.hfkj.common.security.UserCenter;
import com.hfkj.common.utils.ResponseMsgUtil;
import com.hfkj.entity.BsAudit;
import com.hfkj.entity.BsMer;
import com.hfkj.entity.BsMerSettleAcctApply;
import com.hfkj.entity.SecRegion;
import com.hfkj.model.ResponseData;
import com.hfkj.model.UserInfoModel;
import com.hfkj.service.BsAuditService;
import com.hfkj.service.BsMerService;
import com.hfkj.service.BsMerSettleAcctApplyService;
import com.hfkj.service.CommonService;
import com.hfkj.sysenum.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

@Controller
@Api(value = "商户审核")
@RequestMapping(value = "/audit")
public class BsAuditController {

    private static Logger log = LoggerFactory.getLogger(BsAuditController.class);

    @Resource
    private UserCenter userCenter;
    @Resource
    private BsAuditService auditService;

    @RequestMapping(value="/approve",method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "通过")
    public ResponseData approve(@RequestBody JSONObject body) {
        try {
            if (body == null || StringUtils.isBlank(body.getString("auditNo"))) {
                throw ErrorHelp.genException(SysCode.System, ErrorCode.REQ_PARAMS_ERROR, "");
            }
            UserInfoModel userInfoModel = userCenter.getSessionModel(UserInfoModel.class);
            if (userInfoModel == null) {
                throw ErrorHelp.genException(SysCode.System, ErrorCode.SEC_USER_EXPIRED, "");
            }

            // 查询审核
            BsAudit audit = auditService.getAuditDetailByNo(body.getString("auditNo"));
            if (audit == null) {
                throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "未知的审批");
            }
            if (!audit.getStatus().equals(AuditStatusEnum.status1.getNumber())) {
                throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "操作失败，审批状态不处于待审批");
            }
            if (!userInfoModel.getAgent().getId().equals(audit.getReviewedUserId())) {
                throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "操作失败，审批审核人校验不通过");
            }

            auditService.approve(audit.getId());
            return ResponseMsgUtil.success("操作成功");

        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return ResponseMsgUtil.exception(e);
        }
    }

    @RequestMapping(value="/reject",method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "驳回")
    public ResponseData reject(@RequestBody JSONObject body) {
        try {
            if (body == null || body.getString("auditNo") == null || StringUtils.isBlank(body.getString("rejectReason"))) {
                throw ErrorHelp.genException(SysCode.System, ErrorCode.REQ_PARAMS_ERROR, "");
            }

            UserInfoModel userInfoModel = userCenter.getSessionModel(UserInfoModel.class);
            if (userInfoModel == null) {
                throw ErrorHelp.genException(SysCode.System, ErrorCode.SEC_USER_EXPIRED, "");
            }

            // 查询审核
            BsAudit audit = auditService.getAuditDetailByNo(body.getString("auditNo"));
            if (audit == null) {
                throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "未知的审批");
            }
            if (!audit.getStatus().equals(AuditStatusEnum.status1.getNumber())) {
                throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "操作失败，审批状态不处于待审批");
            }
            if (!userInfoModel.getAgent().getId().equals(audit.getReviewedUserId())) {
                throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "操作失败，审批审核人校验不通过");
            }

            auditService.reject(audit.getId(), body.getString("rejectReason"));
            return ResponseMsgUtil.success("操作成功");

        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return ResponseMsgUtil.exception(e);
        }
    }

    @RequestMapping(value="/getAuditDetail",method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "查询审核详情")
    public ResponseData getAuditDetail(@RequestParam(value = "applyNo" , required = true) String applyNo) {
        try {

            return ResponseMsgUtil.success(auditService.getAuditDetailByNo(applyNo));

        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return ResponseMsgUtil.exception(e);
        }
    }

    @RequestMapping(value="/getAuditList",method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "查询审核列表")
    public ResponseData getAuditList(@RequestParam(value = "auditNo" , required = false) String auditNo,
                                     @RequestParam(value = "status" , required = false) Integer status,
                                     @RequestParam(value = "pageNum" , required = true) Integer pageNum,
                                     @RequestParam(value = "pageSize" , required = true) Integer pageSize) {
        try {
            UserInfoModel userInfoModel = userCenter.getSessionModel(UserInfoModel.class);
            if (userInfoModel == null) {
                throw ErrorHelp.genException(SysCode.System, ErrorCode.SEC_USER_EXPIRED, "");
            }

            Map<String,Object> param = new HashMap<>();
            param.put("auditNo", auditNo);
            param.put("status", status);
            if (userInfoModel.getSecUser().getObjectType().equals(SecUserTypeEnum.type2.getNumber())) {
                param.put("reviewedUserId", userInfoModel.getAgent().getId());
            } else {
                throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "无权限访问");
            }

            PageHelper.startPage(pageNum, pageSize);
            return ResponseMsgUtil.success(new PageInfo<>(auditService.getAuditList(param)));

        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return ResponseMsgUtil.exception(e);
        }
    }

}
