package com.order.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.ResponseMsgUtil;
import com.hfkj.entity.BsOrderRefund;
import com.hfkj.model.ResponseData;
import com.hfkj.model.SecUserSessionObject;
import com.hfkj.model.UserSessionObject;
import com.hfkj.model.order.OrderChildModel;
import com.hfkj.model.order.OrderModel;
import com.hfkj.service.order.BsOrderChildService;
import com.hfkj.service.order.BsOrderRefundService;
import com.hfkj.service.order.BsOrderService;
import com.hfkj.sysenum.SecUserObjectTypeEnum;
import com.hfkj.sysenum.UserStatusEnum;
import com.hfkj.sysenum.order.OrderChildProductTypeEnum;
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;

/**
 * @className: OrderController
 * @author: HuRui
 * @date: 2024/4/30
 **/
@Controller
@RequestMapping(value="/refund")
@Api(value="订单退款业务")
public class OrderRefundController {

    Logger log = LoggerFactory.getLogger(OrderRefundController.class);
    @Resource
    private UserCenter userCenter;
    @Resource
    private BsOrderRefundService orderRefundService;
    @Resource
    private BsOrderChildService orderChildService;

    @RequestMapping(value="/tradeOrder",method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "退款交易订单")
    public ResponseData tradeOrder(@RequestBody JSONObject body) {
        try {
            // 用户session
            UserSessionObject userSession = userCenter.getSessionModel(UserSessionObject.class);
            if (userSession == null) {
                throw ErrorHelp.genException(SysCode.System, ErrorCode.ACCOUNT_LOGIN_NOT, "");
            }
            if (body == null || StringUtils.isBlank(body.getString("orderNo"))) {
                throw ErrorHelp.genException(SysCode.System, ErrorCode.REQ_PARAMS_ERROR, "");
            }

            return ResponseMsgUtil.success(orderRefundService.tradeOrderRefund(body.getString("orderNo"),body.getString("remark")));

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

    @RequestMapping(value="/tradeOrderChild",method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "退款交易子订单")
    public ResponseData tradeOrderChild(@RequestBody JSONObject body) {
        try {
            // 用户session
            UserSessionObject userSession = userCenter.getSessionModel(UserSessionObject.class);
            if (userSession == null) {
                throw ErrorHelp.genException(SysCode.System, ErrorCode.ACCOUNT_LOGIN_NOT, "");
            }
            if (!userSession.getUser().getStatus().equals(UserStatusEnum.status1.getCode())) {
                throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "该账户已被禁用，无法进行交易");
            }
            if (body == null
                    || StringUtils.isBlank(body.getString("childOrderNo"))
                    || body.getInteger("productCount") == null) {
                throw ErrorHelp.genException(SysCode.System, ErrorCode.REQ_PARAMS_ERROR, "");
            }

            return ResponseMsgUtil.success(orderRefundService.tradeOrderChildRefund(
                    body.getString("childOrderNo"),
                    body.getInteger("productCount"),
                    body.getString("remark")
            ));

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

    @RequestMapping(value="/queryRefund",method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "查询退款订单")
    public ResponseData queryRefund(@RequestParam(value = "refundOrderNo" , required = false) String refundOrderNo) {
        try {
            if (StringUtils.isBlank(refundOrderNo)) {
                throw ErrorHelp.genException(SysCode.System, ErrorCode.REQ_PARAMS_ERROR, "");
            }
            // 查询退款
            BsOrderRefund refund = orderRefundService.getRefund(refundOrderNo);
            if (refund == null) {
                throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "未知的退款");
            }

            Map<String,Object> map = new HashMap<>();
            map.put("refund", refund);
            map.put("orderChild", orderChildService.getDetail(refund.getOrderChildNo()));
            return ResponseMsgUtil.success(map);

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

    @RequestMapping(value="/queryList",method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "查询退款订单列表")
    public ResponseData queryList(@RequestParam(value = "merId" , required = false) Long merId,
                                  @RequestParam(value = "userPhone" , required = false) String userPhone,
                                  @RequestParam(value = "orderNo" , required = false) String orderNo,
                                  @RequestParam(value = "orderChildNo" , required = false) String orderChildNo,
                                  @RequestParam(value = "refundOrderNo" , required = false) String refundOrderNo,
                                  @RequestParam(value = "refundStatus" , required = false) Integer refundStatus,
                                  @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 {
            // 用户session
            SecUserSessionObject userSession = userCenter.getSessionModel(SecUserSessionObject.class);
            if (userSession == null) {
                throw ErrorHelp.genException(SysCode.System, ErrorCode.ACCOUNT_LOGIN_NOT, "");
            }

            Map<String,Object> param = new HashMap<>();
            if (userSession.getAccount().getObjectType().equals(SecUserObjectTypeEnum.type1.getCode())) {
                param.put("merId", merId);
            } else if (userSession.getAccount().getObjectType().equals(SecUserObjectTypeEnum.type2.getCode())) {
                param.put("merId", userSession.getAccount().getObjectId());
            }else {
                throw ErrorHelp.genException(SysCode.System, ErrorCode.ROLE_NOT_PERMISSIONS, "");
            }
            param.put("userPhone", userPhone);
            param.put("orderNo", orderNo);
            param.put("orderChildNo", orderChildNo);
            param.put("refundOrderNo", refundOrderNo);
            param.put("refundStatus", refundStatus);
            param.put("createTimeS", createTimeS);
            param.put("createTimeE", createTimeE);

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

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


}
