package com.muchinfo.mtp3century.controller;

import com.muchinfo.mtp3century.service.*;
import com.muchinfo.mtp3century.utils.CenturyDecryptUtil;
import com.muchinfo.mtp3common.enumtype.Constants;
import com.muchinfo.mtp3common.enumtype.MessageType;
import com.muchinfo.mtp3common.utils.JsonUtils;
import com.muchinfo.mtp3century.utils.AjaxResult;
import com.muchinfo.mtp3common.vo.PageResult;
import com.muchinfo.mtp3pojos.entity.Accountriskconfig;
import com.muchinfo.mtp3pojos.entity.Accounttradeconfig;
import com.muchinfo.mtp3pojos.entity.Systemmanager;
import com.muchinfo.mtp3pojos.vo.SystemRoleVo;
import com.muchinfo.mtp3pojos.vo.investor.InvestorInitBZResult;
import com.muchinfo.mtp3pojos.vo.investor.PersonalizationDelParam;
import com.muchinfo.mtp3pojos.vo.investor.InvestorTaAccountUpdateParam;
import com.muchinfo.mtp3pojos.vo.organ.*;
import com.muchinfo.mtp3pojos.vo.organ.son.OrganAddSonOrganParam;
import com.muchinfo.mtp3pojos.vo.systemuser.SystemUserVo;
import com.muchinfo.mtp3pojos.vo.taaccount.TaAccountQueryParam;
import com.muchinfo.mtp3pojos.vo.taaccount.TaAccountQueryResult;
import com.muchinfo.mtp3pojos.vo.taaccount.TaAccountQueryResultDetail;
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("/organDetail")
@Api(value = "机构管理API", description = "机构管理")
public class OrganController {
    private final Logger log = LoggerFactory.getLogger(this.getClass());
    @Resource
    private IOrganService iOrganService;
    @Resource
    private ITaaccountService iTaaccountService;
    @Resource
    private ISystemmanagerService iSystemmanagerService;
    @Resource
    private ISystemRoleService iSystemRoleService;
    @Resource
    private IUseraccountService iUseraccountService;

    @ApiOperation("会员机构管理-->机构管理-->系统角色管理-->获取角色列表")
    @RequestMapping(method = RequestMethod.GET, value = "/querysystemrole")
    @ApiResponse(code = 200, message = "成功", response = PageResult.class)
    private PageResult<SystemRoleVo> querysystemrole(Integer type, Integer pageNum, Integer pageSize, HttpServletRequest request) {
        return iSystemRoleService.query(type, pageSize, pageNum, request);
    }

    @ApiOperation("会员机构管理-->机构管理-->系统角色管理-->详情")
    @RequestMapping(method = RequestMethod.GET, value = "/querysystemroleDetail")
    @ApiResponse(code = 200, message = "成功", response = PageResult.class)
    private AjaxResult<SystemRoleVo> querysystemroleDetail(Integer autoId, HttpServletRequest request) {
        return iSystemRoleService.queryDetail(autoId, request);
    }

    @ApiOperation("会员机构管理-->机构管理-->系统角色管理-->获取当前角色关联关系")
    @ResponseBody
    @RequestMapping(method = RequestMethod.GET, value = "/queryRoleUser")
    @ApiResponse(code = 200, message = "成功", response = AjaxResult.class)
    private AjaxResult<Systemmanager> queryRoleUser(Integer autoId, HttpServletRequest request) {
        return iSystemmanagerService.queryRoleUser(autoId, request);
    }

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

    @ApiOperation("会员机构管理-->机构管理-->机构资料管理-->获取机构资料详情信息")
    @RequestMapping(method = RequestMethod.GET, value = "/queryDetail")
    @ApiResponse(code = 200, message = "成功", response = AjaxResult.class)
    private AjaxResult<UserAccountDetailResult> queryDetail(Long areaId, HttpServletRequest request) {
        return iOrganService.queryDetail(areaId, 0, request);
    }

