package com.palmnest.service.dinner.manager.impl;

import cn.jiguang.common.utils.StringUtils;

import com.palmnest.application.bean.Page;
import com.palmnest.application.core.util.DateHelper;
import com.palmnest.application.dto.output.IntegralRecodeDto;
import com.palmnest.dao.UserDao;
import com.palmnest.dao.dinner.IntegralRecodeInfoDao;
import com.palmnest.dao.estateserve.AppUserDao;
import com.palmnest.model.Element;
import com.palmnest.model.OrgElement;
import com.palmnest.model.User;
import com.palmnest.model.dinner.IntegralRecodeInfo;
import com.palmnest.model.estateserve.AppUser;
import com.palmnest.service.dinner.manager.IntegralRecodeInfoManager;
import com.palmnest.service.impl.GenericManagerImpl;

import lombok.extern.slf4j.Slf4j;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Objects;

@Slf4j
@Service("integralRecodeInfoManager")
public class IntegralRecodeInfoManagerImpl extends GenericManagerImpl<IntegralRecodeInfo, Long> implements IntegralRecodeInfoManager {


	private IntegralRecodeInfoDao integralRecodeInfoDao;

	@Autowired
	public void setIntegralRecodeInfoDao(IntegralRecodeInfoDao integralRecodeInfoDao) {
		this.integralRecodeInfoDao = integralRecodeInfoDao;
		this.dao = integralRecodeInfoDao;
	}

	@Autowired
	private AppUserDao appUserDao;
	@Autowired
	private UserDao    userDao;

