package com.web.controller;

import com.alibaba.fastjson.JSONObject;
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.RedisUtil;
import com.hai.common.utils.ResponseMsgUtil;
import com.hai.config.HuiLianTongUnionCardConfig;
import com.hai.entity.HighOilCard;
import com.hai.entity.HighUserCard;
import com.hai.enum_type.UserCardType;
import com.hai.model.HighUserModel;
import com.hai.model.ResponseData;
import com.hai.service.HighOilCardService;
import com.hai.service.HighUserCardService;
import com.hai.service.HighUserService;
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.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @className: CoresController
 * @author: HuRui
 * @date: 2022/10/21
 **/

@Controller
@RequestMapping(value = "/")
@Api(value = "用户核心业务")
public class UserController {

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

    @Resource
    private RedisUtil redisUtil;

    @Resource
    private HighUserService userService;

    @Resource
    private HighUserCardService userCardService;

    @Resource
    private HighOilCardService oilCardService;

    @Resource
    private UserCenter userCenter;

    @RequestMapping(value = "/getUserDetail", method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "获取用户详情")
    public ResponseData getUserDetail(HttpServletRequest request, HttpServletResponse response) {
        try {
            HighUserModel userInfoModel = userCenter.getSessionModel(HighUserModel.class);
            if (userInfoModel == null) {
                log.error("CoresController --> outLogin() error!", "用户身份错误或已过期");
                throw ErrorHelp.genException(SysCode.System, ErrorCode.SEC_USER_EXPIRED, "");
            }
            return ResponseMsgUtil.success(userService.getDetailDataByUser(userInfoModel.getHighUser().getId()));

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

    @RequestMapping(value = "/getUserAccount", method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "获取用户账户余额")
    public ResponseData getUserAccount(HttpServletRequest request, HttpServletResponse response) {
        try {
            HighUserModel userInfoModel = userCenter.getSessionModel(HighUserModel.class);
            if (userInfoModel == null) {
                log.error("CoresController --> outLogin() error!", "用户身份错误或已过期");
                throw ErrorHelp.genException(SysCode.System, ErrorCode.SEC_USER_EXPIRED, "");
            }

            Map<String, Object> map = new HashMap<>();
            map.put("integral", userService.findByUserId(userInfoModel.getHighUser().getId()).getGold());
            map.put("oilCardPrice", 0);
            map.put("hltCardPrice", 0);

            // 嗨森逛油卡
            List<HighUserCard> oilCardList = userCardService.getListByUser(userInfoModel.getHighUser().getId(), UserCardType.type2.getType());
            if (oilCardList.size() > 0) {
                // 查询油卡
                HighOilCard oilCard = oilCardService.getOilCardByCardNo(oilCardList.get(0).getCardNo());
                if (oilCard != null) {
                    map.put("oilCardPrice", oilCard.getAmount());
                }
            }

            // 贵州汇联通工会卡
            List<HighUserCard> hltCardList = userCardService.getListByUser(userInfoModel.getHighUser().getId(), UserCardType.type1.getType());
            if (hltCardList.size() > 0) {
                // 查询工会卡
                JSONObject cardInfo = HuiLianTongUnionCardConfig.queryBalance(hltCardList.get(0).getCardNo());
                if (cardInfo.getInteger("respCode").equals(0000)) {
                    // 解密响应参数
                    JSONObject resolveResponse = HuiLianTongUnionCardConfig.resolveResponse(cardInfo.getString("data"));
                    map.put("hltCardPrice", resolveResponse.getBigDecimal("balance"));
                }
            }
            return ResponseMsgUtil.success(map);

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

}
