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.ware.CombineService;
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 = "/FinanceAccountDetail")
public class AccountDetailController {


    @Autowired
    public AccountDetailService accountDetailService;
    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 = accountDetailService.query(page,rows,customerName,accountFlag,userId,belongId);
        } 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 = accountDetailService.queryBycustomer(customerName,accountFlag,userId,belongId);
        } catch (Exception ex) {
            ex.printStackTrace();
            result = JSONTools.toString(ResponseCodeMsg.CODE_EX, ex.getMessage());
        } finally {
            return result;
        }
    }


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

    @RequestMapping(value = "/queryRcvMoney.do" )
    public String queryRcvMoney(
                          String fromDate,
                          String toDate,
                          HttpServletRequest request) {
        try {
            result = accountDetailService.queryRcvMoney(fromDate,toDate);
        } catch (Exception ex) {
            ex.printStackTrace();
            result = JSONTools.toString(ResponseCodeMsg.CODE_EX, ex.getMessage());
        } finally {
            return result;
        }
    }
    @RequestMapping(value = "/queryRcvMoneyBycustomer.do" )
    public String queryRcvMoneyBycustomer(
            String fromDate,
            String toDate,
            String customerName,
            HttpServletRequest request) {
        try {
            result = accountDetailService.queryRcvMoneyBycustomer(fromDate,toDate,customerName);
        } catch (Exception ex) {
            ex.printStackTrace();
            result = JSONTools.toString(ResponseCodeMsg.CODE_EX, ex.getMessage());
        } finally {
            return result;
        }
    }
}
