package com.bofeng.service;

import com.bofeng.dao.OrderCmsMapper;
import com.bofeng.dao.OrderMapper;
import com.bofeng.dao.RoomMapper;
import com.bofeng.entity.Order;
import com.bofeng.entity.Settle;
import com.google.common.base.Strings;
import com.google.common.collect.Maps;
import com.yvan.PageDb;
import com.yvan.platform.Conv;
import lombok.val;
import org.joda.time.DateTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

@Service
@Transactional(readOnly = true)
public class CashierService {

    @Autowired
    private OrderMapper orderMapper;

    @Autowired
    private CalcService calcService;

    @Autowired
    private OrderCmsMapper orderCmsMapper;

    @Autowired
    private UserOpenService userOpenService;

    @Autowired
    private TemplateMessageService templateMessageService;

    @Autowired
    private NotifyOpenService notifyOpenService;

    @Autowired
    private RoomMapper roomMapper;

    public List<Order> search(PageDb pageDb, Map<String, Object> param) {
        return orderMapper.search(pageDb, param);
    }

    public List<Settle> getSettleOrder(PageDb pageDb, Map<String, Object> queryParam) {
        return orderMapper.getSettleOrder(pageDb, queryParam);
    }

    @Transactional
    public int discard(Long orderId) {
        Order order = orderMapper.selectById(orderId);
        roomMapper.enableRoomById(order.getRoomId());
        return orderMapper.discard(orderId);
    }

    @Transactional
    public int pay(Long orderId, BigDecimal payAmount) {
        int effect = 0;
        val order = orderMapper.selectById(orderId);
        order.setPayAmount(payAmount);
        val cmsList = calcService.dispatch(order);

        for (val cms : cmsList) {
            effect += orderCmsMapper.insert(cms);
        }
        effect += orderMapper.payFinish(orderId, payAmount);

        //发送结算的模版消息
        Map<String, String> messMap = Maps.newLinkedHashMap();
        messMap.put("placeName", order.getPlaceName());
        messMap.put("roomName", order.getRoomName());
        messMap.put("payAmount", Conv.NS(order.getPayAmount()));
        messMap.put("customerName", order.getCustomerName());
        messMap.put("time", DateTime.now().toString("YYYY-MM-DD HH:mm:ss"));
        messMap.put("saleName", order.getSaleName());

        for (val cms : cmsList) {
            val userOpen = userOpenService.getUserByUserId(cms.getSaleUserId());
            if (userOpen != null && !Strings.isNullOrEmpty(userOpen.getOpenId())) {
                templateMessageService.sendAccountMessage(userOpen.getOpenId(), "", messMap);
            }
        }

        // 给场地管理发消息
        val notifyOpenList = notifyOpenService.getByPlaceId(order.getPlaceId());
        for (val n : notifyOpenList) {
            if (n.getOpenId() != null && !Strings.isNullOrEmpty(n.getOpenId())) {
                templateMessageService.sendAccountMessage(n.getOpenId(), "", messMap);
            }
        }

        return effect;
    }
}
