package com.diagbot.facade;

import com.diagbot.client.UserServiceClient;
import com.diagbot.dto.ProductAuthProgressDTO;
import com.diagbot.dto.RespDTO;
import com.diagbot.dto.UserOrgDTO;
import com.diagbot.dto.WaitExamOrderCouDTO;
import com.diagbot.entity.LantoneProduct;
import com.diagbot.entity.OrderDetails;
import com.diagbot.entity.OrderDetailsIndex;
import com.diagbot.entity.ProductOrder;
import com.diagbot.entity.ProductOrderIndex;
import com.diagbot.entity.ServiceInfo;
import com.diagbot.entity.wrapper.OrderDetailsWapper;
import com.diagbot.entity.wrapper.ProductOrderWrapper;
import com.diagbot.enums.VisibleIdTypeEnum;
import com.diagbot.exception.CommonErrorCode;
import com.diagbot.exception.CommonException;
import com.diagbot.idc.VisibleIdCreater;
import com.diagbot.service.impl.ProductOrderServiceImpl;
import com.diagbot.util.BeanUtil;
import com.diagbot.util.DateUtil;
import com.diagbot.util.UserUtils;
import com.diagbot.vo.AddOrderVO;
import com.diagbot.vo.AuditStatusVO;
import com.diagbot.vo.OrderDetialsVO;
import com.diagbot.vo.ProductOrderVO;
import com.diagbot.vo.ProductServiceSaveVO;
import com.diagbot.vo.ServiceSaveVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

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

/**
 * 订单业务
 * @author Administrator
 *
 */
@Component
public class ProductOrderFacade extends ProductOrderServiceImpl {
	@Autowired
	private VisibleIdCreater visibleIdCreater;
	@Autowired
	private OrderDetailsFacade orderDetailsFacade;
	@Autowired
	private ServiceInfoFacade serviceInfoFacade;
	@Autowired
	private LantoneProductFacade lantoneProductFacade;
	@Autowired
	private ProductServiceFacade productServiceFacade;
	@Autowired
	private UserServiceClient userServiceClient;
	@Autowired
	private ProductOrderFacade productOrderFacade;

	public List<ProductAuthProgressDTO> productAuthProgress(){
		List<ProductAuthProgressDTO> retList = new ArrayList<ProductAuthProgressDTO>();
		Long userId = Long.parseLong(UserUtils.getCurrentPrincipleID());
		List<ProductOrder> orderList = baseMapper.getUserProductOrder(userId);
		for(ProductOrder i : orderList){
			ProductAuthProgressDTO productAuthProgressDTO = new ProductAuthProgressDTO();
			productAuthProgressDTO.setNum(i.getNum());
			productAuthProgressDTO.setProducts(baseMapper.getOrderDetails(i.getNum()));
			retList.add(productAuthProgressDTO);
		}

		return retList;
	}

	/**
	 * @Description: 根据用户选择的产品生成订单
	 * @Author: wangyu
	 * @Date: 19:59 2018/9/18
	 */
	@Transactional
	public Boolean addOrders(AddOrderVO addOrderVO){
		Long userId = Long.parseLong(UserUtils.getCurrentPrincipleID());
		ProductOrder productOrder =new ProductOrder();
		productOrder.setCreator(UserUtils.getCurrentPrincipleID());
		productOrder.setGmtCreate(DateUtil.now());
		String orderNum = visibleIdCreater.getNextId(VisibleIdTypeEnum.IS_ORDER.getKey()).toString();
		productOrder.setNum(orderNum);
		productOrder.setUserId(userId);
		productOrder.setTime(DateUtil.now());
		if(!save(productOrder)){
			throw new CommonException(CommonErrorCode.SERVER_IS_ERROR,"添加订单失败");
		}
		for (Long o:addOrderVO.getProductId()) {
			LantoneProduct lantoneProduct = lantoneProductFacade.selectLanProduct(o);
			if(null==lantoneProduct){
				throw new CommonException(CommonErrorCode.SERVER_IS_ERROR,"获取产品信息失败:"+o);
			}
			OrderDetails orderDetails =new OrderDetails();
			orderDetails.setUserId(userId);
			orderDetails.setOrderNum(orderNum);
			orderDetails.setGmtCreate(DateUtil.now());
			orderDetails.setCreator(UserUtils.getCurrentPrincipleID());
			orderDetails.setProductId(o);
			orderDetailsFacade.save(orderDetails);
		}
		return true;
	}

	/**
	 * @Description:  根据订单号查询订单信息
	 * @Author: wangyu
	 * @Date: 19:42 2018/9/20
	 */
	public ProductOrder selectByOrderNum(ProductOrder productOrder){
		return selctOrderTimeByOrderNum(productOrder);
	}
	
	
    public WaitExamOrderCouDTO waitExamOrderCou(){
    	WaitExamOrderCouDTO waitExamOrderCouDTO = new WaitExamOrderCouDTO();
    	waitExamOrderCouDTO.setCount(baseMapper.waitExamOrderCou());
    	return waitExamOrderCouDTO;
    }

