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

import cn.hutool.core.date.DateUtil;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.jpsoft.shinestar.modules.base.dao.ParkingApplyDAO;
import com.jpsoft.shinestar.modules.base.entity.CompanyInfo;
import com.jpsoft.shinestar.modules.base.entity.OrderInfo;
import com.jpsoft.shinestar.modules.base.entity.ParkingApply;
import com.jpsoft.shinestar.modules.base.entity.PersonInfo;
import com.jpsoft.shinestar.modules.base.service.CompanyInfoService;
import com.jpsoft.shinestar.modules.base.service.OrderInfoService;
import com.jpsoft.shinestar.modules.base.service.ParkingApplyService;
import com.jpsoft.shinestar.modules.common.dto.Sort;
import com.jpsoft.shinestar.modules.common.utils.StringUtils;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.UUID;

@Transactional
@Component(value="parkingApplyService")
public class ParkingApplyServiceImpl implements ParkingApplyService {
	@Resource(name="parkingApplyDAO")
	private ParkingApplyDAO parkingApplyDAO;

	@Autowired
	private OrderInfoService orderInfoService;

	@Autowired
	private CompanyInfoService companyInfoService;

	@Autowired
	RabbitTemplate rabbitTemplate;

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

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

		return parkingApplyDAO.insert(model);
	}

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

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

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

		return count > 0 ? true : false;
	}

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

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

        return page;
	}

	@Override
	public ParkingApply findByPopedomIdAndStatus(String popedomId, String status) {
		return parkingApplyDAO.findByPopedomIdAndStatus(popedomId,status);
	}

	@Override
	public ParkingApply findByPopedomIdAndPayStatus(String popedomId, int payStatus) {
		return parkingApplyDAO.findByPopedomIdAndPayStatus(popedomId,payStatus);
	}

	@Override
	public String parkingApplicationCreateOrder(ParkingApply parkingApply, String actualFee, PersonInfo personInfo1,String companyId) {

		OrderInfo orderInfo = new OrderInfo();
		orderInfo.setId(UUID.randomUUID().toString());
		orderInfo.setTotalFee(new BigDecimal(actualFee));
		orderInfo.setBody("车位租金");
		orderInfo.setPayStatus(10);
		orderInfo.setOutOrderNo(StringUtils.getOutTradeNo());
		orderInfo.setOpenId(personInfo1.getOpenId());
		orderInfo.setDelFlag(false);
		orderInfo.setStatus("1");
		orderInfo.setCreateBy(personInfo1.getName());
		orderInfo.setCreateTime(new Date());
		orderInfo.setType("3");
		orderInfo.setGoodsId("4");
		orderInfo.setPersonId(personInfo1.getId());
		orderInfo.setCompanyId(companyId);
		orderInfo.setBelongPopedomId(parkingApply.getPopedomId());

		CompanyInfo companyInfo = companyInfoService.get(companyId);

		JSONObject jsonObject = new JSONObject(true);
		jsonObject.put("缴费单位",companyInfo.getName());
		jsonObject.put("缴费人",personInfo1.getName());
		jsonObject.put("车位",parkingApply.getParkingName());
		jsonObject.put("车牌",parkingApply.getCarNum());
		jsonObject.put("订单号",orderInfo.getOutOrderNo());
		jsonObject.put("创建时间", DateUtil.format(orderInfo.getCreateTime(),"yyyy-MM-dd HH:mm"));
		orderInfo.setDetail(jsonObject.toString());
		orderInfoService.insert(orderInfo);



		parkingApply.setStatus("1");
		parkingApply.setPayType("2");
		parkingApply.setActualFee(new BigDecimal(actualFee));
		parkingApply.setOrderId(orderInfo.getId());
		update(parkingApply);

		//发送订单30分钟后自动关闭的延时消息
		rabbitTemplate.convertAndSend("orderDelayExchange","order",orderInfo.getId(),message -> {
			message.getMessageProperties().setHeader("x-delay",1000*10);
			return message;
		});

		return orderInfo.getId();

	}

	@Override
	public ParkingApply findByOrderId(String orderId) {
		return parkingApplyDAO.findByOrderId(orderId);
	}

	@Override
	public List<ParkingApply> findByPopedomId(String popedomId) {
		return parkingApplyDAO.findByPopedomId(popedomId);
	}
}