package com.sifang.modules.smallprogram;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.google.gson.reflect.TypeToken;
import com.jfinal.kit.HashKit;
import com.jfinal.plugin.activerecord.Record;
import com.sifang.modules.config.SfCacheService;
import com.sifang.modules.customer.Service.CustomerService;
import com.sifang.modules.customer.Service.PowerStationService;
import com.sifang.modules.customer.constant.SourceConstants;
import com.sifang.modules.customer.entity.Customer;
import com.sifang.modules.customer.entity.CustomerWarranty;
import com.sifang.modules.customer.entity.PowerStation;
import com.sifang.modules.customer.entity.PowerStationDevice;
import com.sifang.modules.order.service.OrderService;
import com.sifang.modules.order.utils.GetDistance;
import com.sifang.modules.platform.service.WarrantyService;
import com.sifang.modules.platform.utils.AddressUtils;
import com.sifang.modules.platform.utils.PCDInfo;
import com.sifang.modules.site.registerInfo.service.RegisterService;
import com.sifang.modules.smallprogram.service.SmallProgramOrderService;
import com.sifang.modules.sunshine.SystemSetting.utils.BusinessException;
import com.sifang.modules.sunshine.SystemSetting.utils.Result;
import com.sifang.modules.sunshine.SystemSetting.utils.StringUtil;
import com.sifang.modules.sys.common.entity.User;
import com.sifang.modules.sys.entity.CrmPlatformAnnouncement;
import com.sifang.modules.sys.service.CrmPlatformAnnouncementService;
import com.sifang.modules.sys.util.LayuiPage;
import com.sifang.modules.sys.web.BaseController;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.*;

@Controller
@RequestMapping(value="${adminPath}/main/customer")
public class AppCustomerController extends BaseController {

    private Logger logger = LoggerFactory.getLogger(AppCustomerController.class);

    @Autowired
    private CustomerService customerService;
    @Autowired
    private PowerStationService powerStationService;

    @Value("${sync.sunshine.app.key}")
    private String APPKR;

    @Value("${sync.sunshine.app.prefix}")
    private String prefixUrl;

    @Autowired
    private WarrantyService warrantyService;

    @Autowired
    private SmallProgramOrderService smallProgramOrderService;
    @Autowired
    CrmPlatformAnnouncementService crmPlatformAnnouncementService;
    @Autowired
    private OrderService orderService;

    @Autowired
    private RegisterService registerService;

    @Autowired
    SfCacheService cacheService;

