package com.muchinfo.mtp3century.controller;


import com.muchinfo.mtp3century.service.IProfitSharService;
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.vo.profitshar.*;
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.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

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

@RestController
@RequestMapping("/profitShar")
@Api(value = "分润API", description = "分润管理")
public class ProfitSharController {

    private final Logger log = LoggerFactory.getLogger(this.getClass());

    @Resource
    private IProfitSharService iProfitSharService;

    @ApiOperation("分润管理-->机构分润配置-->子机构分组管理-->获取列表")
    @RequestMapping(method = RequestMethod.GET, value = "/queryPage")
    @ApiResponse(code = 200, message = "成功", response = PageResult.class)
    private PageResult<ProfitSharGroupResult> queryPage(String subareagroupname, Integer pageNum, Integer pageSize, HttpServletRequest request) {
        return iProfitSharService.queryPage(subareagroupname, pageNum, pageSize, request);
    }

    @ApiOperation("分润管理-->机构分润配置-->子机构分组管理-->新增、修改")
    @RequestMapping(method = RequestMethod.POST, value = "/deal")
    @ApiResponse(code = 200, message = "成功", response = AjaxResult.class)
    private AjaxResult dealMessage(@RequestBody String param, HttpServletRequest request) {
        try {
            ProfitSharParam insertParam = JsonUtils.toBean(CenturyDecryptUtil.decryptForJson(param, request), ProfitSharParam.class);
            return iProfitSharService.dealMessage(insertParam, 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 = "/delete")
    @ApiResponse(code = 200, message = "成功", response = AjaxResult.class)
    private AjaxResult delete(Long groupId, HttpServletRequest request) {
        return iProfitSharService.delete(groupId, request);
    }

    @ApiOperation("分润管理-->机构分润配置-->子机构分组管理-->会员列表")
    @RequestMapping(method = RequestMethod.GET, value = "/memberQuery")
    @ApiResponse(code = 200, message = "成功", response = PageResult.class)
    private PageResult<ProfitSharMemberResult> memberQuery(Long subareagroupid, Integer pageNum, Integer pageSize,HttpServletRequest request) {
        return iProfitSharService.memberQuery(subareagroupid, pageNum, pageSize,request);
    }

    @ApiOperation("分润管理-->机构分润配置-->子机构分组管理-->会员列表-->机构获取")
    @RequestMapping(method = RequestMethod.GET, value = "/groupOrgan")
    @ApiResponse(code = 200, message = "成功", response = AjaxResult.class)
    private AjaxResult<ProfitSharGroupOrganResult> groupOrgan(HttpServletRequest request) {
        return iProfitSharService.groupOrgan(request);
    }

    @ApiOperation("分润管理-->机构分润配置-->子机构分组管理-->会员列表-->新增")
    @RequestMapping(method = RequestMethod.POST, value = "/memberAdd")
    @ApiResponse(code = 200, message = "成功", response = AjaxResult.class)
    private AjaxResult memberAdd(@RequestBody String param, HttpServletRequest request) {
        try {
            ProfitSharMemberParam insertParam = JsonUtils.toBean(CenturyDecryptUtil.decryptForJson(param, request), ProfitSharMemberParam.class);
            return iProfitSharService.memberAdd(insertParam,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 = "/deleteMember")
    @ApiResponse(code = 200, message = "成功", response = AjaxResult.class)
    private AjaxResult deleteMember(Long userid,HttpServletRequest request) {
        return iProfitSharService.deleteMember(userid,request);
    }

    @ApiOperation("分润管理-->机构分润配置-->子机构分润配置-->获取所有已配置信息")
    @RequestMapping(method = RequestMethod.GET, value = "/initProfitShar")
    @ApiResponse(code = 200, message = "成功", response = AjaxResult.class)
    private AjaxResult<ProfitSharInitResult> initProfitShar(HttpServletRequest request) {
        return iProfitSharService.initProfitShar(request);
    }

    @ApiOperation("分润管理-->机构分润配置-->子机构分润配置-->新增、修改")
    @RequestMapping(method = RequestMethod.POST, value = "/delSOrganSec")
    @ApiResponse(code = 200, message = "成功", response = AjaxResult.class)
    private AjaxResult delSOrganSec(@RequestBody String param, HttpServletRequest request) {
        try {
            ProfitSharSorgansecParam insertParam = JsonUtils.toBean(CenturyDecryptUtil.decryptForJson(param, request), ProfitSharSorgansecParam.class);
            return iProfitSharService.delSOrganSec(insertParam,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 = "/deleteSOrganSec")
    @ApiResponse(code = 200, message = "成功", response = AjaxResult.class)
    private AjaxResult deleteSOrganSec(Integer dividefeetype,Long marketid,Long subareagroupid,HttpServletRequest request) {
        return iProfitSharService.deleteSOrganSec(dividefeetype,marketid,subareagroupid,request);
    }
}
