package com.zhongdao.jlr.business.action;

import com.util.RequestUtil;
import com.util.UtilTools;
import com.util.Vo;
import com.zhongdao.jlr.business.service.EnterpriseService;
import com.zhongdao.jlr.business.vo.LoginUserVo;
import com.zhongdao.jlr.pojo.EnterpriseJlr;
import org.apache.http.util.TextUtils;
import org.json.JSONObject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

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

/***
 * 经销商维护控制器
 */
@Controller
@RequestMapping("/enterprise")
public class EnterpriseAction {

    @Resource(name = "enterpriseServiceImpl")
    EnterpriseService baseService;

    /**
     * 经销商维护列表页面
     */
    @RequestMapping(value = "/enterprise.do", produces = "text/html;chrset=UTF-8", method = {RequestMethod.GET,
            RequestMethod.POST})
    public String enterprise(HttpServletRequest request, HttpServletResponse response) {
        return "/page/enterprise";
    }

    /**
     * 经销商维护搜索
     */
    @RequestMapping(value = "/search_enterprise.do", produces = "text/html;chrset=UTF-8", method = {RequestMethod.GET,
            RequestMethod.POST})
    public void searchEnterprise(HttpServletRequest request, HttpServletResponse response) {
        //获取参数
        String name = request.getParameter("name");
        String abbrCode = request.getParameter("abbrCode");
        //获取经销商信息
        Vo list = baseService.searchEnterprise(name, abbrCode, UtilTools.getPage(request), UtilTools
                .getRows(request));
        //将数据响应到客户端
        RequestUtil.responseResult4Json(response, RequestUtil.ObjecttoJsonString(list));
    }

    /**
     * 根据编号删除经销商信息
     */
    @RequestMapping(value = "/delete_by_id.do", produces = "text/html;chrset=UTF-8", method = {RequestMethod.GET,
            RequestMethod.POST})
    public void deleteById(HttpServletRequest request, HttpServletResponse response) {
        //声明一个map对象用于存放响应到客户端的数据
        Map<String, String> result = new HashMap<String, String>();
        result.put("flag", "0");
        result.put("msg", "执行失败");
        //获取参数
        String id = request.getParameter("id");

        if (!TextUtils.isEmpty(id)) {
            //根据编号删除经销商信息
            boolean back = baseService.deleteById(id);
            if (back) {
                result.put("flag", "1");
                result.put("msg", "执行成功");
            }
        } else {

            result.put("msg", "参数为空");
        }
        //将数据信息响应到客户端
        RequestUtil.responseResult4Json(response, RequestUtil.ObjecttoJsonString(result));

    }


    /**
     * 经销商信息修改
     */
    @RequestMapping(value = "/put_enterprise.do", produces = "text/html;chrset=UTF-8", method = {RequestMethod.GET,
            RequestMethod.POST})
    public void putMyEnterprise(HttpServletRequest request, HttpServletResponse response, @ModelAttribute EnterpriseJlr
            enterprise) throws Exception {
        //声明一个map集合,存储返回的结果信息
        Map<String, String> result = new HashMap<String, String>();
        result.put("flag", "0");
        result.put("msg", "执行失败");
        //获取参数
        String name = request.getParameter("name");
        String abbrCode = request.getParameter("abbr_code");
        String brands = request.getParameter("brands");
        String contact = request.getParameter("contact");
        String phone1 = request.getParameter("phone1");
        String job1 = request.getParameter("job1");
        String newPassword = request.getParameter("newPassword");
        String confirmPassword = request.getParameter("confirmPassword");
        String contact2 = request.getParameter("contact2");
        String phone2 = request.getParameter("phone2");
        String job2 = request.getParameter("job2");
        String contact3 = request.getParameter("contact3");
        String phone3 = request.getParameter("phone3");
        String job3 = request.getParameter("job3");
        //遍历服务资质信息并将其拼接
        String[] serviceQualifications = request.getParameterValues("service_qualification");
        StringBuffer serviceQualification=new StringBuffer();
        for(String service:serviceQualifications){
            serviceQualification.append(service);
            serviceQualification.append(",");
        }
        String serviceQualificationer=serviceQualification.subSequence(0,serviceQualification.lastIndexOf(",")).toString();
        String address = request.getParameter("address");
        String vinCode = request.getParameter("vin_code");
        String carType = request.getParameter("car_type");
        String firstSellDate = request.getParameter("first_sale_date");
        String lon = request.getParameter("lon");
        String lat = request.getParameter("lat");
        String id = request.getParameter("id");
        // 防止数据累加
        if (enterprise != null) {
            enterprise.setName(name);
            enterprise.setAbbr_code(abbrCode);
            enterprise.setBrands(brands);
            enterprise.setContact(contact);
            enterprise.setPhone1(phone1);
            enterprise.setJob1(job1);
            enterprise.setContact2(contact2);
            enterprise.setPhone2(phone2);
            enterprise.setJob2(job2);
            enterprise.setContact3(contact3);
            enterprise.setPhone3(phone3);
            enterprise.setJob3(job3);
            enterprise.setAddress(address);
            enterprise.setVin_code(vinCode);
            enterprise.setCar_type(carType);
            enterprise.setFirst_sale_date(firstSellDate);
            enterprise.setLon(lon);
            enterprise.setLat(lat);
            enterprise.setService_qualification(serviceQualificationer);
            //如果id不为空则判断此操作为更改
            if(!TextUtils.isEmpty(id)){
                //根据id获取经销商信息
                EnterpriseJlr back = baseService.getById(id);
                //获取创建时间并放入enterprise对象中
                enterprise.setCreate_time(back.getCreate_time());
            }
        }
        if (!TextUtils.isEmpty(name) && !TextUtils.isEmpty(brands) && !TextUtils.isEmpty(abbrCode)) {
            //添加或更改经销商信息
            boolean back = baseService.putEnterprise(enterprise, newPassword, confirmPassword,null);
            if (back) {
                result.put("flag", "1");
                result.put("msg", "执行成功");
            }
        } else {
            result.put("msg", "参数为空");
        }
        //将数据响应到客户端
        RequestUtil.responseResult4Json(response, RequestUtil.ObjecttoJsonString(result));

    }

