package com.cim.idm.service.Impl;

import com.alibaba.fastjson.JSON;
import com.cim.idm.constants.delivery.DeliveryStateEnums;
import com.cim.idm.constants.receive.ReceiveTypeEnums;
import com.cim.idm.dao.DeliveryDao;
import com.cim.idm.exception.GlobalException;
import com.cim.idm.model.dto.PageDto;
import com.cim.idm.model.dto.PurchaseDto;
import com.cim.idm.model.dto.delivery.*;
import com.cim.idm.model.po.delivery.MaterialDelivery;
import com.cim.idm.model.po.delivery.MaterialDeliveryReceive;
import com.cim.idm.model.po.MaterialReceiveRequest;
import com.cim.idm.model.vo.DeliveryReceiveDetailVo;
import com.cim.idm.model.vo.MaterialReceiveRequestVo;
import com.cim.idm.model.vo.ReceiveDetailVo;
import com.cim.idm.service.IDeliveryService;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.page.PageMethod;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.*;

@Service
public class DeliveryServiceImpl implements IDeliveryService {

    @Resource
    private DeliveryDao deliveryDao;

    @Override
    @Transactional
    public int createDelivery(DeliveryDto dto) {
        // 添加到库单关系
        List<DeliveryRelationDto> res = new ArrayList<>();
        for (DeliveryRelationDto item : dto.getReceiveList()) {
            // 数量大于 0
            if (Objects.nonNull(item.getDeliveryNum()) && item.getDeliveryNum() > 0) {
                // 到库单号
                item.setDeliveryName(dto.getDeliveryName());
                res.add(item);
            }
        }
        if (res.isEmpty()) {
            throw new GlobalException("无可用的物料");
        }
        if (deliveryDao.createRelation(res) <= 0) {
            throw new GlobalException("添加到库单关系失败");
        }
        // 添加到库单
        return deliveryDao.createDelivery(dto, DeliveryStateEnums.CREATE.getCode());
    }

    @Override
    public PageInfo<MaterialReceiveRequestVo> getReceiveList(PageDto dto) {
        PageMethod.startPage(dto.getPageNum(), dto.getPageSize());
        PurchaseDto purchaseDto = JSON.toJavaObject(dto.getParams(), PurchaseDto.class);
        return new PageInfo<>(deliveryDao.getReceiveList(purchaseDto, ReceiveTypeEnums.getPurchase()));
    }

    @Override
    public ReceiveDetailVo getStoreDetail(String siteName, List<String> nameList, String deliveryName) {
        ReceiveDetailVo receiveDetailVo = new ReceiveDetailVo();
        // 采购单明细
        List<Map<String, Object>> storeDetail = deliveryDao.getStoreDetail(siteName, nameList, deliveryName);
        receiveDetailVo.setStoreDetail(storeDetail);
        // 批次
        List<Map<String, Object>> storeCharge = deliveryDao.getStoreCharge(siteName, nameList);
        receiveDetailVo.setStoreCharge(storeCharge);
        return receiveDetailVo;
    }

    @Override
    public MaterialDelivery getByKey(StoreDetailDto storeDetailDto) {
        return deliveryDao.getByKey(storeDetailDto);
    }

    @Override
    public MaterialReceiveRequest getReceiveByKey(StoreDetailDto storeDetailDto) {
        return deliveryDao.getReceiveByKey(storeDetailDto);
    }

    @Override
    public List<MaterialDeliveryReceive> getReceiveByDelivery(StoreDetailDto storeDetailDto) {
        return deliveryDao.getReceiveByDelivery(storeDetailDto);
    }

    @Override
    public List<DeliveryReceiveDetailVo> getReceiveDetail(List<String> nameList) {
        return deliveryDao.getReceiveDetail(nameList);
    }

    @Override
    public PageInfo<MaterialDelivery> getDeliveryList(PageDto dto) {
        PageMethod.startPage(dto.getPageNum(), dto.getPageSize());
        DeliveryListDto deliveryListDto = JSON.toJavaObject(dto.getParams(), DeliveryListDto.class);
        return new PageInfo<>(deliveryDao.getDeliveryList(deliveryListDto));
    }

    @Override
    @Transactional
    public int delDelivery(DeliveryDelDto deliveryDelDto) {
        // 移除到货单关系
        if (deliveryDao.delDeliveryRel(deliveryDelDto) <= 0) {
            throw new GlobalException("删除到货单关系失败");
        }
        // 移除到货单
        return deliveryDao.delDelivery(deliveryDelDto);
    }
}
