package com.prolog.rdc.tms.service.impl;

import java.util.List;
import java.util.stream.Collectors;

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

import com.prolog.common.util.ToolKits;
import com.prolog.framework.core.exception.BizException;
import com.prolog.framework.core.exception.PrologException;
import com.prolog.framework.core.restriction.Criteria;
import com.prolog.framework.core.restriction.Restriction;
import com.prolog.framework.core.restriction.Restrictions;
import com.prolog.product.core.framework.entity.UserEntity;
import com.prolog.product.core.framework.util.PlgFxUsrUtils;
import com.prolog.rdc.tms.constant.BussinessConstant;
import com.prolog.rdc.tms.constant.DataDictionaryConstant;
import com.prolog.rdc.tms.constant.StatusConstant;
import com.prolog.rdc.tms.dao.mapper.TmsLoadingBillHeaderMapper;
import com.prolog.rdc.tms.dao.mapper.TmsShipbillHeaderMapper;
import com.prolog.rdc.tms.model.dto.FrontPageStatisticalDTO;
import com.prolog.rdc.tms.model.dto.WaitDeliveredQueryByShipbillDTO;
import com.prolog.rdc.tms.model.entity.TmsShipbillHeaderModel;
import com.prolog.rdc.tms.model.vo.WaitDeliveredQueryByShipbillVO;
import com.prolog.rdc.tms.service.FrontPageService;

/**
 * 首页实现层
 *
 * Company: 湖北普罗格科技股份有限公司
 *
 * @author zhangsj
 * @since 2022/7/15 10:57
 *
 */
@Service
public class FrontPageServiceImpl implements FrontPageService {

	@Autowired
	private TmsShipbillHeaderMapper tmsShipbillHeaderMapper;

	@Autowired
	private TmsLoadingBillHeaderMapper tmsLoadingBillHeaderMapper;

