package com.dxhy.core.controller;

import com.alibaba.fastjson.JSON;
import com.dxhy.common.aspect.SysLog;
import com.dxhy.common.constant.CommonConstants;
import com.dxhy.common.controller.AbstractController;
import com.dxhy.common.utils.R;
import com.dxhy.core.job.entity.TDxVehicleSaleInvoice;
import com.dxhy.core.model.CertifiedInvoiceVo;
import com.dxhy.core.model.InvoiceDetails;
import com.dxhy.core.service.invoicedetail.InvoiceDetailService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

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

/**
 * @author user
 * @Auto xiexuedi
 * @date 2019/05/24 11:21
 */
@SuppressWarnings({"AlibabaUndefineMagicConstant", "DuplicatedCode"})
@RestController
@RequestMapping("/invoiceDetail")
@Slf4j
public class InvoiceDetailController extends AbstractController {

    @Resource
    private InvoiceDetailService invoiceDetailService;

    @RequestMapping(value = "invoiceSubject", method = RequestMethod.GET)
    public ResponseEntity<R> invoiceSubject(@RequestParam String invoiceNo, @RequestParam String invoiceCode,
        @RequestParam String dbNmae) {
        List<CertifiedInvoiceVo> list = invoiceDetailService.queryInvoiceSubject(invoiceNo, invoiceCode, dbNmae);
        Map<String, Object> map = new HashMap<>(1);
        map.put("list", list);
        return ResponseEntity.ok(R.ok().put("data", map));
    }

    @RequestMapping(value = "invoiceDetail", method = RequestMethod.POST)
    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, String> map = new HashMap<>(1);
        if ("03".equals(params.get("invoiceType"))) {
            List<TDxVehicleSaleInvoice> list = invoiceDetailService.selectVehicleSaleInvoice(uuid, dbName);
            map.put("list", JSON.toJSONString(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", JSON.toJSONString(list));
                return ResponseEntity.ok(R.ok().put("data", map));
            }
            List<InvoiceDetails> list = invoiceDetailService.selectInvoiceDetails(invoiceNo, invoiceCode, dbName);
            map.put("list", JSON.toJSONString(list));
            return ResponseEntity.ok(R.ok().put("data", map));
        }
    }

    /**
     * 查询发票全票面信息
     *
     * @param pramsMap
     *            入参
     * @return 返回
     *
     */
    @RequestMapping("/info")
    @ResponseBody
    @SysLog("明细票面信息查询")
    public ResponseEntity<?> pmxxInfo(@RequestBody Map<String, String> pramsMap) {
        // 入参统一在入口处理
        Map<String, String> data = new HashMap<>(8);
        data.put("dbName", getUserInfo().getDbName());
        /*if (pramsMap.get("invoiceCode") != null && !"".equals(pramsMap.get("invoiceCode"))) {
            data.put("invoiceCode", pramsMap.get("invoiceCode"));
        } else {
            return ResponseEntity.ok(R.error("发票代码不能为空！").put("data", "发票代码不能为空！"));
        }*/
        if (pramsMap.get("invoiceCode") != null && !"".equals(pramsMap.get("invoiceCode"))) {
            data.put("invoiceCode", pramsMap.get("invoiceCode"));
        }
        if (pramsMap.get("invoiceNo") != null && !"".equals(pramsMap.get("invoiceNo"))) {
            data.put("invoiceNo", pramsMap.get("invoiceNo"));
        } else {
            return ResponseEntity.ok(R.error("发票号码不能为空！").put("data", "发票号码不能为空！"));
        }

        try {
            String msg = invoiceDetailService.getInvoiceInfo(data);
            if (msg == null) {
                return ResponseEntity.ok(R.error("未获取到发票信息！").put("data", "未获取到发票信息！"));
            } else {
                return ResponseEntity.ok(R.ok().put("data", msg));
            }
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseEntity.ok(R.error(CommonConstants.MSG_ERR_DEFAULT));
        }
    }

    /**
     * 查询发票全票面信息
     *
     * @param pramsMap
     *            入参
     * @return 返回
     *
     */
    @RequestMapping("/saleInfo")
    @ResponseBody
    @SysLog("销项明细票面信息查询")
    public ResponseEntity<?> pmxxSaleInfo(@RequestBody Map<String, String> pramsMap) {
        // 入参统一在入口处理
        Map<String, String> data = new HashMap<>(8);
        data.put("dbName", getUserInfo().getDbName());
        if (pramsMap.get("invoiceCode") != null && !"".equals(pramsMap.get("invoiceCode"))) {
            data.put("invoiceCode", pramsMap.get("invoiceCode"));
        }
//        else {
//            return ResponseEntity.ok(R.error("发票代码不能为空！").put("data", "发票代码不能为空！"));
//        }
        if (pramsMap.get("invoiceNo") != null && !"".equals(pramsMap.get("invoiceNo"))) {
            data.put("invoiceNo", pramsMap.get("invoiceNo"));
        } else {
            return ResponseEntity.ok(R.error("发票号码不能为空！").put("data", "发票号码不能为空！"));
        }

        try {
            String msg = invoiceDetailService.getSaleInvoiceInfo(data);
            if (msg == null) {
                return ResponseEntity.ok(R.error("未获取到发票信息！").put("data", "未获取到发票信息！"));
            } else {
                return ResponseEntity.ok(R.ok().put("data", msg));
            }
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseEntity.ok(R.error(CommonConstants.MSG_ERR_DEFAULT));
        }
    }

}
