package com.muchinfo.mtp3century.controller;

import com.muchinfo.mtp3century.service.IOrganSonService;
import com.muchinfo.mtp3century.service.IUseraccountService;
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.mtp3pojos.vo.common.OrganSelectResult;
import com.muchinfo.mtp3pojos.vo.investor.InvestorModifyAuditParam;
import com.muchinfo.mtp3pojos.vo.organ.UserAccountReqParam;
import com.muchinfo.mtp3pojos.vo.organ.UserAccountRspResult;
import com.muchinfo.mtp3pojos.vo.organ.son.*;
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;

@RestController
@RequestMapping("/organSon")
@Api(value = "子机构管理API", description = "子机构管理")
public class OrganSonController {
    private final Logger log = LoggerFactory.getLogger(this.getClass());
    @Resource
    private IOrganSonService iOrganSonService;
    @Resource
    private IUseraccountService iUseraccountService;

    @ApiOperation("会员机构管理-->子机构管理-->获取机构列表")
    @RequestMapping(method = RequestMethod.GET, value = "/query")
    @ApiResponse(code = 200, message = "成功", response = PageResult.class)
    private PageResult<UserAccountRspResult> querySon(UserAccountReqParam param, HttpServletRequest request) {
        return iOrganSonService.querySon(param, request);
    }

    @ApiOperation("会员机构管理-->子机构管理-->机构详情")
    @RequestMapping(method = RequestMethod.GET, value = "/viewson")
    @ApiResponse(code = 200, message = "成功", response = AjaxResult.class)
    private AjaxResult<OrganSonDetailResult> viewson(Long areaid, Integer modifystatus, HttpServletRequest request) {
        return iOrganSonService.viewson(areaid, modifystatus, request);
    }

    @ApiOperation("会员机构管理-->子机构管理-->撤回")
    @RequestMapping(method = RequestMethod.GET, value = "/rebackChange")
    @ApiResponse(code = 200, message = "成功", response = AjaxResult.class)
    private AjaxResult rebackChange(Long areaid, HttpServletRequest request) {
        return iOrganSonService.rebackChange(areaid, request);
    }

    @ApiOperation("会员机构管理-->子机构管理-->注销")
    @RequestMapping(method = RequestMethod.GET, value = "/delete")
    @ApiResponse(code = 200, message = "成功", response = AjaxResult.class)
    private AjaxResult delete(Long areaid, HttpServletRequest request) {
        return iOrganSonService.delete(areaid, request);
    }

    @ApiOperation("会员机构管理-->子机构管理-->恢复")
    @RequestMapping(method = RequestMethod.GET, value = "/recover")
    @ApiResponse(code = 200, message = "成功", response = AjaxResult.class)
    private AjaxResult recover(Long areaid, HttpServletRequest request) {
        return iOrganSonService.recover(areaid, request);
    }

