Subversion Repositories SmartDukaan

Rev

Rev 28303 | Go to most recent revision | View as "text/plain" | Blame | Compare with Previous | Last modification | View Log | RSS feed

package com.spice.profitmandi.web.controller;

import java.time.LocalDateTime;

import javax.servlet.http.HttpServletRequest;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;
import com.spice.profitmandi.common.web.util.ResponseSender;
import com.spice.profitmandi.dao.entity.dtr.GpsLocation;
import com.spice.profitmandi.dao.entity.dtr.GpsLocationModel;
import com.spice.profitmandi.dao.entity.user.Device;
import com.spice.profitmandi.dao.repository.catalog.DeviceRepository;
import com.spice.profitmandi.dao.repository.user.GpsLocationRepository;
import com.spice.profitmandi.web.req.AddDevicesRequest;

import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

@Controller
@Transactional(rollbackFor = Throwable.class)
public class DevicesController {

        @Autowired
        ResponseSender<?> responseSender;

        @Autowired
        private DeviceRepository deviceRepository;

        @Autowired
        private GpsLocationRepository gpsLocationRepository;

        private static final Logger LOGGER = LogManager.getLogger(DevicesController.class);

        @RequestMapping(value = "/devices/add-new", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
        @ApiImplicitParams({
                        @ApiImplicitParam(name = "Auth-Token", value = "Auth-Token", required = true, dataType = "string", paramType = "header") })
        @ApiOperation(value = "")
        public ResponseEntity<?> devicesInfo1(HttpServletRequest request,
                        @RequestBody AddDevicesRequest addDevicesRequest) {
                Device device = this.deviceInfo(request, addDevicesRequest);
                LOGGER.info("devices1" + device.getId());
                return responseSender.ok(device.getId());
        }

        @RequestMapping(value = "/devices/add", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
        @ApiImplicitParams({
                        @ApiImplicitParam(name = "Auth-Token", value = "Auth-Token", required = true, dataType = "string", paramType = "header") })
        @ApiOperation(value = "")
        public ResponseEntity<?> devicesInfo(HttpServletRequest request, @RequestBody AddDevicesRequest addDevicesRequest)
                        throws ProfitMandiBusinessException {
                Device device = this.deviceInfo(request, addDevicesRequest);
                LOGGER.info("devices2" + device.getId());
                return responseSender.ok(true);
        }

        private Device deviceInfo(HttpServletRequest request, AddDevicesRequest addDevicesRequest) {
                Device device = deviceRepository.selectByImeiNumber(addDevicesRequest.getImeinumber());
                LOGGER.info("devices" + device);
                if (device == null) {
                        device = new Device();
                        device.setImeiNumber(addDevicesRequest.getImeinumber());
                        deviceRepository.persist(device);
                }
                device.setUser_id(addDevicesRequest.getUser_id());
                device.setBrand(addDevicesRequest.getBrand());
                device.setManufacturer(addDevicesRequest.getManufacturer());
                device.setModel(addDevicesRequest.getModel());
                device.setOsVersion(addDevicesRequest.getOsversion());
                device.setVersionCode(addDevicesRequest.getVersioncode());
                device.setVersionName(addDevicesRequest.getVersionname());
                device.setAndroidId(addDevicesRequest.getAndroidid());
                if (addDevicesRequest.getGcm_regid() == null)
                        LOGGER.info("gcm" + addDevicesRequest.getGcm_regid());
                else {
                        device.setFcmId(addDevicesRequest.getGcm_regid());
                }
                device.setModified(LocalDateTime.now());
                return device;
        }

        @RequestMapping(value = "/devices/location", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
        public ResponseEntity<?> deviceLocation(HttpServletRequest request, @RequestBody GpsLocationModel locationModel) {
                GpsLocation gpsLocation = new GpsLocation();
                gpsLocation.setGps(locationModel.isGps());
                gpsLocation.setDeviceId(locationModel.getDeviceId());
                gpsLocation.setLat(locationModel.getLat());
                gpsLocation.setLng(locationModel.getLng());
                gpsLocation.setUserId(locationModel.getUserId());
                gpsLocation.setCreateTime(LocalDateTime.now());
                gpsLocationRepository.persist(gpsLocation);
                return responseSender.ok(true);
        }
}