package com.hc.web.controller.browser.third;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.hc.business.model.Orders;
import com.hc.business.model.UserInvoices;
import com.hc.business.service.OrdersService;
import com.hc.business.service.ThirdCourseService;
import com.hc.business.service.UserInvoicesService;
import com.hc.core.utils.result.Result;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * 开票
 *
 * @author java
 * @since 2020-06-17 17:29:30
 */

@RestController
@RequestMapping("/new/api/invoice")
public class InvoiceController {

    private final UserInvoicesService userInvoicesService;

    private final OrdersService ordersService;

    /**
     * 服务对象
     */
    private final ThirdCourseService thirdCourseService;

    public InvoiceController(UserInvoicesService userInvoicesService, OrdersService ordersService, ThirdCourseService thirdCourseService) {
        this.userInvoicesService = userInvoicesService;
        this.ordersService = ordersService;
        this.thirdCourseService = thirdCourseService;
    }

    /**
     * @Author ymnets
     * @Description  回传结果接口
     * @return com.hc.core.utils.result.Result
     **/
    @PostMapping("/scan")
    @ApiOperation(value = "回传开票结果接口", response = Result.class)
    public Result scan(HttpServletRequest request) {
        String operater = request.getParameter("operater");

        if(operater.equals("callback"))//回传开票结果接口
        {
            return  callback(request);
        }else if(operater=="invoiceInvalid")//回传发票作废结果接口
        {
            return  invoiceInvalid(request);
        }
        else if(operater=="invoiceApply")//开票申请结果数据回传
        {
            return  invoiceApply(request);
        }
        return Result.success(operater);
    }

// 回传开票结果接口
// 1、开票完成，可根据该接口定义的形式接收发票数据，内容包括（订单编号、开票时间、发票代码、发票号码、价税合计、下载url地址等）。
// 2、企业资质-开票失败是否回调 为“是” 时返回 发票状态 字段，并可以返回开票失败/签章失败的发票
// 3、企业资质-开票失败是否回调 为“否” 时返回 不返回发票状态 字段，默认回传开票完成的发票
    private  Result callback(HttpServletRequest request)
    {
        String content = request.getParameter("content");//开票信息（json字符串格式）
        JSONObject object = JSON.parseObject(content);
        String c_status = object.getString("c_status");//发票状态（1：开票完成、2：开票失败、3：开票成功签章失败（电票时））
        String c_errorMessage = object.getString("c_errorMessage");//发票代码
        String c_fpdm = object.getString("c_fpdm");//发票代码
        String c_fphm = object.getString("c_fphm");//发票号码
        String c_remark = object.getString("c_remark");//对应正数发票代码
        String c_orderno = object.getString("c_orderno");//订单号
        String c_fpqqlsh = object.getString("c_fpqqlsh");//发票流水号
        String c_url = object.getString("c_url");//发票pdf地址
        String c_jpg_url = object.getString("c_jpg_url");//发票流水号
        String c_paper_pdf_url = object.getString("c_paper_pdf_url");//纸票pdf地址（含底图）
        String c_ofd_url = object.getString("c_ofd_url");//c_ofd_url

        UserInvoices userInvoices = userInvoicesService.queryBySyncOrderNo(c_orderno);
        if(userInvoices!=null)
        {
            //作废中
            if(userInvoices.getSyncStatus().equals(50)) {
                if(c_status.equals("1")) {
                    userInvoices.setSyncStatus(90);
                }else if(c_status.equals("2")) {
                    userInvoices.setSyncStatus(80);
                }
                else if(c_status.equals("3")) {
                    userInvoices.setSyncStatus(100);
                }
                userInvoices.setSyncUrlRed(c_url);
                userInvoices.setTaxNoRed(c_fphm);

                //作废成功后去掉订单的账单
                if(userInvoices.getSyncStatus()==90) {
                    Orders orders = ordersService.queryById(userInvoices.getOrderId());
                    orders.setUserInvoicesId(0);
                    ordersService.update(orders);
                }
                userInvoices.setTaxCode(c_fpdm);
                userInvoices.setRemarksSign(c_remark);
                userInvoices.setSyncMsg(c_errorMessage);
                userInvoicesService.update(userInvoices);
            }else  if(!userInvoices.getSyncStatus().equals(90) && !userInvoices.getSyncStatus().equals(80) && !userInvoices.getSyncStatus().equals(100)) { //正常开票
                if (c_status.equals("1")) {
                    userInvoices.setSyncStatus(20);
                } else if (c_status.equals("2")) {
                    userInvoices.setSyncStatus(30);
                } else if (c_status.equals("3")) {
                    userInvoices.setSyncStatus(40);
                }
                userInvoices.setSyncUrl(c_url);
                userInvoices.setTaxNo(c_fphm);
                userInvoices.setTaxCode(c_fpdm);
                userInvoices.setRemarksSign(c_remark);
                userInvoices.setSyncMsg(c_errorMessage);
                userInvoicesService.update(userInvoices);
            }




        }


        return Result.success(request);
    }

