package com.platform.yijia.controller;
import com.alibaba.fastjson.JSONObject;
import com.google.gson.Gson;
import com.platform.yijia.param.request.StationRequest;
import com.platform.yijia.param.response.StationInfoVo;
import com.platform.yijia.pojo.*;
import com.platform.yijia.service.AppUserInfoService;
import com.platform.yijia.service.StationNoticeManageService;
import com.platform.yijia.service.StationOilGunService;
import com.platform.yijia.service.StationService;
import com.platform.yijia.utils.CodeMsg;
import com.platform.yijia.utils.FeiEPrinterUtil;
import com.platform.yijia.utils.ResultData;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/demo")
public class StationController {
    @Resource
    private StationService stationService;

    @Resource
    private StationNoticeManageService stationNoticeManageService;
    @Resource
    private StationOilGunService stationOilGunService;
    @Resource
    private AppUserInfoService appUserInfoService;

    //获取油站公众号ID
    @RequestMapping(value = "/getStationGzhAppId", method = RequestMethod.GET)
    @ResponseBody
    public String getStationGzhAppId(@RequestParam Integer stationId){
        Gson gson =new Gson();
        //返回结果集
        ResultData resultData=null;
        if(stationId !=null){
            Map<String, String> m = stationService.getStationAppIdAndAppSecret(stationId);
            if(m !=null && m.containsKey("gzhAppId")) {
                String gzhAppId = m.get("gzhAppId");
                Map<String, Object> map = new HashMap<>();
                map.put("appId", gzhAppId);
                resultData =ResultData.success(map);
            }else{
                resultData =ResultData.error(CodeMsg.REQUEST_FAIL);
            }
        }else {
            resultData =ResultData.error(CodeMsg.REQUEST_FAIL);
        }
        return gson.toJson(resultData);
    }

    //获取油站信息
    @RequestMapping(value = "/selectStationPayManage", method = RequestMethod.GET)
    @ResponseBody
    public String selectStationPayManage(@RequestParam Integer stationId){
        Gson gson =new Gson();
        //返回结果集
        ResultData resultData=null;
        if(stationId !=null){
            StationPayManage stationPayManage = new StationPayManage();
            stationPayManage.setStationId(stationId);
            StationPayManage stationPayManageInfo = stationService.selectStationPayManage(stationPayManage);
            resultData =ResultData.success(stationPayManageInfo);
        }else {
            resultData =ResultData.error(CodeMsg.REQUEST_FAIL);
        }
        return gson.toJson(resultData);
    }


    //获取油站信息
    @RequestMapping(value = "/getStationInfo", method = RequestMethod.GET)
    @ResponseBody
    public String getStationInfo(@RequestParam Integer stationId){
        Gson gson =new Gson();
        //返回结果集
        ResultData resultData=null;
        if(stationId !=null){
            StationInfo stationInfo = new StationInfo();
            stationInfo.setStationId(stationId);
            StationInfo stationInfos = stationService.selectStationInfo(stationInfo);
            resultData =ResultData.success(stationInfos);
        }else {
            resultData =ResultData.error(CodeMsg.REQUEST_FAIL);
        }
        return gson.toJson(resultData);
    }

    //获取油站公告信息
    @RequestMapping(value = "/getStationNoticeInfo", method = RequestMethod.GET)
    @ResponseBody
    public String getStationNoticeInfo(@RequestParam Integer stationId){
        Gson gson =new Gson();
        //返回结果集
        ResultData resultData=null;
        if(stationId !=null){
            Map<String, Object> stationNoticeInfo = stationService.getStationNoticeInfo(stationId);
            resultData =ResultData.success(stationNoticeInfo);
        }else {
            resultData =ResultData.error(CodeMsg.REQUEST_FAIL);
        }
        return gson.toJson(resultData);
    }

    //获取油站轮播图
    @RequestMapping(value = "/getStationPicList", method = RequestMethod.GET)
    @ResponseBody
    public String getStationPicList(@RequestParam Integer stationId){
        Gson gson =new Gson();
        //返回结果集
        ResultData resultData=null;
        if(stationId !=null){
            StationPic stationPic = new StationPic();
            stationPic.setStationId(stationId);
            List<StationPic> stationPicList = stationService.getStationPicList(stationPic);
            resultData =ResultData.success(stationPicList);
        }else {
            resultData =ResultData.error(CodeMsg.REQUEST_FAIL);
        }
        return  gson.toJson(resultData);
    }

