package com.th.demo.controller.finance;

import com.th.demo.model.system.ResponseCodeMsg;
import com.th.demo.model.system.Type;
import com.th.demo.service.finance.AccountService;
import com.th.demo.service.finance.InvoiceService;
import com.th.demo.tools.JSONTools;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping(value = "/FinanceInvoice")
public class InvoiceController {

    @Autowired
    public InvoiceService invoiceService;
    String result = Type.FAIL;

    @RequestMapping(value = "/query.do" )
    public String combine(
                          String accountId,
                          HttpServletRequest request) {
        try {
            String userId = (String) request.getAttribute("userId");
            String belongId = (String) request.getAttribute("belongId");
            result = invoiceService.query( accountId,userId,belongId);
        } catch (Exception ex) {
            ex.printStackTrace();
            result = JSONTools.toString(ResponseCodeMsg.CODE_EX, ex.getMessage());
        } finally {
            return result;
        }
    }
    @RequestMapping(value = "/queryInvoiceNew.do" )
    public String queryInvoiceNew(
            String queryDateFrom,
            String queryDateTo,
            String fkComponeyName,
            HttpServletRequest request) {
        try {
            String userId = (String) request.getAttribute("userId");
            String belongId = (String) request.getAttribute("belongId");
            result = invoiceService.queryInvoiceNew( queryDateFrom,queryDateTo,fkComponeyName,userId,belongId);
        } catch (Exception ex) {
            ex.printStackTrace();
            result = JSONTools.toString(ResponseCodeMsg.CODE_EX, ex.getMessage());
        } finally {
            return result;
        }
    }




    @RequestMapping(value = "/save.do", method = RequestMethod.POST)
    public String save(String json ,
                       HttpServletRequest request) {
        try {
            String userId = (String) request.getAttribute("userId");
            String belongId = (String) request.getAttribute("belongId");
            result = invoiceService.save(json,userId,belongId);
        } catch (Exception ex) {
            ex.printStackTrace();
            result = JSONTools.toString(ResponseCodeMsg.CODE_EX, ex.getMessage());
        } finally {
            return result;
        }
    }




    @RequestMapping(value = "/remove.do", method = RequestMethod.POST)
    public String remove(String id ,
                       HttpServletRequest request) {
        try {
            String userId = (String) request.getAttribute("userId");
            String belongId = (String) request.getAttribute("belongId");
            result = invoiceService.remove(id,userId,belongId);
        } catch (Exception ex) {
            ex.printStackTrace();
            result = JSONTools.toString(ResponseCodeMsg.CODE_EX, ex.getMessage());
        } finally {
            return result;
        }
    }
}
