package com.platform.yijia.controller;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.platform.yijia.config.YiJiaRequest;
import com.platform.yijia.dao.PayOrderMapper;
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.BaiduMapUtil;
import com.platform.yijia.utils.CodeMsg;
import com.platform.yijia.utils.FeiEPrinterUtil;
import com.platform.yijia.utils.ResultData;
import net.sf.json.JSONObject;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@YiJiaRequest()
@CrossOrigin
public class StationController {

    private static Logger logger =(Logger) LoggerFactory.getLogger(StationController.class);

    @Resource
    private StationService stationService;

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


    //获取油站列表
    @RequestMapping(value = "/getStationListByAppId", method = RequestMethod.GET)
    @ResponseBody
    public String getStationListByAppId(@RequestParam String appId){
        Gson gson =new Gson();
        //返回结果集
        ResultData resultData=null;
        if(StringUtils.isNotBlank(appId)){
            StationInfo stationInfo = new StationInfo();
            stationInfo.setAppId(appId);
            logger.info("无经纬度获取油站列表所需参数appID："+appId);
            List<StationInfo> stationList = stationService.getStationListByAppId(stationInfo);
            logger.info("无经纬度油站列表："+stationList.toString());
            resultData = ResultData.success(stationList);
        }else {
            resultData = ResultData.error(CodeMsg.REQUEST_FAIL);
        }

        return gson.toJson(resultData);
    }