    @ResponseBody
    @RequestMapping(value = "customerLogin")
    public Object CustomerLogin(HttpServletRequest request){
        String loginName = request.getParameter("user_account");
        String user_password = request.getParameter("user_password");
        if(StringUtil.isBlank(loginName) || StringUtil.isBlank(user_password)){
            return Result.fail("205","登录账号或者密码为空!");
        }
        Map<String,Object> maps = Maps.newHashMap();



        try {
            //判断是否是系统注册账号
            long count = customerService.checkMobile(loginName,"2");
            if(count > 0){
                //注册的存在,
                //获取基恩信息
                Map<String,String> map = Maps.newHashMap();
                Record rd = customerService.getpowerStation(loginName);
                if(rd != null){
                    String password = rd.getStr("password");
                    String md5Password = HashKit.md5(user_password.trim());
                    if(!md5Password.equals(password)){
                        maps.put("code","0");
                        maps.put("data","密码错误");
                        maps.put("msg","登录失败，密码错误！");
                        return maps;
                    }
                    //更新用户信息
                    customerService.UpdateCustomer(rd.getStr("id"));
                    String userId = rd.getStr("user_id");
                    map.put("userId",userId);
                    map.put("mobileTel",rd.getStr("mobile_tel"));
                    map.put("userAccount",rd.getStr("user_account"));
                    map.put("disableTime",rd.getStr("disable_time"));
                    map.put("token","ryt65769547657696");
                    map.put("loginTimes","");
                    map.put("loginLastDate","");
                    map.put("loginLastIp","");
                    map.put("userType","2");
                }else{
                    maps.put("code","501");
                    maps.put("data","账号不存在");
                    maps.put("msg","登录失败，账号不存在！");
                    return maps;
                }
                maps.put("code","200");
                maps.put("data",map);
                return maps;
            }
            //则需要调用云登录接口
            String sendGet = GetDistance.sendGet(prefixUrl,"service=login&login_type=1&sys_code=901&user_account="+loginName+"&user_password="+user_password+"&appkey="+APPKR);
            Gson gson = new Gson();

            JsonObject json = gson.fromJson(sendGet, JsonObject.class);
            String state = json.get("result_code").getAsString();
            String login_state = "-1";
          //  logger.info("json: "+sendGet);
            //  String token="";
            if("1".equals(state)){      //接口请求正常
                JsonObject json2 = gson.fromJson(json.get("result_data"), JsonObject.class);
              //  logger.info("json2 : "+json.get("result_data"));
                login_state = json2.get("login_state").getAsString();
                if("1".equals(login_state)){
                    //登录成功
                    //   token = json2.get("token").getAsString();
                    Map<String,String> map = Maps.newHashMap();
                    String userId = json2.get("user_id").getAsString();
                    String token = json2.get("token").getAsString();
                    map.put("userId",userId);
                    map.put("mobileTel",json2.get("mobile_tel").getAsString());
                    map.put("userAccount",json2.get("user_account").getAsString());
                    map.put("disableTime",json2.get("disable_time").getAsString());
                    map.put("token",token);
                    map.put("user_password",user_password);
                    map.put("loginTimes",json2.get("loginTimes").getAsString());
                    map.put("loginLastDate",json2.get("loginLastDate").getAsString());
                    map.put("loginLastIp",json2.get("loginLastIp").getAsString());
                    map.put("userType","1");

                    maps.put("data",map);
                    powerStationService.getPsList(map,prefixUrl,APPKR);
                    maps.put("code","200");
                }else{
                    String result_msg = json2.get("result_msg")== null ?json2.get("msg").getAsString(): json2.get("result_msg").getAsString();
                    maps.put("code",login_state);
                    maps.put("msg",result_msg);
                }

            }else{
            String result_msg = json.get("result_msg").getAsString();
                maps.put("code",login_state);
                maps.put("msg",result_msg);
            }

        }catch (Exception e){
            logger.error("Exception CustomerLogin : {}" ,e);
            e.printStackTrace();
            maps.put("code","505");
            maps.put("data",e);
            maps.put("msg","登录失败，e！");
        }


        return maps;
    }

    /**
     * 查询电站信息
    */
    @ResponseBody
    @RequestMapping(value = "getPowerStationByUserId")
    public Object getPowerStation(HttpServletRequest request){
        Map<String,Object> map = Maps.newHashMap();
        try {
            String userId = request.getParameter("userId");
            logger.info("userId : {}",userId );
            PowerStation pow = powerStationService.getPowerStationByuserId(userId);
            logger.info("PowerStation : {}",pow );
            map.put("data",pow);
            map.put("code","200");
        }catch (Exception e){
            map.put("code","205");
            map.put("msg",e);
            e.printStackTrace();
        }
        return map;

    }

    /**
     * 查询电站设备信息列表
    */
    @ResponseBody
    @RequestMapping(value = "getPowerStationDeviceList")
    public Object getPowerStationDeviceList(HttpServletRequest request){
        Map<String,Object> map = Maps.newHashMap();
        try {
            String userId = request.getParameter("userId");
            String id = request.getParameter("id");
            if (StringUtils.isEmpty(userId) || StringUtils.isEmpty(id)){
                map.put("code","205");
                map.put("msg","用户ID、电站id不能为空");
                return map;
            }
            logger.info("userId : {}",userId );
            PowerStation pow = powerStationService.getPowerStationById(id);
            if (pow == null || !userId.equals(pow.getUserId())){
                map.put("code","205");
                map.put("msg","未查询到电站信息");
                return map;
            }
            logger.info("PowerStation : {}",pow );

            List<Record> list = powerStationService.getDeviceList(userId,id);
            List<Object> rds = Lists.newArrayList();
            for(Record rd : list){
                rds.add(rd.getColumns());
            }
            map.put("code","200");
            map.put("data",rds);
        }catch (Exception e){
            map.put("code","205");
            map.put("msg",e);
            e.printStackTrace();
        }
        return map;

    }


