package com.bweb.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hai.common.security.SessionObject;
import com.hai.common.security.UserCenter;
import com.hai.common.utils.ResponseMsgUtil;
import com.hai.model.HighUserModel;
import com.hai.model.ResponseData;
import com.hai.service.BsDistributionUserRelService;
import com.hai.service.HighOrderService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping(value = "/bsDistributionUserRel")
@Api(value = "分销人员")
public class BsDistributionUserRelController {

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

    @Autowired
    private UserCenter userCenter;

    @Resource
    private HighOrderService highOrderService;

    @Resource
    private BsDistributionUserRelService bsDistributionUserRelService;

    @RequestMapping(value = "/getDistributionAgent", method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "查询分销代理商")
    public ResponseData getDistributionAgent() {
        try {

            return ResponseMsgUtil.success(bsDistributionUserRelService.getDistributionAgent());

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

    @RequestMapping(value = "/getDistributionFirst", method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "查询分销一级")
    public ResponseData getDistributionFirst(@RequestParam(name = "agentId", required = false) Long agentId) {
        try {

            return ResponseMsgUtil.success(bsDistributionUserRelService.getDistributionFirst(agentId));

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

    @RequestMapping(value = "/getDistributionSecond", method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "查询分销二级")
    public ResponseData getDistributionSecond(@RequestParam(name = "agentId", required = false) Long agentId,
                                             @RequestParam(name = "popularizeUserId", required = false) Long popularizeUserId) {
        try {

            return ResponseMsgUtil.success(bsDistributionUserRelService.getDistributionSecond(agentId , popularizeUserId));

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

    @RequestMapping(value = "/getDistributionIntegralList", method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "查询分销积分记录")
    public ResponseData getDistributionIntegralList(@RequestParam(name = "title", required = false) String title,
                                              @RequestParam(name = "type", required = false) Integer type,
                                              @RequestParam(name = "orderNo", required = false) String orderNo,
                                              @RequestParam(name = "agentId", required = false) Long agentId,
                                              @RequestParam(name = "popularizeUserId", required = false) Long popularizeUserId,
                                              @RequestParam(name = "userName", required = false) String userName,
                                              @RequestParam(name = "createTimeS", required = false) Long createTimeS,
                                              @RequestParam(name = "createTimeE", required = false) Long createTimeE,
                                              @RequestParam(name = "pageNum", required = true) Integer pageNum,
                                              @RequestParam(name = "pageSize", required = true) Integer pageSize
                                                    ) {
        try {

            Map<String , Object> map = new HashMap<>();

            map.put("title", title);
            map.put("type", type);
            map.put("orderNo", orderNo);
            map.put("agentId", agentId);
            map.put("popularizeUserId", popularizeUserId);
            map.put("userName", userName);

            if (createTimeS != null && createTimeE != null) {
                map.put("createTimeS", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(createTimeS));
                map.put("createTimeE", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(createTimeE));
            }

            PageHelper.startPage(pageNum, pageSize);
            return ResponseMsgUtil.success(new PageInfo<>(bsDistributionUserRelService.getDistributionIntegralList(map)));
        } catch (Exception e) {
            log.error("HighOrderController --> getOrderById() error!", e);
            return ResponseMsgUtil.exception(e);
        }
    }

    @RequestMapping(value = "/agentUserList", method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "查询代理商用户")
    public ResponseData agentUserList() {
        try {

            return ResponseMsgUtil.success(bsDistributionUserRelService.agentUserList());

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

    @RequestMapping(value = "/orderListByAgentId", method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "B端查询代理商订单")
    public ResponseData orderListByAgentId(@RequestParam(name = "agentId", required = false) Long agentId,
                                         @RequestParam(name = "pageNum", required = true) Integer pageNum,
                                         @RequestParam(name = "pageSize", required = true) Integer pageSize) {
        try {

            Map<String , Object> map = new HashMap<>();
            map.put("agentId" , agentId);

            PageHelper.startPage(pageNum,pageSize);
            return ResponseMsgUtil.success(new PageInfo<>(highOrderService.orderListByAgentId(map)));
        } catch (Exception e) {
            log.error("HighOrderController --> getBackendToken() error!", e);
            return ResponseMsgUtil.exception(e);
        }
    }
}
