package com.dxhy.core.controller;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.dxhy.common.controller.AbstractController;
import com.dxhy.common.util.AmountFormatUtil;
import com.dxhy.common.util.InvoiceUtil;
import com.dxhy.common.util.NumberToCN;
import com.dxhy.common.utils.R;
import com.dxhy.api.model.invoice.InvoiceMainBody;
import com.dxhy.core.job.entity.TDxRecordInvoiceDetail;
import com.dxhy.core.job.entity.TDxVehicleSaleInvoice;
import com.dxhy.core.model.InvoiceDetails;
import com.dxhy.core.service.invoicedetail.InvoiceDetailService;
import com.dxhy.core.service.invoicedetail.OtherInvoiceDetailService;

import lombok.extern.slf4j.Slf4j;

/**
 *
 * @author lxx
 * @date
 */
@RequestMapping("/pool")
@RestController
@Slf4j

@SuppressWarnings({"AlibabaLowerCamelCaseVariableNaming", "AlibabaUndefineMagicConstant"})
public class OtherInvoiceDetailController extends AbstractController {

    @Resource
    private OtherInvoiceDetailService otherInvoiceDetailService;
    @Resource
    private InvoiceDetailService invoiceDetailService;

    /**
     * 查询其他发票详情
     *
     * @param pramsMap
     * @return
     */

    @RequestMapping(value = "/selectOtherInvoiceDateil", method = RequestMethod.POST)
    @ResponseBody
    public ResponseEntity<R> selectOtherInvoiceDateil(@RequestBody Map<String, String> pramsMap) {

        String invoiceCode = pramsMap.get("invoiceCode");
        String invoiceNo = pramsMap.get("invoiceNo");
        String gfTaxNo = pramsMap.get("gfTaxNo");
        String selectId = pramsMap.get("selectId");

        String dbName = getUserInfo().getDbName();
        String invoiceType = "";
        if (StringUtils.isNotBlank(invoiceCode)) {
            InvoiceUtil invoiceUtil = new InvoiceUtil(invoiceCode);
            invoiceType = invoiceUtil.getFplxdm();
        }
        log.info("其他发票查看明细票种:{}", invoiceType);
        if ("10".equals(invoiceType) || "11".equals(invoiceType) || "04".equals(invoiceType)) {
            Map<String, Object> map = retrunInvoiceDetilsList(invoiceCode, invoiceNo, dbName);
            return ResponseEntity.ok(R.ok().put("data", map));
        } else {
            Map<String, Object> map = new HashMap<>(2);
            List<InvoiceMainBody> invoiceSubjectList = otherInvoiceDetailService.selectPassengerTheme(selectId, dbName);
            if (invoiceSubjectList != null && invoiceSubjectList.size() > 0) {
                map.put("invoiceSubjectList", invoiceSubjectList.get(0));
            }
            return ResponseEntity.ok(R.ok().put("data", map));
        }
    }

    private Map<String, Object> retrunInvoiceDetilsList(String invoiceCode, String invoiceNo, String dbName) {
        Map<String, Object> map = new HashMap<>(2);
        List<InvoiceMainBody> invoiceSubjectList =
            otherInvoiceDetailService.selectInvoiceMainBody(invoiceNo, invoiceCode, dbName);
        InvoiceMainBody invoiceMainBody = invoiceSubjectList.get(0);
        map.put("invoiceSubjectList", invoiceMainBody);
        Map<String, Object> params = new HashMap<>(5);
        params.put("invoiceNo", invoiceNo);
        params.put("invoiceCode", invoiceCode);
        params.put("dbName", dbName);
        params.put("invoiceType", invoiceMainBody.getInvoiceType());
        ResponseEntity<R> rResponseEntity = this.invoiceDetails(params);
        return retrunInvoiceDetailsList(rResponseEntity, map, invoiceMainBody);
    }