    /**
     * 根据坐标获取油站列表
     */
    @RequestMapping(value = "/getStationInfoList", method = RequestMethod.GET)
    @ResponseBody
     public String getStationInfoList(@RequestParam String stationLongitude,String stationLatitude,Integer pageNum,Integer pageSize, String appId){
        Gson gson =new Gson();
        //返回结果集
        ResultData resultData=null;
        if(stationLongitude!=null && stationLatitude !=null){
            if(StringUtils.isNotBlank(stationLongitude)&&StringUtils.isNotBlank(stationLatitude)){
                StationRequest stationRequest=new StationRequest();
                stationRequest.setStationLatitude(stationLatitude);
                stationRequest.setStationLongitude(stationLongitude);
                stationRequest.setPageNum(pageNum);
                stationRequest.setPageSize(pageSize);
                stationRequest.setAppId(appId);
                //调用接口 根据坐标筛选距离最近的加油站
                StationInfoVo stationInfoVo = stationService.stationInfoList(stationRequest);
                resultData=ResultData.success(stationInfoVo);
            }else{
                resultData=ResultData.error(CodeMsg.REQUEST_FAIL);
            }
        }else{
            resultData=ResultData.error(CodeMsg.REQUEST_FAIL);
        }
        return gson.toJson(resultData);
    }
    /**
     * 根据公众号appId获取油站列表信息
     */
    @RequestMapping(value = "/getGzhStationList", method = RequestMethod.GET)
    @ResponseBody
    public String getGzhStationList(@RequestParam String stationLongitude,String stationLatitude,Integer pageNum,Integer pageSize, String appId){
        Gson gson =new Gson();
        //返回结果集
        ResultData resultData=null;
        if(stationLongitude!=null && stationLatitude !=null){
            if(StringUtils.isNotBlank(stationLongitude)&&StringUtils.isNotBlank(stationLatitude)){
                StationRequest stationRequest=new StationRequest();
                stationRequest.setStationLatitude(stationLatitude);
                stationRequest.setStationLongitude(stationLongitude);
                stationRequest.setPageNum(pageNum);
                stationRequest.setPageSize(pageSize);
                stationRequest.setAppId(appId);
                //调用接口 根据坐标筛选距离最近的加油站
                StationInfoVo stationInfoVo = stationService.gzhStationInfoList(stationRequest);
                resultData=ResultData.success(stationInfoVo);
            }else{
                resultData=ResultData.error(CodeMsg.REQUEST_FAIL);
            }
        }else{
            resultData=ResultData.error(CodeMsg.REQUEST_FAIL);
        }
        return gson.toJson(resultData);
    }
    /**
     * 添加油站信息
     */
    @RequestMapping(value = "/addStationInfo", method = RequestMethod.POST)
    @ResponseBody
    public String addStationInfo(@RequestParam StationRequest request){
        Gson gson =new Gson();
        ResultData resultData=null;
        StationInfo stationInfo =new StationInfo();
        if(request.getStationId()!=null){
            stationInfo.setStationId(request.getStationId());
        }
        if(StringUtils.isNotBlank(request.getStationName())){
            stationInfo.setStationName(request.getStationName());
        }
        if(StringUtils.isNotBlank(request.getContacts())){
            stationInfo.setContacts(request.getContacts());
        }
        if(StringUtils.isNotBlank(request.getPhone())){
            stationInfo.setPhone(request.getPhone());
        }
        if(StringUtils.isNotBlank(request.getStationAddress())){
            stationInfo.setStationAddress(request.getStationAddress());
        }
        if(request.getStationGroupId()!=null){
            stationInfo.setStationGroupId(request.getStationGroupId());
        }
        if(request.getStationGroupName()!=null){
            stationInfo.setStationGroupName(request.getStationGroupName());
        }
        if(request.getContacts()!=null){
            stationInfo.setContacts(request.getContacts());
        }

        try{
            stationService.AddStationInfo(stationInfo);
            resultData=ResultData.success(CodeMsg.SUCCESS);
        }catch (Exception e){
            resultData=ResultData.error(CodeMsg.INSERT_FAIL);
            e.printStackTrace();
        }
        return gson.toJson(resultData);
    }