    /**
     *获取通知公告
     */
    @RequestMapping(value = "listAnnounceData")
    @ResponseBody
    public Object listAnnounceData(HttpServletRequest request) {
        String userId=request.getParameter("userId");
        Map<String,Object> maps = Maps.newHashMap();
        try {
         Customer customer = customerService.getCustomerByUserId(userId);
            if(customer== null){
                maps.put("code","205");
                maps.put("msg","查无通知公告");
                maps.put("data","");
                return maps;
            }
            PowerStation pow = powerStationService.getPowerStationByuserId(userId);
            Map<String,String> map = getParams(request);
            map.put("customerId",customer.getId());
            LayuiPage<Record> page = new LayuiPage<>(request);
            List<Record> list = smallProgramOrderService.listCustomerMessageList(userId,map,page);
            logger.info("通知公告列表 : list :{}, customerId :{},userId:{} ",list,customer.getId(),userId);
            List<Object> rds = Lists.newArrayList();
            for(Record rd : list){
                rds.add(rd.getColumns());
            }
            maps.put("code","200");
            maps.put("data",rds);
        }catch (Exception e ){
            e.printStackTrace();
            maps.put("code","205");
            maps.put("data","");
            maps.put("msg",e);
        }
        return maps;
    }
    /**
     * 获取通知公告详情
     */
    @RequestMapping(value="getAnnounceDetail")
    public String platemShowDetail(HttpServletRequest request, Model model){
        model.addAttribute("id", request.getParameter("id"));
        CrmPlatformAnnouncement announcement = crmPlatformAnnouncementService.get(request.getParameter("id"));
        model.addAttribute("announcement", announcement);
        model.addAttribute("hasRead", crmPlatformAnnouncementService.hasRead(request.getParameter("id")));
        return "modules/yangguang/msgcenter/platemAppShowDetail";
    }
    @RequestMapping(value = "show")
    @ResponseBody
    public Object show(HttpServletRequest request, Model model) {
        String id = request.getParameter("id");
        model.addAttribute("id", id);
        return crmPlatformAnnouncementService.get(id);
    }
    /**
     * 标记已读的通知公告
     */
    @RequestMapping(value = "appMarkMsgAsRead")
    @ResponseBody
    public Object appMarkMsgAsRead(HttpServletRequest request) {
        String id = request.getParameter("id");
        String userId=request.getParameter("userId");
        logger.info("appMarkMsgAsRead : id :{} ,userId :{}",id,userId);
        User user=new User();
        return crmPlatformAnnouncementService.markAnnounceById(id,user);
    }


    /**
     * 校验是否有电子保修卡
    */
    @ResponseBody
    @RequestMapping(value = "checkLookWarranty")
    public Object checkLookWarranty(HttpServletRequest request){
        String userId = request.getParameter("userId");
        Map<String,String> map = Maps.newHashMap();
        Customer customer = customerService.getCustomerByUserId(userId);
        if(customer == null){
            map.put("code","205");
            map.put("msg","用户信息为空！");
            return map;
        }
        PowerStation pow = powerStationService.getPowerStationByuserId(userId);
        if(pow == null){
            map.put("code","205");
            map.put("msg","用户电站为空！");
            return map;
        }
        if(SourceConstants.ONE.equals(customer.getUserType())){
            //云用户
            long count = warrantyService.getCusWarrantyCouont(customer.getId());
            if(count>0){
                map.put("code","200");
            }else{
                map.put("code","205");
                map.put("msg","查无电子保修卡！");
            }

        }else if(SourceConstants.TWO.equals(customer.getUserType())){
            //注册用户
            Record order = orderService.getOrderByCustomerId(pow.getPsName(),customer.getId());
            if(order == null){
                map.put("code","205");
                map.put("msg","工单信息为空,查无电子保修卡！");
                return map;
            }
            List<Record> list=warrantyService.getWarranty(order.getStr("id"));
            if(list.size() >0){
                map.put("code","200");
            }else{
                map.put("code","205");
                map.put("msg","查无电子保修卡！");
            }
        }
        return map;
    }

