package com.gic.haoban.manage.web.controller;


import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSONObject;
import com.gic.api.base.commons.Page;
import com.gic.api.base.commons.ServiceResponse;
import com.gic.authcenter.commons.dto.AuthcenterUserDetails;
import com.gic.authcenter.commons.util.SessionContextUtil;
import com.gic.commons.util.PageHelperUtils;
import com.gic.commons.webapi.reponse.RestResponse;
import com.gic.enterprise.api.dto.EnterpriseDTO;
import com.gic.enterprise.api.service.EnterpriseService;
import com.gic.haoban.manage.api.dto.WxEnterpriseDTO;
import com.gic.haoban.manage.api.dto.licence.LicenceOrderDTO;
import com.gic.haoban.manage.api.dto.licence.LicenceOrderPageDTO;
import com.gic.haoban.manage.api.dto.qdto.licence.LicenceOrderPageQDTO;
import com.gic.haoban.manage.api.service.WxEnterpriseApiService;
import com.gic.haoban.manage.api.service.licence.LicenceOrderApiService;
import com.gic.haoban.manage.web.errCode.HaoBanErrCode;
import com.gic.haoban.manage.web.qo.licence.LicenceOrderPageQO;
import com.gic.haoban.manage.web.vo.licence.LicenceOrderPageVO;
import com.gic.haoban.manage.web.vo.licence.LicenceOrderVO;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

/**
 * 企业微信许可账号购买
 *
 * @author jx
 * @since 2023-03-09
 */
@RestController
@RequestMapping("/operation-licence-order")
public class LicenceOrderController {

    private static Logger logger = LogManager.getLogger(LicenceOrderController.class);

    @Autowired
    private LicenceOrderApiService licenceOrderApiService;
    @Autowired
    private EnterpriseService enterpriseService;
    @Autowired
    private WxEnterpriseApiService wxEnterpriseApiService;

    /**
     * 查询订单详情
     */
    @RequestMapping("licence-order-detail")
    public RestResponse<LicenceOrderVO> getLicenceOrderDetail(@RequestParam Long orderId, HttpServletRequest request) {
        AuthcenterUserDetails loginUser = (AuthcenterUserDetails) SessionContextUtil.getUser(request);
        if (loginUser == null) {
            return RestResponse.failure(Convert.toStr(HaoBanErrCode.ERR_4.getCode()), HaoBanErrCode.ERR_4.getMsg());
        }
        ServiceResponse<LicenceOrderDTO> licenceOrderDetail = licenceOrderApiService.getLicenceOrderDetail(orderId, null);
        LicenceOrderVO licenceOrderVO = JSONObject.parseObject(JSONObject.toJSONString(licenceOrderDetail.getResult()), LicenceOrderVO.class);
        return RestResponse.successResult(licenceOrderVO);
    }

    /**
     * 分页查询订单列表
     */
    @RequestMapping("licence-order-page")
    public RestResponse<Page<LicenceOrderPageVO>> getLicenceOrderPage(@RequestBody LicenceOrderPageQO licenceOrderPageQO, HttpServletRequest request) {
        AuthcenterUserDetails loginUser = (AuthcenterUserDetails) SessionContextUtil.getUser(request);
        if (loginUser == null) {
            return RestResponse.failure(Convert.toStr(HaoBanErrCode.ERR_4.getCode()), HaoBanErrCode.ERR_4.getMsg());
        }
        LicenceOrderPageQDTO qdto = JSONObject.parseObject(JSONObject.toJSONString(licenceOrderPageQO), LicenceOrderPageQDTO.class);
        qdto.setType(0);
        ServiceResponse<Page<LicenceOrderPageDTO>> licenceOrderPage = licenceOrderApiService.getLicenceOrderPage(qdto);
        Page<LicenceOrderPageVO> result = PageHelperUtils.changePageToCurrentPage(licenceOrderPage.getResult(), LicenceOrderPageVO.class);
        if (CollUtil.isNotEmpty(result.getResult())) {
            for (LicenceOrderPageVO vo : result.getResult()) {
                //查询gic品牌名称
                EnterpriseDTO enterpriseDTO = enterpriseService.getEnterpriseById(vo.getEnterpriseId());
                if (ObjectUtil.isNotNull(enterpriseDTO)) {
                    vo.setEnterpriseName(enterpriseDTO.getEnterpriseName());
                }
                WxEnterpriseDTO wxEnterpriseDTO = wxEnterpriseApiService.getOne(vo.getWxEnterpriseId());
                if (ObjectUtil.isNotNull(wxEnterpriseDTO)) {
                    vo.setWxEnterpriseName(wxEnterpriseDTO.getCorpName());
                }
                vo.setNowTime(new Date());
            }
        }
        return RestResponse.successResult(result);
    }

