package com.bweb.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.SessionObject;
import com.hai.common.security.UserCenter;
import com.hai.common.utils.ResponseMsgUtil;
import com.hai.entity.HighOrder;
import com.hai.entity.HighOrderPre;
import com.hai.entity.HighRefundAudit;
import com.hai.entity.OutRechargeOrder;
import com.hai.model.HighAgentModel;
import com.hai.model.ResponseData;
import com.hai.model.UserInfoModel;
import com.hai.service.HighOrderPreService;
import com.hai.service.HighOrderService;
import com.hai.service.HighRefundAuditService;
import com.hai.service.OutRechargeOrderService;
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.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

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

/**
 * @Auther: 袁野
 * @Description:
 * @Date: 2021/3/26 23:08
 */
@Controller
@RequestMapping(value = "/highRefundAudit")
@Api(value = "退款审核接口")
public class HighRefundAuditController {

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

    @Resource
    private HighRefundAuditService highRefundAuditService;

    @Resource
    private OutRechargeOrderService outRechargeOrderService;

    @Resource
    private HighOrderService highOrderService;

    @Resource
    private HighOrderPreService highOrderPreService;

    @Autowired
    private UserCenter userCenter;


    @RequestMapping(value = "/getOrderList", method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "获取审核")
    public ResponseData getOrderList(
            @RequestParam(name = "status", required = false) Integer status,
            @RequestParam(name = "orderNo", required = false) String orderNo,
            @RequestParam(name = "payType", required = false) Integer payType,
            @RequestParam(name = "payTimeS", required = false) Long payTimeS,
            @RequestParam(name = "payTimeE", required = false) Long payTimeE,
            @RequestParam(name = "pageNum", required = true) Integer pageNum,
            @RequestParam(name = "pageSize", required = true) Integer pageSize,
            HttpServletRequest request) {
        try {

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

            SessionObject sessionObject = userCenter.getSessionObject(request);
            UserInfoModel userInfoModel = (UserInfoModel) sessionObject.getObject();

            if (userInfoModel.getBsCompany() == null) {
                log.error("HighCouponController -> getCouponList() error!","权限不足");
                throw ErrorHelp.genException(SysCode.System, ErrorCode.COMPETENCE_INSUFFICIENT, "");
            }

            map.put("companyId", String.valueOf(userInfoModel.getBsCompany().getId()));

            if (status != null) {
                map.put("status", status.toString());
            }
            if (StringUtils.isNotBlank(orderNo)) {
                map.put("orderNo", orderNo);
            }

            if (payType != null) {
                map.put("payType" , payType.toString());
            }

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

            PageHelper.startPage(pageNum,pageSize);
            return ResponseMsgUtil.success(new PageInfo<>(highRefundAuditService.getListRefundAudit(map)));

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

    @RequestMapping(value = "/insertRefundAudit", method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "新增退款审核")
    public ResponseData insertRefundAudit(@RequestBody HighRefundAudit highRefundAudit, HttpServletRequest request) {
        try {
            //发布人员
            SessionObject sessionObject = userCenter.getSessionObject(request);
            UserInfoModel userInfoModel = (UserInfoModel) sessionObject.getObject();
            if (userInfoModel.getBsCompany() == null) {
                log.error("HighAgentController -> insertAgent() error!","该主角色没有权限");
                throw ErrorHelp.genException(SysCode.System, ErrorCode.MENU_TREE_HAS_NOT_ERROR, "");
            }

            if (StringUtils.isBlank(highRefundAudit.getSourceOrderNo()) ||
                    highRefundAudit.getRefundSource() == null
            ) {
                log.error("HighAgentController -> insertAgent() error!","参数错误");
                throw ErrorHelp.genException(SysCode.System, ErrorCode.REQ_PARAMS_ERROR, "");
            }

            highRefundAuditService.insertRefundAudit(highRefundAudit , userInfoModel);

            return ResponseMsgUtil.success("新增成功");

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

    @RequestMapping(value = "/getRefundReject", method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "拒绝退款")
    public ResponseData getRefundReject(@RequestParam(name = "id", required = true) Integer id) {
        try {

            HighRefundAudit highRefundAudit = highRefundAuditService.getRefundAuditById(id);

            if (highRefundAudit.getRefundSource() == 1) {
                OutRechargeOrder outRechargeOrder =  outRechargeOrderService.findByOrderNo(highRefundAudit.getSourceOrderNo());
//                outRechargeOrder.setStatus(2);
                outRechargeOrderService.updateOrder(outRechargeOrder);
            } else {
                HighOrder highOrder = highOrderService.getOrderByOrderNo(highRefundAudit.getSourceOrderNo());
                highOrder.setOrderStatus(2);
                highOrderService.updateOrder(highOrder);
                HighOrderPre highOrderPre = highOrderPreService.findByRefund(highRefundAudit.getSourceOrderNo());
                highOrderPre.setStatus(4);
                highOrderPre.setExt1("退款驳回");
                highOrderPreService.updateOrderPre(highOrderPre);
            }



            highRefundAudit.setStatus(2);
            highRefundAuditService.updateRefundAudit(highRefundAudit);

            return ResponseMsgUtil.success(highRefundAuditService.getRefundAuditById(id));

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

    @RequestMapping(value = "/getRefundAuditById", method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "根据id查询详情")
    public ResponseData getRefundAuditById(@RequestParam(name = "id", required = true) Integer id) {
        try {

            return ResponseMsgUtil.success(highRefundAuditService.getRefundAuditById(id));

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




}
