package com.muchinfo.mtp3century.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.muchinfo.mtp3century.service.ICommonService;
import com.muchinfo.mtp3century.service.IWskhUserinfoService;
import com.muchinfo.mtp3century.utils.CenturyDecryptUtil;
import com.muchinfo.mtp3common.enumtype.Constants;
import com.muchinfo.mtp3common.enumtype.MessageType;
import com.muchinfo.mtp3century.utils.AjaxResult;
import com.muchinfo.mtp3common.utils.JsonUtils;
import com.muchinfo.mtp3common.vo.PageResult;
import com.muchinfo.mtp3interface.mapper.ISystemmanagerMapper;
import com.muchinfo.mtp3pojos.entity.Systemmanager;
import com.muchinfo.mtp3pojos.entity.WskhUserinfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;

/**
 * <p>
 * 网上开户用户表 (开户成功删除记录) 前端控制器
 * </p>
 *
 * @author XKF
 * @since 2024-05-30
 */
@RestController
@RequestMapping("/wskhUserinfo")
@Api(value = "机构网上开户api", description = "机构网上开户管理")
public class WskhUserinfoController {
    private final Logger log = LoggerFactory.getLogger(this.getClass());
    @Resource
    private IWskhUserinfoService iWskhUserinfoService;
    @Resource
    private ISystemmanagerMapper iSystemmanagerMapper;
    @Resource
    private ICommonService iCommonService;


    @ApiOperation("会员机构管理-->机构管理-->机构开户申请-->获取机构开户列表信息")
    @RequestMapping(method = RequestMethod.GET, value = "/query")
    @ApiResponse(code = 200, message = "成功", response = PageResult.class)
    private PageResult<WskhUserinfo> query(String username, Integer pageNum, Integer pageSize, HttpServletRequest request) {
        return iWskhUserinfoService.queryByPage(username, pageNum, pageSize, request);
    }

    @ApiOperation("会员机构管理-->机构管理-->机构开户申请-->获取机构开户列表详情信息")
    @RequestMapping(method = RequestMethod.GET, value = "/queryDetail")
    @ApiResponse(code = 200, message = "成功", response = AjaxResult.class)
    private AjaxResult<WskhUserinfo> queryDetail(Long userId, HttpServletRequest request) {
        return iWskhUserinfoService.queryDetail(userId, request);
    }

    @ApiOperation("会员机构管理-->机构管理-->机构开户申请-->处理开户信息  新增、修改")
    @RequestMapping(method = RequestMethod.POST, value = "/add")
    private AjaxResult add(@RequestBody String wskhUserinfoVo, HttpServletRequest request) {
        try {
            WskhUserinfo currentVo = JsonUtils.toBean(CenturyDecryptUtil.decryptForJson(wskhUserinfoVo, request), WskhUserinfo.class);
            //获取当前登录用户
            Systemmanager systemmanager = iSystemmanagerMapper.selectOne(new QueryWrapper<Systemmanager>().eq("LOGINCODE", iCommonService.getUserLoginCode(request)));
            if (currentVo.getUserid() != null) {
                return iWskhUserinfoService.updateAreainfo(currentVo, systemmanager, request);
            } else {
                return iWskhUserinfoService.addAreainfo(currentVo, systemmanager, request);
            }
        } catch (Exception e) {
            log.error("处理开户信息新增、修改失败，失败原因：" + Arrays.toString(e.getStackTrace()));
            return AjaxResult.error(Constants.Error_Code_Design, MessageType.MESSAGE_ERROR_CODE_SYSTEM002.getCode(), request.getHeader("Accept-Language"));
        }
    }

    @ApiOperation("会员机构管理-->机构管理-->机构开户申请-->撤回提交的待审核信息")
    @RequestMapping(method = RequestMethod.GET, value = "/reBack")
    private AjaxResult reBack(Long userid, HttpServletRequest request) {
        return iWskhUserinfoService.reBack(userid, request);
    }

    @ApiOperation("会员机构管理-->机构管理-->机构开户申请-->删除待提交的开户申请")
    @RequestMapping(method = RequestMethod.GET, value = "/delete")
    private AjaxResult delete(Long userid, HttpServletRequest request) {
        return iWskhUserinfoService.delete(userid, request);
    }

    @ApiOperation("会员机构管理-->机构管理-->机构开户申请-->审核通过")
    @RequestMapping(method = RequestMethod.POST, value = "/approved")
    private AjaxResult approved(@RequestBody String vo, HttpServletRequest request) {
        try {
            WskhUserinfo currentVo = JsonUtils.toBean(CenturyDecryptUtil.decryptForJson(vo, request), WskhUserinfo.class);
            Systemmanager systemmanager = iSystemmanagerMapper.selectOne(new QueryWrapper<Systemmanager>().eq("LOGINCODE", iCommonService.getUserLoginCode(request)));
            //获取当前登录用户
            if (currentVo.getAllocated() == null || currentVo.getAllocated() != 1) {
                if (systemmanager.getReletype() != 1) {
                    return AjaxResult.error(Constants.Error_Code_Design, MessageType.MESSAGE_ERROR_CODE_SYSTEM003.getCode(), request.getHeader("Accept-Language"));
                }
            }
            return iWskhUserinfoService.approved(currentVo, request);
        } catch (Exception e) {
            log.error("审核通过操作失败，失败原因：" + Arrays.toString(e.getStackTrace()));
            return AjaxResult.error(Constants.Error_Code_Design, MessageType.MESSAGE_ERROR_CODE_SYSTEM002.getCode(), request.getHeader("Accept-Language"));
        }
    }
}

