package com.sv.service.oms;

import com.github.pagehelper.PageHelper;
import com.sv.dto.oms.OrderCheatDTO;
import com.sv.entity.Constants;
import com.sv.entity.Order;
import com.sv.mapper.OrderMapper;
import com.sv.service.api.util.DateUtilCard;
import com.ydd.framework.core.common.Pagination;
import com.ydd.framework.core.service.impl.BaseServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.io.File;
import java.io.IOException;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;


/**
 * Service - 订单
 *
 * @author yechanglin
 * @since 2018-08-02
 */
@Service("OrderServiceOms")
@Transactional(readOnly = true)
public class OrderService extends BaseServiceImpl {

	private final Logger logger = LoggerFactory.getLogger(OrderService.class);

	@Resource
	private OrderMapper orderMapper;

	@Resource
	private ExcelService excelService;

	/**
	 * 创建订单
	 *
	 * @param order 订单
	 */
	@Transactional
    public void save(Order order) {
		if (order.getId() != null && order.getId() > 0) {
			// 更新
			orderMapper.update(order);
		} else {
			// 新建
			orderMapper.insert(order);
		}
	}

	/**
	 * 更新订单
 	 *
	 * @param order 订单
	 */

	@Transactional
    public void update(Order order) {
		orderMapper.update(order);
	}

	/**
	 * 删除订单
	 *
	 * @param id 编号
   	 * @return 删除数量
	 */

	@Transactional
    public Integer delete(Integer id) {
		if (id == null || id <= 0) {
			return 0;
		}
		return orderMapper.delete(id);
	}

	/**
	 * 删除订单
	 *
	 * @param ids 编号数组
	 * @return 删除数量
	 */

	@Transactional
    public Integer deleteByIds(Integer[] ids) {
		if (ids == null || ids.length == 0) {
			return 0;
		}
		return orderMapper.deleteByIds(ids);
	}

	/**
	 * 查询订单
	 *
	 * @param id 编号
	 * @return 订单
	 */
	public Order findById(Integer id) {
		return orderMapper.findById(id);
	}

	/**
	 * 查询订单
	 *
	 * @param pagination 分页信息
	 * @return 分页结果
	 */

	public Pagination findPage(Pagination pagination, Integer payStatus, String starTime, String endTime) {
		PageHelper.startPage(pagination.getPage(), pagination.getPageSize());
		PageHelper.orderBy("id desc");
		pagination.setQueryResult(orderMapper.findAlls(payStatus, starTime, endTime));
		return pagination;
	}

	/**
	 * 导出福利订单
	 * @return
	 */
	public File exportOrder(Integer payStatus, String starTime, String endTime) throws IOException {
		List<Order> OrderList = orderMapper.findAlls(payStatus, starTime, endTime);
		File f = excelService.writeToExcel(OrderList,new File(Constants.EXCEL_PATH+System.currentTimeMillis()+".xlsx"));
		return f;
	}

    /**
     * 报表
     * @param time
     * @return
     */
	public List<OrderCheatDTO> findMoney(String time){
		int days = 0;
		if (StringUtils.isNotBlank(time)){
			time = time.replace("-","");
			String searchYear = time.substring(0,4);
			String searchMonth = time.substring(4,6);
			days = DateUtilCard.getDaysByYearMonth(Integer.parseInt(searchYear),Integer.parseInt(searchMonth));
		}else {
			Calendar cal = Calendar.getInstance();
			int year = cal.get(Calendar.YEAR);
			int month = cal.get(Calendar.MONTH )+1;
			time = String.valueOf(year) +"0"+ String.valueOf(month);
			days = DateUtilCard.getCurrentMonthDay();
		}

		OrderCheatDTO orderDTO = new OrderCheatDTO();
		orderDTO.setDays(days);

		List<OrderCheatDTO> orderDTOS = orderMapper.findMoney(time);
		List<OrderCheatDTO> dtoList = new ArrayList<>();

		for (int i=1; i <= days; i++){
			OrderCheatDTO o = new OrderCheatDTO();
			o.setDays(i);
			if (orderDTOS != null && orderDTOS.size() > 0){
				for (int j=0; j < orderDTOS.size(); j++){
					String payTime = orderDTOS.get(j).getPayTime();
					String date = payTime.substring(payTime.length()-2,payTime.length());
					if (date.equals(String.format("%02d", i))){
						o.setPrice(orderDTOS.get(j).getPrice());
						break;
					}else {
						o.setPrice(BigDecimal.valueOf(0));
					}

				}
			}else {
				o.setPrice(BigDecimal.valueOf(0));
			}

			dtoList.add(o);
		}
		return dtoList;
	}

}