    @ApiOperation("会员机构管理-->机构管理-->机构资料管理-->资金账户-->获取列表")
    @RequestMapping(method = RequestMethod.GET, value = "/organTaaccount")
    @ApiResponse(code = 200, message = "成功", response = PageResult.class)
    private PageResult<TaAccountQueryResult> organtaaccount(TaAccountQueryParam param, HttpServletRequest request) {
        return iOrganService.organtaaccount(param, request);
    }

    @ApiOperation("会员机构管理-->机构管理-->机构资料管理-->资金账户-->详情")
    @RequestMapping(method = RequestMethod.GET, value = "/organTaaccountDetail")
    @ApiResponse(code = 200, message = "成功", response = AjaxResult.class)
    private AjaxResult<TaAccountQueryResultDetail> organTaaccountDetail(Long accountId, HttpServletRequest request) {
        return iTaaccountService.queryDetail(request, accountId);
    }

    @ApiOperation("会员机构管理-->机构管理-->机构资料管理-->资金账户-->签解约流水")
    @RequestMapping(method = RequestMethod.GET, value = "/querySignlog")
    @ApiResponse(code = 200, message = "成功", response = PageResult.class)
    private PageResult<UserAccountSignlogResult> querySignlog(Long accountId, Integer querytype, Integer pageNum, Integer pageSize, HttpServletRequest request) {
        return iOrganService.querySignlog(accountId, querytype, pageNum, pageSize, request);
    }

