package com.ffii.tbms.api.service;

import java.util.List;
import java.util.Map;

import com.ffii.core.dao.JdbcDao;
import com.ffii.core.utils.MapUtils;
import com.ffii.core.utils.Params;
import com.ffii.core.web.AbstractService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Transactional;

/**
 * OrderApiService
 */
@Service
public class MeetingApiService extends AbstractService {

    @Autowired
    private JdbcDao jdbcDao;


    @Transactional(isolation = Isolation.SERIALIZABLE, rollbackFor = Exception.class, readOnly = true)
    public List<Map<String, Object>> searchMeeting(String dateStr) {

        StringBuilder sql = new StringBuilder("SELECT"
                + " m.date AS meetingDate,"
                + " m.id AS meetingId,"
                + " m.type AS meetingType,"
                + " cu.id AS custId,"
                + " CONCAT(IF(cu.firstName IS NULL, '', CONCAT(cu.firstName, ' ')), "
                + " IF(cu.surname IS NULL, '', CONCAT(cu.surname, ' ')), "
                + " IF(cu.nameCh IS NULL, '', cu.nameCh)) AS customerName, "
                + " m.orderId,"
                + " CONCAT(o.type, o.code) as orderCode,"
                + " m.type,"
                + " m.remarks"
                + " FROM meeting m"
                + " LEFT JOIN customer cu ON cu.id = m.custId"
                + " LEFT JOIN orders o ON o.id = m.orderId"
                + " WHERE m.deleted =0 ");

        if (dateStr != null) {
            sql.append(" AND DATE(m.date) = :dateStr");
        }

        sql.append(" ORDER BY m.date ASC, m.id ASC ");

        return jdbcDao.queryForList(sql.toString(), MapUtils.toHashMap("dateStr", dateStr));
    }

    @Transactional(isolation = Isolation.SERIALIZABLE, rollbackFor = Exception.class, readOnly = true)
    public Map<String, Object> loadMeeting(Map<String, Object> args) {

        StringBuilder sql = new StringBuilder("SELECT"
                + " m.date AS meetingDate,"
                + " m.id AS meetingId,"
                + " m.type AS meetingType,"
                + " cu.id AS custId,"
                + " CONCAT(IF(cu.firstName IS NULL, '', CONCAT(cu.firstName, ' ')), "
                + " IF(cu.surname IS NULL, '', CONCAT(cu.surname, ' ')), "
                + " IF(cu.nameCh IS NULL, '', cu.nameCh)) AS customerName, "
                + " m.orderId,"
                + " CONCAT(o.type, o.code) as orderCode,"
                + " m.type,"
                + " m.staffName,"
                + " m.remarks"
                + " FROM meeting m"
                + " LEFT JOIN customer cu ON cu.id = m.custId"
                + " LEFT JOIN orders o ON o.id = m.orderId"
                + " WHERE m.deleted =0 AND m.id = :id ");

        return jdbcDao.queryForMap(sql.toString(), args);
    }

    @Transactional(isolation = Isolation.SERIALIZABLE, rollbackFor = Exception.class, readOnly = true)
    public List<Map<String, Object>> searchMeetingByDate(Map<String, Object> args) {

        StringBuilder sql = new StringBuilder("SELECT"
                + " m.date AS meetingDate,"
                + " m.id AS meetingId,"
                + " m.type AS meetingType,"
                + " cu.id AS custId,"
                + " CONCAT(IF(cu.firstName IS NULL, '', CONCAT(cu.firstName, ' ')), "
                + " IF(cu.surname IS NULL, '', CONCAT(cu.surname, ' ')), "
                + " IF(cu.nameCh IS NULL, '', cu.nameCh)) AS customerName, "
                + " m.orderId,"
                + " CONCAT(o.type, o.code) as orderCode,"
                + " m.type,"
                + " m.remarks"
                + " FROM meeting m"
                + " LEFT JOIN customer cu ON cu.id = m.custId"
                + " LEFT JOIN orders o ON o.id = m.orderId"
                + " WHERE m.deleted =0");

        if (args.containsKey("selectedDate")) {
            sql.append(" AND DATE(m.date) = :selectedDate");
        }

        sql.append(" ORDER BY m.date ASC, m.id ASC ");

        return jdbcDao.queryForList(sql.toString(), args);
    }

}