    //获取油站公众号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);
                logger.info("该油站公众号：" +gzhAppId);
                resultData =ResultData.success(map);
            }else{
                resultData =ResultData.error(CodeMsg.SEARCH_APPID_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 = "/getStationAddress", method = RequestMethod.GET)
    @ResponseBody
    public String getStationAddress(@RequestParam String lng, @RequestParam String lat){
        Gson gson =new Gson();
        //返回结果集
        ResultData resultData=null;
        JSONObject location = BaiduMapUtil.getLocation(lng, lat);
        if(location !=null && location.containsKey("status") && location.get("status").toString().equals("0")){
            JSONObject result = location.getJSONObject("result").getJSONObject("addressComponent");
            Map<String, Object> m = new HashMap<>();
            m.put("address", result.getString("street"));
            resultData = ResultData.success(m);
        }else {
            resultData = ResultData.error(CodeMsg.YEWU_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){
            StationInfo stationPic = new StationInfo();
            stationPic.setStationId(stationId);
            StationInfo stationPics = stationService.getStationPicList(stationPic);
            List<String>  listPic = new ArrayList<>();
            if(stationPics != null && StringUtils.isNotBlank(stationPics.getImage1())){
                listPic.add(stationPics.getImage1());
            }
            if(stationPics != null && StringUtils.isNotBlank(stationPics.getImage2())){
                listPic.add(stationPics.getImage2());
            }
            if(stationPics != null && StringUtils.isNotBlank(stationPics.getImage3())){
                listPic.add(stationPics.getImage3());
            }
            if(stationPics != null && StringUtils.isNotBlank(stationPics.getImage4())){
                listPic.add(stationPics.getImage4());
            }
            if(stationPics != null && StringUtils.isNotBlank(stationPics.getImage5())){
                listPic.add(stationPics.getImage5());
            }
            Map map = new HashMap();
            map.put("notice", stationPics.getNotice());
            map.put("listPic", listPic);
            resultData =ResultData.success(map);
        }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(StringUtils.isNotBlank(appId)){
            logger.info("有经纬度获取油站列表所需参数appID："+appId);
            StationRequest stationRequest = new StationRequest();
            //StationInfo stationInfo = new StationInfo();
            stationRequest.setAppId(appId);
            //stationInfo.setAppId(appId);
            if(StringUtils.isNotBlank(stationLatitude) && StringUtils.isNotBlank(stationLongitude)){
                stationRequest.setStationLatitude(stationLatitude);
                stationRequest.setStationLongitude(stationLongitude);
                stationRequest.setPageNum(pageNum);
                stationRequest.setPageSize(pageSize);
                //调用接口 根据坐标筛选距离最近的加油站
                StationInfoVo stationInfoVo = stationService.stationInfoList(stationRequest);
                logger.info("有经纬度获取油站列表："+stationInfoVo.toString());
                resultData=ResultData.success(stationInfoVo);
            }else {
                resultData=ResultData.error(CodeMsg.REQUEST_FAIL);
            }
        }else{
            resultData=ResultData.error(CodeMsg.REQUEST_FAIL);
        }
        return gson.toJson(resultData);
    }

    /**
     * 根据坐标获取油站列表 新的 2021年8月2日16:13:36
     */
    @RequestMapping(value = "/getStationInfoListNew", method = RequestMethod.GET)
    @ResponseBody
    public String getStationInfoListNew(@RequestParam String stationLongitude,String stationLatitude,Integer pageNum,Integer pageSize, String appId){
        Gson gson =new Gson();
        //返回结果集
        ResultData resultData= null;
        if(StringUtils.isNotBlank(appId)){
            logger.info("有经纬度获取油站列表所需参数appID："+appId);
            StationRequest stationRequest = new StationRequest();
            //StationInfo stationInfo = new StationInfo();
            stationRequest.setAppId(appId);
            //stationInfo.setAppId(appId);
            if(StringUtils.isNotBlank(stationLatitude) && StringUtils.isNotBlank(stationLongitude)){
                stationRequest.setStationLatitude(stationLatitude);
                stationRequest.setStationLongitude(stationLongitude);
                stationRequest.setPageNum(pageNum);
                stationRequest.setPageSize(pageSize);
                //调用接口 根据坐标筛选距离最近的加油站
                StationInfoVo stationInfoVo = stationService.stationInfoList(stationRequest);
                logger.info("有经纬度获取油站列表："+stationInfoVo.toString());
                JSONObject location = BaiduMapUtil.getLocation(stationLongitude, stationLatitude);
                if(location !=null && location.containsKey("status") && location.get("status").toString().equals("0")){
                    JSONObject result = location.getJSONObject("result").getJSONObject("addressComponent");
                    if(!result.getString("street").equals("")){
                        stationInfoVo.setAddress(result.getString("street"));
                    }else {
                        stationInfoVo.setAddress("未知街道");
                    }
                    resultData=ResultData.success(stationInfoVo);
                }else {
                    resultData=ResultData.error(CodeMsg.BAIDUMAP_FAIL);
                }
            }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);
    }

    /**
     * 根据加油员ID获取油枪
     * @param personnelId
     * @return
     */
    @RequestMapping(value = "/selectOilGunByPersonalId", method = RequestMethod.GET)
    @ResponseBody
    public String selectOilGunByPersonalId(@RequestParam String personnelId){
        Gson gson=new Gson();
        ResultData resultData=null;
        try{
            Map<String, Object> params = new HashMap<>();
            params.put("personnelId", personnelId);
            List<Map<String, Object>> list = stationOilGunService.selectOilGunByPersonalId(params);
            resultData=ResultData.success(list);
        }catch (Exception e){
            resultData=ResultData.success(CodeMsg.SEARCH_FAIL);
            e.printStackTrace();
        }
        return gson.toJson(resultData);
    }


    /**
     * 获取油站加油员信息
     * @param stationId
     * @return
     */
    @RequestMapping(value = "/selectPersonnelName", method = RequestMethod.GET)
    @ResponseBody
    public String selectPersonnelName(@RequestParam Integer stationId){
        Gson gson=new Gson();
        ResultData resultData=null;
        try{
            HashMap<String, Object> params = new HashMap<>();
            params.put("stationId", stationId);
            List<Map> mapResultList = payOrderMapper.selectPersonelName(params);    //使用list原因：一个枪号对应多个加油员场景
            resultData=ResultData.success(mapResultList);
        }catch (Exception e){
            resultData=ResultData.success(CodeMsg.SEARCH_FAIL);
            e.printStackTrace();
        }
        return gson.toJson(resultData);
    }


    /**
     * 油枪管理
     */
    @RequestMapping(value = "/stationOilGunList", method = RequestMethod.GET)
    @ResponseBody
    public String stationOilGunList(@RequestParam Integer stationId, String personnelId){
        Gson gson =new GsonBuilder().setDateFormat("yyyy-MM-dd HH:mm:ss").create();
        ResultData resultData = null;
        try {
            if(personnelId !=null && personnelId !=""){
                Map<String, Object> params = new HashMap<>();
                params.put("personnelId", personnelId);
                params.put("stationId", stationId);
                List<Map<String, Object>> list = stationOilGunService.selectOilGunByPersonalId(params);
                resultData=ResultData.success(list);
            }else {
                StationOilGun stationOilGun =new StationOilGun();
                stationOilGun.setStationId(stationId);
                List<StationOilGun> stationOilGunList = stationOilGunService.stationOilGunList(stationOilGun);
                resultData=ResultData.success(stationOilGunList);
            }
        }catch (Exception e){
            resultData=ResultData.success(CodeMsg.SEARCH_FAIL);
            e.printStackTrace();
        }
        return gson.toJson(resultData);
    }

    /**
     * 油枪管理 :新版接口：2021年8月2日
     */
    @RequestMapping(value = "/stationOilGunListNew", method = RequestMethod.GET)
    @ResponseBody
    public String stationOilGunListNew(@RequestParam Integer stationId, @RequestParam String personnelId){
        logger.info("获取油枪接口，油站ID：" + stationId + "; 加油员ID：" + personnelId);
        Gson gson =new GsonBuilder().setDateFormat("yyyy-MM-dd HH:mm:ss").create();
        ResultData resultData = null;
        try {
            Map<String, Object> resultMap = new HashMap<>();

            //获取油站加油员列表
            HashMap<String, Object> params_1 = new HashMap<>();
            params_1.put("stationId", stationId);
            List<Map> personnalList = payOrderMapper.selectPersonelName(params_1);    //使用list原因：一个枪号对应多个加油员场景
            resultMap.put("personnalList", personnalList);

            //获取油站图片和公告
            StationInfo stationPic = new StationInfo();
            stationPic.setStationId(stationId);
            StationInfo stationPics = stationService.getStationPicList(stationPic);
            List<String>  listPic = new ArrayList<>();
            if(stationPics != null && StringUtils.isNotBlank(stationPics.getImage1())){
                listPic.add(stationPics.getImage1());
            }
            if(stationPics != null && StringUtils.isNotBlank(stationPics.getImage2())){
                listPic.add(stationPics.getImage2());
            }
            if(stationPics != null && StringUtils.isNotBlank(stationPics.getImage3())){
                listPic.add(stationPics.getImage3());
            }
            if(stationPics != null && StringUtils.isNotBlank(stationPics.getImage4())){
                listPic.add(stationPics.getImage4());
            }
            if(stationPics != null && StringUtils.isNotBlank(stationPics.getImage5())){
                listPic.add(stationPics.getImage5());
            }
            resultMap.put("notice", stationPics.getNotice());
            resultMap.put("listPic", listPic);

            //获取油枪
            if(personnelId !=null && personnelId !="" && !personnelId.equals("undefined")){
                Map<String, Object> params = new HashMap<>();
                params.put("personnelId", personnelId);
                params.put("stationId", stationId);
                List<Map<String, Object>> stationOilGunList = stationOilGunService.selectOilGunByPersonalId(params);
                resultMap.put("stationOilGunList", stationOilGunList);
                //resultData=ResultData.success(stationOilGunList);
            }else {
                StationOilGun stationOilGun =new StationOilGun();
                stationOilGun.setStationId(stationId);
                List<StationOilGun> stationOilGunList = stationOilGunService.stationOilGunList(stationOilGun);
                resultMap.put("stationOilGunList", stationOilGunList);
                //resultData=ResultData.success(stationOilGunList);
            }
            resultData = ResultData.success(resultMap);
        }catch (Exception e){
            resultData=ResultData.success(CodeMsg.SEARCH_FAIL);
            e.printStackTrace();
        }
        return gson.toJson(resultData);
    }


}
