package com.bweb.controller;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hai.common.exception.ErrorCode;
import com.hai.common.exception.ErrorHelp;
import com.hai.common.exception.SysCode;
import com.hai.common.security.UserCenter;
import com.hai.common.utils.ResponseMsgUtil;
import com.hai.entity.HighMerchant;
import com.hai.enum_type.MerStoreAmountSourceTypeEnum;
import com.hai.enum_type.UserObjectTypeEnum;
import com.hai.model.ResponseData;
import com.hai.model.UserInfoModel;
import com.hai.service.HighMerchantAccountRecordService;
import com.hai.service.HighMerchantService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.dao.DeadlockLoserDataAccessException;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping(value = "/merAccountRecord")
@Api(value = "商户门店接口")
public class HighMerchantAccountRecordController {

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

    @Resource
    private HighMerchantAccountRecordService merchantAccountRecordService;

    @Resource
    private HighMerchantService merchantService;

    @Resource
    private UserCenter userCenter;

    @RequestMapping(value = "/getRecordList", method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "查询记录列表")
    public synchronized ResponseData getRecordList(@RequestParam(name = "merId", required = false) Long merId,
                                                   @RequestParam(name = "pageNum", required = true) Integer pageNum,
                                                   @RequestParam(name = "pageSize", required = true) Integer pageSize) {
        try {
            UserInfoModel userInfoModel = userCenter.getSessionModel(UserInfoModel.class);
            if (userInfoModel == null) {
                throw ErrorHelp.genException(SysCode.System, ErrorCode.SEC_USER_EXPIRED, "");
            }

            Map<String, Object> param = new HashMap<>();
            if (userInfoModel.getSecUser().getObjectType().equals(UserObjectTypeEnum.type0.getType())) {
                param.put("merId", merId);

            } else if (userInfoModel.getSecUser().getObjectType().equals(UserObjectTypeEnum.type1.getType())) {

                if (merId == null) {
                    Map<String, Object> merMap = new HashMap<>();
                    merMap.put("companyId", userInfoModel.getBsCompany().getId());
                    List<HighMerchant> merchantList = merchantService.getMerchantList(merMap);

                    List<Long> merIdList = new ArrayList<>();
                    for (HighMerchant merchant : merchantList) {
                        merIdList.add(merchant.getId());
                    }
                    param.put("merIdList", merIdList);
                } else {
                    param.put("merId", merId);
                }

            } else if (userInfoModel.getSecUser().getObjectType().equals(UserObjectTypeEnum.type2.getType())) {
                param.put("merId", userInfoModel.getMerchant().getId());
            } else {
                throw ErrorHelp.genException(SysCode.System, ErrorCode.MERCHANT_NOT_AUTHORIZED, "");
            }
            param.put("type", 1);
            PageHelper.startPage(pageNum,pageSize);
            return ResponseMsgUtil.success(new PageInfo<>(merchantAccountRecordService.getRecordList(param)));

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