    /**
     * 取消订单
     */
    @RequestMapping("licence-order-cancel")
    public RestResponse<Boolean> cancelLicenceOrder(@RequestParam Long orderId, HttpServletRequest request) {
        AuthcenterUserDetails loginUser = (AuthcenterUserDetails) SessionContextUtil.getUser(request);
        if (loginUser == null) {
            return RestResponse.failure(Convert.toStr(HaoBanErrCode.ERR_4.getCode()), HaoBanErrCode.ERR_4.getMsg());
        }
        ServiceResponse<Boolean> response = licenceOrderApiService.cancelQywxOrder(orderId);
        if (!response.isSuccess()) {
            return RestResponse.failure(response.getCode(), response.getMessage());
        }
        return RestResponse.successResult(true);
    }

    /**
     * 支付订单
     */
    @RequestMapping("licence-order-pay")
    public RestResponse<String> payLicenceOrder(@RequestParam Long orderId, HttpServletRequest request) {
        AuthcenterUserDetails loginUser = (AuthcenterUserDetails) SessionContextUtil.getUser(request);
        if (loginUser == null) {
            return RestResponse.failure(Convert.toStr(HaoBanErrCode.ERR_4.getCode()), HaoBanErrCode.ERR_4.getMsg());
        }
        ServiceResponse<String> response = licenceOrderApiService.payLicenceOrder(orderId);
        if (response.isSuccess()) {
            return RestResponse.successResult(response.getResult());
        }
        return RestResponse.failure(response.getCode(), response.getMessage());
    }

    /**
     * 审核订单
     *
     * @param orderId 订单id
     * @param flag    是否通过 0 否 1是
     * @param reason  不通过原因
     * @return
     */
    @RequestMapping("licence-order-check")
    public RestResponse<Boolean> checkLicenceOrder(@RequestParam Long orderId, Integer flag, String reason, HttpServletRequest request) {
        AuthcenterUserDetails loginUser = (AuthcenterUserDetails) SessionContextUtil.getUser(request);
        if (loginUser == null) {
            return RestResponse.failure(Convert.toStr(HaoBanErrCode.ERR_4.getCode()), HaoBanErrCode.ERR_4.getMsg());
        }
        String clerkName = loginUser.getLoginName();
        Boolean result = licenceOrderApiService.checkLicenceOrder(orderId, flag, reason, clerkName).getResult();
        return RestResponse.successResult(result);
    }

    /**
     * 订单编号和企业微信订单编号模糊搜索
     *
     * @param param 模糊查询
     * @param type  0 订单编号 1 企业微信订单编号 2微信订单编号 3付费应用企业微信订单编号
     * @return
     */
    @RequestMapping("get-order-id-list")
    public RestResponse<List<String>> getOrderIdList(String param, Integer type, HttpServletRequest request) {
        AuthcenterUserDetails loginUser = (AuthcenterUserDetails) SessionContextUtil.getUser(request);
        if (loginUser == null) {
            return RestResponse.failure(Convert.toStr(HaoBanErrCode.ERR_4.getCode()), HaoBanErrCode.ERR_4.getMsg());
        }
        ServiceResponse<List<String>> orderIdList = licenceOrderApiService.getOrderIdList(param, type, null);
        return RestResponse.successResult(orderIdList.getResult());
    }

    /**
     * 查询开户信息
     */
    @RequestMapping("get-account-info")
    public RestResponse<HashMap<String, String>> getAccountInfo(HttpServletRequest request) {
        AuthcenterUserDetails loginUser = (AuthcenterUserDetails) SessionContextUtil.getUser(request);
        if (loginUser == null) {
            return RestResponse.failure(Convert.toStr(HaoBanErrCode.ERR_4.getCode()), HaoBanErrCode.ERR_4.getMsg());
        }
        HashMap<String, String> result = licenceOrderApiService.getAccountInfo().getResult();
        return RestResponse.successResult(result);
    }

    /**
     * 申请退款
     */
    @RequestMapping("licence-order-wxRefoud")
    public RestResponse<Boolean> wxRefund(@RequestParam Long orderId, HttpServletRequest request) {
        AuthcenterUserDetails loginUser = (AuthcenterUserDetails) SessionContextUtil.getUser(request);
        if (loginUser == null) {
            return RestResponse.failure(Convert.toStr(HaoBanErrCode.ERR_4.getCode()), HaoBanErrCode.ERR_4.getMsg());
        }
        ServiceResponse<Boolean> response = licenceOrderApiService.wxRefund(orderId);
        if (response.isSuccess()){
            return RestResponse.successResult(true);
        }
        return RestResponse.failure(response.getCode(), response.getMessage());
    }
    /**
     * 获取当前系统时间
     */
    @RequestMapping("get-system-date")
    public RestResponse<Date> getSystemDate(HttpServletRequest request) {
        AuthcenterUserDetails loginUser = (AuthcenterUserDetails) SessionContextUtil.getUser(request);
        if (loginUser == null) {
            return RestResponse.failure(Convert.toStr(HaoBanErrCode.ERR_4.getCode()), HaoBanErrCode.ERR_4.getMsg());
        }
        return RestResponse.successResult(new Date());
    }



}

