package com.jpsoft.shinestar.modules.base.service.impl;

import java.util.*;
import javax.annotation.Resource;

import com.jpsoft.shinestar.config.ParkingConfig;
import com.jpsoft.shinestar.modules.base.entity.ParkingInfo;
import com.jpsoft.shinestar.modules.base.service.ParkingInfoService;
import com.jpsoft.shinestar.modules.common.utils.ParkApiUtil;
import net.sf.json.JSONArray;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import com.jpsoft.shinestar.modules.base.dao.ParkingChannelDAO;
import com.jpsoft.shinestar.modules.base.entity.ParkingChannel;
import com.jpsoft.shinestar.modules.base.service.ParkingChannelService;
import com.github.pagehelper.Page;
import com.jpsoft.shinestar.modules.common.dto.Sort;
import com.github.pagehelper.PageHelper;

@Transactional
@Component(value="parkingChannelService")
public class ParkingChannelServiceImpl implements ParkingChannelService {
	@Resource(name="parkingChannelDAO")
	private ParkingChannelDAO parkingChannelDAO;

	@Autowired
	private ParkingInfoService parkingInfoService;

	@Autowired
	private ParkApiUtil parkApiUtil;

	@Autowired
	private ParkingConfig parkingConfig;

	@Override
	public ParkingChannel get(String id) {
		// TODO Auto-generated method stub
		return parkingChannelDAO.get(id);
	}

	@Override
	public int insert(ParkingChannel model) {
		// TODO Auto-generated method stub
		//model.setId(UUID.randomUUID().toString());

		return parkingChannelDAO.insert(model);
	}

	@Override
	public int update(ParkingChannel model) {
		// TODO Auto-generated method stub
		return parkingChannelDAO.update(model);
	}

	@Override
	public int delete(String id) {
		// TODO Auto-generated method stub
		return parkingChannelDAO.delete(id);
	}

	@Override
	public boolean exist(String id) {
		// TODO Auto-generated method stub
		int count = parkingChannelDAO.exist(id);

		return count > 0 ? true : false;
	}

	@Override
	public List<ParkingChannel> list() {
		// TODO Auto-generated method stub
		return parkingChannelDAO.list();
	}

	@Override
	public Page<ParkingChannel> pageSearch(Map<String, Object> searchParams, int pageNumber, int pageSize,boolean count,List<Sort> sortList) {
        Page<ParkingChannel> page = PageHelper.startPage(pageNumber,pageSize,count).doSelectPage(()->{
            parkingChannelDAO.search(searchParams,sortList);
        });

        return page;
	}

	@Override
	public List<ParkingChannel> findByParkId(Long parkId) {
		// TODO Auto-generated method stub
		return parkingChannelDAO.findByParkId(parkId);
	}

	@Override
	public ParkingChannel findByChannelId(String channelId){
		return parkingChannelDAO.findByChannelId(channelId);
	}

	@Override
	public int deleteNotExist(List<ParkingChannel> list){
		return parkingChannelDAO.deleteNotExist(list);
	}

	@Override
	public List<ParkingChannel> findApiByParkId(Long parkId) throws Exception {


		ParkingInfo parkingInfo = parkingInfoService.findByParkId(parkId);
		if (parkingInfo == null){
			throw new Exception("停车场不存在");
		}
		List<ParkingChannel> list = new ArrayList<>();
		HashMap<String, Object> map = new HashMap<>();
		map.put("comid", parkId);
		net.sf.json.JSONObject jsonObject1 = parkApiUtil.httpRequestSecond(parkingConfig.getIp() + ":8081/cloud/ParkingSetup/query", "POST", map);
		JSONArray jsonArray = jsonObject1.getJSONArray("rows");
		for (Object object : jsonArray) {
			Map<String, Object> merchantMap = (Map) object;
			String channelName = (String) merchantMap.get("channel_name");
			String channelId = (String)merchantMap.get("channel_id");
			ParkingChannel parkingChannel = new ParkingChannel();
			parkingChannel.setChannelId(channelId);
			parkingChannel.setChannelName(channelName);
			parkingChannel.setParkId(parkId);
			list.add(parkingChannel);
		}
		return list;
	}


}