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.InStoreService;
import com.th.demo.tools.JSONTools;
import org.activiti.rest.editor.model.ModelEditorJsonRestResource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
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 = "/WareInStore")
public class InStoreController {
    @Autowired
    public InStoreService inStoreService;
    String result = Type.FAIL;
    protected static final Logger logger = LoggerFactory.getLogger(ModelEditorJsonRestResource.class);
    @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 = inStoreService.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 = inStoreService.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 = inStoreService.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 = inStoreService.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 areaName,
                              String stackName,
                              String plateNo,
                              String ordNo,
                              HttpServletRequest request) {

        try {
            String userId = (String) request.getAttribute("userId");
            String belongId = (String) request.getAttribute("belongId");
            // 参数校验
            if (userId == null || belongId == null) {
                result = JSONTools.toString(ResponseCodeMsg.CODE_EX, "用户ID或归属ID缺失");
                return result;
            }
            result = inStoreService.queryStore(page,rows,
                    wareName,
                    areaName,
                    stackName,
                    plateNo,
                    ordNo,
                    userId,
                    belongId);
        } catch (Exception ex) {
            ex.printStackTrace();
            logger.error("业务逻辑异常", ex);
            result = JSONTools.toString(ResponseCodeMsg.CODE_EX, ex.getMessage());
        } finally {
            logger.info("业务逻辑正常");
            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=inStoreService.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=inStoreService.modifyCustomeradress(id,customerAddressOld,customerAddressNew);

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

}