    @ApiOperation("会员机构管理-->机构管理-->机构资料管理-->资金账户-->修改")
    @RequestMapping(method = RequestMethod.POST, value = "/taaccountedit")
    @ApiResponse(code = 200, message = "成功", response = AjaxResult.class)
    private AjaxResult taaccountedit(@RequestBody String param, HttpServletRequest request) {
        try {
            InvestorTaAccountUpdateParam current = JsonUtils.toBean(CenturyDecryptUtil.decryptForJson(param, request), InvestorTaAccountUpdateParam.class);
            return iTaaccountService.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 = "/resetPwd")
    @ApiResponse(code = 200, message = "成功", response = AjaxResult.class)
    private AjaxResult resetPwd(Long accountid, HttpServletRequest request) {
        return iOrganService.resetPwd(accountid, request);
    }

    @ApiOperation("会员机构管理-->机构管理-->机构资料管理-->登陆账号-->获取列表")
    @RequestMapping(method = RequestMethod.GET, value = "/queryorgan2User")
    @ApiResponse(code = 200, message = "成功", response = PageResult.class)
    private PageResult<UserAccountOrgan2UserResult> queryorgan2User(Long userid, Integer pageNum, Integer pageSize, HttpServletRequest request) {
        return iOrganService.queryorgan2User(userid, pageNum, pageSize, request);
    }

    @ApiOperation("会员机构管理-->机构管理-->机构资料管理-->登陆账号-->重置登入密码")
    @RequestMapping(method = RequestMethod.GET, value = "/updateStatus")
    @ApiResponse(code = 200, message = "成功", response = AjaxResult.class)
    private AjaxResult updateStatus(Long userid, Integer type, HttpServletRequest request) {
        SystemUserVo systemUserVo = new SystemUserVo();
        systemUserVo.setAutoid(userid);
        systemUserVo.setType(type);
        return iSystemmanagerService.dealUserStatus(systemUserVo, request);
    }

    @ApiOperation("会员机构管理-->机构管理-->机构资料管理-->登入账号-->全部启用/全部停用")
    @RequestMapping(method = RequestMethod.GET, value = "/updateorgan2User")
    @ApiResponse(code = 200, message = "成功", response = AjaxResult.class)
    private AjaxResult updateorgan2User(Long userid, Integer status, HttpServletRequest request) {
        return iOrganService.updateorgan2User(userid, status, request);
    }

    @ApiOperation("会员机构管理-->机构管理-->机构资料管理-->登入账号-->启用/停用")
    @RequestMapping(method = RequestMethod.GET, value = "/updatestatusbyautoid")
    @ApiResponse(code = 200, message = "成功", response = AjaxResult.class)
    private AjaxResult updatestatusbyautoid(Long autoid, Integer status, HttpServletRequest request) {
        return iOrganService.updatestatusbyautoid(autoid, status, request);
    }

    @ApiOperation("会员机构管理-->机构管理-->机构资料管理-->自营会员终端登录账号-->获取列表")
    @RequestMapping(method = RequestMethod.GET, value = "/queryselfsupport")
    @ApiResponse(code = 200, message = "成功", response = PageResult.class)
    private PageResult<UserAccountSelfsupportResult> queryselfsupport(UserAccountSelfsupportParam param, HttpServletRequest request) {
        return iOrganService.queryselfsupport(param, request);
    }

    @ApiOperation("会员机构管理-->机构管理-->机构资料管理-->自营会员终端登录账号-->重置登录密码")
    @RequestMapping(method = RequestMethod.GET, value = "/selfsupportresetPwd")
    @ApiResponse(code = 200, message = "成功", response = AjaxResult.class)
    private AjaxResult selfsupportresetPwd(Long loginid, HttpServletRequest request) {
        return iOrganService.selfsupportresetPwd(loginid, request);
    }

    @ApiOperation("会员机构管理-->机构管理-->机构资料管理-->自营会员终端登录账号-->锁定")
    @RequestMapping(method = RequestMethod.GET, value = "/lockAccount")
    @ApiResponse(code = 200, message = "成功", response = AjaxResult.class)
    private AjaxResult lockAccount(Long loginid, Integer status, HttpServletRequest request) {
        return iOrganService.lockAccount(loginid, status, request);
    }

    @ApiOperation("会员机构管理-->机构管理-->会员机构查询-->获取列表(营销中心查看经纪会员)")
    @RequestMapping(method = RequestMethod.GET, value = "/queryrole")
    @ApiResponse(code = 200, message = "成功", response = PageResult.class)
    private PageResult<UserAccountRspResult> queryrole(UserAccountRoleParam param, HttpServletRequest request) {
        return iOrganService.queryrole(param, request);
    }

    @ApiOperation("会员机构管理-->机构管理-->机构详情 ( " +
            " rolename: 2--运营 operate\n" +
            "         * 3--营销 marketing\n" +
            "         * 4--仓库 warehouse\n" +
            "         * 5--三方 tpart\n" +
            "         * 6--自营 selfsupport\n" +
            "         * 7--经纪 broker\n" +
            "         * 8--做市 marketer\n" +
            "         * 9--产业 industry)")
    @RequestMapping(method = RequestMethod.GET, value = "/queryroleDetail")
    @ApiResponse(code = 200, message = "成功", response = AjaxResult.class)
    private AjaxResult<UserAccountRoleDetailResult> queryroleDetail(String rolename, Long userid, HttpServletRequest request) {
        return iOrganService.queryroleDetail(rolename, userid, request);
    }

    @ApiOperation("会员机构管理-->机构管理-->机构角色删除")
    @RequestMapping(method = RequestMethod.GET, value = "/del")
    @ApiResponse(code = 200, message = "成功", response = AjaxResult.class)
    private AjaxResult del(Long areaid, Integer role, HttpServletRequest request) {
        return iOrganService.del(areaid, role, request);
    }

    @ApiOperation("会员机构管理-->机构管理-->机构角色新增初始化")
    @RequestMapping(method = RequestMethod.GET, value = "/initAdd")
    @ApiResponse(code = 200, message = "成功", response = AjaxResult.class)
    private AjaxResult<OrganInitAddResult> initAdd(String role, HttpServletRequest request) {
        return iOrganService.initAdd(role, request);
    }

    @ApiOperation("会员机构管理-->机构管理-->获取角色权限信息")
    @RequestMapping(method = RequestMethod.GET, value = "/getOragnDefaultManager")
    @ApiResponse(code = 200, message = "成功", response = AjaxResult.class)
    private AjaxResult<String> getOragnDefaultManager(Long userid, HttpServletRequest request) {
        return iOrganService.getOragnDefaultManager(userid, request);
    }


    @ApiOperation("会员机构管理-->机构管理-->新增、修改")
    @RequestMapping(method = RequestMethod.POST, value = "/roleadd")
    @ApiResponse(code = 200, message = "成功", response = AjaxResult.class)
    private AjaxResult roleadd(@RequestBody String vo, HttpServletRequest request) {
        try {
            OrganRoleModifyParam current = JsonUtils.toBean(CenturyDecryptUtil.decryptForJson(vo, request), OrganRoleModifyParam.class);
            return iOrganService.roleadd(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 = "/logout")
    @ApiResponse(code = 200, message = "成功", response = AjaxResult.class)
    private AjaxResult logout(Long userid, HttpServletRequest request) {
        return iOrganService.logout(userid, request);
    }

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

    @ApiOperation("会员机构管理-->机构管理-->商品限制设置-->获取列表")
    @RequestMapping(method = RequestMethod.GET, value = "/querymembergoodslimit")
    @ApiResponse(code = 200, message = "成功", response = PageResult.class)
    private PageResult<MemberGoodsLimitResult> querymembergoodslimit(MemberGoodsLimitParam param, HttpServletRequest request) {
        return iOrganService.querymembergoodslimit(param, request);
    }

    @ApiOperation("会员机构管理-->机构管理-->商品限制设置--> 新增、修改")
    @RequestMapping(method = RequestMethod.POST, value = "/addlimit")
    @ApiResponse(code = 200, message = "成功", response = AjaxResult.class)
    private AjaxResult addlimit(@RequestBody String vo, HttpServletRequest request) {
        try {
            MemberGoodsAddParam current = JsonUtils.toBean(CenturyDecryptUtil.decryptForJson(vo, request), MemberGoodsAddParam.class);
            return iOrganService.addlimit(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 = "/dellimit")
    @ApiResponse(code = 200, message = "成功", response = AjaxResult.class)
    private AjaxResult dellimit(Integer autoid, HttpServletRequest request) {
        return iOrganService.dellimit(autoid, request);
    }

    @ApiOperation("会员机构管理-->机构管理-->风控个性化设置-->获取列表")
    @RequestMapping(method = RequestMethod.GET, value = "/accountriskconfig")
    @ApiResponse(code = 200, message = "成功", response = PageResult.class)
    private PageResult<AccountRiskConfigResult> accountriskconfig(AccountRiskConfigParam param, HttpServletRequest request) {
        return iOrganService.accountriskconfig(param, request);
    }

    @ApiOperation("会员机构管理-->机构管理-->风控个性化设置-->详情")
    @RequestMapping(method = RequestMethod.GET, value = "/accountriskconfigview")
    @ApiResponse(code = 200, message = "成功", response = AjaxResult.class)
    private AjaxResult<Accountriskconfig> accountriskconfigview(Long accountid, HttpServletRequest request) {
        return iOrganService.accountriskconfigview(accountid, request);
    }

    @ApiOperation("会员机构管理-->机构管理-->风控个性化设置--> 新增")
    @RequestMapping(method = RequestMethod.POST, value = "/addaccountriskconfig")
    @ApiResponse(code = 200, message = "成功", response = AjaxResult.class)
    private AjaxResult addaccountriskconfig(@RequestBody String vo, HttpServletRequest request) {
        try {
            AccountRiskConfigAddParam current = JsonUtils.toBean(CenturyDecryptUtil.decryptForJson(vo, request), AccountRiskConfigAddParam.class);
            return iOrganService.addaccountriskconfig(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 = "/editaccountriskconfig")
    @ApiResponse(code = 200, message = "成功", response = AjaxResult.class)
    private AjaxResult editaccountriskconfig(@RequestBody AccountRiskConfigAddParam vo, HttpServletRequest request) {
        return iOrganService.editaccountriskconfig(vo, request);
    }

    @ApiOperation("会员机构管理-->机构管理-->风控个性化设置--> 删除")
    @RequestMapping(method = RequestMethod.GET, value = "/delaccountriskconfig")
    @ApiResponse(code = 200, message = "成功", response = AjaxResult.class)
    private AjaxResult delaccountriskconfig(Long accountid, HttpServletRequest request) {
        return iOrganService.delaccountriskconfig(accountid, request);
    }

    @ApiOperation("会员机构管理-->机构管理-->机构资料管理-->修改")
    @RequestMapping(method = RequestMethod.POST, value = "/editorgan")
    @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 iOrganService.editorgan(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 = "/createTree")
    @ApiResponse(code = 200, message = "成功", response = AjaxResult.class)
    private AjaxResult<AccountTradeConfigTreeVo> createTree(HttpServletRequest request) {
        return iOrganService.createTree(request);
    }

    @ApiOperation("会员机构管理-->机构管理-->账户个性化设置--> 详情")
    @RequestMapping(method = RequestMethod.GET, value = "/tradeConfigView")
    @ApiResponse(code = 200, message = "成功", response = AjaxResult.class)
    private AjaxResult<Accounttradeconfig> tradeConfigView(PersonalizationDelParam param, HttpServletRequest request) {
        //这个是投资者的userid ,获取投资者的分组ID
        param.setMemberuserid(0L);
        param.setRuletype(1);
        return iOrganService.tradeConfigView(param, request);
    }

    @ApiOperation("会员机构管理-->机构管理-->账户个性化设置-->修改-->初始化保证金类别")
    @ApiResponse(code = 200, message = "成功", response = AjaxResult.class)
    @RequestMapping(method = RequestMethod.GET, value = "/initBZList")
    public AjaxResult<InvestorInitBZResult> initBZList(Long goodsid, HttpServletRequest request) {
        return iUseraccountService.initBZList(goodsid, request);
    }

    @ApiOperation("会员机构管理-->机构管理-->账户个性化设置-->修改")
    @ApiResponse(code = 200, message = "成功", response = AjaxResult.class)
    @RequestMapping(method = RequestMethod.POST, value = "/tradeConfigEdit")
    public AjaxResult tradeConfigEdit(@RequestBody String param, HttpServletRequest request) {
        try {
            SupportTradeConfigAddParam current = JsonUtils.toBean(CenturyDecryptUtil.decryptForJson(param, request), SupportTradeConfigAddParam.class);
            return iOrganService.tradeConfigEdit(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("会员机构管理-->机构管理-->账户个性化设置-->删除")
    @ApiResponse(code = 200, message = "成功", response = AjaxResult.class)
    @RequestMapping(method = RequestMethod.GET, value = "/tradeConfigDelete")
    public AjaxResult tradeConfigDelete(PersonalizationDelParam param, HttpServletRequest request) {
        return iOrganService.tradeConfigDelete(param, request);
    }

    @ApiOperation("会员机构管理-->机构管理-->账户个性化设置/资金账户个性化设置-->新增初始化列表信息")
    @ApiResponse(code = 200, message = "成功", response = AjaxResult.class)
    @RequestMapping(method = RequestMethod.GET, value = "/loadSelectOption")
    public AjaxResult<OrganInitAccountAddResult> loadSelectOption(PersonalizationDelParam param, HttpServletRequest request) {
        return iOrganService.loadSelectOption(param, request);
    }

    @ApiOperation("会员机构管理-->机构管理-->账户个性化设置/资金账户个性化设置-->新增")
    @ApiResponse(code = 200, message = "成功", response = AjaxResult.class)
    @RequestMapping(method = RequestMethod.POST, value = "/tradeConfigAdd")
    public AjaxResult tradeConfigAdd(@RequestBody String param, HttpServletRequest request) {
        try {
            SupportTradeConfigAddParam current = JsonUtils.toBean(CenturyDecryptUtil.decryptForJson(param, request), SupportTradeConfigAddParam.class);
            return iOrganService.tradeConfigAdd(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"));
        }
    }
}
