package com.zhongdao.jlr.business.action;

import com.util.RequestUtil;
import com.util.Vo;
import com.zhongdao.jlr.business.service.SupplierService;
import com.zhongdao.jlr.business.service.UserInfoService;
import com.zhongdao.jlr.business.vo.Contract;
import com.zhongdao.jlr.business.vo.Destination;
import com.zhongdao.jlr.business.vo.Enterprise;
import com.zhongdao.jlr.pojo.JLRRole;
import com.zhongdao.jlr.pojo.JLRUserContract;
import org.apache.http.util.TextUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/supplier")
public class SupplierAction {

    @Resource(name = "userInfoService")
    UserInfoService service;

    @Resource(name = "supplierServiceImpl")
    SupplierService serviceImpl;

    /***
     * 获取用户信息,跳转数据权限管理页面
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/supplier.do", produces = "text/html;chrset=UTF-8", method = {RequestMethod.GET,
            RequestMethod.POST})
    public String JumpTocaseInfo(HttpServletRequest request, HttpServletResponse response) {
        //获取角色信息
        List<JLRRole> baseRoles=service.getRoleInfo();
        //共享数据
        request.setAttribute("roles",baseRoles);
        return "/page/supplier";
    }

    /***
     * 获取合同信息
     * @param request
     * @param response
     */
    @RequestMapping(value = "/getContractInfo.do", produces = "text/html;chrset=UTF-8", method = {RequestMethod.GET,
            RequestMethod.POST})
    public void caseInfoList(HttpServletRequest request, HttpServletResponse response) {
        //获取参数
        String name = request.getParameter("name");// 合同名称
        String id = request.getParameter("id");// 用户id
        //获取合同信息
        Vo vo = serviceImpl.getContractInfo(request,name,id);
        //将合同信息响应到客户端
        RequestUtil.responseResult4Json(response, RequestUtil.ObjecttoJsonString(vo));
    }

    /***
     * 获取指定的合同信息
     * @param request
     * @param response
     */
    @RequestMapping(value = "/geAssignContract.do", produces = "application/json;chrset=UTF-8", method = {RequestMethod.GET,
            RequestMethod.POST})
    @ResponseBody
    public List<Contract> geAssignContract(HttpServletRequest request, HttpServletResponse response) {
        //获取参数
        String id = request.getParameter("id");// 用户id
        //获取合同信息
        List<Contract> vo = serviceImpl.getAssignContract(id);
        //将数据响应到客户端
        return vo;
    }

    /***
     * 获取指定的目的地信息
     * @param request
     * @param response
     */
    @RequestMapping(value = "/geAssignDestination.do", produces = "application/json;chrset=UTF-8", method = {RequestMethod.GET,
            RequestMethod.POST})
    @ResponseBody
    public List<Destination> geAssignDestination(HttpServletRequest request, HttpServletResponse response) {
        //获取参数
        String id = request.getParameter("id");// 用户id
        //获取合同信息
        List<Destination> vo = serviceImpl.geAssignDestination(id);
        //将数据响应到客户端
        return vo;
    }

    /***
     * 获取指定的供应商信息
     * @param request
     * @param response
     */
    @RequestMapping(value = "/geAssignEnterprise.do", produces = "application/json;chrset=UTF-8", method = {RequestMethod.GET,
            RequestMethod.POST})
    @ResponseBody
    public List<Enterprise> geAssignEnterprise(HttpServletRequest request, HttpServletResponse response) {
        //获取参数
        String id = request.getParameter("id");// 用户id
        //获取合同信息
        List<Enterprise> vo = serviceImpl.geAssignEnterprise(id);
        //将数据响应到客户端
        return vo;
    }


    /***
     * 获取供应商信息
     * @param request
     * @param response
     */
    @RequestMapping(value = "/getEnterpriseInfo.do", produces = "text/html;chrset=UTF-8", method = {RequestMethod.GET,
            RequestMethod.POST})
    public void getEnterpriseInfo(HttpServletRequest request, HttpServletResponse response) {
        //获取参数
        String name = request.getParameter("name");// 合同名称
        String id = request.getParameter("id");// 用户id
        //获取供应商信息
        Vo vo = serviceImpl.getEnterpriseInfo(request,name,id);
        //将用户信息响应到客户端
        RequestUtil.responseResult4Json(response, RequestUtil.ObjecttoJsonString(vo));
    }

    /***
     * 获取合同下的目的地信息
     * @param request
     * @param response
     */
    @RequestMapping(value = "/getDestination.do", produces = "text/html;chrset=UTF-8", method = {RequestMethod.GET,
            RequestMethod.POST})
    public void getList(HttpServletRequest request, HttpServletResponse response) {
        //获取参数
        String name = request.getParameter("name");// 合同名称
        String id= request.getParameter("id");// 合同名称
        //获取合同下的目的地信息
        Vo vo = serviceImpl.getDestination(request,name,id);
        //将目的地信息响应到客户端
        RequestUtil.responseResult4Json(response, RequestUtil.ObjecttoJsonString(vo));
    }

    /**
     * 指定合同
     */
    @RequestMapping(value = "/insertContract.do", produces = "application/json;chrset=UTF-8", method = {RequestMethod.GET,
            RequestMethod.POST})
    @ResponseBody
    public Map putMyEnterprise(HttpServletRequest request, HttpServletResponse response, Integer id, int[] ids) throws Exception {
        //声明一个map集合,存储返回的结果信息
        Map<String, String> result = new HashMap<String, String>();
        result.put("flag", "0");
        result.put("msg", "执行失败");
        if (ids != null && id != null) {
            //添加合同信息
            boolean back = serviceImpl.insertContractInfo(id,ids);
            if (back) {
                result.put("flag", "1");
                result.put("msg", "执行成功");
            } else {
                result.put("msg", "参数为空");
            }
        }
        //将数据响应到客户端
        return result;
    }