    /**
     * 平台查看保修卡页面
     */
    @RequestMapping(value="lookWarranty")
    public String toLookWarranty(HttpServletRequest request,Model model){
        String userId = request.getParameter("userId");
        PowerStation pow = powerStationService.getPowerStationByuserId(userId);
        Customer custo = customerService.getCustomerByUserId(userId);
        CustomerWarranty warranty=null;
        List<Record> list = null;
        if(SourceConstants.ONE.equals(custo.getUserType())){
            //云用户
            warranty = warrantyService.getByCustomerId(custo.getId());
            list=warrantyService.getByWarrantyId(warranty.getId());
        }else if(SourceConstants.TWO.equals(custo.getUserType())){
            //查询是否有工单
            Record card = orderService.getOrderByCustomerId(pow.getPsName(),custo.getId());
            if(card != null){
                warranty = warrantyService.getByOrderId(card.getStr("id"));
                list=customerService.getWarranty(card.getStr("id"));
            }
        }
        model.addAttribute("card", warranty);
        model.addAttribute("list", list);
        return "modules/order/platform/applookForm";
    }

    /**
     * 获取预约时间段
    */
    @ResponseBody
    @RequestMapping(value = "getOrderpromiseLimit")
    public Object  getOrderpromiseLimit(HttpServletRequest request){
        Map<String,Object> map = Maps.newHashMap();
        List<String> list = Lists.newArrayList();
        //6:00-8:00，8:00-10:00，10:00-12:00，12:00-14:00，14:00-16:00，16:00-18:00，18:00-20:00，20:00-22:00
        list.add("6:00-8:00");
        list.add("8:00-10:00");
        list.add("10:00-12:00");
        list.add("12:00-14:00");
        list.add("14:00-16:00");
        list.add("16:00-18:00");
        list.add("18:00-20:00");
        list.add("20:00-22:00");
        list.add("22:00-24:00");
        map.put("code","200");
        map.put("data",list);
        return map;
    }

    @RequestMapping(value = "useProtocol")
    public String indexHistory(HttpServletRequest request) {
        return "modules/yangguang/useProtocol";
    }


    @ResponseBody
    @RequestMapping(value = "getUnreadCount")
    public Object getUnreadCount(HttpServletRequest request){
        Map<String,Object> maps = Maps.newHashMap();
        String userId = request.getParameter("userId");
        Customer customer = customerService.getCustomerByUserId(userId);
        if(customer == null){
            maps.put("code","200");
            maps.put("data",0);
            return maps;
        }
        String customerId=customer.getId();
        if(StringUtil.isBlank(customerId)){
            maps.put("code","200");
            maps.put("msg","查无通知公告");
            maps.put("data",0);
            return maps;
        }
        logger.info("PowerStation : {} , customerId : {} " ,customer ,customerId);

        long count = smallProgramOrderService.getWeiDuCount(userId,customerId);
        logger.info("getUnreadCount :  count {} ",count);
        maps.put("code","200");
        maps.put("data",count);
        return maps;
    }