    /**
     * 修改油站信息
     */
    @RequestMapping(value = "/updateStationInfo", method = RequestMethod.POST)
    @ResponseBody
    public String updateStationInfo(@RequestBody StationInfo stationInfo){
        Gson gson=new Gson();
        ResultData resultData=null;
        try{
            stationService.updateStationInfo(stationInfo);
            resultData=ResultData.success(CodeMsg.SUCCESS);
        }catch (Exception e){
            resultData=ResultData.error(CodeMsg.UPDATE_FAIL);
            e.printStackTrace();
        }
        return gson.toJson(resultData);
    }
    /**
     * 删除油站信息
     */
    @RequestMapping(value = "/deleteStationInfo", method = RequestMethod.POST)
    @ResponseBody
    public String deleteStationInfo(@RequestBody StationInfo stationInfo){
        Gson gson=new Gson();
        ResultData resultData=null;
        try{
            stationService.deleteStationInfo(stationInfo);
            resultData=ResultData.success(CodeMsg.SUCCESS);
        }catch (Exception e){
            resultData=ResultData.error(CodeMsg.DELETE_FAIL);
            e.printStackTrace();
        }
        return gson.toJson(resultData);
    }
    /**
     * 油站设备管理查询
     */
    @RequestMapping(value = "/getStationNoticeManageList", method = RequestMethod.GET)
    @ResponseBody
    public String getStationNoticeManageList(@RequestParam StationRequest request){
        Gson gson=new Gson();
        ResultData resultData=null;
        try{
            StationNoticeManage stationNoticeManage =new StationNoticeManage();
            List<StationNoticeManage> stationNoticeManageList = stationNoticeManageService.getStationNoticeManageList(stationNoticeManage);
            resultData=ResultData.success(stationNoticeManageList);
        }catch (Exception e){
            resultData=ResultData.error(CodeMsg.SEARCH_FAIL);
            e.printStackTrace();
        }
        return gson.toJson(resultData);
    }
    /**
     *修改油站设备管理
     */
    @RequestMapping(value = "/updateStationNoticeManage", method = RequestMethod.POST)
    @ResponseBody
    public String updateStationNoticeManage(@RequestBody StationNoticeManage stationNoticeManage){
        Gson gson=new Gson();
        ResultData resultData=null;
        try{
            stationNoticeManageService.updateStationNoticeManage(stationNoticeManage);
            resultData=ResultData.success(CodeMsg.SUCCESS);
        }catch (Exception e){
            resultData=ResultData.success(CodeMsg.UPDATE_FAIL);
            e.printStackTrace();
        }
        return gson.toJson(resultData);
    }
    /**
     * 删除油站设备信息
     */
    @RequestMapping(value = "/deleteStationNoticeManage", method = RequestMethod.POST)
    @ResponseBody
    public String deleteStationNoticeManage(@RequestBody StationNoticeManage stationNoticeManage){
        Gson gson=new Gson();
        ResultData resultData=null;
        try{
            stationNoticeManageService.deleteStationNoticeManage(stationNoticeManage);
             resultData=ResultData.success(CodeMsg.SUCCESS);
        }catch (Exception e){
            resultData=ResultData.success(CodeMsg.DELETE_FAIL);
            e.printStackTrace();
        }
        return gson.toJson(resultData);
    }

    /**
     * 油枪管理
     */
    @RequestMapping(value = "/stationOilGunList", method = RequestMethod.GET)
    @ResponseBody
   // public void stationOilGunList(@RequestParam StationRequest request,HttpServletResponse response){
//    public String stationOilGunList(@RequestParam String userType,String token,Integer stationId){
      public String stationOilGunList(@RequestParam Integer stationId){
        Gson gson =new Gson();
        StationOilGun stationOilGun =new StationOilGun();
        //查询登录人是否存在
//        AppUserInfo appUserInfo =new AppUserInfo();
        //订单类型 是小程序还是公众号
//        appUserInfo.setUserType(userType);
//        if(userType.equals("1")){
//            //1 是公众号
//            appUserInfo.setBlogToken(token);
//        }else if(userType.equals("2")){
//            //2是小程序
//            appUserInfo.setMinaToken(token);
//        }
//        List<AppUserInfo> appUserInfoList =appUserInfoService.Authentication(appUserInfo);
//        if(appUserInfoList!=null&&appUserInfoList.size()>0){
            //获取油站id根据油站号查询油站对应的所有油枪信息
            stationOilGun.setStationId(stationId);
            List<StationOilGun> stationOilGunList = stationOilGunService.stationOilGunList(stationOilGun);
            ResultData resultData=ResultData.success(stationOilGunList);
            return gson.toJson(resultData);
//        }else {
//            ResultData resultData=ResultData.error(CodeMsg.USER_NOT_EXSIST);
//            return gson.toJson(resultData);
//        }
    }
}
