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

import com.google.common.collect.Lists;
import com.prolog.common.util.ToolKits;
import com.prolog.framework.core.restriction.Criteria;
import com.prolog.framework.core.restriction.Order;
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.app.constant.BussinessConstant;
import com.prolog.rdc.tms.app.constant.DataDictionaryConstant;
import com.prolog.rdc.tms.app.constant.StatusConstant;
import com.prolog.rdc.tms.app.dao.mapper.*;
import com.prolog.rdc.tms.app.model.dto.business.AdjustmentPackageDTO;
import com.prolog.rdc.tms.app.model.dto.business.AdjustmentQueryDTO;
import com.prolog.rdc.tms.app.model.dto.business.AdjustmentShipBillDetailDTO;
import com.prolog.rdc.tms.app.model.entity.TmsShipbillDetailModel;
import com.prolog.rdc.tms.app.model.entity.TmsShipbillPackageModel;
import com.prolog.rdc.tms.app.model.vo.business.AdjustShipbillQueryVO;
import com.prolog.rdc.tms.app.service.business.AdjustmentService;
import com.prolog.rdc.tms.rmi.feign.MaindataFeign;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Slf4j
@Service
public class AdjustmentServiceImpl implements AdjustmentService {

    @Autowired
    private TmsLoadingBillDetailMapper tmsLoadingBillDetailMapper;

    @Autowired
    private TmsShipbillDetailMapper tmsShipbillDetailMapper;

    @Autowired
    private MaindataFeign maindataTmsResource;

    @Autowired
    private TmsAppointmentHeaderMapper tmsAppointmentHeaderMapper;

    @Autowired
    private TmsAppointmentDetailMapper tmsAppointmentDetailMapper;

    @Autowired
    private TmsShipbillTransportLogMapper tmsShipbillTransportLogMapper;

    @Autowired
    private TmsShipbillHeaderMapper tmsShipbillHeaderMapper;

    @Autowired
    private TmsShipbillOperateLogMapper tmsShipbillOperateLogMapper;

    @Autowired
    private TmsShipbillPackageMapper tmsShipbillPackageMapper;


