package com.bweb.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.utils.ResponseMsgUtil;
import com.hfkj.model.ResponseData;
import com.hfkj.service.rebate.BsRebatePartMerService;
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.*;
import java.util.stream.Collectors;

@Controller
@Api(value = "返利金商户")
@RequestMapping(value = "/rebatePartMer")
public class BsRebatePartMerController {

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

    @Resource
    private BsRebatePartMerService rebatePartMerService;

    @RequestMapping(value="/addPartMer",method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "添加参与商户")
    public ResponseData addPartMer(@RequestBody JSONObject body) {
        try {
            if (body == null || body.getJSONArray("merNoArray") == null) {
                throw ErrorHelp.genException(SysCode.System, ErrorCode.REQ_PARAMS_ERROR, "");
            }

            List<String> merList = new ArrayList<>();
            for (Object obj: body.getJSONArray("merNoArray")) {
                JSONObject mer = (JSONObject) obj;
                if (StringUtils.isBlank(mer.getString("merNo"))) {
                    throw ErrorHelp.genException(SysCode.System, ErrorCode.REQ_PARAMS_ERROR, "");
                }
                merList.add(mer.getString("merNo"));
            }

            merList = merList.stream().distinct().collect(Collectors.toList());
            rebatePartMerService.addPartMer(merList);

            return ResponseMsgUtil.success("操作成功");

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

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

            rebatePartMerService.enable(body.getString("merNo"));

            return ResponseMsgUtil.success("操作成功");

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

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

            rebatePartMerService.disable(body.getString("merNo"));

            return ResponseMsgUtil.success("操作成功");

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

    @RequestMapping(value="/queryPartMerList",method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "获取参与商户列表")
    public ResponseData queryPartMerList(@RequestParam(value = "merNo" , required = false) String merNo,
                                         @RequestParam(value = "merName" , required = false) String merName,
                                         @RequestParam(value = "merAbbreviate" , required = false) String merAbbreviate,
                                         @RequestParam(value = "status" , required = false) Integer status,
                                         @RequestParam(value = "pageNum" , required = true) Integer pageNum,
                                         @RequestParam(value = "pageSize" , required = true) Integer pageSize) {

        try {
            Map<String,Object> param = new HashMap<>();
            param.put("merNo", merNo);
            param.put("merName", merName);
            param.put("merAbbreviate", merAbbreviate);
            param.put("status", status);

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

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

    @RequestMapping(value="/queryNotPartMerList",method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "获取未参加的商户列表")
    public ResponseData queryNotPartMerList(@RequestParam(value = "merNo" , required = false) String merNo,
                                            @RequestParam(value = "merName" , required = false) String merName,
                                            @RequestParam(value = "merAbbreviate" , required = false) String merAbbreviate,
                                            @RequestParam(value = "pageNum" , required = true) Integer pageNum,
                                            @RequestParam(value = "pageSize" , required = true) Integer pageSize) {

        try {
            Map<String,Object> param = new HashMap<>();
            param.put("merNo", merNo);
            param.put("merName", merName);
            param.put("merAbbreviate", merAbbreviate);

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

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


}