    public ResponseEntity<R> invoiceDetails(@RequestBody Map<String, Object> params) {
        String invoiceNo = (String)params.get("invoiceNo");
        String invoiceCode = (String)params.get("invoiceCode");
        String uuid = invoiceCode + invoiceNo;
        String dbName = (String)params.get("dbName");
        Map<String, Object> map = new HashMap<>(3);
        if ("03".equals(params.get("invoiceType"))) {
            List<TDxVehicleSaleInvoice> list = invoiceDetailService.selectVehicleSaleInvoice(uuid, dbName);
            map.put("list", list);
            return ResponseEntity.ok(R.ok().put("data", map));
        } else {
            if ("1".equals(params.get("flag"))) {
                List<InvoiceDetails> list = invoiceDetailService.selectInspectionDetails(uuid, dbName);
                map.put("list", list);
                return ResponseEntity.ok(R.ok().put("data", map));
            }
            List<InvoiceDetails> list = invoiceDetailService.selectInvoiceDetails(invoiceNo, invoiceCode, dbName);
            map.put("list", list);
            return ResponseEntity.ok(R.ok().put("data", map));
        }
    }

    private Map<String, Object> retrunInvoiceDetailsList(ResponseEntity<R> rResponseEntity, Map<String, Object> map,
        InvoiceMainBody invoiceMainBody) {
        R body = rResponseEntity.getBody();
        Map<String, Object> listMap = (Map<String, Object>)body.get("data");
        if ("03".equals(invoiceMainBody.getInvoiceType())) {
            List<TDxVehicleSaleInvoice> list = (List<TDxVehicleSaleInvoice>)listMap.get("list");
            List<TDxVehicleSaleInvoice> tDxVehicleSaleInvoice = new ArrayList<>();
            for (int j = 0; j < list.size(); j++) {
                Map<String, Object> ListReturnMap = (Map<String, Object>)list.get(j);
                String json = JSON.toJSONString(ListReturnMap);
                TDxVehicleSaleInvoice tDxRecordInvoiceDetail1 =
                    JSONObject.parseObject(json, TDxVehicleSaleInvoice.class);
                tDxVehicleSaleInvoice.add(tDxRecordInvoiceDetail1);
            }
            map.put("invoiceDetails", tDxVehicleSaleInvoice);
        } else {
            List<TDxRecordInvoiceDetail> list = (List<TDxRecordInvoiceDetail>)listMap.get("list");
            List<TDxRecordInvoiceDetail> tDxRecordInvoiceDetList = new ArrayList<>();
            if (list.size() > 0) {
                for (int j = 0; j < list.size(); j++) {
                    Map<String, Object> listReturnMap = (Map<String, Object>)list.get(j);
                    String json = JSON.toJSONString(listReturnMap);
                    TDxRecordInvoiceDetail tDxRecordInvoiceDetail =
                        JSONObject.parseObject(json, TDxRecordInvoiceDetail.class);
                    if (StringUtils.isNotBlank(tDxRecordInvoiceDetail.getTaxRate())) {
                        StringBuffer sb = new StringBuffer(tDxRecordInvoiceDetail.getTaxRate());
                        sb.append("%");
                        tDxRecordInvoiceDetail.setTaxRate(sb.toString());
                    }
                    tDxRecordInvoiceDetList.add(tDxRecordInvoiceDetail);
                }
            }
            map.put("invoiceDetails", tDxRecordInvoiceDetList);
        }
        String stringTotalAmount = "";
        if (!"".equals(invoiceMainBody.getTotalAmount()) && invoiceMainBody.getTotalAmount() != null) {
            String totalAmount = invoiceMainBody.getTotalAmount();
            BigDecimal bd = new BigDecimal(totalAmount);
            stringTotalAmount = NumberToCN.number2CNMontrayUnit(bd);
        } else {
            BigDecimal bd = new BigDecimal(0);
            BigDecimal invoiceAmount = new BigDecimal(invoiceMainBody.getInvoiceAmount());
            bd.add(invoiceAmount);
            BigDecimal taxAmount = new BigDecimal(invoiceMainBody.getTaxAmount());
            bd.add(taxAmount);
            stringTotalAmount = NumberToCN.number2CNMontrayUnit(bd);
        }
        map.put("taxAmountSum", AmountFormatUtil.fmtMicrometer(invoiceMainBody.getTaxAmount()));
        map.put("detailAmountSum", AmountFormatUtil.fmtMicrometer(invoiceMainBody.getInvoiceAmount()));
        map.put("detailAmountTotal", AmountFormatUtil.fmtMicrometer(invoiceMainBody.getTotalAmount()));
        map.put("stringTotalAmount", stringTotalAmount);
        return map;
    }
}
