package com.th.demo.controller.maint;

import com.th.demo.model.system.ResponseCodeMsg;
import com.th.demo.model.system.Type;
import com.th.demo.service.maint.AreaService;
import com.th.demo.service.maint.CargoService;
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.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;

@RestController
@RequestMapping(value = "/MaintCargo")
public class CargoController {
    @Autowired
    public CargoService cargoService;
    String result = Type.FAIL;

    @RequestMapping(value = "/queryAll.do")
    public String queryAll(int page, int rows,String uperCustomer, String Customer , HttpServletRequest request) {

        try {
            String userId = (String) request.getAttribute("userId");
            String belongId = (String) request.getAttribute("belongId");
            result = cargoService.query(page,rows,uperCustomer,Customer,userId,belongId);
        } catch (Exception ex) {
            ex.printStackTrace();
            result = JSONTools.toString(ResponseCodeMsg.CODE_EX, ex.getMessage());
        } finally {
            return result;
        }
    }

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

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

        try {
            String userId = (String) request.getAttribute("userId");
            String belongId = (String) request.getAttribute("belongId");
            result = cargoService.remove(id,userId,belongId);
        } catch (Exception ex) {
            ex.printStackTrace();
            result = JSONTools.toString(ResponseCodeMsg.CODE_EX, ex.getMessage());
        } finally {
            return result;
        }
    }

    @RequestMapping(value = "/queryRecord.do")
    public String queryRecord(int page, int rows,String uperCustomer, String Customer,
            String RecordDateBegin,String RecordDateEnd,String subStr,String changeFrom,
                              HttpServletRequest request) {

        try {
            Date fromDate2 = new Date();
            Date toDate2 = new Date();
            if (RecordDateBegin != null && RecordDateBegin!= ""){
                fromDate2 = DateTools.StrToDate(RecordDateBegin, "yyyy-MM-dd HH:mm:ss");
            }
            if (RecordDateEnd != null && RecordDateEnd != ""){
                toDate2 = DateTools.StrToDate(RecordDateEnd, "yyyy-MM-dd HH:mm:ss");
            }

            String userId = (String) request.getAttribute("userId");
            String belongId = (String) request.getAttribute("belongId");
            result = cargoService.queryRecord(page,rows,uperCustomer,Customer,fromDate2,toDate2,subStr,changeFrom,userId,belongId);
        } catch (Exception ex) {
            ex.printStackTrace();
            result = JSONTools.toString(ResponseCodeMsg.CODE_EX, ex.getMessage());
        } finally {
            return result;
        }
    }

}
