package com.th.demo.controller.ware;

import com.th.demo.model.system.ResponseCodeMsg;
import com.th.demo.model.system.Type;
import com.th.demo.model.ware.Store;
import com.th.demo.service.maint.CustomerService;
import com.th.demo.service.ware.StoreService;
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;

@RestController
@RequestMapping(value = "/WareStore")
public class StoreController {
    @Autowired
    public StoreService storeService;
    String result = Type.FAIL;


    @RequestMapping(value = "/query.do")//查询方法 前两个参数是分页参数
    public String query(
            int page,
            int rows,
            String wareName,
            String stackName,
            HttpServletRequest request) {

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


    @RequestMapping(value = "/queryLayerByStackId.do")//查询方法 前两个参数是分页参数
    public String queryLayerByStackId(                                        String stackId,
                                        HttpServletRequest request) {

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

    @RequestMapping(value = "/queryByWareIdStackId.do")//查询方法 前两个参数是分页参数
    public String queryByWareIdStackId(String wareId,
                                           String stackId,
                                           HttpServletRequest request) {

        try {
            String userId = (String) request.getAttribute("userId");
            String belongId = (String) request.getAttribute("belongId");
            result = storeService.queryByWareIdStackId(wareId, stackId, userId, belongId);
        } catch (Exception ex) {
            ex.printStackTrace();
            result = JSONTools.toString(ResponseCodeMsg.CODE_EX, ex.getMessage());
        } finally {
            return result;
        }
    }

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

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

    }

    //根据部分字段查询库存记录
    @RequestMapping(value = "/queryStore.do")
    public String queryStore(int page,int rows,
                              String wareName,
                              String stackName,
                              String model,
                              String materialName,
                             String standard,
                             String customerName,
                             String fkcustmerName,
                             String plateNo,
                             String ordNo,
                             String contractNo,
                             String packNo,
                              HttpServletRequest request) {

        try {
            String userId = (String) request.getAttribute("userId");
            String belongId = (String) request.getAttribute("belongId");
            result = storeService.queryStore(page,rows,
                    wareName,
                    stackName,
                    model,
                    materialName,
                    standard,
                      customerName,
                    userId,
                    belongId,plateNo,ordNo,contractNo,fkcustmerName,packNo);
        } catch (Exception ex) {
            ex.printStackTrace();
            result = JSONTools.toString(ResponseCodeMsg.CODE_EX, ex.getMessage());
        } finally {
            return result;
        }
    }

    @RequestMapping(value = "/modifyCustomer.do")
    public String modifyCustomer(String id,String customerNameOld,String customerNameNew,HttpServletRequest request) {

        try {
            String userId = (String) request.getAttribute("userId");
            String belongId = (String) request.getAttribute("belongId");
            result=storeService.modifyCustomer(id,customerNameOld,customerNameNew);

        } catch (Exception ex) {
            ex.printStackTrace();
            result = JSONTools.toString(ResponseCodeMsg.CODE_EX, ex.getMessage());
        } finally {
            return result;
        }
    }

    @RequestMapping(value = "/updateCustomerAddress.do")
    public String updateCustomerAddress(String id,String customerAddressOld,String customerAddressNew,HttpServletRequest request) {

        try {
            String userId = (String) request.getAttribute("userId");
            String belongId = (String) request.getAttribute("belongId");
            result=storeService.modifyCustomeradress(id,customerAddressOld,customerAddressNew);

        } catch (Exception ex) {
            ex.printStackTrace();
            result = JSONTools.toString(ResponseCodeMsg.CODE_EX, ex.getMessage());
        } finally {
            return result;
        }
    }

    @RequestMapping(value = "/changeAll.do")//查询方法 前两个参数是分页参数
    public String changeAll(

            String json,
            String newCustomername,
            String newFKCustomername,
            String newContractno,
            String newOrdno,
            String newReceiveaddress,
            String newProductionplace,
            String newPackno,

            HttpServletRequest request) {

        try {
            String userId = (String) request.getAttribute("userId");
            String belongId = (String) request.getAttribute("belongId");
            result = storeService.changeAll( json,newCustomername,newFKCustomername,newContractno,newOrdno,newReceiveaddress,
                    newProductionplace,newPackno,userId, belongId);
        } catch (Exception ex) {
            ex.printStackTrace();
            result = JSONTools.toString(ResponseCodeMsg.CODE_EX, ex.getMessage());
        } finally {
            return result;
        }
    }

}