    /**
     * 用户注册
     *
    */
    @RequestMapping(value = "CustomerRegister")
    @ResponseBody
    public Object CustomerRegister(HttpServletRequest request){
        Map<String ,Object> map = Maps.newHashMap();
        Map<String,String> maps = getParams(request);
        try {
         String code =   customerService.CustomerRegister(maps);
         if("502".equals(code)){
             map.put("data","该手机号已存在");
             map.put("msg","注册失败,该手机号已存在!");
         }else{
             map.put("msg","");
         }
            map.put("code",code);
            map.put("data","注册成功");

        }catch (Exception e){
            e.printStackTrace();
            map.put("code","501");
            map.put("data",e);
            map.put("msg","注册失败!");
        }
        return map;
    }

    /**
     * 校验手机号是否存在
    */
    @ResponseBody
    @RequestMapping(value = "checkMobile")
    public Object checkMobile(HttpServletRequest request){
        Map<String ,Object> map = Maps.newHashMap();
        String mobile = request.getParameter("mobile");
        String userType = request.getParameter("userType");
        if(StringUtil.isBlank(mobile)){
            map.put("code","501");
            map.put("data","手机号为空！");
            map.put("msg","手机号为空!");
            return map;
        }
        long coount = customerService.checkMobile(mobile,userType);
        if(coount > 0){
            map.put("code","200");
            map.put("data","手机号已存在");
            map.put("msg","502");
            return map;
        }
        map.put("code","200");
        map.put("data","可以注册");
        map.put("msg","");
        return map;
    }

    /**
     * 点击获取验证码按钮---获取验证码
     * @param request
     * @return
     */
    @RequestMapping(value = "sendMsg")
    @ResponseBody
    public Object sendMsg(HttpServletRequest request){
        Map<String,Object> map = Maps.newHashMap();
        String mobile = request.getParameter("mobile");//手机号
        String code = randomCode();//获取生成的随机数
        HttpSession session = request.getSession();
        String checkMobile = "";

        if(StringUtils.isNotBlank(mobile) && StringUtils.isNotBlank(code)){
            checkMobile =customerService.sendingMobile(mobile,code);//发送短信
            if(!"200".equals(checkMobile)){
                map.put("msg","发送失败！");
            }
            map.put("code",checkMobile);
            cacheService.setex(mobile+"confirmCode",5*60,code);
        }else{
            map.put("code","201");
            map.put("msg","手机号为空");
        }
      //  map.put("data",code);
        map.put("sessionId",session.getId());
        return map;
    }


    /**
     * 获取4位随机验证码
     * @return
     */
    public String randomCode(){
        Random rand = new Random();
        String code = "";
        for(int i=0;i<4;i++){
            code = code + String.valueOf(rand.nextInt(10));
        }
        return code;
    }

    /**
     * 校验输入的验证码和手机获取的是否一致
     *
     * @param request
     * @return
     */
    @RequestMapping(value = "checkConfirmCode")
    @ResponseBody
    public String checkConfirmCode(HttpServletRequest request) {
        String code = request.getParameter("confirmCode");
        String mobile = request.getParameter("mobile");
        String password = request.getParameter("password");
        String confirmCode = cacheService.get(mobile+"confirmCode");
        if (StringUtil.isBlank(confirmCode)) {//如果有,就直接返回
            //请重新发送短信
            logger.error("checkConfirmCode:confirmCode is null");
            return "notidNull";
        }
        String codeMsg = confirmCode;
        String checkConfirmCode = "";
        if (StringUtils.isNotBlank(code) && StringUtils.isNotBlank(mobile) && StringUtils.isNotBlank(codeMsg)) {
            checkConfirmCode = registerService.checkConfirmCode(code, codeMsg, mobile);
        }
        try {
            if("ok".equals(checkConfirmCode)){
                cacheService.del(mobile+"confirmCode");
                customerService.ResetTraderPassword(mobile,HashKit.md5(password));
            }
        } catch (BusinessException e) {
            checkConfirmCode = e.getMessage();
        } catch (Exception e) {
            logger.error(e.getMessage());
        }

        return checkConfirmCode;
    }