    /**
     * 指定目的地
     */
    @RequestMapping(value = "/insertDestination.do", produces = "application/json;chrset=UTF-8", method = {RequestMethod.GET,
            RequestMethod.POST})
    @ResponseBody
    public Map insertDestination(HttpServletRequest request, HttpServletResponse response, Integer id, int[] ids) throws Exception {
        //声明一个map集合,存储返回的结果信息
        Map<String, String> result = new HashMap<String, String>();
        result.put("flag", "0");
        result.put("msg", "执行失败");
        if (ids != null && id != null) {
            //添加或更改用户信息
            boolean back = serviceImpl.insertDestinationInfo(id,ids);
            if (back) {
                result.put("flag", "1");
                result.put("msg", "执行成功");
            } else {
                result.put("msg", "参数为空");
            }
        }
        //将数据响应到客户端
        return result;
    }

    /**
     * 指定供应商
     */
    @RequestMapping(value = "/insertEnterprise.do", produces = "application/json;chrset=UTF-8", method = {RequestMethod.GET,
            RequestMethod.POST})
    @ResponseBody
    public Map insertEnterprise(HttpServletRequest request, HttpServletResponse response, Integer id, int[] ids) throws Exception {
        //声明一个map集合,存储返回的结果信息
        Map<String, String> result = new HashMap<String, String>();
        result.put("flag", "0");
        result.put("msg", "执行失败");
        if (ids != null && id != null) {
            //添加或更改用户信息
            boolean back = serviceImpl.insertEnterpriseInfo(id,ids);
            if (back) {
                result.put("flag", "1");
                result.put("msg", "执行成功");
            } else {
                result.put("msg", "参数为空");
            }
        }
        //将数据响应到客户端
        return result;
    }

    /**
     * 删除指定的合同
     */
    @RequestMapping(value = "/deleteContract.do", produces = "application/json;chrset=UTF-8", method = {RequestMethod.GET,
            RequestMethod.POST})
    @ResponseBody
    public Map deleteContract(HttpServletRequest request, HttpServletResponse response, Integer id, int[] ids) throws Exception {
        //声明一个map集合,存储返回的结果信息
        Map<String, String> result = new HashMap<String, String>();
        result.put("flag", "0");
        result.put("msg", "执行失败");
        if (ids != null && id != null) {
            //添加或更改用户信息
            boolean back = serviceImpl.deleteContractInfo(id,ids);
            if (back) {
                result.put("flag", "1");
                result.put("msg", "执行成功");
            }
        } else {
            result.put("msg", "参数为空");
        }
        //将数据响应到客户端
        return result;
    }

    /**
     * 删除指定的供应商
     */
    @RequestMapping(value = "/deleteEnterprise.do", produces = "application/json;chrset=UTF-8", method = {RequestMethod.GET,
            RequestMethod.POST})
    @ResponseBody
    public Map deleteEnterprise(HttpServletRequest request, HttpServletResponse response, Integer id, int[] ids) throws Exception {
        //声明一个map集合,存储返回的结果信息
        Map<String, String> result = new HashMap<String, String>();
        result.put("flag", "0");
        result.put("msg", "执行失败");
        if (ids != null && id != null) {
            //添加或更改用户信息
            boolean back = serviceImpl.deleteEnterpriseInfo(id,ids);
            if (back) {
                result.put("flag", "1");
                result.put("msg", "执行成功");
            }
        } else {
            result.put("msg", "参数为空");
        }
        //将数据响应到客户端
        return result;
    }

    /**
     * 删除指定的目的地
     */
    @RequestMapping(value = "/deleteDestination.do", produces = "application/json;chrset=UTF-8", method = {RequestMethod.GET,
            RequestMethod.POST})
    @ResponseBody
    public Map deleteDestination(HttpServletRequest request, HttpServletResponse response, Integer id, int[] ids) throws Exception {
        //声明一个map集合,存储返回的结果信息
        Map<String, String> result = new HashMap<String, String>();
        result.put("flag", "0");
        result.put("msg", "执行失败");
        if (ids != null && id != null) {
            //添加或更改用户信息
            boolean back = serviceImpl.deleteDestinationInfo(id,ids);
            if (back) {
                result.put("flag", "1");
                result.put("msg", "执行成功");
            }
        }else {
            result.put("msg", "参数为空");
        }
        //将数据响应到客户端
        return result;
    }

    /**
     * 根据用户id查询合同信息
     */
    @RequestMapping(value = "/getDestinationInfo.do", produces = "text/html;chrset=UTF-8", method = {RequestMethod.GET,
            RequestMethod.POST})
    public void getById(HttpServletRequest request, HttpServletResponse response) {
        //声明一个map集合,存储返回的结果信息
        Map<String, Object> result = new HashMap<String, Object>();
        result.put("flag", "0");
        result.put("msg", "执行失败");
        String id = request.getParameter("id");
        if (!TextUtils.isEmpty(id)) {
            //根据id获取合同信息
            List<JLRUserContract> back = serviceImpl.getDestinationInfo(id);
            if (back != null) {
                result.put("flag", "1");
                result.put("msg", "执行成功");
                result.put("data", back);
            }
        } else {
            result.put("msg", "参数为空");
        }
        //将结果信息响应到客户端
        RequestUtil.responseResult4Json(response, RequestUtil.ObjecttoJsonString(result));
    }


}