    /**
     * 经销商个人信息修改
     */
    @RequestMapping(value = "/put_Myenterprise.do", produces = "text/html;chrset=UTF-8", method = {RequestMethod.GET,
            RequestMethod.POST})
    public void put_Myenterprise(HttpServletRequest request, HttpServletResponse response, @ModelAttribute EnterpriseJlr
            enterprise) throws Exception {
        //声明一个map集合,存储返回的结果信息
        Map<String, String> result = new HashMap<String, String>();
        result.put("flag", "0");
        result.put("msg", "执行失败");
        //获取参数
        String name = request.getParameter("name");
        String abbrCode = request.getParameter("abbr_code");
        String brands = request.getParameter("brands");
        String contact = request.getParameter("contact");
        String phone1 = request.getParameter("phone1");
        String job1 = request.getParameter("job1");
        String newPassword = request.getParameter("newPassword");
        String confirmPassword = request.getParameter("confirmPassword");
        String contact2 = request.getParameter("contact2");
        String phone2 = request.getParameter("phone2");
        String job2 = request.getParameter("job2");
        String contact3 = request.getParameter("contact3");
        String phone3 = request.getParameter("phone3");
        String job3 = request.getParameter("job3");
        String address = request.getParameter("address");
        String vinCode = request.getParameter("vin_code");
        String carType = request.getParameter("car_type");
        String firstSellDate = request.getParameter("first_sale_date");
        String lon = request.getParameter("lon");
        String lat = request.getParameter("lat");
        // 防止数据累加
        if (enterprise != null) {
            enterprise.setName(name);
            enterprise.setAbbr_code(abbrCode);
            enterprise.setBrands(brands);
            enterprise.setContact(contact);
            enterprise.setPhone1(phone1);
            enterprise.setJob1(job1);
            enterprise.setContact2(contact2);
            enterprise.setPhone2(phone2);
            enterprise.setJob2(job2);
            enterprise.setContact3(contact3);
            enterprise.setPhone3(phone3);
            enterprise.setJob3(job3);
            enterprise.setAddress(address);
            enterprise.setVin_code(vinCode);
            enterprise.setCar_type(carType);
            enterprise.setFirst_sale_date(firstSellDate);
            enterprise.setLon(lon);
            enterprise.setLat(lat);
        }
        //获取session数据
        LoginUserVo logininfo=(LoginUserVo)request.getSession().getAttribute("logininfo");
        String enpterpriseId=String.valueOf(logininfo.getEnterpriseJlrId());
        //根据id获取经销商信息
        EnterpriseJlr back = baseService.getById(enpterpriseId);
        enterprise.setId(logininfo.getEnterpriseJlrId());
        enterprise.setService_qualification(back.getService_qualification());
        //获取创建时间并放入enterprise对象中
        enterprise.setCreate_time(back.getCreate_time());
        if (!TextUtils.isEmpty(name) && !TextUtils.isEmpty(brands) && !TextUtils.isEmpty(abbrCode)) {
            //更改个人经销商信息
            boolean backs = baseService.putEnterprise(enterprise, newPassword, confirmPassword,String.valueOf(logininfo.getId()));
            if (backs) {
                result.put("flag", "1");
                result.put("msg", "执行成功");
            }
        } else {
            result.put("msg", "参数为空");
        }
        //将数据响应到客户端
        RequestUtil.responseResult4Json(response, RequestUtil.ObjecttoJsonString(result));
    }

    /**
     * 根据id查询经销商
     */
    @RequestMapping(value = "/get_by_id.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获取经销商信息
            EnterpriseJlr back = baseService.getById(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));

    }

    /**
     * 查询自己的经销商信息
     */
    @RequestMapping(value = "/get_my_info.do", produces = "text/html;chrset=UTF-8", method = {RequestMethod.GET,
            RequestMethod.POST})
    public void getMyInfo(HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> result = new HashMap<String, Object>();
        result.put("flag", "0");
        result.put("msg", "执行失败");
        LoginUserVo loginuser = (LoginUserVo) request.getSession().getAttribute("logininfo");
        String id = loginuser.getEnterpriseJlrId() + "";
        if (!TextUtils.isEmpty(id)) {
            EnterpriseJlr back = baseService.getById(id);
            if (back != null) {
                result.put("flag", "1");
                result.put("msg", "执行成功");
                result.put("data", back);
            }else {
                result.put("flag", "0");
                result.put("msg", "暂无经销商信息");
            }
        } else {

            result.put("msg", "参数为空");
        }
        RequestUtil.responseResult4Json(response, RequestUtil.ObjecttoJsonString(result));

    }

    /**
     * 导出报表
     */
    @RequestMapping(value = "/exportData.do", produces = "text/html;chrset=UTF-8", method = {RequestMethod.GET,
            RequestMethod.POST})
    public void exportData(HttpServletRequest request, HttpServletResponse response) {
        //获取参数
        String name = request.getParameter("name");
        String abbrCode = request.getParameter("abbrCode");
        //获取经销商信息
        JSONObject jsonObject = baseService.expordata(request, name, abbrCode);
        //将结果信息响应到客户端
        RequestUtil.responseResult4Json(response, jsonObject.toString());

    }

}