	/**
	 * 根据项目手机号查找该用户下相关的操作记录
	 *
	 * @param mobile
	 * @param orgId
	 * @return
	 */
	@Override
	public List<IntegralRecodeDto> getRecodeInfo(String mobile, String orgId) {
		List<IntegralRecodeDto>  dtoList = new ArrayList<IntegralRecodeDto>();
		List<IntegralRecodeInfo> list    = integralRecodeInfoDao.getRecodeInfo(mobile, orgId);
		if (list != null && list.size() > 0) {
			for (int i = 0; i < list.size(); i++) {
				IntegralRecodeInfo info = list.get(i);
				if (info != null) {
					IntegralRecodeDto dto = new IntegralRecodeDto(info.getId().toString(), info.getType(), info.getTargetName(), info.getTargetNumber(), info.getOldIntegral(), info.getNewIntegral(), info.getExchangeIntegral());
					if (info.getCreateTime() != null) {
						dto.setCreateTime(DateHelper.formatDate(info.getCreateTime(), DateHelper.DATE_TIME_STRING));
					}
					if (StringUtils.isNotEmpty(info.getCreator())) {
						if (appUserDao.exists(Integer.parseInt(info.getCreator()))) {
							AppUser user = appUserDao.get(Integer.parseInt(info.getCreator()));
							dto.setCreatorName(user.getUserName());
							dto.setCreatorMobile(user.getUserMobile());
						} else if (userDao.exists(Long.parseLong(info.getCreator()))) {
							User user = userDao.get(Long.parseLong(info.getCreator()));
							dto.setCreatorName(user.getFullName());
							dto.setCreatorMobile(user.getTelephone());
						}else{
							dto.setCreatorName("系统");
						}
					}
					if (info.getType() == 1) {
						dto.setOpTypeStr("充值");
					} else if (info.getType() == 2) {
						dto.setOpTypeStr("修改");
					} else if (info.getType() == 3) {
						int integral = Integer.parseInt(info.getExchangeIntegral());
						if (integral > 0) {
							dto.setOpTypeStr("取消退款");
						} else {
							dto.setOpTypeStr("订餐");
						}

					} else if (info.getType() == 4) {
						dto.setOpTypeStr("转换到福利商城");
					} else if(info.getType() == 5){
						dto.setOpTypeStr("系统清理");
					}
					dtoList.add(dto);
				}
			}
		}
		return dtoList;
	}

	
	/**
	 * 根据项目手机号查找该用户下相关的操作记录
	 *
	 * @param mobile
	 * @param orgId
	 * @return
	 */
	@Override
	public List<IntegralRecodeDto> getRecodeInfoExcelList(Map<String, Object> paramets) {
		
		List<IntegralRecodeDto>  dtoList = new ArrayList<IntegralRecodeDto>();
		List<IntegralRecodeInfo> list    = integralRecodeInfoDao.getRecodeInfoExcelList(paramets);
		if (list != null && list.size() > 0) {
			for (int i = 0; i < list.size(); i++) {
				IntegralRecodeInfo info = list.get(i);
				if (info != null) {
					IntegralRecodeDto dto = new IntegralRecodeDto(info.getId().toString(), info.getType(), info.getTargetName(), info.getTargetNumber(), info.getOldIntegral(), info.getNewIntegral(), info.getExchangeIntegral());
					if (info.getCreateTime() != null) {
						dto.setCreateTime(DateHelper.formatDate(info.getCreateTime(), DateHelper.DATE_TIME_STRING));
					}
					if (StringUtils.isNotEmpty(info.getCreator())) {
						if (appUserDao.exists(Integer.parseInt(info.getCreator()))) {
							AppUser user = appUserDao.get(Integer.parseInt(info.getCreator()));
							dto.setCreatorName(user.getUserName());
							dto.setCreatorMobile(user.getUserMobile());
						} else if (userDao.exists(Long.parseLong(info.getCreator()))) {
							User user = userDao.get(Long.parseLong(info.getCreator()));
							dto.setCreatorName(user.getFullName());
							dto.setCreatorMobile(user.getTelephone());
						}else{
							dto.setCreatorName("系统");
						}
					}
					if (info.getType() == 1) {
						dto.setOpTypeStr("充值");
					} else if (info.getType() == 2) {
						dto.setOpTypeStr("修改");
					} else if (info.getType() == 3) {
						int integral = Integer.parseInt(info.getExchangeIntegral());
						if (integral > 0) {
							dto.setOpTypeStr("取消退款");
						} else {
							dto.setOpTypeStr("订餐");
						}

					} else if (info.getType() == 4) {
						dto.setOpTypeStr("转换到福利商城");
					} else if(info.getType() == 5){
						dto.setOpTypeStr("系统清理");
					}
					dtoList.add(dto);
				}
			}
		}
		return dtoList;
	}
	
	
	/**
	 * 根据项目手机号查找该用户下相关的操作记录
	 *
	 * @param mobile
	 * @param orgId
	 * @return
	 */
	@Override
	public Page getPageRecodeInfo(Map<String, Object> paramets, int pageSize, int pageNo) {
		
		Page page    = integralRecodeInfoDao.getPageRecodeInfo(paramets, pageSize,pageNo);
		List<Map<String, Object>> list = page.getItems();		
		
		if (list != null && list.size() > 0) {
			for (int i = 0; i < list.size(); i++) {
				Map<String, Object> info = list.get(i);
				if (info != null) {
					
					if (info.get("createTime") != null) {
						info.put("createTime",(DateHelper.formatDate((Date)info.get("createTime"), DateHelper.DATE_TIME_STRING)));
					}
					if (null!=info.get("creator")) {
						if (appUserDao.exists(Integer.decode(info.get("creator").toString()))) {
							AppUser user = appUserDao.get(Integer.decode(info.get("creator").toString()));
							info.put("creatorName",user.getUserName());
							info.put("creatorMobile",user.getUserMobile());
						} else if (userDao.exists(Long.parseLong(info.get("creator").toString()))) {
							User user = userDao.get(Long.parseLong(info.get("creator").toString()));
							info.put("creatorName",user.getFullName());
							info.put("creatorMobile",user.getTelephone());
						}else{
							info.put("creatorName","系统");
						}
					}
					if ((Integer)info.get("type") == 1) {
						info.put("opTypeStr","充值");
					} else if ((Integer)info.get("type") == 2) {
						info.put("opTypeStr","修改");
					} else if ((Integer)info.get("type") == 3) {
						int integral = Integer.parseInt(info.get("exchangeIntegral").toString());
						if (integral > 0) {
							info.put("opTypeStr","取消退款");
						} else {
							info.put("opTypeStr","订餐");
						}

					} else if ((Integer)info.get("type") == 4) {
						info.put("opTypeStr","转换到福利商城");
					} else if((Integer)info.get("type") == 5){
						info.put("opTypeStr","系统清理");
					}
				}
			}
		}
		return page;
	}
	
	@Override
	public Page findList(Map<String, Object> map, int pageSize, int pageNo) {
		return integralRecodeInfoDao.findList(map, pageSize, pageNo);
	}

}