    //作废
    private  Result cancelCallback(HttpServletRequest request)
    {
        String content = request.getParameter("content");//开票信息（json字符串格式）
        JSONObject object = JSON.parseObject(content);
        String c_status = object.getString("c_status");//发票状态（1：开票完成、2：开票失败、3：开票成功签章失败（电票时））
        String c_errorMessage = object.getString("c_errorMessage");//发票代码
        String c_fpdm = object.getString("c_fpdm");//发票代码
        String c_fphm = object.getString("c_fphm");//发票号码
        String c_remark = object.getString("c_remark");//对应正数发票代码
        String c_orderno = object.getString("c_orderno");//订单号
        String c_fpqqlsh = object.getString("c_fpqqlsh");//发票流水号
        String c_url = object.getString("c_url");//发票pdf地址
        String c_jpg_url = object.getString("c_jpg_url");//发票流水号
        String c_paper_pdf_url = object.getString("c_paper_pdf_url");//纸票pdf地址（含底图）
        String c_ofd_url = object.getString("c_ofd_url");//c_ofd_url
        String c_hjje = object.getString("c_hjje");//合计，红票是负数
        UserInvoices userInvoices = userInvoicesService.queryByFPCode(c_fpdm);
        if(userInvoices!=null)
        {
            if(c_status.equals("1")) {
                userInvoices.setSyncStatus(90);
            }else if(c_status.equals("2")) {
                userInvoices.setSyncStatus(80);
            }
            else if(c_status.equals("3")) {
                userInvoices.setSyncStatus(100);
            }
            userInvoices.setSyncUrl(c_url);
            userInvoices.setSyncPaperUrl(c_paper_pdf_url);
            userInvoices.setSyncOfdUrl(c_ofd_url);
            userInvoices.setTaxCode(c_fpdm);
            userInvoices.setTaxNo(c_fphm);
            userInvoices.setRemarksSign(c_remark);
            userInvoices.setSyncMsg(c_errorMessage);
            userInvoicesService.update(userInvoices);
        }


        return Result.success(request);
    }

// 回传发票作废结果接口
//1、企业资质-作废结果是否回调 为“是” 时，当作废完成时返回 作废结果
//2、发票作废请求完成，可根据该接口定义的形式接收发票作废状态，内容包括（发票流水号、发票代码、号码、作废时间、作废状态（成功/失败：失败返回失败原因））。
//            3、支持A9+开票服务器类型的作废结果回调
    private  Result invoiceInvalid(HttpServletRequest request)
    {
        return Result.success(request);
    }

//开票申请结果数据回传
//用于扫码开票，C端消费者发起开票请求申请后，返回开票申请结果：申请成功或者失败
    private  Result invoiceApply(HttpServletRequest request)
    {
        return Result.success(request);
    }
}