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.InRecordService;
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 = "/WareInRecord")
public class InRecordController {
    @Autowired
    public InRecordService inRecordService;
    String result = Type.FAIL;

    @RequestMapping(value = "/query.do" /*method =RequestMethod.POST*/)
    public String query(int page, int rows,
                        String wareName,
                        String stackName,
                        String model,
                        String materialName,
                        String standard,
                        String customerName,
                        String plateNo,
                        String fromDate,
                        String toDate,
                        String ordNo,
                        String carNo,
                        String tallyPeople,
                        String fkComponyName,
                        String remark1,
                        HttpServletRequest request) {

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


    @RequestMapping(value = "/updateIn.do", method = RequestMethod.POST)
    public String updateIn(String json, HttpServletRequest request) {

        try {
            String userId = (String) request.getAttribute("userId");
            String belongId = (String) request.getAttribute("belongId");
            result = inRecordService.updateIn(json, userId, belongId);
        } 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 fromDate,
                        String toDate,
                        String ordNo,
                        String carNo,
                        String tallyPeople,
                        String fkComponyName,
                        String remark1,
                        HttpServletRequest request) {

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

    @RequestMapping(value = "/deleterecord.do")
    public String deleterecord(String json,
                             HttpServletRequest request) {

        try {
            String userId = (String) request.getAttribute("userId");
            String belongId = (String) request.getAttribute("belongId");

            result = inRecordService.deleterecord(json ,userId, belongId);
        } catch (Exception ex) {
            ex.printStackTrace();
            result = JSONTools.toString(ResponseCodeMsg.CODE_EX, ex.getMessage());
        } finally {
            return result;
        }
    }
    @RequestMapping(value = "/changeRecordInDtm.do")
    public String changeRecordInDtm(String json, String ChangeInDtm,
                               HttpServletRequest request) {

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

            result = inRecordService.changeRecordInDtm(json ,fromDate2, userId, belongId);
        } catch (Exception ex) {
            ex.printStackTrace();
            result = JSONTools.toString(ResponseCodeMsg.CODE_EX, ex.getMessage());
        } finally {
            return result;
        }
    }

}
