/*******************************************************************************
 * Copyright 2019 2Fi Business Solutions Ltd.
 *
 *
 *
 * This code is copyrighted. Under no circumstances should any party, people,
 * or organization should redistribute any portions of this code in any form,
 * either verbatim or through electronic media, to any third parties, unless
 * under explicit written permission by 2Fi Business Solutions Ltd.
 ******************************************************************************/
package com.ffii.tbms.reports.service;

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

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

import com.ffii.core.dao.JdbcDao;
import com.ffii.core.utils.SecurityUtils;
import com.ffii.core.web.AbstractService;


@Service
public class CustomerFirstOrderReportService extends AbstractService {

	@Autowired
	private JdbcDao jdbcDao;

	@Transactional(isolation = Isolation.READ_COMMITTED, rollbackFor = Exception.class, readOnly = true)
	public List<Map<String, Object>> search() {
		
		Map<String,Object> args = new HashMap();
		SecurityUtils.authArgs(args);

		StringBuilder sql = new StringBuilder(" SELECT o.id AS orderId, "
			+" o.custId, "
			+" minOrderDate.minDate, "
			+" CONCAT(o.type, o.code) AS orderNo, "
			+" CONCAT(IF(c.firstName IS NULL, '', CONCAT(c.firstName, ' ')), "
			+" IF(c.surname IS NULL, '', CONCAT(c.surname, ' ')), "
			+" IF(c.nameCh IS NULL, '', c.nameCh)) AS customerName, "
			+" c.phone1, "
			+" c.phone2, "
			+" c.email, "
			+" c.address, "
			+" c.companyName, "
			+" c.companyAddress, "
			+" c.companyEmail, "
			+" c.companyPhone, "
			+" c.remarks "
			+" FROM orders o "
			+" JOIN (SELECT MIN(_o.date) AS minDate, "
			+" _o.custId FROM orders _o GROUP BY _o.custId) minOrderDate "
			+" ON o.date = minOrderDate.minDate AND minOrderDate.custId = o.custId "
			+" LEFT JOIN customer c ON c.id = o.custId "
			+" WHERE 1=1 "
			);

			if(args != null){
				if(args.containsKey("sysGroupId"))
					sql.append(" AND o.sysGroupId = :sysGroupId AND c.sysGroupId = :sysGroupId ");
			}

			sql.append(" GROUP BY o.date, o.custId "
			+" ORDER BY o.date , o.custId  ");
	
		return jdbcDao.queryForList(sql.toString(), args);
	}

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

		Map<String,Object> args = new HashMap();
		SecurityUtils.authArgs(args);

		StringBuilder sql = new StringBuilder( " Select *, "
			+" CONCAT(IF(c.firstName IS NULL, '', CONCAT(c.firstName, ' ')), "
			+" IF(c.surname IS NULL, '', CONCAT(c.surname, ' ')), "
			+" IF(c.nameCh IS NULL, '', c.nameCh)) AS customerName "
			+" From customer c where c.id not in ( "
			+" SELECT  "
			+" o.custId "
			+" FROM orders o "
			+" JOIN (SELECT MIN(_o.date) AS minDate, "
			+" _o.custId FROM orders _o GROUP BY _o.custId) minOrderDate "
			+" ON o.date = minOrderDate.minDate AND minOrderDate.custId = o.custId ");

			if(args != null){
				if(args.containsKey("sysGroupId"))
					sql.append(" AND o.sysGroupId = :sysGroupId ");
			}

			sql.append( " GROUP BY o.date, o.custId "
			+" ORDER BY o.date , o.custId  "
			+" ) ");
	
		return jdbcDao.queryForList(sql.toString(), args);
	}


}
