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

import cn.hutool.core.convert.Convert;
import com.alibaba.fastjson.JSONObject;
import com.gic.api.base.commons.Page;
import com.gic.api.base.commons.ServiceResponse;
import com.gic.commons.util.PageHelperUtils;
import com.gic.commons.webapi.reponse.RestResponse;
import com.gic.enterprise.api.service.EnterpriseService;
import com.gic.haoban.base.api.common.pojo.dto.WebLoginDTO;
import com.gic.haoban.common.utils.AuthWebRequestUtil;
import com.gic.haoban.manage.api.dto.licence.AppOrderCheckDTO;
import com.gic.haoban.manage.api.dto.licence.AppOrderNoticeDTO;
import com.gic.haoban.manage.api.dto.licence.AppOrderPageDTO;
import com.gic.haoban.manage.api.dto.qdto.licence.AppOrderPageQDTO;
import com.gic.haoban.manage.api.dto.qdto.licence.AppOrderQDTO;
import com.gic.haoban.manage.api.service.WxEnterpriseApiService;
import com.gic.haoban.manage.api.service.licence.AppOrderApiService;
import com.gic.haoban.manage.web.errCode.HaoBanErrCode;
import com.gic.haoban.manage.web.qo.licence.AppOrderPageQO;
import com.gic.haoban.manage.web.qo.licence.AppOrderQO;
import com.gic.haoban.manage.web.vo.licence.AppOrderCheckVO;
import com.gic.haoban.manage.web.vo.licence.AppOrderNoticeVO;
import com.gic.haoban.manage.web.vo.licence.AppOrderPageVO;
import org.apache.commons.lang3.StringUtils;
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;

/**
 * @description:
 * @Author: wenhua
 * @Date: 2023/6/25 15:54
 */
@RestController
@RequestMapping("/app-order")
public class AppOrderController {

    private static Logger logger = LogManager.getLogger(AppOrderController.class);
    @Autowired
    private AppOrderApiService appOrderApiService;

    /**
     * 分页查询应用订单列表
     */
    @RequestMapping("app-order-page")
    public RestResponse<Page<AppOrderPageVO>> getAppOrderPage(@RequestBody AppOrderPageQO qo) {
        WebLoginDTO loginUser = AuthWebRequestUtil.getLoginUser();
        if (loginUser == null || StringUtils.isBlank(loginUser.getEnterpriseId())) {
            return RestResponse.failure(Convert.toStr(HaoBanErrCode.ERR_4.getCode()), HaoBanErrCode.ERR_4.getMsg());
        }
        AppOrderPageQDTO qdto = JSONObject.parseObject(JSONObject.toJSONString(qo), AppOrderPageQDTO.class);
        qdto.setWxEnterpriseId(loginUser.getWxEnterpriseId());
        Page<AppOrderPageDTO> page = appOrderApiService.getAppOrderPage(qdto).getResult();
        Page<AppOrderPageVO> result = PageHelperUtils.changePageToCurrentPage(page, AppOrderPageVO.class);
        return RestResponse.successResult(result);
    }

    /**
     * 新增订单
     */
    @RequestMapping("app-order-save")
    public RestResponse<Boolean> saveAppOrder(@RequestBody AppOrderQO qo) {
        WebLoginDTO loginUser = AuthWebRequestUtil.getLoginUser();
        if (loginUser == null || StringUtils.isBlank(loginUser.getEnterpriseId())) {
            return RestResponse.failure(Convert.toStr(HaoBanErrCode.ERR_4.getCode()), HaoBanErrCode.ERR_4.getMsg());
        }
        String wxEnterpriseId = loginUser.getWxEnterpriseId();
        String clerkName = loginUser.getClerkName();
        String clerkId = loginUser.getClerkId();

        AppOrderQDTO qdto = JSONObject.parseObject(JSONObject.toJSONString(qo), AppOrderQDTO.class);
        qdto.setWxEnterpriseId(wxEnterpriseId);
        qdto.setCreatorName(clerkName);
        qdto.setCreatorId(clerkId);
        ServiceResponse<Boolean> response = appOrderApiService.saveAppOrder(qdto);
        if (response.isSuccess()) {
            return RestResponse.successResult(true);
        }
        return RestResponse.failure(response.getCode(), response.getMessage());

    }

    /**
     * 取消订单
     */
    @RequestMapping("app-order-cancel")
    public RestResponse<Boolean> cancelAppOrder(@RequestParam Long orderId) {
        WebLoginDTO loginUser = AuthWebRequestUtil.getLoginUser();
        if (loginUser == null || StringUtils.isBlank(loginUser.getEnterpriseId())) {
            return RestResponse.failure(Convert.toStr(HaoBanErrCode.ERR_4.getCode()), HaoBanErrCode.ERR_4.getMsg());
        }
        ServiceResponse<Boolean> response = appOrderApiService.cancelAppOrder(orderId);
        if (response.isSuccess()) {
            return RestResponse.successResult(true);
        }
        return RestResponse.failure(response.getCode(), response.getMessage());
    }

    /**
     * 购买弹窗校验
     */
    @RequestMapping("app-order-check")
    public RestResponse<AppOrderCheckVO> checkAppOrder() {
        WebLoginDTO loginUser = AuthWebRequestUtil.getLoginUser();
        if (loginUser == null || StringUtils.isBlank(loginUser.getEnterpriseId())) {
            return RestResponse.failure(Convert.toStr(HaoBanErrCode.ERR_4.getCode()), HaoBanErrCode.ERR_4.getMsg());
        }
        ServiceResponse<AppOrderCheckDTO> response = appOrderApiService.checkAppOrder(loginUser.getWxEnterpriseId());
        AppOrderCheckVO vo = JSONObject.parseObject(JSONObject.toJSONString(response.getResult()), AppOrderCheckVO.class);
        return RestResponse.successResult(vo);
    }

    /**
     * 登录好办弹窗校验
     */
    @RequestMapping("app-order-notice")
    public RestResponse<AppOrderNoticeVO> noticeAppOrder() {
        WebLoginDTO loginUser = AuthWebRequestUtil.getLoginUser();
        if (loginUser == null || StringUtils.isBlank(loginUser.getEnterpriseId())) {
            return RestResponse.failure(Convert.toStr(HaoBanErrCode.ERR_4.getCode()), HaoBanErrCode.ERR_4.getMsg());
        }
        ServiceResponse<AppOrderNoticeDTO> response = appOrderApiService.noticeAppOrder(loginUser.getWxEnterpriseId());
        AppOrderNoticeVO vo = JSONObject.parseObject(JSONObject.toJSONString(response.getResult()), AppOrderNoticeVO.class);
        return RestResponse.successResult(vo);
    }


}