	/**
	 * @Description: 查询所有订单明细
	 * @Author: wangyu
	 * @Date: 16:19 2018/9/21
	 */
	public List<OrderDetailsWapper> getAllOrderDetials(OrderDetialsVO orderDetialsVO){
		OrderDetailsIndex orderDetailsIndex =new OrderDetailsIndex();
		Long curren = orderDetialsVO.getCurrent();
		Long size =orderDetialsVO.getSize();
		if(curren==null){
			orderDetialsVO.setCurrent(1L);
		}
		if (size==null){
			orderDetialsVO.setSize(10L);
		}
		BeanUtil.copyProperties(orderDetialsVO, orderDetailsIndex);
		orderDetailsIndex.setIndex((orderDetialsVO.getCurrent().longValue()-1)*orderDetialsVO.getSize().longValue());
		List<OrderDetailsWapper> list =orderDetailsFacade.seleAllOrderDetials(orderDetailsIndex);
		List<Long> list1 =new ArrayList<>();
		for (OrderDetails orderDetails:list) {
			Long longs = orderDetails.getUserId();
			list1.add(longs);
		}
		RespDTO<Map<Long, UserOrgDTO>> mapRespDTO = userServiceClient.getUserAndOrg(list1);
		if(mapRespDTO == null || !"0".equals(mapRespDTO.code) ) {
			throw new CommonException(CommonErrorCode.SERVER_IS_ERROR,
					"获取用户和机构信息失败");
		}
		Map<Long, UserOrgDTO> dataMap = mapRespDTO.data;
		for(OrderDetailsWapper bean : list) {
			UserOrgDTO uo = dataMap.get(bean.getUserId());
			if(uo != null) {
				bean.setUsername(uo.getUsername());
				bean.setLinkman(uo.getLinkman());
				bean.setEmail(uo.getEmail());
				bean.setOrgId(uo.getOrgId());
				bean.setOrgName(uo.getOrgName());
				bean.setIsReject(uo.getIsReject());
				bean.setAuStatus(uo.getAuStatus());
			}
		}
		return list;
	}

	/**
	 * @Description: 产品申请查询所有订单信息
	 * @Author: wangyu
	 * @Date: 20:23 2018/9/24
	 */
	public List<ProductOrderWrapper> selectAllProductOrder(ProductOrderVO productOrderVO){
		ProductOrderIndex productOrderIndex =new ProductOrderIndex();
		Long curren = productOrderVO.getCurrent();
		Long size =productOrderVO.getSize();
		if(curren==null){
			productOrderVO.setCurrent(1L);
		}
		if (size==null){
			productOrderVO.setSize(10L);
		}
		BeanUtil.copyProperties(productOrderVO, productOrderIndex);
		productOrderIndex.setIndex((productOrderVO.getCurrent().longValue()-1)*productOrderVO.getSize().longValue());
		//TODO
		List<ProductOrderWrapper> list =productOrderFacade.selectAllProductOrder(productOrderIndex);
		List<Long> list1 =new ArrayList<>();
		for (ProductOrderWrapper productOrderWrapper:list) {
			Long longs = productOrderWrapper.getUserId();
			list1.add(longs);
		}
		RespDTO<Map<Long, UserOrgDTO>> mapRespDTO = userServiceClient.getUserAndOrg(list1);
		if(mapRespDTO == null || !"0".equals(mapRespDTO.code) ) {
			throw new CommonException(CommonErrorCode.SERVER_IS_ERROR,
					"获取用户和机构信息失败");
		}
		Map<Long, UserOrgDTO> dataMap = mapRespDTO.data;
		for(ProductOrderWrapper bean : list) {
			UserOrgDTO uo = dataMap.get(bean.getUserId());
			if(uo != null) {
				bean.setUsername(uo.getUsername());
				bean.setLinkman(uo.getLinkman());
				bean.setEmail(uo.getEmail());
				bean.setOrgId(uo.getOrgId());
				bean.setOrgName(uo.getOrgName());
				bean.setIsReject(uo.getIsReject());
				bean.setAuStatus(uo.getAuStatus());
			}
		}
		return list;
	}

	/**
	 * @Description: 产品申请审核接口
	 * @Author: wangyu
	 * @Date: 21:39 2018/9/24
	 */
	@Transactional
	public Boolean updateAuditStatus(AuditStatusVO auditStatusVO){
		OrderDetails orderDetails =new OrderDetails();
		orderDetails.setId(auditStatusVO.getId());
		orderDetails.setGmtModified(DateUtil.now());
		orderDetails.setModifier(UserUtils.getCurrentPrincipleID());
		orderDetails.setAuditStatus(1);
		if(!orderDetailsFacade.updateById(orderDetails)){
			throw new CommonException(CommonErrorCode.UPDATE_INFO_FAIL);
		}
		LantoneProduct lantoneProduct =new LantoneProduct();
		lantoneProduct.setId(auditStatusVO.getId());
		ServiceSaveVO serviceSaveVO =new ServiceSaveVO();
		ProductServiceSaveVO productServiceSaveVO =new ProductServiceSaveVO();
		serviceSaveVO.setDescription(lantoneProduct.getDecription());
		serviceSaveVO.setName(lantoneProduct.getName());
		serviceSaveVO.setType(1);
		ServiceInfo serviceInfo = serviceInfoFacade.createService(serviceSaveVO);
		productServiceSaveVO.setProductId(auditStatusVO.getId());
		productServiceSaveVO.setServiceId(serviceInfo.getId());
		productServiceSaveVO.setType(1);
		productServiceFacade.genProductService(productServiceSaveVO);
		return true;
	}
}


