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.security.UserCenter;
import com.hfkj.common.utils.PageUtil;
import com.hfkj.common.utils.ResponseMsgUtil;
import com.hfkj.entity.*;
import com.hfkj.model.ResponseData;
import com.hfkj.model.SecUserSessionObject;
import com.hfkj.service.agent.BsAgentApiAccountRecordService;
import com.hfkj.service.agent.BsAgentApiAccountService;
import com.hfkj.service.agent.BsAgentDiscountService;
import com.hfkj.service.discount.BsDiscountPkService;
import com.hfkj.service.discount.BsDiscountService;
import com.hfkj.sysenum.SecUserObjectTypeEnum;
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;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Controller
@RequestMapping(value = "/agentApiAccount")
@Api(value = "优惠券管理")
public class BsAgentApiAccountController {
    private static Logger log = LoggerFactory.getLogger(BsAgentApiAccountController.class);
    @Resource
    private BsAgentApiAccountService agentApiAccountService;
    @Resource
    private BsAgentApiAccountRecordService agentApiAccountRecordService;
    @Resource
    private UserCenter userCenter;

    @RequestMapping(value = "/getAccount", method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "查询账户")
    public ResponseData getAccount(@RequestParam(value = "agentId", required = false) Long agentId) {
        try {
            SecUserSessionObject userSession = userCenter.getSessionModel(SecUserSessionObject.class);
            if (SecUserObjectTypeEnum.type1.getCode() == userSession.getAccount().getObjectType()) {

            } else if (SecUserObjectTypeEnum.type4.getCode() == userSession.getAccount().getObjectType()) {
                agentId = userSession.getAccount().getObjectId();;

            } else {
                throw ErrorHelp.genException(SysCode.System, ErrorCode.ROLE_NOT_PERMISSIONS, "");
            }
            // 查询账户
            return ResponseMsgUtil.success(agentApiAccountService.getAccount(agentId));

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

    @RequestMapping(value = "/recharge", method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "充值")
    public ResponseData recharge(@RequestBody JSONObject body) {
        try {
            SecUserSessionObject userSession = userCenter.getSessionModel(SecUserSessionObject.class);
            if (SecUserObjectTypeEnum.type1.getCode() != userSession.getAccount().getObjectType()) {
                throw ErrorHelp.genException(SysCode.System, ErrorCode.ROLE_NOT_PERMISSIONS, "");
            }
            if (body == null || body.getLong("agentId") == null || body.getBigDecimal("amount") == null) {
                throw ErrorHelp.genException(SysCode.System, ErrorCode.REQ_PARAMS_ERROR, "");
            }

            // 账户充值
            agentApiAccountService.recharge(body.getLong("agentId"), body.getBigDecimal("amount"));

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

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


    @RequestMapping(value = "/getRecordList", method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "记录列表")
    public ResponseData getRecordList(@RequestParam(value = "agentId", required = false) Long agentId,
                                      @RequestParam(value = "type", required = false) Integer type,
                                      @RequestParam(value = "sourceType", required = false) Integer sourceType,
                                      @RequestParam(value = "sourceOrderNo", required = false) String sourceOrderNo,
                                      @RequestParam(value = "createTimeS", required = false) Long createTimeS,
                                      @RequestParam(value = "createTimeE", required = false) Long createTimeE,
                                      @RequestParam(value = "pageNum", required = true) Integer pageNum,
                                      @RequestParam(value = "pageSize", required = true) Integer pageSize) {
        try {
            SecUserSessionObject userSession = userCenter.getSessionModel(SecUserSessionObject.class);
            if (SecUserObjectTypeEnum.type1.getCode() == userSession.getAccount().getObjectType()) {

            } else if (SecUserObjectTypeEnum.type4.getCode() == userSession.getAccount().getObjectType()) {
                agentId = userSession.getAccount().getObjectId();;

            } else {
                throw ErrorHelp.genException(SysCode.System, ErrorCode.ROLE_NOT_PERMISSIONS, "");
            }
            // 查询账户
            BsAgentApiAccount account = agentApiAccountService.getAccount(agentId);
            if (account == null) {
                throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "未找到账户");
            }
            Map<String,Object> param = new HashMap<>();
            param.put("agentApiAccountId", account.getId());
            param.put("type", type);
            param.put("sourceType", sourceType);
            param.put("sourceOrderNo", sourceOrderNo);
            param.put("createTimeS", createTimeS);
            param.put("createTimeE", createTimeE);

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

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

}
