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.AccountDetailService;
import com.th.demo.service.finance.AccountService;
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 = "/FinanceAccount")
public class AccountController {
    @Autowired
    public AccountService accountService;
    String result = Type.FAIL;

    @RequestMapping(value = "/query.do" )
    public String combine(int page,
                          int rows,
                          String customerName,
                          String accountFlag,
                          HttpServletRequest request) {
        try {
            String userId = (String) request.getAttribute("userId");
            String belongId = (String) request.getAttribute("belongId");
            result = accountService.query(page,rows,customerName,userId,belongId,accountFlag);
        } catch (Exception ex) {
            ex.printStackTrace();
            result = JSONTools.toString(ResponseCodeMsg.CODE_EX, ex.getMessage());
        } finally {
            return result;
        }
    }
    @RequestMapping(value = "/queryVerifyAccount.do" )  // 20220514 新开票界面查询
    public String queryVerifyAccount(
                          String queryDateFrom,
                          String queryDateTo,
                          String fkComponeyName,
                          HttpServletRequest request) {
        try {
            String userId = (String) request.getAttribute("userId");
            String belongId = (String) request.getAttribute("belongId");
            result = accountService.queryVerifyAccount(queryDateFrom,queryDateTo,fkComponeyName,userId,belongId);
        } catch (Exception ex) {
            ex.printStackTrace();
            result = JSONTools.toString(ResponseCodeMsg.CODE_EX, ex.getMessage());
        } finally {
            return result;
        }
    }

    @RequestMapping(value = "/queryInvoiceRslt.do" )  // 20220514 新开票界面查询
    public String queryInvoiceRslt(
            String queryDateFrom,
            String queryDateTo,
            String fkComponeyName,
            HttpServletRequest request) {
        try {
            String userId = (String) request.getAttribute("userId");
            String belongId = (String) request.getAttribute("belongId");
            result = accountService.queryInvoiceRslt(queryDateFrom,queryDateTo,fkComponeyName,userId,belongId);
        } catch (Exception ex) {
            ex.printStackTrace();
            result = JSONTools.toString(ResponseCodeMsg.CODE_EX, ex.getMessage());
        } finally {
            return result;
        }
    }

    @RequestMapping(value = "/queryProgress.do" )  // 20220514 新开票界面查询
    public String queryProgress(
            String queryDateFrom,
            String queryDateTo,
            String fkComponeyName,
            HttpServletRequest request) {
        try {
            String userId = (String) request.getAttribute("userId");
            String belongId = (String) request.getAttribute("belongId");
            result = accountService.queryProgress(queryDateFrom,queryDateTo,fkComponeyName,userId,belongId);
        } catch (Exception ex) {
            ex.printStackTrace();
            result = JSONTools.toString(ResponseCodeMsg.CODE_EX, ex.getMessage());
        } finally {
            return result;
        }
    }


    @RequestMapping(value = "/writeInvoice.do", method = RequestMethod.POST)
    public String writeInvoice(String json,
                                String invoiceCmp,
                                String invoiceDate,
                                String invoiceAmt,
                                HttpServletRequest request) {
        try {
            String userId = (String) request.getAttribute("userId");
            String belongId = (String) request.getAttribute("belongId");
            result = accountService.writeInvoice(json,userId,belongId,invoiceCmp,invoiceDate,invoiceAmt);
        } catch (Exception ex) {
            ex.printStackTrace();
            result = JSONTools.toString(ResponseCodeMsg.CODE_EX, ex.getMessage());
        } finally {
            return result;
        }
    }
    @RequestMapping(value = "/backProgress.do", method = RequestMethod.POST)
    public String backProgress(String json,
                               String bachType,
                               HttpServletRequest request) {
        try {
            String userId = (String) request.getAttribute("userId");
            String belongId = (String) request.getAttribute("belongId");
            result = accountService.backProgress(json,bachType,userId,belongId);
        } catch (Exception ex) {
            ex.printStackTrace();
            result = JSONTools.toString(ResponseCodeMsg.CODE_EX, ex.getMessage());
        } finally {
            return result;
        }
    }
    @RequestMapping(value = "/deleteInvoice.do", method = RequestMethod.POST)
    public String deleteInvoice(String json,
                               HttpServletRequest request) {
        try {
            String userId = (String) request.getAttribute("userId");
            String belongId = (String) request.getAttribute("belongId");
            result = accountService.deleteInvoice(json,userId,belongId);
        } catch (Exception ex) {
            ex.printStackTrace();
            result = JSONTools.toString(ResponseCodeMsg.CODE_EX, ex.getMessage());
        } finally {
            return result;
        }
    }

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


    @RequestMapping(value = "/queryGroup.do" )
    public String queryGroup(int page,
                          int rows,
                          String customerName,
                          String accountFlag,
                          HttpServletRequest request) {
        try {
            String userId = (String) request.getAttribute("userId");
            String belongId = (String) request.getAttribute("belongId");
            result = accountService.queryGroup(page,rows,customerName,userId,belongId,accountFlag);
        } catch (Exception ex) {
            ex.printStackTrace();
            result = JSONTools.toString(ResponseCodeMsg.CODE_EX, ex.getMessage());
        } finally {
            return result;
        }
    }
    @RequestMapping(value = "/queryBycustomer.do" )
    public String queryBycustomer(
                          String customerName,
                          String accountFlag,
                          HttpServletRequest request) {
        try {
            String userId = (String) request.getAttribute("userId");
            String belongId = (String) request.getAttribute("belongId");
            result = accountService.queryBycustomer(customerName,userId,belongId,accountFlag);
        } catch (Exception ex) {
            ex.printStackTrace();
            result = JSONTools.toString(ResponseCodeMsg.CODE_EX, ex.getMessage());
        } finally {
            return result;
        }
    }



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