	/**
	 * 统计查询
	 *
	 * @return
	 */
	@Override
	public FrontPageStatisticalDTO statisticalData() {
		UserEntity user = PlgFxUsrUtils.getUserEntity();
		ToolKits.isParamNull(user, "当前人员没有操作权限");

		// 返回数据
		FrontPageStatisticalDTO frontPageStatisticalDTO = new FrontPageStatisticalDTO();
		// 当前时间
//		Date date = DateUtils.nowDate();
		// 获取30天前时间
//		Date starDate = DateUtils.minu(date, 30, ChronoUnit.DAYS);
		// 添加查询条件
		Criteria criteria = Criteria.forClass(TmsShipbillHeaderModel.class);
		Restriction r1 = Restrictions.eq(TmsShipbillHeaderModel::getEnterpriseId, user.getEnterpriseId());
		// 权限控制
		Restriction r2 = null;
		Restriction r3 = null;
		Restriction r4 = null;
		Restriction r5 = null;
		Restriction r6 = null;
		Restriction r7 = null;
		Restriction r29 = null;
		// 判断账号类型
		if (user.getUserType().equals(BussinessConstant.USER_TYPE_ZY)) {
			// 自有账号
			Restriction r30 = Restrictions.eq("splitOrderStatus", DataDictionaryConstant.SPLIT_ORDER_STATUS_UN);
			// 承运商拆单类型 1未拆单
			Restriction r31 = Restrictions.eq("carrierSplitOrderType", DataDictionaryConstant.CARRIER_SPLIT_ORDER_TYPE_UN);
			Restriction r32 = Restrictions.and(r30, r31);
			// 承运商拆单类型 2拆分主单
			Restriction r33 = Restrictions.eq("carrierSplitOrderType", DataDictionaryConstant.CARRIER_SPLIT_ORDER_TYPE_SPLIT_MASTER);
			// 承运商拆单类型 2中转主单
			Restriction r34 = Restrictions.eq("carrierSplitOrderType", DataDictionaryConstant.CARRIER_SPLIT_ORDER_TYPE_TRANSIT_MASTER);
			r29 = Restrictions.or(r32, r33, r34);
			// 判断是否为部门权限
			if (user.getOwnedAllDataAuthFlag().equals(BussinessConstant.USER_DEPARTMENT_AUTH_Y)) {
				r2 = Restrictions.in("departmentId", user.getDepts().stream().toArray(String[]::new));
			} else {
				r2 = Restrictions.in("departmentId", user.getDepts().stream().toArray(String[]::new));
				// 其它权限
				// 货主
				r3 = Restrictions.in("cargoOwnerId", user.getConsignors().toArray());
				// 客户
				r4 = Restrictions.in("consigneeUnitId", user.getCustomers().toArray());
				// 供应商
				r5 = Restrictions.in("consigneeUnitId", user.getSuppliers().toArray());
				// 仓库
				r6 = Restrictions.in("consigneeUnitId", user.getWarehouses().toArray());

				// 客户
				Restriction r14 = Restrictions.in(TmsShipbillHeaderModel::getShipperUnitId, user.getCustomers().toArray());
				// 供应商
				Restriction r15 = Restrictions.in(TmsShipbillHeaderModel::getShipperUnitId, user.getSuppliers().toArray());
				// 仓库
				Restriction r16 = Restrictions.in(TmsShipbillHeaderModel::getShipperUnitId, user.getWarehouses().toArray());

				r7 = Restrictions.or(r4, r5, r6, r14, r15, r16);
			}
		} else if (user.getUserType().equals(BussinessConstant.USER_TYPE_CYS)) {
			// 承运商账号
			r2 = Restrictions.eq("carrierId", Long.valueOf(user.getLoginUserCompanyInfo().getId()));
			r29 = Restrictions.eq("carrierSplitOrderStatus", DataDictionaryConstant.CARRIER_SPLIT_ORDER_STATUS_UN);
		} else {
			throw new BizException("当前人员没有操作权限！");
		}
		criteria.setRestriction(Restrictions.and(r1, r2, r3, r7, r29));
		List<TmsShipbillHeaderModel> tmsShipbillHeaderModels = tmsShipbillHeaderMapper.findByCriteria(criteria);

		// 待提交数量
		Integer waitSubmitNum = 0;
		// 待审核数量
		Integer pendingReviewNum = 0;
		// 待调度数量
		Integer pendingDispatchNum = 0;
		// 待调度审核数量
		Integer pendingDispatchReviewNum = 0;
		// 待提货数量
		Integer waitPickUpNum = 0;
		// 待送达数量
		Integer waitDeliveredNum = 0;

		WaitDeliveredQueryByShipbillVO waitDeliveredQueryByShipbillVO = new WaitDeliveredQueryByShipbillVO();
		// 权限控制
		if (user.getUserType().equals(BussinessConstant.USER_TYPE_ZY)) {
			// 判断是否是部门权限
			if (user.getOwnedAllDataAuthFlag().equals(BussinessConstant.USER_DEPARTMENT_AUTH_Y)) {
				waitDeliveredQueryByShipbillVO.setIsDept(StatusConstant.IS_DEPT_YES);
				waitDeliveredQueryByShipbillVO.setDepts(user.getDepts().toArray(new String[] {}));
			} else {
				waitDeliveredQueryByShipbillVO.setIsDept(StatusConstant.IS_DEPT_NO);
				// 设置部门权限
				waitDeliveredQueryByShipbillVO.setDepts(user.getDepts().toArray(new String[] {}));
				// 设置数据权限
				waitDeliveredQueryByShipbillVO.setConsignors(user.getConsignors().toArray(new Long[] {}));
				waitDeliveredQueryByShipbillVO.setWsarehouses(user.getWarehouses().toArray(new Long[] {}));
				waitDeliveredQueryByShipbillVO.setCustomers(user.getCustomers().toArray(new Long[] {}));
				waitDeliveredQueryByShipbillVO.setSuppliers(user.getSuppliers().toArray(new Long[] {}));
			}
		} else if (user.getUserType().equals(BussinessConstant.USER_TYPE_CYS)) {
			waitDeliveredQueryByShipbillVO.setUnitId(Long.valueOf(user.getLoginUserCompanyInfo().getId()));
		} else {
			throw new PrologException("当前人员没有操作权限");
		}
		waitDeliveredQueryByShipbillVO.setUserType(user.getUserType());
		waitDeliveredQueryByShipbillVO.setEnterpriseId(user.getEnterpriseId());
		List<WaitDeliveredQueryByShipbillDTO> waitDeliveredQueryByShipbillDTOS = tmsLoadingBillHeaderMapper.waitDeliveredQueryByShipbillPage(waitDeliveredQueryByShipbillVO);

		if (user.getUserType().equals(BussinessConstant.USER_TYPE_ZY)) {
			waitSubmitNum = tmsShipbillHeaderModels.stream().filter(o -> DataDictionaryConstant.SHIPBILL_STATUS_YTH.equals(o.getStatus()) || DataDictionaryConstant.SHIPBILL_STATUS_CSH.equals(o.getStatus()) || DataDictionaryConstant.SHIPBILL_STATUS_DSH.equals(o.getStatus())).collect(Collectors.toList()).size();
			pendingReviewNum = tmsShipbillHeaderModels.stream().filter(o -> DataDictionaryConstant.SHIPBILL_STATUS_YTJ.equals(o.getStatus()) || DataDictionaryConstant.SHIPBILL_STATUS_WZ.equals(o.getStatus()) || DataDictionaryConstant.SHIPBILL_STATUS_WS.equals(o.getStatus())).collect(Collectors.toList()).size();
			pendingDispatchNum = tmsShipbillHeaderModels.stream().filter(o -> DataDictionaryConstant.SHIPBILL_STATUS_REVIEW.equals(o.getStatus()) && DataDictionaryConstant.SPLIT_ORDER_STATUS_UN.equals(o.getSplitOrderStatus()) && DataDictionaryConstant.DELIVERY_METHOD_TYPE_SEND.equals(o.getDeliveryMethodType())).collect(Collectors.toList()).size();

			pendingDispatchReviewNum = tmsShipbillHeaderModels.stream().filter(o -> DataDictionaryConstant.SHIPBILL_STATUS_DISPATCH.equals(o.getStatus()) && DataDictionaryConstant.SPLIT_ORDER_STATUS_UN.equals(o.getSplitOrderStatus())).collect(Collectors.toList()).size();
			waitPickUpNum = tmsShipbillHeaderModels.stream().filter(o -> DataDictionaryConstant.SHIPBILL_STATUS_QY.equals(o.getStatus()) && DataDictionaryConstant.SPLIT_ORDER_STATUS_UN.equals(o.getSplitOrderStatus())).collect(Collectors.toList()).size();
		} else if (user.getUserType().equals(BussinessConstant.USER_TYPE_CYS)) {
			waitSubmitNum = tmsShipbillHeaderModels.stream().filter(o -> DataDictionaryConstant.SHIPBILL_STATUS_DISPATCH_CHECK.equals(o.getStatus()) && (DataDictionaryConstant.CARRIER_SHIPBILL_STATUS_RECEIVED.equals(o.getCarrierShipbillStatus()) || DataDictionaryConstant.CARRIER_SHIPBILL_STATUS_ROLLBACK.equals(o.getCarrierShipbillStatus()) || DataDictionaryConstant.CARRIER_SHIPBILL_STATUS_WAIT_RECEIVE.equals(o.getCarrierShipbillStatus()))).collect(Collectors.toList()).size();
			pendingReviewNum = tmsShipbillHeaderModels.stream().filter(o -> DataDictionaryConstant.SHIPBILL_STATUS_DISPATCH_CHECK.equals(o.getStatus()) && DataDictionaryConstant.CARRIER_SHIPBILL_STATUS_COMMIT.equals(o.getCarrierShipbillStatus())).collect(Collectors.toList()).size();
			pendingDispatchNum = tmsShipbillHeaderModels.stream().filter(o -> DataDictionaryConstant.SHIPBILL_STATUS_DISPATCH_CHECK.equals(o.getStatus()) && DataDictionaryConstant.CARRIER_SHIPBILL_STATUS_AUDITED.equals(o.getCarrierShipbillStatus())).collect(Collectors.toList()).size();

			pendingDispatchReviewNum = tmsShipbillHeaderModels.stream().filter(o -> (DataDictionaryConstant.SHIPBILL_STATUS_CONSIGNMENT_HANDOVER.equals(o.getStatus()) || DataDictionaryConstant.SHIPBILL_STATUS_DISPATCH.equals(o.getStatus())) && DataDictionaryConstant.CARRIER_SHIPBILL_STATUS_DISPATCHED.equals(o.getCarrierShipbillStatus())).collect(Collectors.toList()).size();
			waitPickUpNum = tmsShipbillHeaderModels.stream().filter(o -> DataDictionaryConstant.SHIPBILL_STATUS_QY.equals(o.getStatus()) && DataDictionaryConstant.CARRIER_SHIPBILL_STATUS_DISPATCHED_CHECK.equals(o.getCarrierShipbillStatus()) && DataDictionaryConstant.CARRIER_SPLIT_ORDER_STATUS_UN.intValue() == o.getCarrierSplitOrderStatus()).collect(Collectors.toList()).size();
		} else {
			throw new PrologException("当前人员没有操作权限！");
		}

		waitDeliveredNum = ToolKits.isEmpty(waitDeliveredQueryByShipbillDTOS) ? 0 : waitDeliveredQueryByShipbillDTOS.size();
		System.out.println(waitSubmitNum + ":件数");
		frontPageStatisticalDTO.setWaitSubmitNum(waitSubmitNum);
		frontPageStatisticalDTO.setPendingReviewNum(pendingReviewNum);
		frontPageStatisticalDTO.setPendingDispatchNum(pendingDispatchNum);
		frontPageStatisticalDTO.setPendingDispatchReviewNum(pendingDispatchReviewNum);
		frontPageStatisticalDTO.setWaitPickUpNum(waitPickUpNum);
		frontPageStatisticalDTO.setWaitDeliveredNum(waitDeliveredNum);

		return frontPageStatisticalDTO;
	}

}
