package com.th.demo.controller.ware;

import com.th.demo.model.system.ResponseCodeMsg;
import com.th.demo.model.system.Type;
import com.th.demo.service.ware.OutRecordService;
import com.th.demo.tools.DateTools;
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;
import java.util.Date;

@RestController
@RequestMapping(value = "/WareOutRecord")
public class OutRecordController {
    @Autowired
    public OutRecordService outRecordService;
    String result = Type.FAIL;
    @RequestMapping(value = "/query.do")
    public String query(int page,int rows,  String wareName,
                           String stackName,
                           String model,
                           String materialName,
                           String standard,
                           String customerName,
                           String plateNo,
                           String pFromDate,
                           String pToDate,
                           String carNo,
                           String tallyPeople,
                           String ordNo,
                           String fkComponyName,
                           String contractNo,
                           HttpServletRequest request) {

        try {
            String userId = (String) request.getAttribute("userId");
            String belongId = (String) request.getAttribute("belongId");
            Date fromDate = DateTools.StrToDate(pFromDate, "yyyy-MM-dd HH:mm:ss");
            Date toDate = DateTools.StrToDate(pToDate, "yyyy-MM-dd HH:mm:ss");

            result = outRecordService.query(page, rows, wareName, stackName, model, materialName, standard, customerName,
                    plateNo,fromDate,toDate, userId, belongId,carNo,tallyPeople,ordNo,fkComponyName,contractNo);
        } catch (Exception ex) {
            ex.printStackTrace();
            result = JSONTools.toString(ResponseCodeMsg.CODE_EX, ex.getMessage());
        } finally {
            return result;
        }
    }
    @RequestMapping(value = "/queryTotal.do")
    public String queryTotal(int page,int rows,  String wareName,
                        String stackName,
                        String model,
                        String materialName,
                        String standard,
                        String customerName,
                        String plateNo,
                        String pFromDate,
                        String pToDate,
                        String carNo,
                        String tallyPeople,
                        String ordNo,
                        String fkComponyName,
                        HttpServletRequest request) {

        try {
            String userId = (String) request.getAttribute("userId");
            String belongId = (String) request.getAttribute("belongId");
            Date fromDate = DateTools.StrToDate(pFromDate, "yyyy-MM-dd HH:mm:ss");
            Date toDate = DateTools.StrToDate(pToDate, "yyyy-MM-dd HH:mm:ss");
            result = outRecordService.queryTotal(page, rows, wareName, stackName, model, materialName, standard, customerName,
                    plateNo,fromDate,toDate, userId, belongId,carNo,tallyPeople,ordNo,fkComponyName);
        } catch (Exception ex) {
            ex.printStackTrace();
            result = JSONTools.toString(ResponseCodeMsg.CODE_EX, ex.getMessage());
        } finally {
            return result;
        }
    }

    @RequestMapping(value = "/saveVerifyAcc.do" , method = RequestMethod.POST)
    public String saveVerifyAcc(String json,
                       String accWgt,
                       String accAmt,
                       String invoiceAmt,
                       String accDate,
                       String accReminder,
                       HttpServletRequest request) {

        try {
            String userId = (String) request.getAttribute("userId");
            String belongId = (String) request.getAttribute("belongId");
            result = outRecordService.saveVerifyAcc(json,
                                                 accWgt,
                                                 accAmt,
                                                 invoiceAmt,
                                                 accDate,
                                                 accReminder,userId,belongId);
        } catch (Exception ex) {
            ex.printStackTrace();
            result = JSONTools.toString(ResponseCodeMsg.CODE_EX, ex.getMessage());
        } finally {
            return result;
        }
    }
    @RequestMapping(value = "/savePriceMod.do" , method = RequestMethod.POST)
    public String savePriceMod(String json,
                                String modPrice,
                                HttpServletRequest request) {

        try {
            String userId = (String) request.getAttribute("userId");
            String belongId = (String) request.getAttribute("belongId");
            result = outRecordService.savePriceMod(json,modPrice,userId,belongId);
        } catch (Exception ex) {
            ex.printStackTrace();
            result = JSONTools.toString(ResponseCodeMsg.CODE_EX, ex.getMessage());
        } finally {
            return result;
        }
    }


}