    /**
     * 注册用户修改密码
    */
    @ResponseBody
    @RequestMapping(value = "updatePassword")
    public Object updatePassword(HttpServletRequest request){
        //新密码
        String newPassword = request.getParameter("newPassword");
        //旧密码
        String password = request.getParameter("password");
        //手机号
        String mobile = request.getParameter("mobile");
        //userId
        String userId = request.getParameter("userId");
        return customerService.updatePasswork(mobile,password,newPassword,userId);
    }


    /**
     * 忘记密码
     */
    @ResponseBody
    @RequestMapping(value = "ResetPassword")
    public Object ResetPassword(HttpServletRequest request){
        Map<String,Object> map = Maps.newHashMap();
        try {
            String mobile = request.getParameter("mobile");
            String password = request.getParameter("password");
            if(StringUtil.isEmpty(mobile) || StringUtil.isEmpty(password)){
                map.put("code","205");
                map.put("data","");
                map.put("msg","手机号或者密码为空");
                return map;
            }
            customerService.ResetPassword(mobile,HashKit.md5(password));
            map.put("code","200");
            map.put("data","");
            map.put("msg","修改密码成功");
        }catch (Exception e){
            e.printStackTrace();
            map.put("code","205");
            map.put("data",e);
            map.put("msg","修改密码失败");
        }
        return map;
    }

    /**
     * 获取电站下设备信息
    */
    @ResponseBody
    @RequestMapping(value = "getListByPowerStationId")
    public Object getListByPowerStationId(HttpServletRequest request){
        Map<String,Object> map = Maps.newHashMap();
        try {
            String id = request.getParameter("id");
            String userId = request.getParameter("userId");
            List<PowerStationDevice> list = powerStationService.getListByPowerStationId(id,userId);
            map.put("code","200");
            map.put("data",list);
            map.put("msg","");
        }catch (Exception e){
            e.printStackTrace();
            map.put("code","205");
            map.put("data",e);
            map.put("msg","获取数据失败");
        }
        return map;
    }

    /**
     * 删除电站下设备信息
     */
    @ResponseBody
    @RequestMapping(value = "updateStatus")
    public Object updateStatus(HttpServletRequest request){
        Map<String,Object> map = Maps.newHashMap();
        try {
            String id = request.getParameter("id");
            String userId = request.getParameter("userId");
           Customer cus = customerService.getCustomerByUserId(userId);
           if(SourceConstants.TWO.equals(cus.getUserType())){
               powerStationService.updateStatus(id);
               map.put("code","200");
               map.put("data","删除成功");
               map.put("msg","删除成功");
           }else{
               map.put("code","205");
               map.put("data","非注册用户不可删除");
               map.put("msg","非注册用户不可删除");
           }
        }catch (Exception e){
            e.printStackTrace();
            map.put("code","205");
            map.put("data",e);
            map.put("msg","删除失败");
        }
        return map;
    }

    /**
     * 获取设备类型
    */
    @ResponseBody
    @RequestMapping(value = "getdeviceType")
    public Object getdeviceType(HttpServletRequest request){
        Map<String,Object> maps = Maps.newHashMap();
        List<Map<String,String>> list = Lists.newArrayList();
        Map<String,String> map = Maps.newHashMap();
        map.put("typekey","1");
        map.put("typename","逆变器");
        list.add(map);
        Map<String,String> map2 = Maps.newHashMap();
        map2.put("typekey","4");
        map2.put("typename","汇流箱");
        Map<String,String> map3 = Maps.newHashMap();
        map3.put("typekey","6");
        map3.put("typename","变压器");
        Map<String,String> map4 = Maps.newHashMap();
        map4.put("typekey","11");
        map4.put("typename","电站");
        Map<String,String> map5 = Maps.newHashMap();
        map5.put("typekey","22");
        map5.put("typename","无线设备");
        list.add(map2);
        list.add(map3);
        list.add(map4);
        list.add(map5);
        maps.put("code","200");
        maps.put("data",list);
        maps.put("msg","");
     return maps;
    }

