package com.dxhy.passengerticket.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.dxhy.api.model.invoice.TDxInvoiceReimburse;
import com.dxhy.common.controller.AbstractController;
import com.dxhy.common.utils.DateUtils;
import com.dxhy.common.utils.R;
import com.dxhy.passengerticket.entity.TdxRecordInvoiceDetail;
import com.dxhy.passengerticket.entity.TdxVehicleSaleInvoice;
import com.dxhy.passengerticket.service.PassengerReimbursementSystemService;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author xiexuedi
 * @date 2019/05/10 08:59:44
 */
@SuppressWarnings("AlibabaUndefineMagicConstant")
@RestController
@RequestMapping("reimbursementSystem")
public class ReimbursementSystemController extends AbstractController {
    @Resource
    private PassengerReimbursementSystemService passengerReimbursementSystemService;

    @RequestMapping(value = "insertReimbursementSystem", method = RequestMethod.POST)
    public ResponseEntity<R> insertReimbursementSystem(@RequestBody String reimbursementData) {
        if ("".equals(reimbursementData) || reimbursementData == null) {
            return ResponseEntity.ok(R.error("发票信息不能为空"));
        }
        JSONObject jsonObject = JSON.parseObject(reimbursementData);
        JSONObject jstr = jsonObject.getJSONObject("reimbursementData");
        // 解析获取发票主题
        TDxInvoiceReimburse vo = JSON.parseObject(jstr.toString(), TDxInvoiceReimburse.class);
        String uuid;
        if ("01".equals(vo.getInvoiceType()) || "04".equals(vo.getInvoiceType()) || "10".equals(vo.getInvoiceType())
            || "11".equals(vo.getInvoiceType()) || "14".equals(vo.getInvoiceType())
            || "03".equals(vo.getInvoiceType())) {
            String invoiceNo = vo.getInvoiceNo();
            String invoiceCode = vo.getInvoiceCode();
            uuid = invoiceCode + invoiceNo;
        } else {
            String passenger = vo.getPassenger();
            String invoiceDate = DateUtils.dateToStr(vo.getInvoiceDate(),"yyyy-MM-dd");
            String ticketNumber = vo.getTicketNumber();
            String passengerSfzh = vo.getPassengerSFZH();
            uuid = passenger + invoiceDate + ticketNumber + passengerSfzh;
        }

        String dbName = selectDbName(vo.getGfTaxNo());
        if ("".equals(dbName) || dbName == null) {
            return ResponseEntity.ok(R.error("购方税号不存在"));
        }
        // 通过uuid去判断底账表是否存在
        boolean b = passengerReimbursementSystemService.selectUuid(uuid, dbName);
        if (b) {
            vo.setUuid(uuid);
            passengerReimbursementSystemService.insertReimbursementSystem(vo, dbName);
            // 判断机动车取出不同的明细
            if ("03".equals(vo.getInvoiceType())) {
                JSONArray jsonArray = jstr.getJSONArray("receiptInvoiceDetail");
                List<TdxVehicleSaleInvoice> invoiceDetailsList =
                    JSON.parseArray(jsonArray.toJSONString(), TdxVehicleSaleInvoice.class);
                if (invoiceDetailsList.size() > 0) {
                    passengerReimbursementSystemService.insertVehicleDetail(invoiceDetailsList, uuid, dbName);
                }
            } else {
                JSONArray jsonArray = jstr.getJSONArray("receiptInvoiceDetail");
                List<TdxRecordInvoiceDetail> tdxRecordInvoiceDetailList =
                    JSON.parseArray(jsonArray.toJSONString(), TdxRecordInvoiceDetail.class);
                if (tdxRecordInvoiceDetailList.size() > 0) {
                    passengerReimbursementSystemService.addInvoiceDetails(tdxRecordInvoiceDetailList, uuid, dbName,
                        vo.getInvoiceCode(), vo.getInvoiceNo());
                }
            }
            return ResponseEntity.ok(R.ok());
        }
        return ResponseEntity.ok(R.error("发票已存在"));

    }

    @RequestMapping(value = "updateiInsertReimbursement", method = RequestMethod.POST)
    public ResponseEntity<R> updateiInsertReimbursement(@RequestBody String data) {
        if ("".equals(data) || data == null) {
            return ResponseEntity.ok(R.error("传入参数不能为空"));
        }
        JSONObject jsonObject = JSON.parseObject(data);
        JSONObject str = jsonObject.getJSONObject("data");
        String selectId = str.getString("selectId");
        String gfTaxNo = str.getString("gfTaxNo");
        if ("".equals(gfTaxNo) || gfTaxNo == null) {
            return ResponseEntity.ok(R.error("购方税号不存在"));
        }
        String reimbursementStatus = str.getString("reimbursementStatus");
        String dbName = selectDbName(gfTaxNo);
        TDxInvoiceReimburse vo = passengerReimbursementSystemService.deleteInsertReimbursement(selectId, dbName);
        if ("1".equals(reimbursementStatus)) {
            if ("03".equals(vo.getInvoiceType())) {
                passengerReimbursementSystemService.deleteVehicleDetail(vo.getUuid(), dbName);
            } else {
                passengerReimbursementSystemService.deleteInvoiceDetails(vo.getUuid(), dbName);
            }
        } else {
            passengerReimbursementSystemService.updateiInsertReimbursement(selectId, reimbursementStatus, dbName);
        }
        return ResponseEntity.ok(R.ok());

    }

    public String selectDbName(String gfTaxNo) {
        return passengerReimbursementSystemService.selectDbName(gfTaxNo);
    }

}
