package com.bweb.controller;

import com.alibaba.fastjson.JSONObject;
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.entity.BsPlatform;
import com.hfkj.model.ResponseData;
import com.hfkj.service.BsPlatformService;
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.HashMap;

@Controller
@RequestMapping(value = "/platform")
@Api(value = "渠道平台")
public class BsPlatformController {

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

    @Resource
    private BsPlatformService bsPlatformService;

    @RequestMapping(value = "/configPlatformSort", method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "配置平台排序")
    public ResponseData configPlatformSort(@RequestBody JSONObject body) {
        try {

            if (body == null
                    || body.getLong("id") == null
                    || body.getInteger("sort") == null
            ) {
                log.error("BsCompanyController --> selectCompanyList() error!", "参数错误");
                throw ErrorHelp.genException(SysCode.System, ErrorCode.REQ_PARAMS_ERROR, "");
            }

            BsPlatform platform = bsPlatformService.getDetail(body.getLong("id"));
            if (platform == null) {
                log.error("BsCompanyController --> selectCompanyList() error!", "参数错误");
                throw ErrorHelp.genException(SysCode.System, ErrorCode.REQ_PARAMS_ERROR, "");
            }
            platform.setSort(body.getInteger("sort"));
            bsPlatformService.editPlatform(platform);

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

        } catch (Exception e) {
            log.error("BsPlatformController --> configPlatformSort() error!", e);
            return ResponseMsgUtil.exception(e);
        }
    }

    @RequestMapping(value = "/getPlatformList", method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "查询商户平台号")
    public ResponseData getPlatformList() {
        try {

            return ResponseMsgUtil.success(bsPlatformService.getPlatformList(new HashMap<>()));

        } catch (Exception e) {
            log.error("BsPlatformController --> getPlatformList() error!", e);
            return ResponseMsgUtil.exception(e);
        }
    }


}