    @ApiOperation("会员机构管理-->子机构管理-->新增")
    @RequestMapping(method = RequestMethod.POST, value = "/addsonorgan")
    @ApiResponse(code = 200, message = "成功", response = AjaxResult.class)
    private AjaxResult addsonorgan(@RequestBody String param, HttpServletRequest request) {
        try {
            OrganAddSonOrganParam current = JsonUtils.toBean(CenturyDecryptUtil.decryptForJson(param, request), OrganAddSonOrganParam.class);
            return iOrganSonService.addsonorgan(current, 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.POST, value = "/editsonorgan")
    @ApiResponse(code = 200, message = "成功", response = AjaxResult.class)
    private AjaxResult editsonorgan(@RequestBody String param, HttpServletRequest request) {
        try {
            OrganAddSonOrganParam current = JsonUtils.toBean(CenturyDecryptUtil.decryptForJson(param, request), OrganAddSonOrganParam.class);
            return iOrganSonService.editsonorgan(current, 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.POST, value = "/edit")
    @ApiResponse(code = 200, message = "成功", response = AjaxResult.class)
    private AjaxResult edit(@RequestBody String param, HttpServletRequest request) {
        try {
            OrganAddSonOrganParam current = JsonUtils.toBean(CenturyDecryptUtil.decryptForJson(param, request), OrganAddSonOrganParam.class);
            return iOrganSonService.edit(current, 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 = "/queryaudit")
    @ApiResponse(code = 200, message = "成功", response = PageResult.class)
    private PageResult<UserAccountRspResult> queryaudit(UserAccountReqParam param, HttpServletRequest request) {
        return iOrganSonService.queryaudit(param, request);
    }

    @ApiOperation("会员机构管理-->子机构管理-->新增子机构审核-->审核")
    @RequestMapping(method = RequestMethod.GET, value = "/auditorganson")
    @ApiResponse(code = 200, message = "成功", response = PageResult.class)
    private AjaxResult auditorganson(Long userid, Long auditflag, String msg, HttpServletRequest request) {
        return iOrganSonService.auditorganson(userid, auditflag, msg, request);
    }

    @ApiOperation("会员机构管理-->子机构管理-->子机构变更审核-->获取机构列表")
    @RequestMapping(method = RequestMethod.GET, value = "/queryOrganSonModify")
    @ApiResponse(code = 200, message = "成功", response = PageResult.class)
    private PageResult<UserAccountRspResult> queryOrganSonModify(UserAccountReqParam param, HttpServletRequest request) {
        return iOrganSonService.queryOrganSonModify(param, request);
    }

    @ApiOperation("会员机构管理-->子机构管理-->子机构变更审核-->审核")
    @RequestMapping(method = RequestMethod.GET, value = "/modifyAudit")
    @ApiResponse(code = 200, message = "成功", response = AjaxResult.class)
    public AjaxResult modifyAudit(InvestorModifyAuditParam param, HttpServletRequest request) {
        return iUseraccountService.modifyAudit(param, request);
    }

    @ApiOperation("会员机构管理-->子机构管理-->子机构转移-->获取机构列表")
    @RequestMapping(method = RequestMethod.GET, value = "/queryOrganSonTransfer")
    @ApiResponse(code = 200, message = "成功", response = PageResult.class)
    private PageResult<OrganSonTransferResult> queryOrganSonTransfer(OrganSonTransferParam param, HttpServletRequest request) {
        return iOrganSonService.queryOrganSonTransfer(param, request);
    }

    @ApiOperation("会员机构管理-->子机构管理-->子机构转移-->执行转移")
    @RequestMapping(method = RequestMethod.GET, value = "/executeTransfer")
    @ApiResponse(code = 200, message = "成功", response = AjaxResult.class)
    private AjaxResult executeTransfer(HttpServletRequest request) {
        return iOrganSonService.executeTransfer(request);
    }

    @ApiOperation("会员机构管理-->子机构管理-->子机构转移-->撤销")
    @RequestMapping(method = RequestMethod.GET, value = "/backtranfor")
    @ApiResponse(code = 200, message = "成功", response = AjaxResult.class)
    private AjaxResult backtranfor(Long autoid, HttpServletRequest request) {
        return iOrganSonService.backtranfor(autoid, request);
    }

    @ApiOperation("会员机构管理-->子机构管理-->子机构转移-->新增-->获取转出子机构")
    @RequestMapping(method = RequestMethod.GET, value = "/querySonOrganPageByLevel")
    @ApiResponse(code = 200, message = "成功", response = AjaxResult.class)
    private AjaxResult<OrganSelectResult> querySonOrganPageByLevel(Integer subarealevel, String searchcode, HttpServletRequest request) {
        return iOrganSonService.querySonOrganPageByLevel(subarealevel,searchcode, request);
    }

    @ApiOperation("会员机构管理-->子机构管理-->子机构转移-->新增")
    @RequestMapping(method = RequestMethod.POST, value = "/organSonTransferAdd")
    @ApiResponse(code = 200, message = "成功", response = AjaxResult.class)
    private AjaxResult organSonTransferAdd(@RequestBody String param, HttpServletRequest request) {
        try {
            OrganSonTransferAddParam current = JsonUtils.toBean(CenturyDecryptUtil.decryptForJson(param, request), OrganSonTransferAddParam.class);
            return iOrganSonService.organSonTransferAdd(current, 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"));
        }
    }
}