    /**
     * 新增设备信息
    */
    @ResponseBody
    @RequestMapping(value = "savePowerStationDevice")
    public Object savePowerStationDevice(HttpServletRequest request,PowerStationDevice device){
        Map<String,Object> map = Maps.newHashMap();
        try {
            device.setCreateTime(new Date());
            powerStationService.savePowerStationDevice(device);
            map.put("code","200");
            map.put("data",device);
            map.put("msg","保存成功");
        }catch (Exception e){
            e.printStackTrace();
            map.put("code","205");
            map.put("data",e);
            map.put("msg","添加失败");
        }
        return map;
 }

    /**
     * 修改电站信息
     */
    @ResponseBody
    @RequestMapping(value = "updateStationDevice")
    public Object updateStationDevice(HttpServletRequest request){
        Map<String,Object> maps = Maps.newHashMap();
        try {
            Map<String,String> map = getParams(request);
            if(StringUtil.isEmpty(map.get("id"))){
                maps.put("code","205");
                maps.put("data","数据有误");
                maps.put("msg","修改失败，数据有误");
                return maps;
            }
            String userId = map.get("userId");
            Customer cus = customerService.getCustomerByUserId(userId);
            if(SourceConstants.TWO.equals(cus.getUserType())){
                powerStationService.updateStationDevice(map);
                maps.put("code","200");
                maps.put("data","修改成功");
                maps.put("msg","修改成功");
            }else{
                maps.put("code","205");
                maps.put("data","非注册用户不可修改");
                maps.put("msg","非注册用户不可修改");
            }
        }catch (Exception e){
            e.printStackTrace();
            maps.put("code","205");
            maps.put("data",e);
            maps.put("msg","修改失败");
        }
        return maps;
    }



    /**
         * 修改设备信息
        */
        @ResponseBody
        @RequestMapping(value = "updatePowerStation")
        public Object updatePowerStation(HttpServletRequest request){
            Map<String,Object> maps = Maps.newHashMap();
            try {
                Map<String,String> map = getParams(request);
                if(StringUtil.isEmpty(map.get("id"))){
                    maps.put("code","205");
                    maps.put("data","数据有误");
                    maps.put("msg","修改失败，数据有误");
                    return maps;
                }
                String userId = map.get("userId");
                Customer cus = customerService.getCustomerByUserId(userId);
                if(SourceConstants.TWO.equals(cus.getUserType())){
                    powerStationService.updatePowerStation(map);
                    maps.put("code","200");
                    maps.put("data","修改成功");
                    maps.put("msg","修改成功");
                }else{
                    maps.put("code","205");
                    maps.put("data","非注册用户不可修改");
                    maps.put("msg","非注册用户不可修改");
                }
            }catch (Exception e){
                e.printStackTrace();
                maps.put("code","205");
                maps.put("data",e);
                maps.put("msg","修改失败");
            }
            return maps;
        }

        /**
         * 获取省市区
        */
        @ResponseBody
        @RequestMapping(value = "getProvinceCityDistrictData")
        public Object getProvinceCityDistrictData(HttpServletRequest request){
            Map<String,Object> map = Maps.newHashMap();
            try {
                List<PCDInfo> pcdes = AddressUtils.getProvinceCityDistrictData();
                map.put("code","200");
                map.put("data",pcdes);
                map.put("msg","保存成功");
            }catch (Exception e){
                e.printStackTrace();
                map.put("code","205");
                map.put("data",e);
                map.put("msg","添加失败");
            }
            return map;
        }

    @RequestMapping(value = "getPlanByTest")
    @ResponseBody
    public Object getPlanByTest(@RequestBody String body) throws Exception {

        logger.info("request body is:{}", body);
        HashMap<String, Object> aprams = new HashMap<>();
        aprams.put("ok", "ok");
        return aprams;
    }

}