    /**
     * 调整运单模式查询
     *
     * @param adjustShipbillQueryVO
     * @return
     */
    public List<AdjustmentQueryDTO> queryAdjustShipbill(AdjustShipbillQueryVO adjustShipbillQueryVO){

        UserEntity user = PlgFxUsrUtils.getUserEntity();
        ToolKits.isParamNull(user, "当前人员没有操作权限");

        adjustShipbillQueryVO.setUserType(user.getUserType());
        adjustShipbillQueryVO.setEnterpriseId(user.getEnterpriseId());
        if (user.getUserType().equals(BussinessConstant.USER_TYPE_ZY)) {
            // 判断是否为部门权限
            if (user.getOwnedAllDataAuthFlag().equals(BussinessConstant.USER_DEPARTMENT_AUTH_Y)) {
                adjustShipbillQueryVO.setDepts(user.getDepts().stream().toArray(String[]::new));
                adjustShipbillQueryVO.setIsDept(StatusConstant.IS_DEPT_YES);
            } else {
                adjustShipbillQueryVO.setDepts(user.getDepts().stream().toArray(String[]::new));
                adjustShipbillQueryVO.setIsDept(StatusConstant.IS_DEPT_NO);
                // 货主
                adjustShipbillQueryVO.setConsignors(user.getConsignors().stream().toArray(Long[]::new));
                // 客户
                adjustShipbillQueryVO.setCustomers(user.getCustomers().stream().toArray(Long[]::new));
                // 供应商
                adjustShipbillQueryVO.setSuppliers(user.getSuppliers().stream().toArray(Long[]::new));
                // 仓库
                adjustShipbillQueryVO.setWsarehouses(user.getWarehouses().stream().toArray(Long[]::new));
            }
        } else {
            adjustShipbillQueryVO.setUnitId(Long.valueOf(user.getLoginUserCompanyInfo().getId()));
        }
        List<AdjustmentQueryDTO> adjustmentQueryDTOS = tmsShipbillHeaderMapper.queryAdjustmentShipbill(adjustShipbillQueryVO);

        // 行号
        int id = 1;
        for (AdjustmentQueryDTO adjustmentQueryDTO : adjustmentQueryDTOS) {
            adjustmentQueryDTO.setId(id);
            id++;
            StringBuffer consigneeProvinceCityDistrict = new StringBuffer();
            // 目的地
            consigneeProvinceCityDistrict.append(adjustmentQueryDTO.getConsigneeProvinceName());
            consigneeProvinceCityDistrict.append(adjustmentQueryDTO.getConsigneeCityName());
            consigneeProvinceCityDistrict.append(adjustmentQueryDTO.getConsigneeDistrictName());
            adjustmentQueryDTO.setConsigneeProvinceCityDistrict(consigneeProvinceCityDistrict.toString());
            // 始发地
            StringBuffer shipperProvinceCityDistrict = new StringBuffer();
            shipperProvinceCityDistrict.append(adjustmentQueryDTO.getShipperProvinceName());
            shipperProvinceCityDistrict.append(adjustmentQueryDTO.getShipperCityName());
            shipperProvinceCityDistrict.append(adjustmentQueryDTO.getShipperDistrictName());
            adjustmentQueryDTO.setShipperProvinceCityDistrict(shipperProvinceCityDistrict.toString());

            // 仓库信息
            adjustmentQueryDTO.setBranchId(adjustmentQueryDTO.getShipperId());
            adjustmentQueryDTO.setBranchCode(adjustmentQueryDTO.getShipperCode());
            adjustmentQueryDTO.setBranchName(adjustmentQueryDTO.getShipperName());

            // 收货方电话
            StringBuffer tel = new StringBuffer();
            if (ToolKits.isNotEmpty(adjustmentQueryDTO.getConsigneePhoneNumber())) {
                tel.append(adjustmentQueryDTO.getConsigneePhoneNumber());
            } else if (ToolKits.isNotEmpty(adjustmentQueryDTO.getConsigneeLandlineNumber())) {
                tel.append(adjustmentQueryDTO.getConsigneeLandlineNumber());
            }
            if (ToolKits.isNotEmpty(tel.toString())){
                adjustmentQueryDTO.setConsigneeTel(tel.toString());
            }

            // 收货方类型
            if (DataDictionaryConstant.CONSIGNEE_TYPE_KH.equals(adjustmentQueryDTO.getConsigneeType())) {
                // 收货方名称
                adjustmentQueryDTO.setConsigneeName(adjustmentQueryDTO.getConsigneeUnitName() +
                        DataDictionaryConstant.Line_SYMBOL + adjustmentQueryDTO.getConsigneeName());
            } else if (DataDictionaryConstant.CONSIGNEE_TYPE_GYS.equals(adjustmentQueryDTO.getConsigneeType())) {
                // 收货方名称
                adjustmentQueryDTO.setConsigneeName(adjustmentQueryDTO.getConsigneeName() +
                        DataDictionaryConstant.Line_SYMBOL + adjustmentQueryDTO.getShipperName());
            }

            // 获取包裹信息
            Criteria criteriaPkg = Criteria.forClass(TmsShipbillPackageModel.class);
            Restriction r1 = Restrictions.eq(TmsShipbillPackageModel::getShipbillId, adjustmentQueryDTO.getShipbillId());
            Restriction r2 = Restrictions.eq(TmsShipbillPackageModel::getEnterpriseId, user.getEnterpriseId());
            criteriaPkg.setRestriction(Restrictions.and(r1, r2));
            criteriaPkg.setOrder(Order.newInstance().desc(new String[]{"updatetime"}));

            List<TmsShipbillPackageModel> tmsShipbillPackageModels = tmsShipbillPackageMapper.findByCriteria(criteriaPkg);

            List<AdjustmentPackageDTO> packageDTOS = Lists.newArrayList();
            for (TmsShipbillPackageModel packageModel : tmsShipbillPackageModels) {
                AdjustmentPackageDTO packageDTO = new AdjustmentPackageDTO();
                BeanUtils.copyProperties(packageModel, packageDTO);
                packageDTOS.add(packageDTO);
            }
            adjustmentQueryDTO.setPackageDTOS(packageDTOS);

            if (packageDTOS.size() == 0) {
                // 获取商品信息
                Criteria criteriaDetail = Criteria.forClass(TmsShipbillDetailModel.class);
                criteriaDetail.setRestriction(Restrictions.and(r1, r2));
                criteriaDetail.setOrder(Order.newInstance().desc(new String[]{"updatetime"}));
                List<TmsShipbillDetailModel> tmsShipbillDetailModels = tmsShipbillDetailMapper.findByCriteria(criteriaDetail);

                List<AdjustmentShipBillDetailDTO> detailDTOList = Lists.newArrayList();
                for (TmsShipbillDetailModel detailModel : tmsShipbillDetailModels) {
                    AdjustmentShipBillDetailDTO detailDTO = new AdjustmentShipBillDetailDTO();
                    BeanUtils.copyProperties(detailModel, detailDTO);
                    detailDTOList.add(detailDTO);
                }
                // 商品信息
                adjustmentQueryDTO.setShipBillDetailDTOS(detailDTOList);
            }
        }

        return adjustmentQueryDTOS;
    }

}
