package com.cweb.controller;

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.HighOilCard;
import com.hai.enum_type.OilCardStatusEnum;
import com.hai.model.ResponseData;
import com.hai.model.UserInfoModel;
import com.hai.service.HighCompanyTwoPwdService;
import com.hai.service.HighOilCardRecordService;
import com.hai.service.HighOilCardService;
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.Map;

@Controller
@RequestMapping(value = "/oilCard")
@Api(value = "油卡")
public class HighOilCardController {

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

    @Resource
    private UserCenter userCenter;

    @Resource
    private HighOilCardService oilCardService;

    @Resource
    private HighOilCardRecordService oilCardRecordService;

    @RequestMapping(value = "/getDetailByCardNo", method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "根据卡号查询详情")
    public ResponseData getDetailByCardNo(@RequestParam(name = "cardNo", required = true) String cardNo) {
        try {
            // 查询油卡
            HighOilCard card = oilCardService.getOilCardByCardNo(cardNo);
            if (card == null) {
                throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "卡号不正确");
            }
            if (StringUtils.isBlank(card.getContactPhone())) {
                throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "油卡未配置完成");
            }

            Map<String, Object> map = new HashMap<>();
            map.put("cardNo", card.getCardNo());
            map.put("amount", card.getAmount());
            map.put("status", card.getStatus());
            map.put("contactName", card.getContactName());
            map.put("contactPhone", card.getContactPhone());
            return ResponseMsgUtil.success(map);

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

    @RequestMapping(value = "/getOilCardRecordList", method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "查询油卡记录列表")
    public ResponseData getOilCardRecordList(@RequestParam(name = "cardNo", required = true) String cardNo,
                                             @RequestParam(name = "sourceOrderNo", required = false) String sourceOrderNo,
                                             @RequestParam(name = "type", required = false) Integer type,
                                             @RequestParam(name = "sourceType", required = false) String sourceType,
                                             @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> param = new HashMap<>();
            param.put("cardNo", cardNo);
            param.put("sourceOrderNo", sourceOrderNo);
            param.put("type", type);
            param.put("sourceType", sourceType);
            param.put("createTimeS", createTimeS);
            param.put("createTimeE", createTimeE);

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

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